aws_stack_builder 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +20 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +2 -0
- data/aws_stack_builder.gemspec +29 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/aws_stack_builder.rb +5 -0
- data/lib/aws_stack_builder/version.rb +3 -0
- data/lib/generators/aws_stack_builder/USAGE +25 -0
- data/lib/generators/aws_stack_builder/aws_stack_builder_generator.rb +47 -0
- data/lib/generators/aws_stack_builder/templates/aws/doc/README.md +69 -0
- data/lib/generators/aws_stack_builder/templates/aws/doc/app-cf-designer.png +0 -0
- data/lib/generators/aws_stack_builder/templates/aws/geradores/ami-userdata.sh.template +54 -0
- data/lib/generators/aws_stack_builder/templates/aws/geradores/build_templates.sh.template +6 -0
- data/lib/generators/aws_stack_builder/templates/aws/geradores/cf-base-instance.py.template +96 -0
- data/lib/generators/aws_stack_builder/templates/aws/geradores/cf-stack-generator.py.template +456 -0
- data/lib/generators/aws_stack_builder/templates/aws/scripts/app_deploy.sh.template +32 -0
- data/lib/generators/aws_stack_builder/templates/aws/scripts/app_pack.sh +36 -0
- data/lib/generators/aws_stack_builder/templates/aws/scripts/app_stop_instances_by_role.sh.template +35 -0
- data/lib/generators/aws_stack_builder/templates/aws/scripts/app_update.sh.template +77 -0
- data/lib/generators/aws_stack_builder/templates/aws/scripts/run_delayed_job.sh +25 -0
- metadata +102 -0
@@ -0,0 +1,54 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
# create log file set -x
|
3
|
+
exec > >(tee /tmp/user-data.log|logger -t user-data ) 2>&1
|
4
|
+
# update OS
|
5
|
+
sudo yum -y update
|
6
|
+
# create user deploy
|
7
|
+
sudo useradd deploy
|
8
|
+
sudo bash -c "echo -e \"deploy ALL=(ALL) NOPASSWD: ALL\" >> /etc/sudoers"
|
9
|
+
# install rvm and ruby
|
10
|
+
sudo yum install -y gcc openssl-devel libyaml libyaml-devel libffi-devel libreadline readline-devel zlib1g zlib-devel gdbm-devel ncurses ncurses-devel ruby-devel gcc-c++ jq git patch libtool bison build-essential libc6 libgdbm
|
11
|
+
sudo su - deploy -c "curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -"
|
12
|
+
sudo su - deploy -c "curl -sSL https://get.rvm.io | bash -s stable"
|
13
|
+
sudo su - deploy -c "yes | rvm install 2.2.9"
|
14
|
+
# install bundler
|
15
|
+
sudo su - deploy -c "gem install bundler"
|
16
|
+
# create ~/app and install application dependencies
|
17
|
+
sudo su - deploy -c "mkdir -p ~/app/current"
|
18
|
+
# - java
|
19
|
+
sudo yum -y install java-1.7.0-openjdk.x86_64
|
20
|
+
sudo yum -y install java-1.7.0-openjdk-devel
|
21
|
+
sudo su - deploy -c "echo \"export JAVA_HOME=/usr/lib/jvm/java-1.7.0\" >> ~/.bashrc"
|
22
|
+
# - libs needed by charlock holmes
|
23
|
+
sudo yum -y install icu libicu libicu-devel xmlstarlet
|
24
|
+
# - mysql
|
25
|
+
sudo yum -y install mysql-devel
|
26
|
+
sudo su - deploy -c "echo \"export MYSQL_PASSWORD=taxweb2016\" >> ~/.bashrc"
|
27
|
+
sudo su - deploy -c "echo \"export TAXRULES_PORTAL_DB=mysql\" >> ~/.bashrc"
|
28
|
+
# install apache
|
29
|
+
sudo yum -y install glibc.i686
|
30
|
+
sudo yum -y install curl-devel httpd-devel-2.4.34
|
31
|
+
sudo yum -y install httpd-2.4.34
|
32
|
+
# execute <%= @app_name %> app_update.sh
|
33
|
+
sudo su - deploy -c "cd ~/app/current; wget http://taxweb-deploy.s3.amazonaws.com/<%= @app_name %>/app_update.sh -O app_update.sh >/dev/null 2>&1"
|
34
|
+
sudo su - deploy -c "cd ~/app/current && chmod 755 app_update.sh && ./app_update.sh staging web <%= @app_name %>"
|
35
|
+
# set RAILS_ENV
|
36
|
+
sudo su - deploy -c "echo \"export RAILS_ENV=staging\" >> ~/.bashrc"
|
37
|
+
# install passenger
|
38
|
+
sudo su - deploy -c "cd ~/app/current && gem install passenger -v 5.3.4 --no-rdoc --no-ri"
|
39
|
+
sudo su - deploy -c "cd ~/app/current && passenger-install-apache2-module --languages ruby --auto"
|
40
|
+
sudo su - deploy -c "cd ~/app/current && passenger-install-apache2-module --snippet > /tmp/passenger.conf"
|
41
|
+
sudo su - deploy -c "echo \"\" >> /tmp/passenger.conf"
|
42
|
+
sudo su - deploy -c "echo \"PassengerMaxRequestQueueSize 50\" >> /tmp/passenger.conf"
|
43
|
+
sudo su - deploy -c "echo \"PassengerMaxPoolSize 15\" >> /tmp/passenger.conf"
|
44
|
+
sudo su - deploy -c "sudo mv /tmp/passenger.conf /etc/httpd/conf.d/00passenger.conf"
|
45
|
+
# config apache
|
46
|
+
sudo su - -c "cd /etc/httpd/conf.d/ && rm <%= @app_name %>_app.conf; wget http://taxweb-deploy.s3.amazonaws.com/<%= @app_name %>/<%= @app_name %>_app.conf -O <%= @app_name %>_app.conf"
|
47
|
+
# file system permissions
|
48
|
+
sudo chmod 755 /home
|
49
|
+
sudo chmod 755 /home/deploy
|
50
|
+
sudo chmod -R 755 /home/deploy/app
|
51
|
+
# restart apache
|
52
|
+
sudo service httpd restart
|
53
|
+
# finished
|
54
|
+
echo "Instance configuration finished!"
|
@@ -0,0 +1,96 @@
|
|
1
|
+
from troposphere import Base64, Join, Parameter, Ref, Template, Tags
|
2
|
+
import troposphere.ec2 as ec2
|
3
|
+
|
4
|
+
# template
|
5
|
+
template = Template()
|
6
|
+
template.add_version("2010-09-09")
|
7
|
+
template.add_description("AWS CloudFormation template for base instance")
|
8
|
+
|
9
|
+
# parameters
|
10
|
+
subnet_id = Parameter(
|
11
|
+
"SubnetId",
|
12
|
+
Type="AWS::EC2::Subnet::Id",
|
13
|
+
Default="subnet-0b3b0d6e",
|
14
|
+
Description="Id of the Subnet which will hold the instance"
|
15
|
+
)
|
16
|
+
template.add_parameter(subnet_id)
|
17
|
+
|
18
|
+
vpc_id = Parameter(
|
19
|
+
"VpcId",
|
20
|
+
Type="AWS::EC2::VPC::Id",
|
21
|
+
Default="vpc-ebe0c38e",
|
22
|
+
Description="Id of the VPC which will hold the instance"
|
23
|
+
)
|
24
|
+
template.add_parameter(vpc_id)
|
25
|
+
|
26
|
+
instance_profile = Parameter(
|
27
|
+
"InstanceProfileName",
|
28
|
+
Type="String",
|
29
|
+
Default="<%= @app_name %>",
|
30
|
+
Description="Name of the instance profile to associate with the instance"
|
31
|
+
)
|
32
|
+
template.add_parameter(instance_profile)
|
33
|
+
|
34
|
+
|
35
|
+
# security group
|
36
|
+
sg = ec2.SecurityGroup(
|
37
|
+
'<%= @app_name %>SG',
|
38
|
+
GroupDescription="Security Group for base instance.",
|
39
|
+
Tags=Tags(
|
40
|
+
Name='<%= @app_name %>-base-instance-sg',
|
41
|
+
Custo='<%= @app_name %>'
|
42
|
+
),
|
43
|
+
SecurityGroupIngress=[
|
44
|
+
ec2.SecurityGroupRule(
|
45
|
+
IpProtocol="tcp",
|
46
|
+
FromPort="22",
|
47
|
+
ToPort="22",
|
48
|
+
CidrIp="0.0.0.0/0"
|
49
|
+
),
|
50
|
+
ec2.SecurityGroupRule(
|
51
|
+
IpProtocol="tcp",
|
52
|
+
FromPort="9090",
|
53
|
+
ToPort="9090",
|
54
|
+
CidrIp="0.0.0.0/0"
|
55
|
+
),
|
56
|
+
ec2.SecurityGroupRule(
|
57
|
+
IpProtocol="tcp",
|
58
|
+
FromPort="80",
|
59
|
+
ToPort="80",
|
60
|
+
CidrIp="0.0.0.0/0"
|
61
|
+
),
|
62
|
+
ec2.SecurityGroupRule(
|
63
|
+
IpProtocol="tcp",
|
64
|
+
FromPort="443",
|
65
|
+
ToPort="443",
|
66
|
+
CidrIp="0.0.0.0/0"
|
67
|
+
)
|
68
|
+
],
|
69
|
+
VpcId=Ref(vpc_id)
|
70
|
+
)
|
71
|
+
template.add_resource(sg)
|
72
|
+
|
73
|
+
userdata_file = open("ami-userdata.sh", "r")
|
74
|
+
userdata = userdata_file.read()
|
75
|
+
|
76
|
+
# instance
|
77
|
+
instance = ec2.Instance(
|
78
|
+
"<%= @app_name %>BaseInstance",
|
79
|
+
Tags=Tags(
|
80
|
+
Name='<%= @app_name %>-Base-Instance',
|
81
|
+
Custo='<%= @app_name %>'
|
82
|
+
),
|
83
|
+
ImageId="ami-a9d09ed1",
|
84
|
+
InstanceType="t3.medium",
|
85
|
+
KeyName="taxweb-AWS-US-West",
|
86
|
+
SubnetId=Ref(subnet_id),
|
87
|
+
SecurityGroupIds=[Ref(sg)],
|
88
|
+
#IamInstanceProfile=Ref(instance_profile),
|
89
|
+
UserData=Base64(userdata)
|
90
|
+
)
|
91
|
+
template.add_resource(instance)
|
92
|
+
|
93
|
+
|
94
|
+
f = open("cf-base-instance.json", "w+")
|
95
|
+
f.write(template.to_json())
|
96
|
+
f.close()
|
@@ -0,0 +1,456 @@
|
|
1
|
+
from troposphere import Base64, Join, GetAtt, Tags, Parameter, Ref, Template
|
2
|
+
from troposphere.autoscaling import AutoScalingGroup, Tag, LaunchConfiguration
|
3
|
+
from troposphere.elasticloadbalancing import LoadBalancer
|
4
|
+
from troposphere.policies import AutoScalingReplacingUpdate, AutoScalingRollingUpdate, UpdatePolicy
|
5
|
+
from troposphere.route53 import RecordSetType
|
6
|
+
from troposphere.ec2 import SecurityGroupIngress
|
7
|
+
|
8
|
+
import troposphere.ec2 as ec2
|
9
|
+
import troposphere.elasticloadbalancingv2 as elb
|
10
|
+
|
11
|
+
# global variables
|
12
|
+
# python parameters
|
13
|
+
app_name = 'taxrules'
|
14
|
+
env = 'staging'
|
15
|
+
# derived variables
|
16
|
+
stack_name = None
|
17
|
+
stack_name_strict = None
|
18
|
+
|
19
|
+
|
20
|
+
# get user inputs
|
21
|
+
def define_python_parameters():
|
22
|
+
# input params
|
23
|
+
global app_name
|
24
|
+
default = app_name
|
25
|
+
app_name = raw_input('What is the application name? [' + default + '] ') or default
|
26
|
+
|
27
|
+
global env
|
28
|
+
default = env
|
29
|
+
env = raw_input('What is the environment (production/staging)? [' + default + '] ') or default
|
30
|
+
|
31
|
+
# derived variables
|
32
|
+
env_suffix = 'Hml' if env == 'staging' else ''
|
33
|
+
global stack_name
|
34
|
+
stack_name = app_name.lower().replace(' ', '-')
|
35
|
+
if env_suffix:
|
36
|
+
stack_name = stack_name + "-" + env_suffix.lower()
|
37
|
+
|
38
|
+
global stack_name_strict
|
39
|
+
stack_name_strict = app_name.replace(' ', '').replace('-', '').replace('_', '') + env_suffix
|
40
|
+
|
41
|
+
|
42
|
+
def define_cloud_formation_parameters(template):
|
43
|
+
db_security_group_id = Parameter(
|
44
|
+
"DbSecurityGroupId",
|
45
|
+
Type="AWS::EC2::SecurityGroup::Id",
|
46
|
+
Default="sg-0f84c5afcb16d79fe",
|
47
|
+
Description="The id of the security group of the database (database is not part of the stack created). The resources of the stack will be given access to the DB port in this security group.",
|
48
|
+
)
|
49
|
+
template.add_parameter(db_security_group_id)
|
50
|
+
|
51
|
+
db_port = Parameter(
|
52
|
+
"DbPort",
|
53
|
+
Type="Number",
|
54
|
+
Default="3306",
|
55
|
+
Description="The port where the DB listens for incoming connections. The resources of the stack will be given access to this port in the DB security group.",
|
56
|
+
)
|
57
|
+
template.add_parameter(db_port)
|
58
|
+
|
59
|
+
ssl_certificate_arn = Parameter(
|
60
|
+
"SSLCertificateARN",
|
61
|
+
Type="String",
|
62
|
+
Default="arn:aws:acm:us-west-2:676854543053:certificate/ab8afefd-5f93-48c2-a3fa-ae5a8e5f99e6",
|
63
|
+
Description="ARN of the SSL certificate for load balancer."
|
64
|
+
)
|
65
|
+
template.add_parameter(ssl_certificate_arn)
|
66
|
+
|
67
|
+
vpc_id = Parameter(
|
68
|
+
"VpcId",
|
69
|
+
Type="AWS::EC2::VPC::Id",
|
70
|
+
Default="vpc-ebe0c38e",
|
71
|
+
Description="Id of the VPC which will hold the resources"
|
72
|
+
)
|
73
|
+
template.add_parameter(vpc_id)
|
74
|
+
|
75
|
+
subnet_id1 = Parameter(
|
76
|
+
"SubnetId1",
|
77
|
+
Type="AWS::EC2::Subnet::Id",
|
78
|
+
Default="subnet-0b3b0d6e",
|
79
|
+
Description="Id of the 1st Subnet which will hold the resources"
|
80
|
+
)
|
81
|
+
template.add_parameter(subnet_id1)
|
82
|
+
|
83
|
+
subnet_id2 = Parameter(
|
84
|
+
"SubnetId2",
|
85
|
+
Type="AWS::EC2::Subnet::Id",
|
86
|
+
Default="subnet-2f267258",
|
87
|
+
Description="Id of the 2nd Subnet which will hold the resources"
|
88
|
+
)
|
89
|
+
template.add_parameter(subnet_id2)
|
90
|
+
|
91
|
+
alb_health_check_path = Parameter(
|
92
|
+
"AlbHealthCheckPath",
|
93
|
+
Type="String",
|
94
|
+
Default="/authentication/sign_in",
|
95
|
+
Description="Ping path destination used by the load balancer to check server health"
|
96
|
+
)
|
97
|
+
template.add_parameter(alb_health_check_path)
|
98
|
+
|
99
|
+
base_ami = Parameter(
|
100
|
+
"BaseAMI",
|
101
|
+
Type="AWS::EC2::Image::Id",
|
102
|
+
Default="ami-025ff841a2680ff81",
|
103
|
+
Description="Id of the base AMI"
|
104
|
+
)
|
105
|
+
template.add_parameter(base_ami)
|
106
|
+
|
107
|
+
instance_type = Parameter(
|
108
|
+
"InstanceType",
|
109
|
+
Type="String",
|
110
|
+
Default="t2.small",
|
111
|
+
Description="Type of the instance to be used."
|
112
|
+
)
|
113
|
+
template.add_parameter(instance_type)
|
114
|
+
|
115
|
+
instance_profile = Parameter(
|
116
|
+
"InstanceProfileName",
|
117
|
+
Type="String",
|
118
|
+
Default="<%= @app_name %>",
|
119
|
+
Description="Name of the instance profile to associate with the instance"
|
120
|
+
)
|
121
|
+
template.add_parameter(instance_profile)
|
122
|
+
|
123
|
+
return {"db_security_group_id": db_security_group_id,
|
124
|
+
"db_port": db_port,
|
125
|
+
"ssl_certificate_arn": ssl_certificate_arn,
|
126
|
+
"vpc_id": vpc_id,
|
127
|
+
"subnet_id1": subnet_id1,
|
128
|
+
"subnet_id2": subnet_id2,
|
129
|
+
"alb_health_check_path": alb_health_check_path,
|
130
|
+
"base_ami": base_ami,
|
131
|
+
"instance_type": instance_type,
|
132
|
+
"instance_profile": instance_profile}
|
133
|
+
|
134
|
+
|
135
|
+
def define_security_group(template, vpc_id):
|
136
|
+
sg = ec2.SecurityGroup(
|
137
|
+
stack_name_strict + 'SG',
|
138
|
+
GroupDescription="Security Group for " + stack_name + " stack.",
|
139
|
+
Tags=Tags(
|
140
|
+
Name=stack_name,
|
141
|
+
Custo=app_name
|
142
|
+
),
|
143
|
+
SecurityGroupIngress=[
|
144
|
+
ec2.SecurityGroupRule(
|
145
|
+
IpProtocol="tcp",
|
146
|
+
FromPort="22",
|
147
|
+
ToPort="22",
|
148
|
+
CidrIp="0.0.0.0/0"
|
149
|
+
),
|
150
|
+
ec2.SecurityGroupRule(
|
151
|
+
IpProtocol="tcp",
|
152
|
+
FromPort="9090",
|
153
|
+
ToPort="9090",
|
154
|
+
CidrIp="0.0.0.0/0"
|
155
|
+
),
|
156
|
+
ec2.SecurityGroupRule(
|
157
|
+
IpProtocol="tcp",
|
158
|
+
FromPort="80",
|
159
|
+
ToPort="80",
|
160
|
+
CidrIp="0.0.0.0/0"
|
161
|
+
),
|
162
|
+
ec2.SecurityGroupRule(
|
163
|
+
IpProtocol="tcp",
|
164
|
+
FromPort="443",
|
165
|
+
ToPort="443",
|
166
|
+
CidrIp="0.0.0.0/0"
|
167
|
+
)
|
168
|
+
],
|
169
|
+
VpcId=Ref(vpc_id)
|
170
|
+
)
|
171
|
+
template.add_resource(sg)
|
172
|
+
return sg
|
173
|
+
|
174
|
+
|
175
|
+
def define_load_balancer(template,
|
176
|
+
sg,
|
177
|
+
alb_health_check_path,
|
178
|
+
vpc_id,
|
179
|
+
subnet_id1,
|
180
|
+
subnet_id2,
|
181
|
+
ssl_certificate_arn):
|
182
|
+
alb_target_group_80 = elb.TargetGroup(
|
183
|
+
stack_name_strict + "TG80",
|
184
|
+
Tags=Tags(
|
185
|
+
Name=stack_name,
|
186
|
+
Custo=app_name
|
187
|
+
),
|
188
|
+
HealthCheckPath=Ref(alb_health_check_path),
|
189
|
+
HealthCheckIntervalSeconds="30",
|
190
|
+
HealthCheckProtocol="HTTP",
|
191
|
+
HealthCheckTimeoutSeconds="30",
|
192
|
+
HealthyThresholdCount="3",
|
193
|
+
Matcher=elb.Matcher(
|
194
|
+
HttpCode="200,301"),
|
195
|
+
Port=80,
|
196
|
+
Protocol="HTTP",
|
197
|
+
UnhealthyThresholdCount="2",
|
198
|
+
VpcId=Ref(vpc_id)
|
199
|
+
)
|
200
|
+
template.add_resource(alb_target_group_80)
|
201
|
+
|
202
|
+
alb_target_group_9090 = elb.TargetGroup(
|
203
|
+
stack_name_strict + "TG9090",
|
204
|
+
Tags=Tags(
|
205
|
+
Name=stack_name,
|
206
|
+
Custo=app_name
|
207
|
+
),
|
208
|
+
HealthCheckPath=Ref(alb_health_check_path),
|
209
|
+
HealthCheckIntervalSeconds="30",
|
210
|
+
HealthCheckProtocol="HTTP",
|
211
|
+
HealthCheckTimeoutSeconds="30",
|
212
|
+
HealthyThresholdCount="3",
|
213
|
+
Matcher=elb.Matcher(
|
214
|
+
HttpCode="200,301"),
|
215
|
+
Port=9090,
|
216
|
+
Protocol="HTTP",
|
217
|
+
UnhealthyThresholdCount="2",
|
218
|
+
VpcId=Ref(vpc_id)
|
219
|
+
)
|
220
|
+
template.add_resource(alb_target_group_9090)
|
221
|
+
|
222
|
+
alb = elb.LoadBalancer(
|
223
|
+
stack_name_strict + "ALB",
|
224
|
+
Tags=Tags(
|
225
|
+
Name=stack_name,
|
226
|
+
Custo=app_name
|
227
|
+
),
|
228
|
+
Scheme="internet-facing",
|
229
|
+
Subnets=[Ref(subnet_id1), Ref(subnet_id2)],
|
230
|
+
SecurityGroups=[Ref(sg)]
|
231
|
+
)
|
232
|
+
template.add_resource(alb)
|
233
|
+
|
234
|
+
alb_listener_80 = elb.Listener(
|
235
|
+
stack_name_strict + "ListenerALB80",
|
236
|
+
Port=80,
|
237
|
+
Protocol="HTTP",
|
238
|
+
LoadBalancerArn=Ref(alb),
|
239
|
+
DefaultActions=[elb.Action(
|
240
|
+
Type="forward",
|
241
|
+
TargetGroupArn=Ref(alb_target_group_80)
|
242
|
+
)]
|
243
|
+
)
|
244
|
+
template.add_resource(alb_listener_80)
|
245
|
+
|
246
|
+
alb_listener_443 = elb.Listener(
|
247
|
+
stack_name_strict + "ListenerALB443",
|
248
|
+
Port=443,
|
249
|
+
Protocol="HTTPS",
|
250
|
+
Certificates=[elb.Certificate(
|
251
|
+
CertificateArn=Ref(ssl_certificate_arn)
|
252
|
+
)],
|
253
|
+
LoadBalancerArn=Ref(alb),
|
254
|
+
DefaultActions=[elb.Action(
|
255
|
+
Type="forward",
|
256
|
+
TargetGroupArn=Ref(alb_target_group_9090)
|
257
|
+
)]
|
258
|
+
)
|
259
|
+
template.add_resource(alb_listener_443)
|
260
|
+
|
261
|
+
return {"loadbalancer": alb,
|
262
|
+
"alb_target_group_80": alb_target_group_80,
|
263
|
+
"alb_target_group_9090": alb_target_group_9090,
|
264
|
+
"alb_listener_80": alb_listener_80,
|
265
|
+
"alb_listener_9090": alb_listener_443}
|
266
|
+
|
267
|
+
|
268
|
+
def define_web_auto_scaling(template,
|
269
|
+
base_ami,
|
270
|
+
instance_type,
|
271
|
+
instance_profile,
|
272
|
+
subnet_id1,
|
273
|
+
subnet_id2,
|
274
|
+
sg,
|
275
|
+
alb_target_group_80,
|
276
|
+
alb_target_group_9090):
|
277
|
+
web_launch_config = LaunchConfiguration(
|
278
|
+
stack_name_strict + "WebLC",
|
279
|
+
UserData=Base64(Join('', [
|
280
|
+
'#!/bin/bash\n',
|
281
|
+
'set -x\n',
|
282
|
+
'exec > >(tee /tmp/user-data.log|logger -t user-data ) 2>&1\n',
|
283
|
+
'sudo su - deploy -c "echo \\"export RAILS_ENV=' + env + '\\" >> ~/.bashrc"\n',
|
284
|
+
'sudo su - deploy -c "cd ~/app/current; wget http://taxweb-deploy.s3.amazonaws.com/' + app_name + '/app_update.sh -O app_update.sh >/dev/null 2>&1"\n',
|
285
|
+
'sudo su - deploy -c "cd ~/app/current && chmod 755 app_update.sh && ./app_update.sh ' + env + ' web ' + app_name + '"\n'
|
286
|
+
])),
|
287
|
+
ImageId=Ref(base_ami),
|
288
|
+
InstanceType=Ref(instance_type),
|
289
|
+
IamInstanceProfile=Ref(instance_profile),
|
290
|
+
KeyName="taxweb-AWS-US-West",
|
291
|
+
SecurityGroups=[Ref(sg)]
|
292
|
+
)
|
293
|
+
template.add_resource(web_launch_config)
|
294
|
+
|
295
|
+
web_autoscaling_group = AutoScalingGroup(
|
296
|
+
stack_name_strict + "WebASG",
|
297
|
+
Tags=[
|
298
|
+
Tag("Name", stack_name + "-web", True),
|
299
|
+
Tag("Custo", app_name, True),
|
300
|
+
Tag("Env", env, True),
|
301
|
+
Tag("Role", "web", True),
|
302
|
+
],
|
303
|
+
LaunchConfigurationName=Ref(web_launch_config),
|
304
|
+
MinSize=1,
|
305
|
+
MaxSize=1,
|
306
|
+
DesiredCapacity=1,
|
307
|
+
VPCZoneIdentifier=[Ref(subnet_id1), Ref(subnet_id2)],
|
308
|
+
TargetGroupARNs=[Ref(alb_target_group_80), Ref(alb_target_group_9090)],
|
309
|
+
HealthCheckType="ELB",
|
310
|
+
HealthCheckGracePeriod="300",
|
311
|
+
)
|
312
|
+
template.add_resource(web_autoscaling_group)
|
313
|
+
|
314
|
+
return {"launch_config": web_launch_config,
|
315
|
+
"autoscaling_group": web_autoscaling_group}
|
316
|
+
|
317
|
+
|
318
|
+
def define_auto_scaling(template,
|
319
|
+
base_ami,
|
320
|
+
instance_type,
|
321
|
+
instance_profile,
|
322
|
+
subnet_id1,
|
323
|
+
subnet_id2,
|
324
|
+
sg,
|
325
|
+
role,
|
326
|
+
initial_size):
|
327
|
+
role_name_strict = role.replace(' ', '').replace('-', '').replace('_', '')
|
328
|
+
launch_config = LaunchConfiguration(
|
329
|
+
stack_name_strict + role_name_strict.capitalize() + "LC",
|
330
|
+
UserData=Base64(Join('', [
|
331
|
+
'#!/bin/bash\n',
|
332
|
+
'set -x\n',
|
333
|
+
'exec > >(tee /tmp/user-data.log|logger -t user-data ) 2>&1\n',
|
334
|
+
'sudo su - deploy -c "echo \\"export RAILS_ENV=' + env + '\\" >> ~/.bashrc"\n',
|
335
|
+
'sudo su - deploy -c "cd ~/app/current; wget http://taxweb-deploy.s3.amazonaws.com/' + app_name + '/app_update.sh -O app_update.sh >/dev/null 2>&1"\n',
|
336
|
+
'sudo su - deploy -c "cd ~/app/current && chmod 755 app_update.sh && ./app_update.sh ' + env + ' ' + role + ' ' + app_name + '"\n'
|
337
|
+
])),
|
338
|
+
ImageId=Ref(base_ami),
|
339
|
+
InstanceType=Ref(instance_type),
|
340
|
+
IamInstanceProfile=Ref(instance_profile),
|
341
|
+
KeyName="taxweb-AWS-US-West",
|
342
|
+
SecurityGroups=[Ref(sg)]
|
343
|
+
)
|
344
|
+
template.add_resource(launch_config)
|
345
|
+
|
346
|
+
autoscaling_group = AutoScalingGroup(
|
347
|
+
stack_name_strict + role_name_strict.capitalize() + "ASG",
|
348
|
+
Tags=[
|
349
|
+
Tag("Name", stack_name + "-" + role, True),
|
350
|
+
Tag("Custo", app_name, True),
|
351
|
+
Tag("Env", env, True),
|
352
|
+
Tag("Role", role, True),
|
353
|
+
],
|
354
|
+
LaunchConfigurationName=Ref(launch_config),
|
355
|
+
MinSize=initial_size,
|
356
|
+
MaxSize=initial_size,
|
357
|
+
DesiredCapacity=initial_size,
|
358
|
+
VPCZoneIdentifier=[Ref(subnet_id1), Ref(subnet_id2)],
|
359
|
+
# TargetGroupARNs=[Ref(alb_target_group_80), Ref(alb_target_group_9090)],
|
360
|
+
HealthCheckType="EC2",
|
361
|
+
HealthCheckGracePeriod="300",
|
362
|
+
)
|
363
|
+
template.add_resource(autoscaling_group)
|
364
|
+
|
365
|
+
return {"launch_config": launch_config,
|
366
|
+
"autoscaling_group": autoscaling_group}
|
367
|
+
|
368
|
+
|
369
|
+
def define_dns(template, alb):
|
370
|
+
route53_record = template.add_resource(RecordSetType(
|
371
|
+
stack_name_strict + "WebDNS",
|
372
|
+
HostedZoneName="taxweb.com.br.",
|
373
|
+
Name=stack_name + ".taxweb.com.br.",
|
374
|
+
ResourceRecords=[GetAtt(alb, "DNSName")],
|
375
|
+
TTL=60,
|
376
|
+
Type="CNAME"
|
377
|
+
))
|
378
|
+
|
379
|
+
return route53_record
|
380
|
+
|
381
|
+
|
382
|
+
def add_ingress_rule_to_db_security_group(template, db_sg, db_port, stack_sg):
|
383
|
+
sg_ingress = SecurityGroupIngress(
|
384
|
+
stack_name_strict + "DbIngressRule",
|
385
|
+
IpProtocol="tcp",
|
386
|
+
FromPort=Ref(db_port),
|
387
|
+
ToPort=Ref(db_port),
|
388
|
+
SourceSecurityGroupId=Ref(stack_sg),
|
389
|
+
GroupId=Ref(db_sg)
|
390
|
+
)
|
391
|
+
template.add_resource(sg_ingress)
|
392
|
+
|
393
|
+
|
394
|
+
def gerador():
|
395
|
+
# template
|
396
|
+
template = Template()
|
397
|
+
template.add_version("2010-09-09")
|
398
|
+
template.add_description("AWS CloudFormation template for Taxweb RoR applications")
|
399
|
+
|
400
|
+
# python parameters
|
401
|
+
define_python_parameters()
|
402
|
+
|
403
|
+
# parameters
|
404
|
+
parameters = define_cloud_formation_parameters(template)
|
405
|
+
|
406
|
+
# security group
|
407
|
+
sg = define_security_group(template, parameters["vpc_id"])
|
408
|
+
|
409
|
+
# ALB (Application Load Balancer)
|
410
|
+
alb = define_load_balancer(template,
|
411
|
+
sg,
|
412
|
+
parameters["alb_health_check_path"],
|
413
|
+
parameters["vpc_id"],
|
414
|
+
parameters["subnet_id1"],
|
415
|
+
parameters["subnet_id2"],
|
416
|
+
parameters["ssl_certificate_arn"])
|
417
|
+
|
418
|
+
# web auto scaling
|
419
|
+
define_web_auto_scaling(template,
|
420
|
+
parameters["base_ami"],
|
421
|
+
parameters["instance_type"],
|
422
|
+
parameters["instance_profile"],
|
423
|
+
parameters["subnet_id1"],
|
424
|
+
parameters["subnet_id2"],
|
425
|
+
sg,
|
426
|
+
alb["alb_target_group_80"],
|
427
|
+
alb["alb_target_group_9090"])
|
428
|
+
|
429
|
+
<%- @roles.each do |role| %>
|
430
|
+
# worker-relatorio auto scaling
|
431
|
+
define_auto_scaling(template,
|
432
|
+
parameters["base_ami"],
|
433
|
+
parameters["instance_type"],
|
434
|
+
parameters["instance_profile"],
|
435
|
+
parameters["subnet_id1"],
|
436
|
+
parameters["subnet_id2"],
|
437
|
+
sg,
|
438
|
+
"worker-<%= role %>",
|
439
|
+
1)
|
440
|
+
<% end %>
|
441
|
+
|
442
|
+
# DNS
|
443
|
+
define_dns(template, alb["loadbalancer"])
|
444
|
+
|
445
|
+
# Add ingress rule to db security group
|
446
|
+
add_ingress_rule_to_db_security_group(template, parameters["db_security_group_id"], parameters["db_port"], sg)
|
447
|
+
|
448
|
+
final_json = template.to_json()
|
449
|
+
|
450
|
+
f = open(stack_name + "-stack-cf.json", "w+")
|
451
|
+
f.write(final_json)
|
452
|
+
f.close()
|
453
|
+
|
454
|
+
|
455
|
+
# Chammo o Gerador do Arquivo
|
456
|
+
gerador()
|