Skip to content

Commit 36a622c

Browse files
committed
Add cloud formation script
1 parent c854fea commit 36a622c

File tree

3 files changed

+143
-5
lines changed

3 files changed

+143
-5
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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

Management-Utilities/ec2-user-data-iscsi-create-and-mount/linux_userData.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ AWS_REGION="${AWS_REGION:=$2}"
2121
FSXN_ADMIN_IP="${FSXN_ADMIN_IP:=$3}"
2222
VOLUME_NAME="${VOLUME_NAME:=$4}"
2323
VOLUME_SIZE="${VOLUME_SIZE:=$5}"
24+
SVM_NAME="${6:-$SVM_NAME}"
2425

2526
min=100
2627
max=999

Management-Utilities/ec2-user-data-iscsi-create-and-mount/windows_userData.ps1

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ param(
55
[string]$FSxNAdminIpParam,
66
[string]$VolumeNameParam,
77
[string]$VolumeSizeParam,
8-
[string]$DriveLetterParam
8+
[string]$DriveLetterParam,
9+
[string]$SvmNameParam
910
)
1011
# "AWS secret ARN, e.g arn:aws:secretsmanager:us-east-1:111222333444:secret:MySecret-123456"
1112
$secretId=
@@ -18,15 +19,16 @@ $volSize=
1819
# "drive letter to use, e.g. d"
1920
$drive_letter=
2021

22+
# Defaults
23+
$user="fsxadmin"
24+
$svm_name="fsx"
25+
2126
$secretId = if ($SecretIdParam) { $SecretIdParam } else { $secretId }
2227
$ip = if ($FSxNAdminIpParam) { $FSxNAdminIpParam } else { $ip }
2328
$volName = if ($VolumeNameParam) { $VolumeNameParam } else { $volName }
2429
$volSize = if ($VolumeSizeParam) { $VolumeSizeParam } else { $volSize }
2530
$drive_letter = if ($DriveLetterParam) { $DriveLetterParam } else { $drive_letter }
26-
27-
# Defaults
28-
$user="fsxadmin"
29-
$svm_name="fsx"
31+
$svm_name = if ($SvmNameParam) { $SvmNameParam } else { $svm_name }
3032

3133
# default values
3234
# The script will create a log file and uninstall script

0 commit comments

Comments
 (0)