forked from schen1628/ec2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_operator.json
218 lines (206 loc) · 5.78 KB
/
ec2_operator.json
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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Launch an EC2 Operator to perform auto-start and auto-stop instances for all regions.",
"Parameters": {
"InstanceType" : {
"Description" : "Instance type for EC2 Operator",
"Type" : "String",
"Default" : "t2.micro",
"AllowedValues" : [ "t2.micro","t2.small","t2.medium" ],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"KeyName": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type": "String",
"Default": "schen"
},
"SSHLocation" : {
"Description" : " The IP address range that can be used to SSH to the EC2 instance",
"Type": "String",
"MinLength": "9",
"MaxLength": "18",
"Default": "0.0.0.0/0",
"AllowedPattern": "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
"ConstraintDescription": "must be a valid IP CIDR range of the form x.x.x.x/x."
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-08842d60" },
"us-west-1" : { "AMI" : "ami-cfa8a18a" },
"us-west-2" : { "AMI" : "ami-8786c6b7" },
"eu-west-1" : { "AMI" : "ami-748e2903" },
"sa-east-1" : { "AMI" : "ami-956cc688" },
"ap-southeast-1" : { "AMI" : "ami-d6e1c584" },
"ap-southeast-2" : { "AMI" : "ami-fd4724c7" },
"ap-northeast-1" : { "AMI" : "ami-35072834" }
}
},
"Resources": {
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "EC2 Operator Admin Access",
"SecurityGroupIngress": [
{
"IpProtocol": "tcp",
"FromPort": "22",
"ToPort": "22",
"CidrIp": {
"Ref": "SSHLocation"
}
}
]
}
},
"OperatorInstance": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"config" : {
"packages" : {
"yum" : {
"python26-pip" : [],
"gcc" : []
}
},
"files" : {
"/home/ec2-user/ec2_operator.py" : {
"source" : "https://raw2.github.com/schen1628/ec2/master/ec2_operator.py",
"mode" : "000644",
"owner" : "ec2-user",
"group" : "ec2-user"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"RegionMap",
{
"Ref": "AWS::Region"
},
"AMI"
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"IamInstanceProfile": {
"Ref": "OperatorInstanceProfile"
},
"KeyName": {
"Ref": "KeyName"
},
"SecurityGroups": [
{
"Ref": "SecurityGroup"
}
],
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"#!/bin/bash -v\n",
"yum update -y aws-cfn-bootstrap\n",
"# Helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"# Install python-pip, gcc and ec2 oeprator\n",
"/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r OperatorInstance ",
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"# Set up ec2 operator\n",
"pip install croniter\n",
"OPERATOR=/home/ec2-user/ec2_operator.py\n",
"echo \"*/5 * * * * ec2-user python $OPERATOR \" >> /etc/crontab\n",
"# If all is well so signal success\n",
"/opt/aws/bin/cfn-signal -e $? -r \"EC2 Operator setup complete\" '",
{
"Ref": "WaitHandle"
},
"'\n"
]
]
}
}
}
},
"WaitHandle": {
"Type": "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition": {
"Type": "AWS::CloudFormation::WaitCondition",
"DependsOn": "OperatorInstance",
"Properties": {
"Handle": {
"Ref": "WaitHandle"
},
"Timeout": "300"
}
},
"OperatorInstanceProfile": {
"Properties": {
"Path": "/",
"Roles": [
{
"Ref": "OperatorRole"
}
]
},
"Type": "AWS::IAM::InstanceProfile"
},
"OperatorRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"ec2.amazonaws.com"
]
}
}
]
},
"Path": "/",
"Policies": [
{
"PolicyDocument": {
"Statement": [
{
"Action": [
"ec2:DescribeInstances",
"ec2:StartInstances",
"ec2:StopInstances"
],
"Effect": "Allow",
"Resource": "*"
}
]
},
"PolicyName": "EC2OperatorPolicy"
}
]
},
"Type": "AWS::IAM::Role"
}
},
"Outputs": {
"OperatorInstanceId": {
"Value": {
"Ref": "OperatorInstance"
},
"Description": "Instance Id of the ec2 operator instance"
}
}
}