Using VSTS to deploy to AWS

VSTS touts that it can be used to deploy your code anywhere.  While that’s true, they don’t necessarily give you the tools to do it unless that “anywhere” is Azure.  This guide will give you the steps to set up a file drop onto an S3 AWS resource that you can use to trigger an AWS CodeDeploy.

Prerequisites

Before beginning, you’ll need the following:

  1. AWS key/secret pair with permissions to access your S3 container
  2. VSTS PAT for your build agent
  3. An existing AWS S3 bucket

Set up a private build agent

Amazon has been kind enough to build tools that work on Windows to interact with AWS.  Unfortunately, Microsoft has not installed this on their hosted agents. Enter private agents.  You’ll need to set up your own agent so you can install the software we need.  In testing, I chose to pick the most elaborate set up I could to see if it was possible, meaning I set up an agent on an Azure VM.  That is 100% an option if you want to keep off on-prem resources and keep to the cloud.

Once your agent is installed, you need to install the following on the server:

  1. AWS Tools for Windows Powershell
  2. AWS CLI

Create your build definition

The bare-bones definition should be created as an Empty definition to remove requirements on Visual Studio (unless you installed VS on your agent’s machine) and requires 4 steps:

  1. Get sources – required
  2. Build solution – to make the output
  3. Copy publish artifact – to move the output to a output designated folder
  4. PowerShell script – to run the S3 upload script

1 – 3 are pretty obvious what they do.  The magic occurs in step 4.  The PS script task should have the following settings:

  1. Display name: whatever you want to call it, or leave the default
  2. Type: Inline Script
  3. Arguments: <blank>
  4. Inline Script: details below
  5. Working folder: <blank>

The S3 upload PS script

The PS script looks like below:

Import-Module "C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1" -ErrorAction STOP;
$targetFolder = $(Build.StagingDirectory) . "\drops\Release";  #this should be the path to your output directory

Get-ChildItem -recurse $Files | 
   ForEach {
      Write-S3Object -Bucket "<bucket name>" -File $_.FullName -Key $_.Name -AccessKey "your AWS Key ID" -SecretKey "<your AWS secret>";
      Write-Output "$_ Upload complete!";
   }

This script isn’t perfect and you may have to play with it a little to not get any upload errors, but it gets the job done.  Note that you should be storing your AWS key and secret in a locked variable and passed in via arguments so as to not expose your access credentials.

In the end, your S3 should have all the files from your output directory.

Next steps

Convert this to a task!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: