Sometimes you want to deploy your CloudFormation templates in a way that results in specific and different configuration per stack. Perhaps you deploy to a production stack and a development stack and need to flag a different logging value. Maybe your stacks connect to different databases, or expect a different authentication token. To achieve any of these you will want to pass some parameter to CloudFormation that can be used in the deployment.
Following on from my recent post on lambda variables with CloudFormation - where I passed an environment variable to a lambda function - I have a need to actually parameterize the value of that variable. So when I deploy to a stack, I want to be able to provide a value that will be available to the functions within that stack.
CloudFormation provides the ability to add parameters during the
deploy command. We can provide these to the API command using the
--parameter-overrides option, and they will be templated or injected into the template and ultimately end up inside a lambda, as part of an API Gateway method or resource, or in any other component that is deployed via CloudFormation. Let's see how it works.
Define a parameter in a CloudFormation template as simply as this:
#### `template.cfn.yml` AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Parameters: TestValueParam: Type: String Description: The value to be provided to lambda function environment Resources: MyLambdaFunction: Type: AWS::Serverless::Function Properties: CodeUri: . Environment: Variables: TestValue: !Ref TestValueParam Handler: handlers.handle_event Runtime: python3.6
To note here (and compare with the previous version)
Parameters section defines the expected parameters, and
- the parameter value is referenced by
!Ref TestValueParam during deploy
Including parameters in
If we run the simple deployment commands we'll get an error:
$ aws cloudformation package --template-file template.cfn.yml \ --output-template-file template-xfm.cfn.yml --s3-bucket us-east-1-embassy-test $ aws cloudformation deploy --template-file template-xfm.cfn.yml --stack-name my-stack \ --region us-east-1 --capabilities CAPABILITY_IAM An error occurred (ValidationError) when calling the CreateChangeSet operation: Parameters: [TestValueParam] must have values
We need to specify the value of the
aws cloudformation deploy --template-file template-xfm.cfn.yml --stack-name my-stack \ --region us-east-1 --parameter-overrides TestValueParam="hi!" --capabilities CAPABILITY_IAM
The parameters section allows you to specify
- default values, so you wouldn't need to provide a value
AllowedPatterns against which your value will be verified.
MaxValue for numeric types
MaxLength for string types
Very easy, very useful. Now we can build high automated deployments based on - for example - git branches.