CloudFormation Modules

Michael Wittig – 25 Feb 2021

Copying and pasting code or configuration comes with a lot of challenges. Fixing a bug requires you to find all the copies to fix them as well. Adding new features to all the copies becomes difficult if copies diverge. That’s why many programming languages allow us to define functions and classes. In CloudFormation, you want to reuse templates as well. Think about a VPC template, database template, load-balanced Fargate container, and so on. Let me introduce CloudFormation Modules. A new feature to reuse CloudFormation templates.

CloudFormation Modules

CloudFormation Modules are available since November 2020. A module is a plain CloudFormation template (referred to as a fragment in the docs). To use a module, you have to upload it to the CloudFormation Registry. The CloudFormation CLI helps you with this process. You can define the resource type of your module, but you have to follow the schema <Organization>::<Service>::<Name>::MODULE. Modules can be nested up to 3 levels deep.

I created a VPC module that I can use like this:

Looking for a new challenge?

  • tecRacer

    Cloud Consultant • AWS Migrations

    tecRacer • Premier AWS Consulting Partner • Germany, Austria, Portugal, and Switzerland
    Assessment Transformation Change Management

    Senior Lead Full Stack Developer

    DEMICON • AWS Advanced Consulting Partner • Remote
    AWS JavaScript/TypeScript Angular React

AWSTemplateFormatVersion: '2010-09-09'
Type: 'cloudonaut::VPC::TwoZones::MODULE'
ClassB: '42'

With a concise template, I can create a lot of AWS resources. The template for the VPC module looks like this:

AWSTemplateFormatVersion: '2010-09-09'
Description: 'Class B of VPC (10.XXX.0.0/16)'
Type: Number
Default: 0
ConstraintDescription: 'Must be in the range [0-255]'
MinValue: 0
MaxValue: 255
Type: 'AWS::EC2::VPC'
CidrBlock: !Sub '10.${ClassB}.0.0/16'
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
Type: 'AWS::EC2::InternetGateway'
Properties: {}
Type: 'AWS::EC2::VPCGatewayAttachment'
VpcId: !Ref Vpc
InternetGatewayId: !Ref InternetGateway
DependsOn: [VpcGatewayAttachment]
Type: 'cloudonaut::VPC::Subnet::MODULE'
VpcId: !Ref Vpc
VpcCidrBlock: !GetAtt 'Vpc.CidrBlock'
InternetGatewayId: !Ref InternetGateway
Reach: public
AZIndex: '0'
AZChar: A
SubnetIndex: '0'
SubnetCount: 4
DependsOn: [VpcGatewayAttachment]
Type: 'cloudonaut::VPC::Subnet::MODULE'
# [...]
# [...]
# [...]
Value: !Ref Vpc
Value: !GetAtt 'Vpc.CidrBlock'
Value: !Ref InternetGateway

The VPC module includes the subnet module, which demonstrates nested modules.

Unfortunately, there are a ton of limitations. I was struggling to create something useful with CloudFormation Modules. Learn from my experience in the following video.

Source Code:


Become a cloudonaut supporter

Michael Wittig

Michael Wittig ( Email, Twitter, or LinkedIn )

We launched the cloudonaut blog in 2015. Since then, we have published 360 articles, 50 podcast episodes, and 48 videos. It's all free and means a lot of work in our spare time. We enjoy sharing our AWS knowledge with you.

Please support us

Have you learned something new by reading, listening, or watching our content? With your help, we can spend enough time to keep publishing great content in the future. Learn more

Amount must be a multriply of 5. E.g, 5, 10, 15.

Thanks to Alan Leech, Alex DeBrie, ANTHONY RAITI, Christopher Hipwell, Jaap-Jan Frans, Jason Yorty, Jeff Finley, Jens Gehring, jhoadley, Johannes Grumböck, Johannes Konings, John Culkin, Jonas Mellquist, Juraj Martinka, Kamil Oboril, Ken Snyder, Markus Ellers, Ross Mohan, Ross Mohan, sam onaga, Satyendra Sharma, Shawn Tolidano, Simon Devlin, Thorsten Hoeger, Todd Valentine, Victor Grenu, and all anonymous supporters for your help! We also want to thank all supporters who purchased a cloudonaut t-shirt.