Skip to content

Instantly share code, notes, and snippets.

@sebnyberg
Forked from yefim/Dockerrun.aws.json
Last active October 27, 2020 21:34
Show Gist options
  • Save sebnyberg/c699674a5e037db2a95c4b4b414c4dc1 to your computer and use it in GitHub Desktop.
Save sebnyberg/c699674a5e037db2a95c4b4b414c4dc1 to your computer and use it in GitHub Desktop.
Build a Docker image, push it to AWS EC2 Container Registry, then deploy it to AWS Elastic Beanstalk
#!/bin/bash
# usage: ./deploy.sh staging f0478bd7c2f584b41a49405c91a439ce9d944657
# license: public domain
BRANCH=$1
SHA1=$2
AWS_ACCOUNT_ID=12345678900
NAME=custom-lowercase-name-of-app
ENV_NAME=environment-name
APP_NAME=-elastic-beanstalk-app-name
EB_BUCKET=aws-s3-bucket-to-hold-application-versions
VERSION=$BRANCH-$SHA1
ZIP=$VERSION.zip
aws configure set default.region eu-central-1
# Authenticate against our Docker registry
eval $(aws ecr get-login --no-include-email)
# Build and push the image
docker build -t $NAME:$VERSION .
docker tag $NAME:$VERSION $AWS_ACCOUNT_ID.dkr.ecr.eu-central-1.amazonaws.com/$NAME:$VERSION
docker push $AWS_ACCOUNT_ID.dkr.ecr.eu-central-1.amazonaws.com/$NAME:$VERSION
# Copy the Dockerrun file
cp Dockerrun.aws.template.json Dockerrun.aws.json
# Replace the <AWS_ACCOUNT_ID> with the real ID
sed -i "s/<AWS_ACCOUNT_ID>/$AWS_ACCOUNT_ID/" Dockerrun.aws.json
# Replace the <NAME> with the real name
sed -i "s/<NAME>/$NAME/" Dockerrun.aws.json
# Replace the <TAG> with the real version number
sed -i "s/<TAG>/$VERSION/" Dockerrun.aws.json
# Zip up the Dockerrun file (feel free to zip up an .ebextensions directory with it)
zip -r $ZIP Dockerrun.aws.json
aws s3 cp $ZIP s3://$EB_BUCKET/$ZIP
rm -rf $ZIP
# Create a new application version with the zipped up Dockerrun file
aws elasticbeanstalk create-application-version --application-name $APP_NAME \
--version-label $VERSION --source-bundle S3Bucket=$EB_BUCKET,S3Key=$ZIP
# Update the environment to use the new application version
aws elasticbeanstalk update-environment --environment-name $ENV_NAME \
--version-label $VERSION
# Example Dockerfile
FROM hello-world
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "<AWS_ACCOUNT_ID>.dkr.ecr.eu-central-1.amazonaws.com/<NAME>:<TAG>",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "443"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment