How to use output artifact of CodeBuild in CloudFormation?
Your CodeBuild should be dropping your zip file to an S3 bucket. So in the Code section of your LambdaFunction resource you point to it.
Code:
S3Bucket: the_bucket_where_CodeBuild_dropped_your_zip
S3Key: the_name_of_the_zip_file_dropped
You don't need 'ZipFile: '
I realize this question is old, but thought I'd answer it with respect to SAM
project_root/
template.yaml
buildspec.yaml
my_lambda/
my_lambda.py
requirements.txt
template.yaml:
Transform: AWS::Serverless-2016-10-31
Resources:
MyLambda:
Type: AWS::Serverless::Function
Properties:
Handler: my_lambda.lambda_handler
CodeUri: my_lambda/
Runtime: python3.8
buildspec.yaml:
version: 0.2
phases:
install:
runtime-versions:
python: 3.8
commands:
- pip install aws-sam-cli
build:
commands:
- sam build
- sam package --s3-bucket mybucket --s3-prefix sam | sam deploy -t /dev/stdin --stack-name FOOSTACK --capabilities CAPABILITY_IAM
Notes:
sam build
willpip install
your lambdarequirements.txt
sam package
will zip your lambda, and name it with the md5 of its contents and upload to S3 for you (only if it has changed)sam deploy
will create a CloudFormation changeset and run it for you
Finally found a solution to this thanks to AWS support. First, I placed this JSON in the Parameter Overrides in the CloudFormation deployment step in CodePipeline:
{
"buildBucketName" : { "Fn::GetArtifactAtt" : ["MyAppBuild", "BucketName"]},
"buildObjectKey" : { "Fn::GetArtifactAtt" : ["MyAppBuild", "ObjectKey"]}
}
Then changed my CF template like so:
AWSTemplateFormatVersion: 2010-09-09
Parameters:
buildBucketName:
Type: String
buildObjectKey:
Type: String
Resources:
...
LambdaFunction:
...
Code:
S3Bucket: !Ref buildBucketName
S3Key: !Ref buildObjectKey
This passes the output artifact bucket name and object key that CodeBuild outputs as parameters to CF, so that it can dynamically grab the output artifact location in S3 without having to hardcode anything, making the template way more portable.