Skip to content

Migration to cdk: How to include existing Serverless::Function yaml files in cdk codepipeline

2

We have several yaml files that contain Type: AWS::Serverless::Function and CodeUri: ../some-module/dist/. We deploy these using CLI and the following commands:

aws cloudformation package --template input.yaml --output-template-file /build/output.yaml --s3-bucket <some-bucket> --s3-prefix <some path>
aws cloudformation deploy --template-file /build/output.yaml --stack-name <some stack>  --capabilities CAPABILITY_NAMED_IAM  --parameter-overrides <parameters>

We are in the process of migrating to CDK2 and CodePipeline, and as a first step we just want to reuse the existing yaml files in CDK2.

When we include the original yaml file using:

new CfnInclude(this, 'stack', {
  templateFile: 'input.yaml',
  parameters
});

We get the following error message:

Transform AWS::Serverless failed with: Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [X] is invalid. 'CodeUri' is not a valid S3 Uri of the form 's3://bucket/key' with optional versionId query parameter.

The way we interpret this is that the package step that we do when we're using CLI is not happening automatically when we just include the original yaml with CfnInclude, and we are doing it wrong.

What is the proper way to include such yaml file in CDK2?

Thanks in advance :) -wab

  • I'm getting stuck behind this a full two years later and have been going nuts for days trying to find a workaround. If I come up with something, I'll update.

1 Answer
0

I worked on this problem for a few days and didn't really discover an entry-point into the source bundling code used by the CDK. I thought about writing a custom construct but gave up when I saw that there are no plans to support async code in CDK constructs.

I resorted to the following workaround:

  • Run whatever your build process is prior to running cdk deploy.
  • Zip up the manually-built code.
  • Copy the zip file to an S3 bucket with appropriate permissions.
  • Change the CodeUri attribute of your AWS::Serverless::Function to a FunctionCode type value.
  • Change the Bucket and Key values of CodeUri to reflect the zip you just uploaded.
  • Run cdk deploy.

It's not as beautiful as keeping everything within the CDK scripts, but it's working (and without sam).

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.