AWS EC2 user data script windows

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:

  • Download binaries;
  • Execute them in silent mode;
  • Pre-configure application in order to link it to CloudBerry Backup (MBS) backend;
  • Clean up everything.

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)

<powershell>
# Variables

$temp = "c:\temp\"
$link = LINK_TO_BINARIES
$file = "cloudberry-backup.exe"
$silent = "/S"
$sleep = 30

# MBS related variables

$company = THE_NAME_OF_YOUR_COMPANY_IN_MBS_SETTINGS
$product = PRODUCT_NAME_IN_MBS_SETTINGS
$product_path = "${env:ProgramFiles(x86)}\$company\$product"

$email = EMAIL_TO_SIGN_IN
$password = PASSWORD_TO_SIGN_IN

New-Item $temp -ItemType directory
cd $temp
Invoke-WebRequest -Uri $link -OutFile $file
Start-Sleep -s $sleep
Start-Process -FilePath $file -ArgumentList $silent
Start-Sleep -s $sleep

# Sign in!

cd $product_path
./cbb editAccount -e $email -p $password

# Clean up

Start-Sleep -s $sleep
Remove-Item $temp -Force -Recurse
</powershell>

## ... 

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

Hope it helps.

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy