Using the CloudFormation AWS::Include Macro
If you have ever used an imperative programming language like Javascript, C, or Python, then you are familiar with the concept of importing or including other files into your code.
JavaScript:
fs = reqire("fs")
C:
#include "<studio.h"
Python:
import boto3
This is good as it promotes re-use and prevents “reinventing the wheel”. As a result, you can get your code written faster, deploy it sooner, and provide value to your user community.
CloudFormation cannot natively import another template or template section into the template it is processing. This is where the AWS::Include transformation comes to the rescue.
To see this in action, let’s start with a simple CloudFormation template as the basis for this discussion.
# Parameters:
Resources:
bucket:
Type: AWS::S3::Bucket
Outputs:
bucketDomainName:
Value: !GetAtt bucket.DomainName
This is a very simple template. I have added the Parameters section and commented it out because we will be adding to it as we move along in this article. The template outputs the DomainName for the bucket. As we know, we don’t supply a name for the bucket, CloudFormation will generate a name for us. When we run this in the AWS Console, we will see: