Integrating with the serverless framework
In order to use the serverless framework in a CloudFormation Stack you'll need to do a few things: create a Docker image with the serverless framework included, invoke the serverless CLI in before_init hook, sync your artifacts with S3, and make sure the serverless config has your template bucket configured as the artifact location.
The first one can be done using a Dockerfile akin to this one:
1
FROM public.ecr.aws/spacelift/runner-terraform
2
USER root
3
WORKDIR /home/spacelift
4
RUN apk add --update --no-cache curl nodejs npm
5
RUN npm install -g serverless
6
RUN serverless --version
7
USER spacelift
Copied!
You should build it, push it to a repository and set it as the Runner Image of your Stack.
You'll also have to invoke the serverless CLI in order to generate raw CloudFormation files. You can do this by adding the following to your before initialization hooks:
serverless package --region ${CF_METADATA_REGION}
You can add the following script as a mounted file:
1
#!/bin/bash
2
3
set -eu
4
set o pipefile
5
6
STATE_FILE=.serverless/serverless-state.json
7
S3_PREFIX=$(jq -r '.package.artifactDirectoryName' < "$STATE_FILE")
8
ARTIFACT=$(jq -r '.package.artifact' < "$STATE_FILE")
9
10
aws s3 cp .serverless/$ARTIFACT s3://$CF_METADATA_TEMPLATE_BUCKET/$S3_PREFIX/$ARTIFACT
Copied!
and invoke it in your before initialization hooks: sh sync.sh
Finally, specify the S3 bucket for artifacts in your serverless.yml configuration file:
1
provider:
2
deploymentBucket: your-s3-bucket
Copied!
Copy link