Skip to main content
  1. Posts/

Use Conditions to create reusable CloudFormation templates

·538 words·3 mins· loading · loading ·
The indie coder
Author
The indie coder
Full-stack developer

Raise your hand if you’ve struggled at least once while trying to write CloudFormation templates that can be resusable across different environments 🤚.

In this article, I will show you how to use Conditions to create reusable CloudFormation templates.

it’s important to know that you can define conditions at two different levels:

  • Resource level
  • Property level

When you define conditions at the resource level, you can specify whether CloudFormation should create a resource, depending on the environment you’ve set.

When you define conditions at the Property level, you can either:

  • define different values for a specific property based on the environment.
  • decide whether to include or exclude a certain property for your resource, depending on the environment.

Conditions at the resource level
#

If you want to determine whether to create a resource based on the deployment environment, you can utilize the Condition property within the resource definition.

For instance, let’s assume you want to create an SQS queue in the test environment but not in the production environment.

The template definition would appear as follows:

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  Environment:
    Description: Deploy Environment.
    Type: String
    AllowedValues:
      - prod
      - test

Conditions: 
  IsTestEnvironment: !Equals 
    - !Ref Environment
    - test

Resources:
  Queue:
    Type: 'AWS::SQS::Queue'
    Condition: IsTestEnvironment

Conditions at the property level
#

Let’s assume, in this case instead, that you want to create an SQS queue both in test and production environments but with differing MessageRetentionPeriod values (e.g., 60 seconds for test and 432000 seconds for production).

The template definition would appear as follows:

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  Environment:
    Description: Deploy Environment.
    Type: String
    AllowedValues:
      - prod
      - test

Conditions: 
  IsTestEnvironment: !Equals 
    - !Ref Environment
    - test

Resources:
  Queue:
    Type: 'AWS::SQS::Queue'
    Properties:
      MessageRetentionPeriod: !If
        - IsTestEnvironment
        - 60
        - 432000

Conditions at the property level - Including or Excluding a Property
#

Suppose you’re creating an SQS queue in both test and production environments, but you only want to set the ReceiveMessageWaitTimeSeconds property for test.

The template definition would appear as follows:

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  Environment:
    Description: Deploy Environment.
    Type: String
    AllowedValues:
      - prod
      - test

Conditions: 
    IsTestEnvironment: !Equals 
        - !Ref Environment
        - test

Resources:
  Queue:
    Type: 'AWS::SQS::Queue'
      Properties:
        ReceiveMessageWaitTimeSeconds: !If
          - IsTestEnvironment
          - 20
          - Ref: AWS::NoValue

Working with Nested Conditions
#

In some cases, you may need to consider one or more development environments as well.

Let’s see how the Conditions section would appear when you want to create a resource both in test and develpoment environments but not in production.

AWSTemplateFormatVersion: 2010-09-09

Parameters:
  Environment:
    Description: Deploy Environment.
    Type: String
    AllowedValues:
      - prod
      - test
      - dev

Conditions: 
  IsTestEnvironment: !Equals 
    - !Ref Environment
    - test

  IsDevEnvironment: !Equals 
    - !Ref Environment
    - dev
  
  IsNotProductionEnvironment: !Or
    - !Condition IsTestEnvironment
    - !Condition IsDevEnvironment

Resources:
  Queue:
    Type: 'AWS::SQS::Queue'
    Condition: IsNotProductionEnvironment

Conclusion
#

That’all folks! In this article we have seen how to use Conditions to write CloudFormation templates that can be reused across different environment.

I’ve created a GitHub repository where I’ve stored the templates we’ve seen above in YAML files. You can find instructions in the README.md file on how to create CloudFormation stacks from these templates to test out the Conditions.

Enjoy it! 🚀

If you have questions or would like to provide feedback, email me at martina.theindiecoder@gmail.com.

Resources
#

Conditions