Skip to content

Instantly share code, notes, and snippets.

@c0debreaker
Forked from pszabop/Dockerfile
Created August 2, 2020 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c0debreaker/611852dc76cd97c4af561e7b1afaac61 to your computer and use it in GitHub Desktop.
Save c0debreaker/611852dc76cd97c4af561e7b1afaac61 to your computer and use it in GitHub Desktop.
All these go in a deploy directory. The CDK is under the deploy directory which isn't the prettiest thing but neither is docker pretty
# default-name.sh
# create a somewhat unique name for a CloudFormation instance that is a legal name
name_url_legal=$(git config --global user.name | awk '{ gsub (" ", "", $0); print}')
repo_url_legal=$(git branch | awk '{print $2}')
# the sandbox name can't include the full directory path because
# the name must be about 75 characters or less
# @see https://forums.aws.amazon.com/thread.jspa?messageID=863882&tstart=0
# @see https://ubuntuforums.org/showthread.php?t=1478809
sandbox_name_url_legal=$(dirname $PWD | rev | cut -d"/" -f1-4 | rev | sed 's/\//-/g')
user_branch_url_legal=${name_url_legal}${sandbox_name_url_legal}-${repo_url_legal}
echo $user_branch_url_legal
#!/bin/bash
#
# parse command line
usage="delete.sh [-n <legal cloudformation stack name> ]"
while getopts ":n:" opt; do
case ${opt} in
n )
stack_name=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG\n${usage}" 1>&2
;;
: )
echo "Invalid option: $OPTARG requires an argument\n${usage}" 1>&2
;;
esac
done
shift $((OPTIND -1))
# @see https://stackoverflow.com/questions/31331788/using-aws-cli-what-is-best-way-to-determine-the-current-region
# for why it is so stupidly complicated to get the current region as the AWS CLI is going to see it
region=`aws configure list | grep region | awk '{print $2}'`
# get or create a name for the stack
default_stack_name=$($(dirname $0)/default-name.sh)
stack_name=${stack_name:-${default_stack_name}}
ddb_stack_name=${stack_name}-ddb
# Clean up the stack
#aws cloudformation delete-stack --stack-name ${stack_name}
#aws cloudformation delete-stack --stack-name ${ddb_stack_name}
#echo "Waiting for stack ${stack_name} to be deleted. This could take 30-300 seconds depending on the stack. No progress feedback provided, sorry"
#aws cloudformation wait stack-delete-complete --stack-name ${stack_name}
docker-compose -f deploy/docker-compose.yml run deploy bash -c "cd /home/code/cdk-code/umlss-stack/;cdk --app \"npx ts-node bin/umlss.ts ${stack_name}\" destroy"
version: '3'
services:
deploy:
image: umlss/aws-cdk # your image name goes here
build: .
environment:
- API_GATEWAY_URL
- NPM_TOKEN
# @see https://github.com/aws/aws-sdk-js/pull/1391
# @see https://stackoverflow.com/questions/57768714/get-region-from-aws-cli-using-node
# @see https://stackoverflow.com/questions/31331788/using-aws-cli-what-is-best-way-to-determine-the-current-region
# the SDK uses `AWS_REGION` and the CLI uses `AWS_DEFAULT_REGION`. What the f*ck
- AWS_DEFAULT_REGION
- AWS_REGION=${AWS_DEFAULT_REGION}
- AWS_SDK_LOAD_CONFIG
- CFT_INSTANCE_NAME
volumes:
- $HOME/.aws/:/root/.aws/:ro
- .:/home/code/
FROM node:12.16.1
RUN npm update -g aws-cdk@1.51.0
RUN npm install -g aws-cdk@1.51.0
#!/bin/bash
#
# Initialize the deploy system
#
# command line args
usage="init.sh [-n <legal cloudformation stack name> ]"
while getopts ":t:n:" opt; do
case ${opt} in
n )
stack_name=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG\n${usage}" 1>&2
;;
: )
echo "Invalid option: $OPTARG requires an argument\n${usage}" 1>&2
;;
esac
done
shift $((OPTIND -1))
default_stack_name=$($(dirname $0)/default-name.sh)
stack_name=${stack_name:-${default_stack_name}}
echo $stack_name
# force build of docker image
docker-compose -f deploy/docker-compose.yml build
#
# Note cdk deployment name is typically your project name, and it's hard coded. It really
# should be in a variable and probably in a file like default-name.sh is for the actual stack name
#
# Typescript is used, so must build
docker-compose -f deploy/docker-compose.yml run deploy bash -c 'cd /home/code/cdk-code/umlss-stack/;npm install;npm run build'
# verify cdk works by listing all the CFT instances controlled by CDK
docker-compose -f deploy/docker-compose.yml run deploy bash -c "cd /home/code/cdk-code/umlss-stack/;cdk --app \"npx ts-node bin/umlss.ts ${stack_name}\" list"
#!/bin/bash
#
# parse command line
usage="create.sh [-n <legal cloudformation stack name> ] [ -t <template file> ]"
while getopts ":t:n:" opt; do
case ${opt} in
t )
template_file=$OPTARG
;;
n )
stack_name=$OPTARG
;;
\? )
echo "Invalid option: $OPTARG\n${usage}" 1>&2
;;
: )
echo "Invalid option: $OPTARG requires an argument\n${usage}" 1>&2
;;
esac
done
shift $((OPTIND -1))
# get or create a name for the stack
default_stack_name=$($(dirname $0)/default-name.sh)
stack_name=${stack_name:-${default_stack_name}}
# update the stack. Which for CDT is just a deploy. Yay idempotence
docker-compose -f deploy/docker-compose.yml run deploy bash -c "cd /home/code/cdk-code/umlss-stack/;cdk --app \"npx ts-node bin/umlss.ts ${stack_name}\" deploy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment