CloudFormation Conditions

  CloudFormation

Course Main Menu

Section 7 Main Menu

Overview

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8162164?start=0

What are conditionals used for?

  • Conditionals are used to control the creation of resources or outputs based on a condition.
  • Conditions can be whatever you want them to be, but common ones are:
    • Environment (dev / test / prod)
    • AWS Region
    • Any parameter value
  • Each condition can reference another condition, parameter value or mapping.

How to define a condition

Conditions:
   Logical_Id:
      Intrinsic_Function
  • The logical ID is for you to choose. It is how you name your condition.
    • Comment: This seems more like a variable name, set to either TRUE or FALSE
  • The intrinsic function (logical) can be any of the following:
    • Fn::And
    • Fn::Equals
    • Fn::If
    • Fn::Not
    • Fn::or
  • These are good for determining if you’re going to create some resources or not.

Conditions Hands On

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8139156?start=0

This lesson analyzes a CF template that optionally creates a volume and a mount point only if “prod” is specified as a parameter.

It uses parameters, mappings, conditionals, outputs, so it’s a great all around example.

AWSTemplateFormatVersion: "2010-09-09"
Mappings:
  RegionMap:
    us-east-1:
      AMI: "ami-a4c7edb2"
      TestAz: "us-east-1a"
    us-west-1:
      AMI: "ami-6df1e514"
      TestAz: "us-west-1a"
    us-west-2:
      AMI: "ami-327f5352"
      TestAz: "us-west-2a"
    eu-west-1:
      AMI: "ami-d7b9a2b1"
      TestAz: "eu-west-1a"
    sa-east-1:
      AMI: "ami-87dab1eb"
      TestAz: "sa-east-1a"
    ap-southeast-1:
      AMI: "ami-77af2014"
      TestAz: "ap-southeast-1a"
    ap-southeast-2:
      AMI: "ami-10918173"
      TestAz: "ap-southeast-2a"
    ap-northeast-1:
      AMI: "ami-e21cc38c"
      TestAz: "ap-northeast-1a"
Parameters:
  EnvType:
    Description: Environment type.
    Default: test
    Type: String
    AllowedValues:
      - prod
      - test
    ConstraintDescription: must specify prod or test.

Conditions:
  CreateProdResources: !Equals [ !Ref EnvType, prod ]

Resources:
  EC2Instance:
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", AMI]
      InstanceType: t2.micro
      AvailabilityZone: !FindInMap [RegionMap, !Ref "AWS::Region", TestAz]

  MountPoint:
    Type: "AWS::EC2::VolumeAttachment"
    Condition: CreateProdResources
    Properties:
      InstanceId:
        !Ref EC2Instance
      VolumeId:
        !Ref NewVolume
      Device: /dev/sdh

  NewVolume:
    Type: "AWS::EC2::Volume"
    Condition: CreateProdResources
    Properties:
      Size: 100
      AvailabilityZone:
        !GetAtt EC2Instance.AvailabilityZone

Outputs:
  VolumeId:
    Condition: CreateProdResources
    Value:
      !Ref NewVolume

Description:

  • If Parameter EnvType = ‘prod’:
    • Create VolumeAttachment “MountPoint” on EC2Instance and mount NewVolume to it at /dev/sdh
    • Create Volume “NewVolume” in the same AZ as EC2Instance and make it 100GB
    • Output VolumeId with the ID of NewVolume

Conditions Functions

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8139162?start=0

Ha! He only points to the documentation!  LAME!

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html

Fn::GetAtt (Get Attribute)

https://www.udemy.com/aws-cloudformation-master-class/learn/v4/t/lecture/8162166?start=0

  • Attributes are attached to any resources you create
  • To know the attributes that exist for each resource, you need to look at the documentation
Fn::GetAtt: [ logicalNameOfResource, attributeName ]
!GetAtt logicalNameOfResource.attributeName

*Notice the OOP notation! 🙂

Quiz

Conditions cannot be applied to

  • Resources
  • Outputs
  • Parameters

 

LEAVE A COMMENT