-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.sh
executable file
·153 lines (112 loc) · 3.89 KB
/
build.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Globals
declare -r CLOUDFORMATION_DIR="cloudformation"
declare -r EC2_CF_TEMPLATE_FILE="ec2.yaml"
declare -r REGION_VIRGINIA="us-east-1"
declare -r REGION_OHIO="us-east-2"
declare -r STACK_NAME="ec2-stack"
declare -r DEPLOYMENT_ENVIRONMENT="dev"
# IMPORTANT: You need to change ww-dev to your account PEM Key name
declare -r PEM_KEY="ww-dev"
declare -r VPC_STACK_NAME="vpc-stack"
declare -r EFS_STACK_NAME="efs-stack"
declare _deployment_region=${REGION_VIRGINIA}
declare _deployment_environment=${DEPLOYMENT_ENVIRONMENT}
deploy_ec2() {
echo "Deploying EC2 instances..."
# Get reference to efs from exports
_efs_web_file_share=$(aws cloudformation list-exports --region ${_deployment_region} --query "Exports [?contains(Name,'${_deployment_environment}-${EFS_STACK_NAME}-WebFileShare')].Value" --output text)
aws --region ${_deployment_region} cloudformation deploy \
--template-file ${CLOUDFORMATION_DIR}/${EC2_CF_TEMPLATE_FILE} \
--stack-name ${_deployment_environment}-${STACK_NAME} \
--capabilities CAPABILITY_NAMED_IAM \
--parameter-overrides "OwnerParameter=Your Name" \
"KeyNameParameter=${PEM_KEY}" \
"VpcStackNameParameter=${_deployment_environment}-${VPC_STACK_NAME}" \
"EnvironmentParameter=${_deployment_environment}" \
"EFSWebFileShareParameter=${_efs_web_file_share}"
}
display_usage() {
echo
echo "-----------------------------------------------------------------------"
echo " Usage: `basename $0` [dev | test | prod] [va | oh] ec2 "
echo " "
echo " Example: ./build.sh test va ec2 # Deploy test environment ec2 instances in region US-EAST-1"
echo " "
echo " EFS WebFileShare == ${_efs_web_file_share}"
echo "-----------------------------------------------------------------------"
}
print_script_complete() {
echo
echo "-------------------------------------------------"
echo "- Script Complete -"
echo "-------------------------------------------------"
}
set_region() {
case $1 in
va)
_deployment_region=${REGION_VIRGINIA}
echo "Deployment Region = " ${_deployment_region}
;;
oh)
_deployment_region=${REGION_OHIO}
echo "Deployment Region = " ${_deployment_region}
;;
*)
echo "Setting region to " ${REGION_VIRGINIA}
_deployment_region=${REGION_VIRGINIA}
echo "Deployment Region = " ${_deployment_region}
esac
}
set_environment() {
case $1 in
dev)
_deployment_environment="dev"
echo "Deployment Environment = " ${_deployment_environment}
;;
test)
_deployment_environment="test"
echo "Deployment Environment = " ${_deployment_environment}
;;
prod)
_deployment_environment="prod"
echo "Deployment Environment = " ${_deployment_environment}
;;
*)
_deployment_environment="dev"
echo "Deployment Environment = " ${_deployment_environment}
;;
esac
}
deploy_cloudformation_script() {
case $1 in
ec2)
#validate_template
deploy_ec2
;;
*)
display_usage
esac
}
validate_template(){
echo "Validating CF Template: " ${CLOUDFORMATION_DIR}/${EC2_CF_TEMPLATE_FILE}
aws cloudformation validate-template --template-body file://${CLOUDFORMATION_DIR}/${EC2_CF_TEMPLATE_FILE}
}
process_arguments() {
#process first argument
set_environment $1; shift
#process second argument
set_region $1; shift
#process third argument
deploy_cloudformation_script $1
}
main() {
if [ "$#" -ne 3 ]; then
display_usage
exit 1
fi
process_arguments $1 $2 $3
print_script_complete
exit 1
}
main "$@"