1+
2+ AWSTemplateFormatVersion : ' 2010-09-09'
3+ Description : Launch EC2 instance with user data script downloaded from Github and dynamic parameters
4+ Metadata :
5+ AWS::CloudFormation::Interface :
6+ ParameterGroups :
7+ - Label :
8+ default : " EC2 Configuration"
9+ Parameters :
10+ - OperationSystem
11+ - InstanceType
12+ - InstanceName
13+ - KeyName
14+ - VpcId
15+ - SubnetId
16+ - Label :
17+ default : " AMI Configuration"
18+ Parameters :
19+ - LatestLinuxAMI
20+ - LatestWindowsAMI
21+ - Label :
22+ default : " FSxN Configuration"
23+ Parameters :
24+ - SecretArn
25+ - ManagementEndpointIP
26+ - VolumeName
27+ - VolumeSize
28+ - SvmName
29+ - Username
30+ - DriveLetter
31+ - Label :
32+ default : " Networking"
33+ Parameters :
34+ - CidrIp
35+ - Label :
36+ default : " User Data Scripts"
37+ Parameters :
38+ - LinuxUserDataUrl
39+ - WindowsUserDataUrl
40+ ParameterLabels :
41+ OperationSystem :
42+ default : " Operating System"
43+ InstanceType :
44+ default : " Instance Type"
45+ InstanceName :
46+ default : " Instance Name"
47+ KeyName :
48+ default : " Key Pair Name"
49+ VpcId :
50+ default : " VPC ID"
51+ SubnetId :
52+ default : " Subnet ID"
53+ SecretArn :
54+ default : " AWS Secret ARN"
55+ ManagementEndpointIP :
56+ default : " Management Endpoint IP"
57+ VolumeName :
58+ default : " Volume Name"
59+ VolumeSize :
60+ default : " Volume Size (GiB)"
61+ SvmName :
62+ default : " SVM Name"
63+ Username :
64+ default : " Username"
65+ DriveLetter :
66+ default : " Drive Letter (Windows Only)"
67+ CidrIp :
68+ default : " CIDR IP for SSH/RDP Access"
69+ LinuxUserDataUrl :
70+ default : " Linux User Data Script URL"
71+ WindowsUserDataUrl :
72+ default : " Windows User Data Script URL"
73+ LatestLinuxAMI :
74+ default : " Linux AMI"
75+ LatestWindowsAMI :
76+ default : " Windows AMI"
77+
78+ Parameters :
79+ OperationSystem :
80+ Type : String
81+ AllowedValues :
82+ - Linux
83+ - Windows
84+ InstanceType :
85+ Type : String
86+ Default : t3.large
87+ Description : EC2 instance type
88+ InstanceName :
89+ Type : String
90+ Description : EC2 instance name
91+ KeyName :
92+ Type : AWS::EC2::KeyPair::KeyName
93+ Description : Name of an existing EC2 KeyPair
94+ VpcId :
95+ Type : AWS::EC2::VPC::Id
96+ Description : VPC ID
97+ SubnetId :
98+ Type : AWS::EC2::Subnet::Id
99+ Description : Subnet ID
100+ SecretArn :
101+ Type : String
102+ Description : Full ARN of the AWS Secrets Manager secret containing FSxN credentials
103+ ManagementEndpointIP :
104+ Type : String
105+ Description : Management endpoint IP Address of your FSxN
106+ VolumeName :
107+ Type : String
108+ Description : Volume Name
109+ VolumeSize :
110+ Type : Number
111+ Description : Volume Size in GiB
112+ SvmName :
113+ Type : String
114+ Default : fsx
115+ Description : SVM Name
116+ Username :
117+ Type : String
118+ Description : Username to run under
119+ DriveLetter :
120+ Type : String
121+ Default : d
122+ Description : Drive Letter - valid for Windows only
123+ CidrIp :
124+ Type : String
125+ Description : CIDR IP for SSH access to the instance
126+ LinuxUserDataUrl :
127+ Type : String
128+ Default : https://raw.githubusercontent.com/NetApp/FSx-ONTAP-samples-scripts/refs/heads/main/Management-Utilities/ec2-user-data-iscsi-create-and-mount/linux_userData.sh
129+ Description : URL to Linux user data script
130+ WindowsUserDataUrl :
131+ Type : String
132+ Default : https://raw.githubusercontent.com/NetApp/FSx-ONTAP-samples-scripts/refs/heads/main/Management-Utilities/ec2-user-data-iscsi-create-and-mount/windows_userData.ps1
133+ Description : URL to Windows user data script
134+ LatestLinuxAMI :
135+ Type : ' AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
136+ Default : ' /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64'
137+ Description : ' Linux AMI to use for the EC2 instance (default is the latest Amazon Linux 2023)'
138+ LatestWindowsAMI :
139+ Type : ' AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
140+ Default : ' /aws/service/ami-windows-latest/TPM-Windows_Server-2025-English-Full-Base'
141+ Description : ' Windows AMI to use for the EC2 instance (default is the latest Windows Server 2025)'
142+
143+ Conditions :
144+ IsLinux : !Equals [ !Ref OperationSystem, "Linux" ]
145+ IsWindows : !Equals [ !Ref OperationSystem, "Windows" ]
146+
147+ Resources :
148+ EC2InstanceSecurityGroup :
149+ Type : AWS::EC2::SecurityGroup
150+ Properties :
151+ GroupDescription : Security group for the EC2 instance
152+ VpcId : !Ref VpcId
153+ SecurityGroupIngress :
154+ - IpProtocol : tcp
155+ FromPort : !If
156+ - IsLinux
157+ - 22
158+ - 3389
159+ ToPort : !If
160+ - IsLinux
161+ - 22
162+ - 3389
163+ CidrIp : !Ref CidrIp
164+ EC2InstanceRole :
165+ Type : AWS::IAM::Role
166+ Properties :
167+ AssumeRolePolicyDocument :
168+ Version : ' 2012-10-17'
169+ Statement :
170+ - Effect : Allow
171+ Principal :
172+ Service : ec2.amazonaws.com
173+ Action : sts:AssumeRole
174+ Path : /
175+
176+ Policies :
177+ - PolicyName : " LambdaPolicy"
178+ PolicyDocument :
179+ Version : " 2012-10-17"
180+ Statement :
181+ - Effect : " Allow"
182+ Action :
183+ - " secretsManager:GetSecretValue"
184+ Resource :
185+ - !Ref SecretArn
186+
187+ EC2InstanceProfile :
188+ Type : AWS::IAM::InstanceProfile
189+ Properties :
190+ Roles :
191+ - !Ref EC2InstanceRole
192+ MyEC2Instance :
193+ Type : AWS::EC2::Instance
194+ Properties :
195+ InstanceType : !Ref InstanceType
196+ ImageId : !If [IsLinux, !Ref LatestLinuxAMI, !Ref LatestWindowsAMI]
197+ KeyName : !Ref KeyName
198+ SecurityGroupIds :
199+ - !Ref EC2InstanceSecurityGroup
200+ SubnetId : !Ref SubnetId
201+ IamInstanceProfile : !Ref EC2InstanceProfile
202+ Tags :
203+ - Key : Name
204+ Value : !Ref InstanceName
205+ UserData : !If
206+ - IsLinux
207+ - Fn::Base64 : !Sub |
208+ # !/bin/bash
209+ curl -o /tmp/userdata-script.sh ${LinuxUserDataUrl}
210+ chmod +x /tmp/userdata-script.sh
211+ # Pass parameters to the script
212+ /tmp/userdata-script.sh "${SecretArn}" "${ManagementEndpointIP }" "${VolumeName}" "${VolumeSize}" "${SvmName}" "${Username}"
213+ - Fn::Base64 : !Sub |
214+ <powershell>
215+ Invoke-WebRequest -Uri ${WindowsUserDataUrl} -OutFile C:\userdata-script.ps1
216+ (Get-Content 'C:\userdata-script.ps1') | Where-Object { $_ -notmatch '^<powershell>$|^</powershell>$' } | Set-Content 'C:\userdata-script.ps1'
217+ powershell.exe -ExecutionPolicy Bypass -File C:\userdata-script.ps1 -SecretIdParam "${SecretArn}" -FSxNAdminIpParam "${ManagementEndpointIP }" -VolumeNameParam "${VolumeName}" -VolumeSizeParam "${VolumeSize}" -DriveLetterParam "${DriveLetter}" -SvmNameParam "${SvmName}" -UsernameParam "${Username}"
218+ </powershell>
219+ Outputs :
220+ InstanceId :
221+ Description : EC2 Instance ID
222+ Value : !Ref MyEC2Instance
0 commit comments