I have source-code that lives in a Git repo (GitHub). I have several branches for different environments (e.g. develop
, production
) and I prefer to manage releases inside the GitHub interface using protected branches.
I want a push to a branch to trigger tests and deployment.
However, I also use CloudFormation to deploy AWS services in a reproducible way. My problem is in interfacing CloudFormation with my Git process.
For example, AWS Lambda functions are described in CloudFormation templates like this:
{
"Type" : "AWS::Lambda::Function",
"Properties" : {
"Code": "source code here"
}
}
... where Code
is "Amazon Simple Storage Service (Amazon S3) bucket or specify your source code as inline text. " (Docs)
This means I need to do a manual step after deploying my CloudFormation template:
$BRANCH
at $REPO
(This can be done in a CI provider, but then I still have to click "Rebuild" on each repo)
What I would rather do is define my CI pipeline inside the CloudFormation template. This should be possible using EC2 etc, but I don't know how. The Git repo URL would then be a parameter of the CloudFormation template.
How do I define Git hooks, build steps and deployment in a CloudFormation template? The steps should also run as part of a fresh CloudFormation deployment.
CloudFormation creates a bucket for each region in which you upload a template file. The buckets are accessible to anyone with Amazon Simple Storage Service (Amazon S3) permissions in your AWS account. If a bucket created by CloudFormation is already present, the template is added to that bucket.
For those learning AWS/AWS CLI, CloudFormation is a tool for building infrastructure with AWS. Here is a very simple document on how to use CloudFormation to build an AWS EC2 Linux instance and execute a bash script from CloudFormation against the newly created Linux instance.
Custom resources enable you to write custom provisioning logic in templates that AWS CloudFormation runs anytime you create, update (if you changed the custom resource), or delete stacks.
Normally, you would not build as part of a CloudFormation deployment. The builds happen before CloudFormation deployment happens.
So, as part of your Ci pipeline (not in CloudFormation), you would:
/artifacts/{build number}/MyLambda.zip
As part of step 4, you would pass the location of the artifacts created in step 3 into your CloudFormation stack as parameters and use those parameters to build your Lambda source code location.
Also you should utilize Lambda environment variables in your CloudFormation template to dictate dev/prod/staging parameters, and don't have them hard coded into your Lambda package. This allows you to re-use the same build package between dev/prod/staging.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With