1+ AWSTemplateFormatVersion : ' 2010-09-09'
2+ Description : Launch EC2 instance with user data script downloaded from S3 and dynamic parameters
3+
4+ Parameters :
5+ OperationSystem :
6+ Type : String
7+ AllowedValues :
8+ - Linux
9+ - Windows
10+ InstanceType :
11+ Type : String
12+ Default : t3.micro
13+ Description : EC2 instance type
14+ InstanceName :
15+ Type : String
16+ Description : EC2 instance name
17+ KeyName :
18+ Type : AWS::EC2::KeyPair::KeyName
19+ Description : Name of an existing EC2 KeyPair
20+ ImageId :
21+ Type : AWS::EC2::Image::Id
22+ Description : AMI ID for the instance
23+ VpcId :
24+ Type : AWS::EC2::VPC::Id
25+ Description : VPC ID
26+ SubnetId :
27+ Type : AWS::EC2::Subnet::Id
28+ Description : Subnet ID
29+ SecretName :
30+ Type : String
31+ Description : Aws Secret name
32+ AWSRegion :
33+ Type : String
34+ Description : AWS Region
35+ FSxNAdminIp :
36+ Type : String
37+ Description : FSxN Admin IP
38+ VolumeName :
39+ Type : String
40+ Description : Volume Name
41+ VolumeSize :
42+ Type : Number
43+ Description : Volume Size in GiB
44+ SvmName :
45+ Type : String
46+ Default : fsx
47+ Description : SVM Name
48+ DriveLetter :
49+ Type : String
50+ Default : d
51+ Description : Drive Letter - valid for Windows only
52+ CidrIp :
53+ Type : String
54+ Default : 0.0.0.0/0 # For testing; restrict to your IP for production
55+ Description : CIDR IP for SSH access to the instance
56+ LinuxUserDataUrl :
57+ Type : String
58+ 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
59+ Description : URL to Linux user data script
60+ WindowsUserDataUrl :
61+ Type : String
62+ 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
63+ Description : URL to Windows user data script
64+
65+ Conditions :
66+ IsLinux : !Equals [ !Ref OperationSystem, "Linux" ]
67+ IsWindows : !Equals [ !Ref OperationSystem, "Windows" ]
68+
69+ Resources :
70+ EC2InstanceSecurityGroup :
71+ Type : AWS::EC2::SecurityGroup
72+ Properties :
73+ GroupDescription : Security group for the EC2 instance
74+ VpcId : !Ref VpcId
75+ SecurityGroupIngress :
76+ - IpProtocol : tcp
77+ FromPort : !If
78+ - IsLinux
79+ - 22
80+ - 3389
81+ ToPort : !If
82+ - IsLinux
83+ - 22
84+ - 3389
85+ CidrIp : !Ref CidrIp
86+ EC2InstanceRole :
87+ Type : AWS::IAM::Role
88+ Properties :
89+ AssumeRolePolicyDocument :
90+ Version : ' 2012-10-17'
91+ Statement :
92+ - Effect : Allow
93+ Principal :
94+ Service : ec2.amazonaws.com
95+ Action : sts:AssumeRole
96+ Path : /
97+ ManagedPolicyArns :
98+ - arn:aws:iam::aws:policy/SecretsManagerReadWrite
99+
100+ EC2InstanceProfile :
101+ Type : AWS::IAM::InstanceProfile
102+ Properties :
103+ Roles :
104+ - !Ref EC2InstanceRole
105+ MyEC2Instance :
106+ Type : AWS::EC2::Instance
107+ Properties :
108+ InstanceType : !Ref InstanceType
109+ ImageId : !Ref ImageId
110+ KeyName : !Ref KeyName
111+ SecurityGroupIds :
112+ - !Ref EC2InstanceSecurityGroup
113+ SubnetId : !Ref SubnetId
114+ IamInstanceProfile : !Ref EC2InstanceProfile
115+ Tags :
116+ - Key : Name
117+ Value : !Ref InstanceName
118+ UserData : !If
119+ - IsLinux
120+ - Fn::Base64 : !Sub |
121+ # !/bin/bash
122+ curl -o /tmp/userdata-script.sh ${LinuxUserDataUrl}
123+ chmod +x /tmp/userdata-script.sh
124+ # Pass parameters to the script
125+ /tmp/userdata-script.sh "${SecretName}" "${AWSRegion}" "${FSxNAdminIp}" "${VolumeName}" "${VolumeSize}" "${SvmName}"
126+ - Fn::Base64 : !Sub |
127+ <powershell>
128+ Invoke-WebRequest -Uri ${WindowsUserDataUrl} -OutFile C:\userdata-script.ps1
129+ (Get-Content 'C:\userdata-script.ps1') | Where-Object { $_ -notmatch '^<powershell>$|^</powershell>$' } | Set-Content 'C:\userdata-script.ps1'
130+ powershell.exe -ExecutionPolicy Bypass -File C:\userdata-script.ps1 -SecretIdParam "${SecretName}" -FSxNAdminIpParam "${FSxNAdminIp}" -VolumeNameParam "${VolumeName}" -VolumeSizeParam "${VolumeSize}" -DriveLetterParam "${DriveLetter}" -SvmNameParam "${SvmName}"
131+ </powershell>
132+ Outputs :
133+ InstanceId :
134+ Description : EC2 Instance ID
135+ Value : !Ref MyEC2Instance
0 commit comments