Skip to content

Instantly share code, notes, and snippets.

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 delijati/006c0f94527cfacc11c4c599348c666b to your computer and use it in GitHub Desktop.
Save delijati/006c0f94527cfacc11c4c599348c666b to your computer and use it in GitHub Desktop.
Elastic Beanstalk deploy
#!/bin/bash
region="us-east-1"
application="service_name"
environment="production"
instance_profile="eb_application"
ec2_key="sshkey"
bucket="bucket_name"
vpc="vpc-1dd65123"
vpc_subnets="subnet-c23f2123,subnet-939fa456"
version=$(git log --format="%H" -n 1)
envfile=".env.prod"
# Build and push docker image
docker build -t company/$application:$version .
docker push company/$application:$version
#Push it again tagged as latest
docker tag company/$application:$version company/$application:latest
docker push company/$application:latest
# Convert .env.prod into json for OptionSettings.
# WARNING: Includes trailing comma
# ORS="" means no new lines after each print
# The for statement iterates on columns 2-n and joins them with an = sign (but no sign after the nth column).
# This is in case an env var contains an = sign in its value
envjson=$(cat $envfile | awk -F '=' '{ORS=""}
NF>1 { print "{ \"Namespace\": \"aws:elasticbeanstalk:application:environment\", \"OptionName\": \"" $1 "\", \"Value\": \"" }
NF>1 { for(i=2; i<=NF; i++) print $i (i==NF?"":"=") }
NF>1 { print "\" },\n" }')
sed "s%<image>%$application:$version%" Dockerrun.aws.json.template > Dockerrun.aws.json
zip -r deploy.zip Dockerrun.aws.json
# Upload .zip to S3
aws s3 cp deploy.zip s3://$bucket/docker/$application/$version.zip
# Create application if not exists
output=$(aws elasticbeanstalk describe-applications --region $region --output text --application-names "$application")
if [ -z "$output" ]; then
aws elasticbeanstalk create-application --region $region --application-name "$application"
fi
# Create new version
aws elasticbeanstalk create-application-version --region $region --application-name $application --version-label $version --source-bundle S3Bucket=$bucket,S3Key=docker/$application/$version.zip
# Create environment if not exists
output=$(aws elasticbeanstalk describe-environments --region $region --output text --application-name "$application" --environment-names "$environment")
if [ -z "$output" ]; then
aws elasticbeanstalk create-environment --region $region --cli-input-json "$(cat <<EOF
{
"ApplicationName": "$application",
"EnvironmentName": "$environment",
"Tags": [
{
"Key": "Roles",
"Value": "$application"
}
],
"VersionLabel": "$version",
"SolutionStackName": "64bit Amazon Linux 2016.03 v2.1.3 running Docker 1.11.1",
"OptionSettings": [
$envjson
{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MinSize",
"Value": "1"
},
{
"Namespace": "aws:autoscaling:asg",
"OptionName": "MaxSize",
"Value": "4"
},
{
"Namespace": "aws:autoscaling:launchconfiguration",
"OptionName": "InstanceType",
"Value": "t2.micro"
},
{
"Namespace": "aws:autoscaling:launchconfiguration",
"OptionName": "EC2KeyName",
"Value": "$key"
},
{
"Namespace": "aws:autoscaling:launchconfiguration",
"OptionName": "IamInstanceProfile",
"Value": "arn:aws:iam::201009178507:instance-profile/$instance_profile"
},
{
"Namespace": "aws:elb:loadbalancer",
"OptionName": "CrossZone",
"Value": "true"
},
{
"Namespace": "aws:elb:policies",
"OptionName": "ConnectionDrainingEnabled",
"Value": "true"
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "VPCId",
"Value": "$vpc"
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "Subnets",
"Value": "$subnets"
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "ELBSubnets",
"Value": "$subnets"
},
{
"Namespace": "aws:ec2:vpc",
"OptionName": "ELBScheme",
"Value": "internal"
},
{
"Namespace": "aws:autoscaling:updatepolicy:rollingupdate",
"OptionName": "RollingUpdateEnabled",
"Value": "true"
}
]
}
EOF
)"
else
if [ -z "$envjson" ]; then
envjsonNoTrailingComma=""
else
envjsonNoTrailingComma=${envjson%?}
fi
echo $envjsonNoTrailingComma
json="$(cat <<EOM
{
"VersionLabel": "$version",
"OptionSettings": [
$envjsonNoTrailingComma
]
}
EOM
)"
echo "Updating environment [$environment] for application [$application]: $json"
aws elasticbeanstalk update-environment --region $region --application-name "$application" --environment-name "$environment" --cli-input-json "$json"
fi
{
"auths": {
"auth": "",
"email": ""
},
"https://index.docker.io/v1/": {
"auth": "",
"email": ""
}
}
{
"AWSEBDockerrunVersion": "1",
"Authentication": {
"Bucket": "bucket_name",
"Key": "docker/dockercfg"
},
"Image": {
"Name": "company/<image>"
},
"Ports": [
{
"ContainerPort": "80"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment