-
Notifications
You must be signed in to change notification settings - Fork 7
/
ec2_approval_template.yaml
223 lines (220 loc) · 6.63 KB
/
ec2_approval_template.yaml
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
AWSTemplateFormatVersion: '2010-09-09'
Description: 'A sample AWS Cloudformation template to demonstrate a provisioning of EC2 Linux Instance from defined list of instance types.
This template has a wait condition which can be used to trigger a approval workflow. This template depends on a stack to import values
that are required to lookup a linux ami id and also trigger approval workflow'
Metadata:
LICENSE: >-
MIT No Attribution
Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Parameters:
BusinessEntity:
Type: String
AllowedValues:
- business_entity_1
- business_entity_2
- business_entity_3
- business_entity_4
ConstraintDescription: must be the name of the business entity
Description: Name of the Business Entity
Default: business_entity_1
InstanceType:
AllowedValues:
- t2.nano
- t2.micro
- t2.small
- t2.medium
- t2.large
- t2.xlarge
- m4.large
- m4.xlarge
- m4.2xlarge
- m4.4xlarge
- m4.10xlarge
- c4.large
- c4.xlarge
- c4.2xlarge
- c4.4xlarge
- c4.8xlarge
- r3.large
- r3.xlarge
- r3.2xlarge
- r3.4xlarge
- r3.8xlarge
- i2.xlarge
- i2.2xlarge
- i2.4xlarge
- i2.8xlarge
- d2.xlarge
- d2.2xlarge
- d2.4xlarge
- d2.8xlarge
ConstraintDescription: must be a valid EC2 instance type.
Default: t2.small
Description: EC2 instance type
Type: String
KeyName:
ConstraintDescription: must be the name of an existing EC2 KeyPair.
Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
Type: AWS::EC2::KeyPair::KeyName
SSHLocation:
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.
Default: 0.0.0.0/0
Description: The IP address range that can be used to SSH to the EC2 instances
MaxLength: '18'
MinLength: '9'
Type: String
UserEmail:
AllowedPattern: '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}'
ConstraintDescription: This is not a valid email id.
Default: "[email protected]"
Description: Enter your Email ID. You will be contacted by approver for more information.
MaxLength: '64'
MinLength: '1'
Type: String
Mappings:
AWSInstanceType2Arch:
t2.micro:
Arch: HVM64
t2.small:
Arch: HVM64
t2.medium:
Arch: HVM64
t2.nano:
Arch: HVM64
t2.xlarge:
Arch: HVM64
t2.large:
Arch: HVM64
t2.2xlarge:
Arch: HVM64
m4.large:
Arch: HVM64
m4.xlarge:
Arch: HVM64
m4.2xlarge:
Arch: HVM64
m4.4xlarge:
Arch: HVM64
m4.10xlarge:
Arch: HVM64
c4.large:
Arch: HVM64
c4.xlarge:
Arch: HVM64
c4.2xlarge:
Arch: HVM64
c4.4xlarge:
Arch: HVM64
c4.8xlarge:
Arch: HVM64
r3.large:
Arch: HVM64
r3.xlarge:
Arch: HVM64
r3.2xlarge:
Arch: HVM64
r3.4xlarge:
Arch: HVM64
r3.8xlarge:
Arch: HVM64
i2.xlarge:
Arch: HVM64
i2.2xlarge:
Arch: HVM64
i2.4xlarge:
Arch: HVM64
i2.8xlarge:
Arch: HVM64
d2.xlarge:
Arch: HVM64
d2.2xlarge:
Arch: HVM64
d2.4xlarge:
Arch: HVM64
d2.8xlarge:
Arch: HVM64
Resources:
WaitHandle:
Type: 'AWS::CloudFormation::WaitConditionHandle'
WaitCondition:
Type: 'AWS::CloudFormation::WaitCondition'
Properties:
Handle:
Ref: 'WaitHandle'
Timeout: '43200'
SaveRequestFunction:
Type: Custom::SaveRequestFunction
Properties:
ServiceToken:
!ImportValue "<name-of-stack-deployed-in-step-1>-SaveRequestLambda" # Replace with output of CloudFormation template deployed in Step 1
WaitUrl: !Ref WaitHandle
EmailID: !Ref UserEmail
ImageId: !GetAtt GetAMIInfo.Id
InstanceType: !Ref InstanceType
ProductName: EC2-LINUX
BusinessEntity: !Ref BusinessEntity
StackName: !Ref AWS::StackName
EC2Pricing: !GetAtt GetEC2PricingInfo.Pricing
GetAMIInfo:
Type: Custom::GetAMIInfo
Properties:
ServiceToken:
!ImportValue "<name-of-stack-deployed-in-step-1>-LinuxAMILookupLambda" # Replace with output of CloudFormation template deployed in Step 1
Architecture: !FindInMap [AWSInstanceType2Arch, !Ref InstanceType, Arch]
GetEC2PricingInfo:
Type: Custom::GetEC2PricingInfo
Properties:
ServiceToken:
!ImportValue "<name-of-stack-deployed-in-step-1>-EC2PricingLambda" # Replace with output of CloudFormation template deployed in Step 1
InstanceType: !Ref InstanceType
OperatingSystem: Linux
TermType: OnDemand
LinuxEC2Instance:
Type: AWS::EC2::Instance
DependsOn: 'WaitCondition'
Properties:
ImageId: !GetAtt GetAMIInfo.Id
InstanceType:
Ref: InstanceType
KeyName:
Ref: KeyName
SecurityGroups:
- Ref: LinuxEC2SecurityGroup
Tags:
- Key: business-entity
Value: !Ref BusinessEntity
LinuxEC2SecurityGroup:
Type: AWS::EC2::SecurityGroup
Metadata:
cfn_nag:
rules_to_suppress:
- id: W40
reason: 'this is a sample template demonstrating ec2 in public subnet'
DependsOn: 'WaitCondition'
Properties:
GroupDescription: "Enable ssh access via port 22 to specified CIDR"
SecurityGroupEgress:
- IpProtocol: -1
Description: 'allow outbound traffic'
SecurityGroupIngress:
- CidrIp: !Ref SSHLocation
Description: 'allow ssh access'
FromPort: '22'
IpProtocol: tcp
ToPort: '22'
Outputs:
EC2PublicIP:
Description: Requested EC2 public IP
Value: !GetAtt LinuxEC2Instance.PublicIp