Skip to content

Commit 264bb9e

Browse files
Feat: Added EC2-DCV Support
1 parent b154264 commit 264bb9e

2 files changed

Lines changed: 240 additions & 1 deletion

File tree

cft-templates/ec2-dcv.yml

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
Metadata:
2+
License: Apache-2.0
3+
AWSTemplateFormatVersion: '2010-09-09'
4+
Description: 'AWS CloudFormation Template to create an GPU based EC2 instance with NICE DCV pre installed.'
5+
6+
Mappings:
7+
RegionMap:
8+
us-east-1:
9+
AmiID: "ami-0759d9ecf1fcb7be9"
10+
us-east-2:
11+
AmiID: "ami-018bd425554ab6cf4"
12+
us-west-1:
13+
AmiID: "ami-02258ab533d22fa75"
14+
us-west-2:
15+
AmiID: "ami-02d3838a1d948d3ca"
16+
ap-south-1:
17+
AmiID: "ami-0c33a5a105981b1fb"
18+
ap-southeast-1:
19+
AmiID: "ami-061f216d271a880ce"
20+
ap-southeast-2:
21+
AmiID: "ami-0d1748650516fb8a3"
22+
23+
Parameters:
24+
Namespace:
25+
Type: String
26+
Description: An environment name that will be prefixed to resource names
27+
S3Mounts:
28+
Type: String
29+
Description: A JSON array of objects with name, bucket, and prefix properties used to mount data
30+
IamPolicyDocument:
31+
Type: String
32+
Description: The IAM policy to be associated with the launched workstation
33+
EnvironmentInstanceFiles:
34+
Type: String
35+
Description: >-
36+
An S3 URI (starting with "s3://") that specifies the location of files to be copied to
37+
the environment instance, including any bootstrap scripts
38+
InstanceType:
39+
Description: Choose the instance type for this instance. e.g. t3.large
40+
Type: String
41+
Default: t3.large
42+
AllowedValues: [t3.large, t3.xlarge, t3.2xlarge, g4dn.xlarge, g4dn.2xlarge, g4dn.4xlarge, g4dn.8xlarge, g4dn.12xlarge, g4dn.16xlarge, g4dn.metal, g3.4xlarge, g3.8xlarge, g3.16xlarge]
43+
ConstraintDescription: must be a valid EC2 instance type.
44+
KeyPair:
45+
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance. If no key pairs exist, please create one from the button next to the dropdown. Please contact your Administrator if you are unable to create one.
46+
Type: AWS::EC2::KeyPair::KeyName
47+
ConstraintDescription: must be the name of an existing EC2 KeyPair.
48+
AllowedIpAddress:
49+
Description: The IP address range that can be used to SSH to instance and Connect to DCV
50+
Type: String
51+
MinLength: '9'
52+
MaxLength: '18'
53+
Default: 0.0.0.0/0
54+
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
55+
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
56+
57+
Conditions:
58+
IamPolicyEmpty: !Equals [!Ref IamPolicyDocument, '{}']
59+
60+
Resources:
61+
IAMRole:
62+
Type: 'AWS::IAM::Role'
63+
Properties:
64+
RoleName: !Join ['-', [Ref: Namespace, 'ec2-role']]
65+
Path: '/'
66+
AssumeRolePolicyDocument:
67+
Version: '2012-10-17'
68+
Statement:
69+
- Effect: 'Allow'
70+
Principal:
71+
Service:
72+
- 'ec2.amazonaws.com'
73+
Action:
74+
- 'sts:AssumeRole'
75+
Policies:
76+
- !If
77+
- IamPolicyEmpty
78+
- !Ref 'AWS::NoValue'
79+
- PolicyName: !Join ['-', [Ref: Namespace, 's3-studydata-policy']]
80+
PolicyDocument: !Ref IamPolicyDocument
81+
82+
InstanceProfile:
83+
Type: 'AWS::IAM::InstanceProfile'
84+
Properties:
85+
InstanceProfileName: !Join ['-', [Ref: Namespace, 'ec2-profile']]
86+
Path: '/'
87+
Roles:
88+
- Ref: IAMRole
89+
90+
EC2Instance:
91+
Type: AWS::EC2::Instance
92+
CreationPolicy:
93+
ResourceSignal:
94+
Timeout: PT10M
95+
Properties:
96+
UserData:
97+
Fn::Base64: !Sub |
98+
#!/usr/bin/env bash
99+
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
100+
101+
# Set password for ec2-user
102+
instance_id=$(curl -s "http://169.254.169.254/latest/meta-data/instance-id")
103+
echo "ec2-user:$instance_id" | /usr/sbin/chpasswd
104+
105+
# Create dcv session start script
106+
cat << EOF > /usr/local/bin/start-dcv-session
107+
dcv create-session rg-session --name rg-session --user ec2-user --owner ec2-user
108+
EOF
109+
110+
# Create dcv session on reboot
111+
sudo chown root: "/usr/local/bin/start-dcv-session"
112+
sudo chmod 775 "/usr/local/bin/start-dcv-session"
113+
sudo crontab -l 2>/dev/null > "/tmp/crontab"
114+
sudo sh "/usr/local/bin/start-dcv-session"
115+
echo '@reboot /usr/local/bin/start-dcv-session 2>&1 >> /var/log/start-dcv-session.log' >> "/tmp/crontab"
116+
sudo crontab "/tmp/crontab"
117+
118+
# Download and execute bootstrap script
119+
aws s3 cp "${EnvironmentInstanceFiles}/get_bootstrap.sh" "/tmp"
120+
chmod 500 "/tmp/get_bootstrap.sh"
121+
/tmp/get_bootstrap.sh "${EnvironmentInstanceFiles}" '${S3Mounts}'
122+
123+
# Signal result to CloudFormation
124+
/opt/aws/bin/cfn-signal -e $? --stack "${AWS::StackName}" --resource "EC2Instance" --region "${AWS::Region}"
125+
InstanceType: !Ref 'InstanceType'
126+
SecurityGroups: [!Ref 'InstanceSecurityGroup']
127+
KeyName: !Ref 'KeyPair'
128+
ImageId: !FindInMap
129+
- RegionMap
130+
- !Ref 'AWS::Region'
131+
- AmiID
132+
IamInstanceProfile: !Ref InstanceProfile
133+
PropagateTagsToVolumeOnCreation: true
134+
Tags:
135+
- Key: Name
136+
Value: !Join ['-', [Ref: Namespace, 'ec2-linux']]
137+
- Key: Description
138+
Value: EC2 workspace instance
139+
- Key: cost_resource
140+
Value: !Sub ${AWS::StackName}
141+
142+
IPAddress:
143+
Type: AWS::EC2::EIP
144+
Properties:
145+
Tags:
146+
- Key: cost_resource
147+
Value: !Sub ${AWS::StackName}
148+
149+
IPAssoc:
150+
Type: AWS::EC2::EIPAssociation
151+
Properties:
152+
InstanceId: !Ref 'EC2Instance'
153+
EIP: !Ref 'IPAddress'
154+
155+
InstanceSecurityGroup:
156+
Type: AWS::EC2::SecurityGroup
157+
Properties:
158+
GroupDescription: Enable SSH access
159+
SecurityGroupIngress:
160+
- IpProtocol: tcp
161+
FromPort: '22'
162+
ToPort: '22'
163+
CidrIp: !Ref 'AllowedIpAddress'
164+
- IpProtocol: tcp
165+
FromPort: '8443'
166+
ToPort: '8443'
167+
CidrIp: !Ref 'AllowedIpAddress'
168+
169+
Outputs:
170+
InstanceId:
171+
Description: InstanceId of the newly created EC2 instance
172+
Value: !Ref 'EC2Instance'
173+
InstanceIPAddress:
174+
Description: IP address of the newly created EC2 instance
175+
Value: !GetAtt [EC2Instance, PublicIp]
176+
InstanceDNSName:
177+
Description: DNS name of the newly created EC2 instance
178+
Value: !GetAtt [EC2Instance, PublicDnsName]
179+
ApplicationPort:
180+
Description: The Port in which the application is running
181+
Value: '8443'

