forked from Securing-DevOps/deployer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_ebs_env.sh
executable file
·73 lines (59 loc) · 2.49 KB
/
create_ebs_env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
# requires: pip install awscli awsebcli
# uncomment to debug
#set -x
fail() {
echo configuration failed
exit 1
}
export AWS_DEFAULT_REGION=eu-west-2
datetag=$(date +%Y%m%d%H%M)
identifier=deployer$datetag
mkdir -p tmp/$identifier
echo "Creating EBS application $identifier"
# Find the ID of the default VPC
aws ec2 describe-vpcs --filters Name=isDefault,Values=true > tmp/$identifier/defaultvpc.json || fail
vpcid=$(jq -r '.Vpcs[0].VpcId' tmp/$identifier/defaultvpc.json)
echo "default vpc is $vpcid"
# Create an elasticbeantalk application
aws elasticbeanstalk create-application \
--application-name $identifier \
--description "deployer $env $datetag" > tmp/$identifier/ebcreateapp.json || fail
echo "ElasticBeanTalk application created"
# Get the name of the latest Docker solution stack
dockerstack="$(aws elasticbeanstalk list-available-solution-stacks | \
jq -r '.SolutionStacks[]' | grep -P '.+Amazon Linux.+running Docker' | head -1)"
# Create the EB API environment
aws elasticbeanstalk create-environment \
--application-name $identifier \
--environment-name deployer-api \
--description "deployer API environment" \
--tags "Key=Owner,Value=$(whoami)" \
--solution-stack-name "$dockerstack" \
--tier "Name=WebServer,Type=Standard,Version=''" > tmp/$identifier/ebcreateapienv.json || fail
apieid=$(jq -r '.EnvironmentId' tmp/$identifier/ebcreateapienv.json)
echo "API environment $apieid is being created"
# Upload the application version
aws s3 mb s3://$identifier
aws s3 cp app-version-deployer.json s3://$identifier/
aws elasticbeanstalk create-application-version \
--application-name "$identifier" \
--version-label deployer-api \
--source-bundle "S3Bucket=$identifier,S3Key=app-version-deployer.json" > tmp/$identifier/appversion.json
# Wait for the environment to be ready (green)
echo -n "waiting for environment"
while true; do
aws elasticbeanstalk describe-environments --environment-id $apieid > tmp/$identifier/$apieid.json
health="$(jq -r '.Environments[0].Health' tmp/$identifier/$apieid.json)"
if [ "$health" == "Green" ]; then break; fi
echo -n '.'
sleep 10
done
echo
# Deploy the docker container to the instances
aws elasticbeanstalk update-environment \
--application-name $identifier \
--environment-id $apieid \
--version-label deployer-api > tmp/$identifier/$apieid.json
url="$(jq -r '.CNAME' tmp/$identifier/$apieid.json)"
echo "Environment is being deployed. Public endpoint is http://$url"