Our CloudFormation templates are stored in GitHub. Inside CodePipeline we're using GitHub as our Source, but we can't reference nested CloudFormation Stacks when they're not stored on S3.
How can we reference CloudFormation nested Stacks when using GitHub as our source in CodePipeline?
If this is not possible, how can we upload the CloudFormation Templates from GitHub to S3 between the Source Stage (from GitHub) and the Deploy Stage in CodePipeline?
Besides the solution with a lambda stage, a simple approach is to use CodeBuild and AWS SAM.
In the main CloudFormation template (let's call it main.yaml), use 'Transform: AWS::Serverless-2016-10-31'
Note that you just need to put the relative path to the child template instead of an absolution s3 uri.
Add a CodeBuild stage with the following buildspecification.yaml
The build command 'aws cloudformation package' will upload the nested-template.yaml to the s3 bucket 'my_bucket' and inject the absolute s3 uri to the transformed template.
In the CloudFormation deployment stage, use 'Create change set' and 'Execute change set' to create the stack. Note that 'Create or update stack' does not work for 'Transform: AWS::Serverless-2016-10-31'.
Here are the docs you may find useful:
The second doc shows how to deploy a lambda function, but it is essentially the same to reference a nested stack.
There are two approaches I can think of to reference nested CloudFormation Stacks from a GitHub source for a CodePipeline deployment:
1. pre-commit Git hook
Add a
pre-commit
client-side Git hook that runsaws cloudformation package
on your template, committing a generated template with the S3 reference to your GitHub repository alongside the changes to the source template.The benefit to this approach is that you can leverage the existing template-rewriting logic in
aws cloudformation package
, and you don't have to modify your existing CodePipeline configuration.2. Lambda pipeline Stage
Add a Lambda-based pipeline Stage that extracts the specified nested-stack template file from the GitHub Source Artifact and uploads it to a specified location in S3 referenced in the parent stack template.
The benefit to this approach is that the Pipeline will remain entirely self-contained, without any extra pre-processing step required by the committer.
I've published a complete reference example implementation to
wjordan/aws-codepipeline-nested-stack
: