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)

# Variables

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

# MBS related variables

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


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

## ... 

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