AWS Language Extension

Ripon Banik
2 min readAug 16, 2023

A practical use case

Overview

AWS Cloudformation allows to use conditions but in limited way e.g. conditions cannot be used in mappings and resource properties cannot be changed by using condition and mapping together. AWS has brought a new macro to overcome the limitations -

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'

Use Case

In this article, I will talk about a particular use case which this macro extension helped me to achieve without using any other workaround.

I had many environments and needed to configure lots of properties for reach environment. Simply copy and paste for each environment setting in Mapping was not feasible since using a default value for all non-prod environment would require me to change in multiple places. Also it will increase the size of cloudformation.

So, I started looking at using conditions in my resource properties.

AWSTemplateFormatVersion: 2010-09-09
Parameters:
Environment:
Default: "DEV"
AllowedValues:
- "PRD"
- "UAT"
- "SIT"
- "TST"
- "DEV"
- "POC"
AllowedPattern: ^[A-Z]{3}$
Type: String

Mappings:
QueueConfig:
DEV:
DelaySeconds: 5
UAT:
DelaySeconds: 10
PROD:
DelaySeconds: 15

Conditions:
IsProd: !Equals [!Ref Environment, PRD
IsProdOrUAT: !Or [!Equals [!Ref Environment, UAT], Condition: IsProd]

Resources:
Queue:
Type: 'AWS::SQS::Queue'
Properties:
DelaySeconds: !If [IsProdOrUAT, !FindInMap [QueueConfig, !Ref Environment, DelaySeconds], !FindInMap [QueueConfig, DEV, DelaySeconds]]

Which did not work using the intrensic function in the FindInMap.

So what is the work around? Nesting the if conditions -

AWSTemplateFormatVersion: 2010-09-09
Parameters:
Environment:
Default: "DEV"
AllowedValues:
- "PRD"
- "UAT"
- "SIT"
- "TST"
- "DEV"
- "POC"
AllowedPattern: ^[A-Z]{3}$
Type: String

Mappings:
QueueConfig:
DEV:
DelaySeconds: 5
UAT:
DelaySeconds: 10
PROD:
DelaySeconds: 15

Conditions:
IsProd: !Equals [!Ref Environment, PRD]
IsUAT: !Equals [!Ref Environment, UAT]

Resources:
Queue:
Type: 'AWS::SQS::Queue'
Properties:
DelaySeconds: !If [IsProd, !FindInMap [QueueConfig, PRD, DelaySeconds], !If [IsUAT, !FindInMap [QueueConfig, UAT, DelaySeconds], !FindInMap [QueueConfig, DEV, DelaySeconds]]]

The simple solution using the new macro using DefaultValue.

AWSTemplateFormatVersion: 2010-09-09
Transform: 'AWS::LanguageExtensions'
Parameters:
Environment:
Default: "DEV"
AllowedValues:
- "PRD"
- "UAT"
- "SIT"
- "TST"
- "DEV"
- "POC"
AllowedPattern: ^[A-Z]{3}$
Type: String

Mappings:
QueueConfig:
DEV:
DelaySeconds: 5
UAT:
DelaySeconds: 10
PROD:
DelaySeconds: 15

Resources:
Queue:
Type: 'AWS::SQS::Queue'
Properties:
DelaySeconds: !FindInMap [QueueConfig, !Ref Environment, DelaySeconds, DefaultValue: 5 ]

Conclusion

The above practical use case may assist you to start with using AWS Language extension and use it your project. Please provide a 👏 if it has been helpful to you.

If you need assistance for your business, please reach out via this page.

  1. https://aws.amazon.com/blogs/mt/introducing-new-language-extensions-in-aws-cloudformation/

--

--

Ripon Banik

A Cloud and DevSecOps Engineer passionate about simplification of technology and make it consumable.