-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit
More file actions
executable file
·139 lines (95 loc) · 3.53 KB
/
init
File metadata and controls
executable file
·139 lines (95 loc) · 3.53 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
read -p "Application Name: " APPLICATION
if [[ ! "$APPLICATION" =~ ^[a-z][a-z0-9]*$ ]]
then
echo "ERROR: APPLICATION must start with a lowercase letter and contain only lowercase letters and numbers"
exit 1
fi
read -p "GitHub Owner/Organization: " GITHUB_OWNER
read -p "GitHub Repository: " GITHUB_REPO
read -p "GitHub Branch: " ENVIRONMENT
ALERT_EMAIL=""
ALERT_PHONE=""
if [[ ! "$ENVIRONMENT" =~ ^[a-z][a-z0-9]*$ ]]
then
echo "ERROR: ENVIRONMENT (\$1) must start with a lowercase letter and contain only lowercase letters and numbers"
exit 1
fi
read -p "Fully Qualified Domain Name: " DOMAIN_NAME
printf "\n\tApplication: $APPLICATION\n\tEnvironment: $ENVIRONMENT\n\tGitHub Repo: $GITHUB_OWNER/$GITHUB_REPO\n\tFQDN: $DOMAIN_NAME\n\n"
read -p "Is this correct (y/n)? " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit
fi
STACK_NAME="$APPLICATION-$ENVIRONMENT"
if ! [ -x "$(command -v aws)" ]
then
echo "ERROR: You must install the awscli"
echo "https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html"
exit 1
fi
if [[ ! "$AWS_ACCOUNT_ID" =~ ^[0-9]{12}$ ]]
then
echo "AWS_ACCOUNT_ID environment variable not set or invalid"
exit 1
fi
STACK_DESCRIPTION=$(aws cloudformation describe-stacks --stack-name $STACK_NAME 2>&1)
if [[ $STACK_DESCRIPTION == *"locate credentials"* || $STACK_DESCRIPTION == *"specify a region"* ]]
then
echo "ERROR: You must set your AWS credentials/profile and default region via Environment Variables"
echo "https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html"
exit 1
fi
if [[ $STACK_DESCRIPTION != *"ValidationError"* ]]
then
echo "$STACK_NAME exists, no need to create/build..."
else
rm -rf ./tmp/build
mkdir -p ./tmp/build
cp -LR ./lambdas ./tmp/build/
cp ./build/cfn_*.yml ./tmp/build/
cd ./tmp/build
INIT_BUCKET="deployment-artifacts-$AWS_ACCOUNT_ID-$AWS_DEFAULT_REGION"
aws s3 mb s3://$INIT_BUCKET 2> /dev/null
echo "Creating Web Distribution Certificate in us-east-1..."
WEB_DISTRIBUTION_CERTIFICATE=$(aws acm request-certificate \
--region 'us-east-1' \
--domain-name $DOMAIN_NAME \
--subject-alternative-names *.$DOMAIN_NAME \
--validation-method DNS \
--query 'CertificateArn' \
--output text)
CERTIFICATE_RESOURCE_RECORD=$(aws acm describe-certificate \
--certificate-arn $WEB_DISTRIBUTION_CERTIFICATE \
--region 'us-east-1' \
--query 'Certificate.DomainValidationOptions[0].ResourceRecord' \
--output text)
echo "Created Certificate: $WEB_DISTRIBUTION_CERTIFICATE"
printf "Certificate DNS Validation Resource Record:\n$CERTIFICATE_RESOURCE_RECORD\n"
echo "Packaging CloudFormation Template..."
aws cloudformation package \
--template-file ./cfn_main.yml \
--s3-bucket $INIT_BUCKET \
--output-template-file ./cfn_main_packaged.yml
echo "Initializing $STACK_NAME CloudFormation Stack..."
aws cloudformation deploy \
--stack-name "$STACK_NAME" \
--template-file ./cfn_main_packaged.yml \
--s3-bucket $INIT_BUCKET \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM CAPABILITY_AUTO_EXPAND \
--parameter-overrides \
"Application=$APPLICATION" \
"Environment=$ENVIRONMENT" \
"DomainName=$DOMAIN_NAME" \
"GitHubOwner=$GITHUB_OWNER" \
"GitHubRepo=$GITHUB_REPO" \
"WebDistributionCertificate=$WEB_DISTRIBUTION_CERTIFICATE" \
"AlertEmail=$ALERT_EMAIL" \
"AlertPhone=$ALERT_PHONE" \
--tags \
"Application=$APPLICATION" \
"Environment=$ENVIRONMENT" \
cd ../..
rm -rf ./tmp/build
fi