AWS Cloudformation Part2 — Conditions, outputs, meta-data, ec2 UserData

Khemlall Mangal
3 min readDec 30, 2022

Aright guys lets pick up from where we left off. We will now talk about conditions.

Conditions section contains statements that define the circumstances under which entities are created or configured.

  • Example: 1 — We can create a condition and then associate it with a resource or output so that AWS CloudFormation only creates the resource or output if the condition is true.
  • • Example:2 — We can associate the condition with a property so that AWS CloudFormation only sets the property to a specific value if the condition is true, if the condition is false, AWS CloudFormation sets the property to a different value that we specify.
  • • We will use conditions, when we want to re-use the template in different contexts like dev and prod environments.
  • Conditions are evaluated based on predefined Psuedo parameters or input parameter values that we specify when we create or update stack. • Within each condition we can reference the other condition.
  • • We can associate these conditions in three places.
  • • Resources
  • • Resource Properties
  • • Outputs
  • • At stack creation or stack update, AWS CloudFormation evaluates all conditions in our template. During stack update, Resources that are now associated with a false condition are deleted. • Important Note: During stack update, we cannot update conditions by themselves. We can update conditions only when we include changes that add, modify or delete resources.

Base

AWSTemplateFormatVersion: 2010-09-09
Description: Conditions Practice

Parameters:
MyKeyName:
Description: Select Key name
Type: AWS::EC2::KeyPair::KeyName
EnvironmentName:
Description: Select the environment
Type: String
Default: dev
AllowedValues:
- dev
- prod
ConstraintDescription: must be development or production

Mappings:
MyRegionMap:
us-east-2:
HVM64: ami-0cd3dfa4e37921605
us-west-1:
HVM64: ami-0ec6517f6edbf8044

MyEnvironmentMap:
dev:
instanceType: t2.micro
prod:
instanceType: t2.small

Resources:
MyVMInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap
- MyRegionMap
- !Ref 'AWS::Region'
- HVM64
InstanceType: !FindInMap
- MyEnvironmentMap
- !Ref EnvironmentName
- instanceType
KeyName: !Ref MyKeyName

We can use the below listed intrinsic functions to define conditions in cloud formation template.
- • Fn::And
- Fn::Equals
- Fn::If
- Fn::Not
- Fn::Or

We will be covering all these functions in our practice exercises. Here is what we are going to do.

Step 01: Create an EIP when environment is prod, use intrinsic function Fn::Equals

  • Step 02: Create a security group for dev environment when condition is met and demonstrate Pseudo parameter “AWS::NoValue” for when environment is prod. Use Intrinsic function Fn::If
  • Step 03: Create a security group for prod env with prod related condition added. Use Intrinsic function Fn::If
  • Step 04: Demonstrate Intrinsic function Fn::Not
  • Step 05: Demonstrate Intrinsic function Fn::Or
  • Step 06: Demonstrate Intrinsic function Fn::And

Alright, lets do step01 and use the fn::Equals

AWSTemplateFormatVersion: 2010-09-09
Description: Conditions Practice

Parameters:
MyKeyName:
Description: Select Key name
Type: AWS::EC2::KeyPair::KeyName
EnvironmentName:
Description: Select the environment
Type: String
Default: dev
AllowedValues:
- dev
- prod
ConstraintDescription: must be development or production

Mappings:
MyRegionMap:
us-east-2:
HVM64: ami-0cd3dfa4e37921605
us-west-1:
HVM64: ami-0ec6517f6edbf8044

MyEnvironmentMap:
dev:
instanceType: t2.micro
prod:
instanceType: t2.small

Conditions:
CreateEIPForProd: !Equals [!Ref EnvironmentName, prod]


Resources:
MyVMInstance:
Type: AWS::EC2::Instance
Properties:
ImageId: !FindInMap
- MyRegionMap
- !Ref 'AWS::Region'
- HVM64
InstanceType: !FindInMap
- MyEnvironmentMap
- !Ref EnvironmentName
- instanceType
KeyName: !Ref MyKeyName

MyProdEIP:
Type: AWS::EC2::EIP
Condition: CreateEIPForProd
Properties:
InstanceId: !Ref MyVMInstance

So lets start out here:

Conditions:
CreateEIPForProd: !Equals [!Ref EnvironmentName, prod]

if condition is true then it will create prodid

MyProdEIP:
Type: AWS::EC2::EIP
Condition: CreateEIPForProd
Properties:
InstanceId: !Ref MyVMInstance

Then once you have this template, you can try this out in the cloud formation on aws console.

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.