-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathubuntu-developer-machine.yaml
executable file
·320 lines (285 loc) · 9.61 KB
/
ubuntu-developer-machine.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
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
319
320
AWSTemplateFormatVersion: 2010-09-09
Description: >-
AWS Ubuntu Developer Machine
Parameters:
AWSUbuntuAMIType:
Description: Ubuntu Pro 22.04 LTS.
Type: String
Default: "UbuntuPro2204LTS"
AllowedValues:
- "UbuntuPro2204LTS"
VpcId:
Description: Machine VPC ID
Type: 'AWS::EC2::VPC::Id'
VpcSubnetId:
Description: Machine VPC Subnet ID. Subnet must be public for access over Internet.
Type: 'AWS::EC2::Subnet::Id'
EbsVolumeSize:
Default: 200
Description: Ebs volume size (GB)
Type: Number
MinValue: 200
EbsVolumeType:
Default: 'gp3'
Description: Ebs volume type
Type: String
AllowedValues:
- 'gp2'
- 'gp3'
SecurityGroupId:
Description: >-
(Optional) Advanced option to specify existing Machine Security Group Id.
Leave blank to create new Security Group.
Type: String
AllowedPattern: '(^sg-[0-9a-z]+)$|()$'
Default: ''
ConstraintDescription: Should be a Valid SecurityGroup Id in selected VPC, or blank
KeyName:
Description: >-
Name of an existing Amazon EC2 KeyPair to enable SSH and DCV access to the
Machine
Type: 'AWS::EC2::KeyPair::KeyName'
EC2InstanceType:
Description: EC2 instance type
Type: String
Default: m5.xlarge
AllowedValues:
- t3.xlarge
- t3a.xlarge
- m5.xlarge
- m5a.xlarge
SecurityGroupAccessCIDR:
Description: >-
Restrict Machine access for SSH and DCV client from a valid CIDR range
Type: String
MinLength: '9'
MaxLength: '18'
AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
ConstraintDescription: Must be a valid CIDR range of the form x.x.x.x/x
EBSOptimized:
Description: >-
Is the instance EBS optimized?
Type: String
Default: 'true'
AllowedValues:
- 'false'
- 'true'
UbuntuAMIOverride:
Description: >-
(Optional) Advanced option to override the default Ubuntu AMI.
Leave blank, if unsure.
Type: String
AllowedPattern: '(ami-[0-9a-z]{17})?'
Mappings:
UbuntuPro2204LTS:
us-east-1:
AMI: ami-0c460fdf6a8a1edef
us-east-2:
AMI: ami-0bddc40b31973ff95
us-west-2:
AMI: ami-058168290d30b9c52
eu-west-1:
AMI: ami-0a0aadde3561fdc1e
eu-central-1:
AMI: ami-04bd4f5e1a831e002
ap-southeast-1:
AMI: ami-0fd4144f52678fe37
ap-southeast-2:
AMI: ami-0ac438f9a63fdd525
ap-south-1:
AMI: ami-05205cb302b6dca6f
ap-northeast-1:
AMI: ami-096d2286904664854
ap-northeast-2:
AMI: ami-00324b863a508e247
Conditions:
OverrideAMI: !Not
- !Equals
- !Ref UbuntuAMIOverride
- ''
CreateNewSecurityGroup: !Equals
- !Ref SecurityGroupId
- ''
Resources:
MachineSecurityGroup:
Condition: CreateNewSecurityGroup
Type: 'AWS::EC2::SecurityGroup'
Properties:
GroupDescription: Machine security group
VpcId: !Ref VpcId
Tags:
- Key: Name
Value: !Ref 'AWS::StackName'
MachineSecurityGroupCIDRIngressSSH:
Condition: CreateNewSecurityGroup
Type: 'AWS::EC2::SecurityGroupIngress'
Properties:
Description: Ingress from CIDR for SSH access to graphics Machine
GroupId: !GetAtt MachineSecurityGroup.GroupId
CidrIp: !Ref SecurityGroupAccessCIDR
IpProtocol: tcp
FromPort: 22
ToPort: 22
MachineSecurityGroupCIDREgress:
Condition: CreateNewSecurityGroup
Type: 'AWS::EC2::SecurityGroupEgress'
Properties:
Description: Egress rule for out bound traffic
GroupId: !GetAtt MachineSecurityGroup.GroupId
IpProtocol: tcp
FromPort: 0
ToPort: 65535
CidrIp: '0.0.0.0/0'
InstanceRole:
Type: 'AWS::IAM::Role'
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Path: /
ManagedPolicyArns:
- arn:aws:iam::aws:policy/PowerUserAccess
Policies:
- PolicyName: iam-policies
PolicyDocument:
Statement:
- Effect: Allow
Action:
- "iam:GetRole"
- "iam:CreateRole"
- "iam:AttachRolePolicy"
- "iam:PutRolePolicy"
- "iam:DeleteRolePolicy"
- "iam:DetachRolePolicy"
- "iam:DeleteRole"
Resource:
- !Sub 'arn:aws:iam::${AWS::AccountId}:role/cdk*${AWS::AccountId}-${AWS::Region}'
InstanceProfile:
Type: 'AWS::IAM::InstanceProfile'
Properties:
Path: /
Roles:
- !Ref InstanceRole
MachineLaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
MetadataOptions:
HttpTokens: "required"
HttpEndpoint: "enabled"
MachineInstance:
Type: 'AWS::EC2::Instance'
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref MachineLaunchTemplate
Version: !GetAtt MachineLaunchTemplate.LatestVersionNumber
ImageId: !If
- OverrideAMI
- !Ref UbuntuAMIOverride
- !FindInMap
- !Ref 'AWSUbuntuAMIType'
- !Ref 'AWS::Region'
- AMI
InstanceType: !Ref EC2InstanceType
EbsOptimized: !Ref EBSOptimized
IamInstanceProfile: !Ref InstanceProfile
BlockDeviceMappings:
- DeviceName: "/dev/sda1"
Ebs:
VolumeSize: !Ref EbsVolumeSize
VolumeType: !Ref EbsVolumeType
Encrypted: true
DeleteOnTermination: true
NetworkInterfaces:
- AssociatePublicIpAddress: true
DeviceIndex: "0"
GroupSet:
- !If
- CreateNewSecurityGroup
- !GetAtt MachineSecurityGroup.GroupId
- !Ref SecurityGroupId
SubnetId: !Ref VpcSubnetId
KeyName: !Ref KeyName
Tags:
- Key: "Name"
Value: !Sub '${AWS::StackName}-developer-machine'
UserData: !Base64
'Fn::Join':
- ''
- - |
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash -xe
echo "Cloud init in progress!" > /etc/motd
# Find Ubuntu Version
VERSION=$(lsb_release -a | grep Release | awk -F ":" '{print $2}' | sed -E -e 's/[[:blank:]]+//g')
echo "Detected Ubuntu $VERSION"
# setup graphics Machine
export DEBIAN_FRONTEND=noninteractive
export DEBCONF_NONINTERACTIVE_SEEN=true
# setup software repo for docker
wget -qO - https://download.docker.com/linux/ubuntu/gpg > ./GPG_DOCKER.PUB
gpg --no-default-keyring --keyring ./docker_keyring.gpg --import ./GPG_DOCKER.PUB
gpg --no-default-keyring --keyring ./docker_keyring.gpg --export > ./docker.gpg
mv ./docker.gpg /etc/apt/trusted.gpg.d/
rm ./GPG_DOCKER.PUB
add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
# update and install required packages
apt-get update
apt-get -y install git tar
apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
# install docker if it is not installed
if [ ! -x "$(command -v docker)" ]; then
apt-get -y install docker-ce docker-ce-cli containerd.io
usermod -aG docker ubuntu
fi
# install aws cli
apt-get -y install awscli
# install nodejs
NODE_VERSION=v18.13.0
NODE_DISTRO=linux-x64
wget https://nodejs.org/dist/$NODE_VERSION/node-$NODE_VERSION-$NODE_DISTRO.tar.xz
mkdir -p /usr/local/lib/nodejs
tar -xJvf node-$NODE_VERSION-$NODE_DISTRO.tar.xz -C /usr/local/lib/nodejs
echo "export PATH=/usr/local/lib/nodejs/node-$NODE_VERSION-$NODE_DISTRO/bin:$PATH" > /home/ubuntu/.bashrc
# install aws cdk
export PATH=/usr/local/lib/nodejs/node-$NODE_VERSION-$NODE_DISTRO/bin:$PATH
npm install -g aws-cdk
# Link python to python3
[[ -f /usr/bin/python ]] || ln -s /usr/bin/python3 /usr/bin/python
# install pip
apt-get install -y python3-pip
# install virtual env package
python3 -m pip install virtualenv
echo "AWS developer machine is ready!" > /etc/motd
Outputs:
MachineInstanceId:
Description: Machine instance Id
Value: !Ref MachineInstance
MachineRole:
Description: IAM role attached to Machine instance profile
Value: !GetAtt InstanceRole.Arn
MachineSecurityGroup:
Description: Machine security group
Value: !If
- CreateNewSecurityGroup
- !GetAtt MachineSecurityGroup.GroupId
- !Ref SecurityGroupId