Bootstrap a Google Cloud Engine (GCE) Windows VM to install Chocolatey

On GCE (Google Cloud Engine) we can choose to specify a start up script that will run when our instance boots up, restarts or both.

We can used a script located on our local computer or a script stored on Google Storage (or other URL) to install Chocolatey (or any other application).

One interesting feature of GCE is that we have 3 options to choose when we run the bootstrap script (you can choose .bat, .cmd or .ps1 files):

1. Before the Sysprep.
2. After the Sysprep.
3. After the Sysprep and in every restart.

 

1. Create a PowerShell Script to Install Chocolatey

First we need to create a PowerShell file to install Chocolatey.

The example below will download and install Chocolatey first and then use Chocolatey to install Adobe Acrobat (nothing fancy here!)

Set-ExecutionPolicy Unrestricted -force
(iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')))>$null 2>&1
(gc C:ProgramDatachocolateychocolateyinstallchocolatey.config).replace("true","false") | sc C:ProgramDatachocolateychocolateyinstallchocolatey.config
choco install adobereader -y>$null 2>&1

 

2. Install Google Cloud SDK

Download the SDK from https://cloud.google.com/sdk and then Next, Next, Next.

 

3. Login into GCE

Type the following command on Google Cloud SDK Shell:

gcloud auth login --no-launch-browser

Copy the URL generated on your favorite Internet Browser and the copy the code generated back to SDK Shell.

4. Select you project

Select you project using the following command on Google Cloud SDK Shell:

gcloud config set project valid-project-12345

 

5. Create the VM

Run the following command to create the VM:
gcloud compute instances create "gcekopic01"
--image windows-2008-r2
--zone "europe-west1-d"
--machine-type "g1-small"
--metadata "gce-initial-windows-user=kopiadmin" "gce-initial-windows-password=S3cur3P@ssw0rd"
--metadata-from-file sysprep-oobe-script-ps1="D:/KopiCloud/GCE-Chocolatey-Adobe.ps1"
--boot-disk-type "pd-standard"
--boot-disk-device-name "gcekopic01"

The output will be internal and external IP addresses and the status:

Install Chocolatey in Amazon AWS Instances

Chocolatey (https://chocolatey.org) is a package manager for Windows (similar to Yum/Apt-Get in Linux) based on NuGet.

Chocolatey is very useful to automate the deployment of Windows applications on the cloud.

We can use AWS instance bootstrap (aka User Data) to install Chocolatey and some applications on our AWS Instances.

Create a PowerShell Script

First we need to create a .ps1 file to install Chocolatey.

The example below will download and install Chocolatey first and then use Chocolatey to install SQL Server 2012 Express

Bootstrap a Chocolatey script from the AWS Management Console

In the step 3 (configure instance), expand the Advanced Details section.

Click as file option and use the Browse button to select the PowerShell Script.

Bootstrap a Chocolatey script from the AWS EC2 CLI

Open the AWS EC2 CLI console and type:

ec2-run-instances ami-69ae251e -n1 -k AWS-KopiCloud -g sg-b4016ed1 –instance-type t2.micro –subnet sunet-ad8b5ada –user-data-file D:ec2-api-toolsChocolatey-SQl.ps1

The User-Data-File parameter is bootstrap the PowerShell script on the start up of the AWS instance.

If the execution is successful, you will got the AWS Instance ID and more data such IP Address.

More info about parameters and output at

http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-RunInstances.html