AWS EC2 user data script windows


AWS Amazon-EC2 cloudberrylab


Table of contents:

Another interesting customer case at CloudBerry Lab brought me to Amazon EC2 User Data script back again. I used to make it in the past for Linux (check this article about EC2 user data script example). Now the task is similar, but OS is different.

The challenge #

We need to have application installed into the Guest OS in our EC2 instance on launch. In order to do this we need to walk through the following steps:

We are going to user powershell. I would recommend to start with the “Running Commands on Your Windows Instance at Launch” article before you keep reading since it gives good idea what is going on next.

Powershell script for user data (EC2) #

 1<powershell>
 2# Variables
 3
 4$temp = "c:\temp\"
 5$link = LINK_TO_BINARIES
 6$file = "cloudberry-backup.exe"
 7$silent = "/S"
 8$sleep = 30
 9
10# MBS related variables
11
12$company = THE_NAME_OF_YOUR_COMPANY_IN_MBS_SETTINGS
13$product = PRODUCT_NAME_IN_MBS_SETTINGS
14$product_path = "${env:ProgramFiles(x86)}\$company\$product"
15
16$email = EMAIL_TO_SIGN_IN
17$password = PASSWORD_TO_SIGN_IN
18
19New-Item $temp -ItemType directory
20cd $temp
21Invoke-WebRequest -Uri $link -OutFile $file
22Start-Sleep -s $sleep
23Start-Process -FilePath $file -ArgumentList $silent
24Start-Sleep -s $sleep
25
26# Sign in!
27
28cd $product_path
29./cbb editAccount -e $email -p $password
30
31# Clean up
32
33Start-Sleep -s $sleep
34Remove-Item $temp -Force -Recurse
35</powershell>
36
37## ... 

Make sure you have the above script wrapped between <powershell>[your code goes here]</powershell>

Hope it helps.

comments powered by Disqus