Using parameter override functions with CodePipeline pipelines
In a CodePipeline stage, you can specify parameter overrides for AWS CloudFormation actions. Parameter overrides let you specify template parameter values that override values in a template configuration file. AWS CloudFormation provides functions to help you to specify dynamic values (values that are unknown until the pipeline runs).
Fn::GetArtifactAtt
The Fn::GetArtifactAtt
function retrieves the value of an attribute from an
input artifact, such as the S3 bucket name where the artifact is stored. Use this function to
specify attributes of an artifact, such as its file name or Amazon S3 bucket name.
When you run a pipeline, CodePipeline copies and writes files to the pipeline's artifact store (an S3 bucket). CodePipeline generates the file names in the artifact store. These file names are unknown before you run the pipeline.
For example, in your pipeline, you might have a source stage where CodePipeline copies your
AWS Lambda function source code to the artifact store. In the next stage, you have an AWS CloudFormation
template that creates the Lambda function, but AWS CloudFormation requires the file name to create the
function. You must use the Fn::GetArtifactAtt
function to pass the exact S3
bucket and file names.
Syntax
Use the following syntax to retrieve an attribute value of an artifact.
{ "Fn::GetArtifactAtt" : [ "
artifactName
", "attributeName
" ] }
artifactName
-
The name of the input artifact. You must declare this artifact as input for the associated action.
attributeName
-
The name of the artifact attribute whose value you want to retrieve. For details about each artifact attribute, see the following Attributes section.
Example
The following parameter overrides specify the BucketName
and
ObjectKey
parameters by retrieving the S3 bucket name and file name of the
LambdaFunctionSource
artifact. This example assumes that CodePipeline copied Lambda
function source code and saved it as an artifact, for example, as part of a source
stage.
{ "BucketName" : { "Fn::GetArtifactAtt" : ["LambdaFunctionSource", "BucketName"]}, "ObjectKey" : { "Fn::GetArtifactAtt" : ["LambdaFunctionSource", "ObjectKey"]} }
Attributes
You can retrieve the following attributes for an artifact.
BucketName
-
The name of the S3 bucket where the artifact is stored.
ObjectKey
-
The name of the
.zip
file that contains the artifact that is generated by CodePipeline, such as1ABCyZZ.zip
. URL
-
The Amazon Simple Storage Service (Amazon S3) URL of the artifact, such as
https://s3.us-west-2.amazonaws.com/artifactstorebucket-yivczw8jma0c/test/TemplateSo/1ABCyZZ.zip
.
Fn::GetParam
The Fn::GetParam
function returns a value from a key-value pair in a
JSON-formatted file. The JSON file must be included in an artifact.
Use this function to retrieve output values from an AWS CloudFormation stack and use them as input for
another action. For example, if you specify an output file name for an AWS CloudFormation action, CodePipeline
saves the output in a JSON file and then adds it to the output artifact's .zip
file. Use the Fn::GetParam
function to retrieve the output value, and use it as
input for another action.
Syntax
Use the following syntax to retrieve a value from a key-value pair.
{ "Fn::GetParam" : [ "
artifactName
", "JSONFileName
", "keyName
" ] }
artifactName
-
The name of the artifact, which must be included as an input artifact for the associated action.
JSONFileName
-
The name of a JSON file that is contained in the artifact.
keyName
-
The name of the key whose value you want to retrieve.
Examples
The following examples demonstrate how to use the Fn::GetParam
function in
a parameter override.
Syntax
The following parameter override specifies the WebSiteURL
parameter by
retrieving the value of the URL
key from the stack-output.json
file that is in the WebStackOutput
artifact.
{ "WebSiteURL" : { "Fn::GetParam" : ["WebStackOutput", "stack-output.json", "URL"]} }
AWS CloudFormation template snippets
The following AWS CloudFormation template snippets, from a CodePipeline pipeline, demonstrate how to pass
stack outputs. These snippets show two stages of pipeline definition. The first stage
creates a stack and saves its outputs in the TestOutput.json
file in the
StackAOutput
artifact. These values are specified by the
OutputFileName
and OutputArtifacts
properties.
The name of the source input artifact for the stages is TemplateSource
.
The file name for the stack template is teststackA.yaml
, and the file name
for the configuration file is test-configuration.json
. In both stages, these
values are specified for the TemplateConfiguration
and
TemplatePath
properties as shown:
TemplateConfiguration: TemplateSource::test-configuration.json TemplatePath: TemplateSource::teststackA.yaml
Example Create stack A stage
- Name: CreateTestStackA Actions: - Name: CloudFormationCreate ActionTypeId: Category: Deploy Owner: AWS Provider: CloudFormation Version: '1' Configuration: ActionMode: CREATE_UPDATE Capabilities: CAPABILITY_IAM OutputFileName: TestOutput.json RoleArn: !GetAtt [CFNRole, Arn] StackName: StackA TemplateConfiguration: TemplateSource::test-configuration.json TemplatePath: TemplateSource::teststackA.yaml InputArtifacts: - Name: TemplateSource OutputArtifacts: - Name: StackAOutput RunOrder: '1'
In a subsequent stage, stack B uses the outputs from stack A. In the
ParameterOverrides
property, the example uses the Fn::GetParam
function to specify the StackBInputParam
parameter. The resulting value is
the value associated with the StackAOutputName
key.
Example Create stack B stage
- Name: CreateTestStackB Actions: - Name: CloudFormationCreate ActionTypeId: Category: Deploy Owner: AWS Provider: CloudFormation Version: '1' Configuration: ActionMode: CREATE_UPDATE Capabilities: CAPABILITY_IAM RoleArn: !GetAtt [CFNRole, Arn] StackName: StackB TemplateConfiguration: TemplateSource::test-configuration.json TemplatePath: TemplateSource::teststackB.yaml ParameterOverrides: | { "StackBInputParam" : { "Fn::GetParam" : ["StackAOutput", "TestOutput.json", "StackAOutputName"]} } InputArtifacts: - Name: TemplateSource - Name: StackAOutput RunOrder: '1'
See also
The following related resources can help you as you work with these parameters.
-
For more information about the AWS CloudFormation action parameters in CodePipeline, see the AWS CloudFormation action configuration reference in the AWS CodePipeline User Guide.
-
For example template values by action provider, such as for the
Owner
field or theconfiguration
fields, see the Action structure reference in the AWS CodePipeline User Guide. -
To download example pipeline stack templates in YAML or JSON format, see the tutorials under Create a pipeline with AWS CloudFormation in the AWS CodePipeline User Guide.
-
For an example template configuration file, see AWS CloudFormation artifacts.