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:

Andreas and Michael Wittig

Hej, Andreas & Michael here!

We launched the cloudonaut blog in 2015. Since then, we have published 325 articles: small tips and tricks, best practices, and service reviews. We enjoy writing about all things AWS a lot.

Do you like our blog posts and podcast episodes? Have you learned something new? Consider supporting us create in-depth and independent AWS content. Please help us with a monthly or one-time payment through GitHub Sponsors.

Start supporting us today!
---
AWSTemplateFormatVersion: '2010-09-09'
Resources:
Vpc:
Type: 'cloudonaut::VPC::TwoZones::MODULE'
Properties:
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'
Parameters:
ClassB:
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
Resources:
Vpc:
Type: 'AWS::EC2::VPC'
Properties:
CidrBlock: !Sub '10.${ClassB}.0.0/16'
EnableDnsSupport: true
EnableDnsHostnames: true
InstanceTenancy: default
InternetGateway:
Type: 'AWS::EC2::InternetGateway'
Properties: {}
VpcGatewayAttachment:
Type: 'AWS::EC2::VPCGatewayAttachment'
Properties:
VpcId: !Ref Vpc
InternetGatewayId: !Ref InternetGateway
SubnetAPublic:
DependsOn: [VpcGatewayAttachment]
Type: 'cloudonaut::VPC::Subnet::MODULE'
Properties:
VpcId: !Ref Vpc
VpcCidrBlock: !GetAtt 'Vpc.CidrBlock'
InternetGatewayId: !Ref InternetGateway
Reach: public
AZIndex: '0'
AZChar: A
SubnetIndex: '0'
SubnetCount: 4
SubnetAPrivate:
DependsOn: [VpcGatewayAttachment]
Type: 'cloudonaut::VPC::Subnet::MODULE'
Properties:
# [...]
SubnetBPublic:
# [...]
SubnetBPrivate:
# [...]
Outputs:
Id:
Value: !Ref Vpc
CidrBlock:
Value: !GetAtt 'Vpc.CidrBlock'
InternetGatewayId:
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.

Alternatives

Michael Wittig

Michael Wittig

I'm an independent consultant, technical writer, and programming founder. All these activities have to do with AWS. I'm writing this blog and all other projects together with my brother Andreas.

In 2009, we joined the same company as software developers. Three years later, we were looking for a way to deploy our software—an online banking platform—in an agile way. We got excited about the possibilities in the cloud and the DevOps movement. It’s no wonder we ended up migrating the whole infrastructure of Tullius Walden Bank to AWS. This was a first in the finance industry, at least in Germany! Since 2015, we have accelerated the cloud journeys of startups, mid-sized companies, and enterprises. We have penned books like Amazon Web Services in Action and Rapid Docker on AWS, we regularly update our blog, and we are contributing to the Open Source community. Besides running a 2-headed consultancy, we are entrepreneurs building Software-as-a-Service products.

We are available for projects.

Feedback? Questions? Drop me a line: Email, Twitter, LinkedIn.

Briefcase icon
Hire me