Skip to content

Instantly share code, notes, and snippets.

@lprhodes
Last active December 22, 2022 07:25
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 lprhodes/89e4436df3d73ac26cf5e89a6fc8ec0a to your computer and use it in GitHub Desktop.
Save lprhodes/89e4436df3d73ac26cf5e89a6fc8ec0a to your computer and use it in GitHub Desktop.
A script that will create a Formation template from a CDK script and also create its assets. To be used along side SAM and tsc-watch for real-time lambda updates.
#!/bin/bash
# This script is designed to replace the existing cdk.out asset directory used by SAM
# otherwise a new asset directory will be created that can't be seen by SAM until SAM is re-run.
echo "Creating cdk output directory"
mkdir -p tmp/cdk.out
ASSET_DIRS=($(ls -d1 tmp/cdk.out/asset*))
DIR_COUNT=${#ASSET_DIRS[*]}
echo "Checking existing asset directories"
if (($DIR_COUNT > 1)); then
rm -Rf ./tmp/cdk.out/asset*
echo "Too many asset directories found in ./tmp/cdk.out/"
echo "All asset directories have now been removed"
echo "Please quit SAM (CTRL + C) and re-run \`yarn start\`"
exit 0
fi
if [ -n "$ASSET_DIRS" ]; then
FIRST_ASSET_DIR=${ASSET_DIRS[0]}
echo "Removing existing asset directory"
rm -Rf ./tmp/cdk.out/asset*
echo "Running \`cdk synth\` which generated a new asset directory"
cdk synth --output "tmp/cdk.out" --no-staging > tmp/cdk.out/template.yml
ASSET_DIRS=($(ls -d1 tmp/cdk.out/asset*))
NEW_ASSET_DIR=${ASSET_DIRS[0]}
echo "Moving new asset directory back to the original location"
echo $FIRST_ASSET_DIR
echo $NEW_ASSET_DIR
mv $NEW_ASSET_DIR $FIRST_ASSET_DIR
else
cdk synth --output "tmp/cdk.out" --no-staging > tmp/cdk.out/template.yml
fi
echo "Completed \`cdk synth\` successfuly"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment