forked from ltbl/sentry-cloudformation-teamplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentry-aws.template
318 lines (276 loc) · 12 KB
/
sentry-aws.template
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template Sentry",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type" : "String",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern" : "[-_ a-zA-Z0-9]*",
"ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
},
"SentryEmail" : {
"Description" : "Sentry email",
"Type": "String",
"MinLength": "1",
"MaxLength": "64"
},
"SentryUserName" : {
"Default": "sentry",
"Description" : "Sentry superuser name",
"Type": "String",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
},
"SentryPassword" : {
"NoEcho": "true",
"Description" : "Sentry superuser password",
"Type": "String",
"MinLength": "1",
"MaxLength": "64"
},
"DBName": {
"Default": "sentry",
"Description" : "Sentry database name",
"Type": "String",
"MinLength": "1",
"MaxLength": "64",
"AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
},
"DBUsername": {
"Default": "sentry",
"Description" : "Username for PostgreSQL database access",
"Type": "String",
"MinLength": "1",
"MaxLength": "16",
"AllowedPattern" : "[a-zA-Z][a-zA-Z0-9]*",
"ConstraintDescription" : "must begin with a letter and contain only alphanumeric characters."
},
"DBPassword": {
"NoEcho": "true",
"Description" : "Password PostgreSQL database access",
"Type": "String",
"MinLength": "1",
"MaxLength": "41",
"AllowedPattern" : "[a-zA-Z0-9]*",
"ConstraintDescription" : "must contain only alphanumeric characters."
},
"InstanceType" : {
"Description" : "WebServer EC2 instance type",
"Type" : "String",
"Default" : "t1.micro",
"AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge"],
"ConstraintDescription" : "must be a valid EC2 instance type."
}
},
"Mappings" : {
"AWSInstanceType2Arch" : {
"t1.micro" : { "Arch" : "64" },
"m1.small" : { "Arch" : "64" },
"m1.medium" : { "Arch" : "64" },
"m1.large" : { "Arch" : "64" },
"m1.xlarge" : { "Arch" : "64" },
"m2.xlarge" : { "Arch" : "64" },
"m2.2xlarge" : { "Arch" : "64" },
"m2.4xlarge" : { "Arch" : "64" },
"c1.medium" : { "Arch" : "64" },
"c1.xlarge" : { "Arch" : "64" },
"cc1.4xlarge" : { "Arch" : "64HVM" },
"cc2.8xlarge" : { "Arch" : "64HVM" },
"cg1.4xlarge" : { "Arch" : "64HVM" }
},
"AWSRegionArch2AMI" : {
"us-east-1" : { "32" : "ami-ac9943c5", "64" : "ami-a29943cb", "64HVM" : "ami-0da96764" },
"us-west-2" : { "32" : "ami-3e800c0e", "64" : "ami-20800c10", "64HVM" : "NOT_YET_SUPPORTED" },
"us-west-1" : { "32" : "ami-85712ac0", "64" : "ami-87712ac2", "64HVM" : "NOT_YET_SUPPORTED" },
"eu-west-1" : { "32" : "ami-e7e8d393", "64" : "ami-e1e8d395", "64HVM" : "NOT_YET_SUPPORTED" },
"ap-southeast-1" : { "32" : "ami-a6ca8df4", "64" : "ami-a4ca8df6", "64HVM" : "NOT_YET_SUPPORTED" },
"ap-northeast-1" : { "32" : "ami-5ec7775f", "64" : "ami-60c77761", "64HVM" : "NOT_YET_SUPPORTED" },
"sa-east-1" : { "32" : "ami-92d8068f", "64" : "ami-8cd80691", "64HVM" : "NOT_YET_SUPPORTED" }
}
},
"Resources" : {
"CfnUser" : {
"Type" : "AWS::IAM::User",
"Properties" : {
"Path": "/",
"Policies": [{
"PolicyName": "root",
"PolicyDocument": { "Statement":[{
"Effect":"Allow",
"Action":"cloudformation:DescribeStackResource",
"Resource":"*"
}]}
}]
}
},
"HostKeys" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : {"Ref": "CfnUser"}
}
},
"SentryWebServer" : {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
"apt" : { }
},
"sources" : {
},
"files" : {
"/home/ubuntu/.sentry/supervisord.conf" : {
"content" : { "Fn::Join" : ["", [
"[program:sentry-web]","\n",
"user=www-data\n",
"group=www-data\n",
"directory=/home/ubuntu/sentry","\n",
"command=/home/ubuntu/sentry/env/bin/sentry --config=/home/ubuntu/.sentry/sentry.conf.py start", "\n",
"environment=PATH=\"/home/ubuntu/sentry/env/bin\"", "\n",
"autostart=true", "\n",
"autorestart=true", "\n",
"redirect_stderr=true", "\n"
]]},
"mode" : "000644",
"owner" : "ubuntu",
"group" : "ubuntu"
},
"/home/ubuntu/.sentry/sentry.conf.py" : {
"content" : { "Fn::Join" : ["", [
"DATABASES = {\n",
" 'default': {\n",
" 'ENGINE': 'django.db.backends.postgresql_psycopg2',\n",
" 'NAME': '", { "Ref" : "DBName" }, "',\n",
" 'USER': '", { "Ref" : "DBUsername" }, "',\n",
" 'PASSWORD': '", { "Ref" : "DBPassword" }, "',\n",
" 'HOST': 'localhost',\n",
" 'PORT': '',\n",
" }\n",
"}\n",
"SENTRY_WEB_HOST = '0.0.0.0'\n",
"SENTRY_WEB_PORT = 9000\n",
"SENTRY_WEB_OPTIONS = { 'workers': 4, 'worker_class': 'gevent' }"
]]},
"mode" : "000644",
"owner" : "ubuntu",
"group" : "ubuntu"
},
"/home/ubuntu/.sentry/sentry.nginx.conf" : {
"content" : { "Fn::Join" : ["", [
"upstream sentry {\n",
" server 127.0.0.1:9000 fail_timeout=0;\n",
"}\n\n",
"server {\n",
" listen 80 default deferred;\n",
" server_name _;\n\n",
" access_log /var/log/nginx/sentry.access.log;\n\n",
" root /home/ubuntu/sentry;\n\n",
" location / {\n",
" proxy_set_header X-Real-IP $remote_addr;\n",
" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n",
" proxy_set_header Host $http_host;\n\n",
" proxy_pass http://sentry;\n",
" proxy_redirect off;\n",
" }\n",
"}"
]]},
"mode" : "000644",
"owner" : "ubuntu",
"group" : "ubuntu"
}
}
}
}
},
"Properties": {
"ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
"InstanceType" : { "Ref" : "InstanceType" },
"SecurityGroups" : [ {"Ref" : "SentryWebServerGroup"} ],
"KeyName" : { "Ref" : "KeyName" },
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"function error_exit\n",
"{\n",
" cfn-signal -e 1 -r \"$1\" '", { "Ref" : "SentryWebServerWaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"function virtual_py\n",
"{\n",
" sudo -u ubuntu /home/ubuntu/sentry/env/bin/python $* || error_exit \"Failed to $*\"\n",
"}\n",
"apt-get install -y build-essential python python-dev python-setuptools libevent-dev supervisor nginx postgresql postgresql-server-dev-all\n",
"easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-1.0-6.tar.gz\n",
"cfn-init -s ", { "Ref" : "AWS::StackName" }, " -r SentryWebServer ",
" --access-key ", { "Ref" : "HostKeys" },
" --secret-key ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]},
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"sudo -u postgres createuser -D -S -R ", { "Ref" : "DBUsername" },
" || error_exit 'Failed to create pgsql user'\n",
"sudo -u postgres createdb -O ", { "Ref" : "DBUsername" },
" ", { "Ref" : "DBName" }, " || error_exit 'Failed to create pgsql db'\n",
"sudo -u postgres psql -c \"alter user ", { "Ref" : "DBUsername" },
" with encrypted password '", { "Ref" : "DBPassword" }, "';\"",
" || error_exit 'Failed to setup pgsql password'\n",
"sudo -u ubuntu mkdir -p /home/ubuntu/sentry\n",
"easy_install -U virtualenv || error_exit 'Failed to Setup virtualenv'\n",
"sudo -u ubuntu virtualenv /home/ubuntu/sentry/env || error_exit 'Failed to init virtualenv'\n",
"echo -e \"\nSENTRY_KEY = '`openssl rand -hex 8`'\n\" >> /home/ubuntu/.sentry/sentry.conf.py\n",
"cat /home/ubuntu/.sentry/sentry.conf.py\n",
"virtual_py /home/ubuntu/sentry/env/bin/pip install psycopg2 gevent sentry >> /home/ubuntu/.pip.log 2>&1\n",
"virtual_py /home/ubuntu/sentry/env/bin/sentry --config=/home/ubuntu/.sentry/sentry.conf.py upgrade --noinput\n",
"echo \"from django.contrib.auth.models import User;",
"User.objects.create_superuser('", { "Ref" : "SentryUserName" }, "'",
", '", { "Ref" : "SentryEmail" }, "'",
", '", { "Ref" : "SentryPassword" }, "'",
")\"",
" | virtual_py /home/ubuntu/sentry/env/bin/sentry --config=/home/ubuntu/.sentry/sentry.conf.py shell\n",
"virtual_py /home/ubuntu/sentry/env/bin/sentry repair",
" --owner=", { "Ref" : "SentryUserName" }, "\n",
"echo -e \"\n# sentry\n\" >> /etc/supervisor/supervisord.conf\n",
"cat /home/ubuntu/.sentry/supervisord.conf >> /etc/supervisor/supervisord.conf\n",
"supervisorctl update\n",
"unlink /etc/nginx/sites-enabled/default\n",
"cp /home/ubuntu/.sentry/sentry.nginx.conf /etc/nginx/sites-available/sentry.conf\n",
"ln -s /etc/nginx/sites-available/sentry.conf /etc/nginx/sites-enabled/sentry.conf\n",
"/etc/init.d/nginx restart\n",
"# All is well so signal success\n",
"cfn-signal -e 0 -r \"Sentry application setup complete\" '", { "Ref" : "SentryWebServerWaitHandle" }, "'\n"
]]}}
}
},
"SentryWebServerWaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
},
"SentryWebServerWaitCondition" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "SentryWebServer",
"Properties" : {
"Handle" : {"Ref" : "SentryWebServerWaitHandle"},
"Timeout" : "1500"
}
},
"SentryWebServerGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable HTTP access via port 80",
"SecurityGroupIngress" : [
{"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0"},
{"IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0"}
]
}
}
},
"Outputs" : {
"SentryURL" : {
"Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "SentryWebServer", "PublicDnsName" ]} ]] },
"Description" : "URL for newly created Sentry application"
}
}
}