This blog demonstrates how to
- create a windows server via CloudFormation
- create a user directory
- install the aws cli
- add the aws cli to the windows path
- Create a log file to track the output of the commands executed in the user data section
- Download a file from an s3 bucket
By using these commands, you can download and install software after the Windows instance is created.
The IAM profile assigned to the EC2 instance should have access to the s3 bucket used.
The code is below:
AWSTemplateFormatVersion: 2010-09-09
Description: Innovate - Create App and Web Servers
Resources:
rMyBastionInstance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: 'ami-I-want-to-use'
KeyName: 'my_windows_key'
IamInstanceProfile: 'My-IAM-profile'
InstanceType: 'm4.2xlarge'
SecurityGroupIds:
- sg-security-group-id
SubnetId: 'my-subnet-id'
BlockDeviceMappings:
- DeviceName: /dev/sda1
Ebs:
VolumeType: gp2
DeleteOnTermination: true
VolumeSize: 100
Encrypted: true
UserData:
'Fn::Base64': !Sub |
<script>
cd \
mkdir tempdc
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi /qn
cd \tempdc
echo Current date and time > c:\tempdc\s3_output.txt
echo %DATE% %TIME% >> c:\tempdc\s3_output.txt
SET PATH=%PATH%;C:\Program Files\Amazon\AWSCLIV2
echo %PATH% >> c:\tempdc\s3_output.txt
aws s3 cp s3://mybucket/my-file.exe c:\tempdc\my-file.exe >> c:\tempdc\s3_output.txt
</script>
Tags:
- Key: Name
Value: MyWindowsInstance