config/settings-config.json

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,63 @@
375375
"terminated"
376376
]
377377
},
378+
"ec2-dcv":{
379+
"actions": {
380+
"running" : {
381+
"connect":[
382+
{
383+
"menu": "SSH/RDP",
384+
"imageUrl": "../../assets/images/technology@2x.png"
385+
},
386+
{
387+
"menu": "Remote Desktop",
388+
"imageUrl": "../../assets/images/Screen-icon.png"
389+
}
390+
],
391+
"action":[
392+
{
393+
"action":"Stop",
394+
"imageUrl": "../../assets/images/no-stopping@2x.png"
395+
},
396+
{
397+
"action":"Reboot",
398+
"imageUrl": "../../assets/images/reset@2x.png"
399+
}
400+
]
401+
},
402+
"stopped": {
403+
"connect":[],
404+
"action":[
405+
{
406+
"action":"Start",
407+
"imageUrl": "../../assets/images/play@2x.png"
408+
}
409+
]
410+
},
411+
"default" : {
412+
"connect":[
413+
{
414+
"menu": "SSH/RDP",
415+
"imageUrl": "../../assets/images/technology@2x.png"
416+
}
417+
],
418+
"action":[
419+
{
420+
"action":"Stop",
421+
"imageUrl": "../../assets/images/no-stopping@2x.png"
422+
},
423+
{
424+
"action":"Reboot",
425+
"imageUrl": "../../assets/images/reset@2x.png"
426+
}
427+
]
428+
}
429+
},
430+
"transient_states": ["pending", "shutting-down", "stopping", "deleting"],
431+
"active_states": ["running", "active"],
432+
"stopped_states":["stopped"],
433+
"terminated_states": ["terminated"]
434+
},
378435
"nextflow": {
379436
"actions": {
380437
"running": {
@@ -810,7 +867,8 @@
810867
"rstudio",
811868
"nextflow",
812869
"cromwell",
813-
"pcluster"
870+
"pcluster",
871+
"ec2-dcv"
814872
],
815873
"cft_active_states": [
816874
"available",

0 commit comments

Comments
 (0)