Skip to content

Instantly share code, notes, and snippets.

@ranguard
Last active January 23, 2022 01:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranguard/3befa8344678c9fe90f34a81e3a5ca8d to your computer and use it in GitHub Desktop.
Save ranguard/3befa8344678c9fe90f34a81e3a5ca8d to your computer and use it in GitHub Desktop.
CDK + SAM for local lambda devel
#!/bin/sh
# Because I use layers, referenced by a SSM Param I need to replace
# that in the generated template
export LAYERARN="arn:aws:lambda:eu-west-1:xxx:layer:YYYY:1";
export LAYERMATCH="/layers/baseLayer" # <- Param name
if [ -z "$STACK" ]
then
echo "Usage: STACK=xxx gen_sam_template.sh"
else
cdk synth $STACK --no-staging > template.yaml
perl -pi -e 's/$ENV{LAYERMATCH}/$ENV{LAYERARN}/ge' template.yaml
echo "template.yaml created and LayerArn set to: $LAYERARN"
fi
#!/bin/sh
## Usage: AWS_REGION=XXX AWS_PROFILE=YYY ./run_local.sh
## Note: must be from same dir as template.yaml
## which is generated using the gen_sam_template.sh
# Get from template.yaml ( see also https://docs.aws.amazon.com/cdk/latest/guide/tools.html#sam )
FUNCTION_REFERENCE=XXXXXXXXX
## Create user specific files that can be put into version control, but not
## step on each other's toes!
export ENVFILE="$USER-env.json"
export EVENTFILE="$USER-event.json"
# Run
sam local invoke --env-vars $ENVFILE --event $EVENTFILE \
--region $AWS_REGION --profile $AWS_PROFILE $FUNCTION_REFERENCE;
{
"FUNCTION_REFERENCE_FROM_TEMPLATE": {
"SOME_ENV_KEY: "and-the-value-a-bucket-name-for-example"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment