How can I use launch configurations to automatically install the AWS CodeDeploy agent on an Amazon EC2 Windows instance?

Last updated: 2019-08-02

I want to install the AWS CodeDeploy agent on my Amazon Elastic Compute Cloud (Amazon EC2) Windows instance. How can I do that using launch configurations?

Short Description

Do one of the following:

  • Use a launch configuration to pass Windows PowerShell commands to an instance using user data.
  • Launch instances from a custom Amazon Machine Image (AMI) that has CodeDeploy installed.


Use Windows PowerShell commands to specify launch configuration details

Note: The following steps use Amazon EC2 Auto Scaling launch configurations to install the CodeDeploy agent and the agent updater, but you can also apply the instructions to individual instances.

1.    Open the Amazon EC2 console.

2.    From the navigation pane, in the AUTO SCALING section, choose Launch Configurations.

3.    Choose Create Launch Configuration.

4.    Choose a Microsoft Windows AMI that's supported by the CodeDeploy agent.

5.    Choose an instance type, and then choose Next: Configure details.

Note: Use a t2.medium instance or larger when running Windows Server.

6.    For Name, enter a name for the launch configuration.

7.    For IAM role, choose an AWS Identity and Access Management (IAM) role that's compatible with CodeDeploy permissions.

8.    Choose Advanced Details.

9.    For User data, enter the following Windows PowerShell commands:

Set-ExecutionPolicy RemoteSigned -Force
Import-Module AWSPowerShell
$REGION = (ConvertFrom-Json (Invoke-WebRequest -Uri -UseBasicParsing).Content).region
New-Item -Path c:\temp -ItemType "directory" -Force
powershell.exe -Command Read-S3Object -BucketName aws-codedeploy-$REGION -Key latest/codedeploy-agent-updater.msi -File c:\temp\codedeploy-agent-updater.msi
// Start-Sleep -Seconds 30 *optional
c:\temp\codedeploy-agent-updater.msi /quiet /l c:\temp\host-agent-updater-log.txt

Note: Running these Windows PowerShell commands installs the CodeDeploy agent updater. It’s a best practice to install the CodeDeploy agent updater for Windows Server in both new and existing instances.

10.    Complete the storage and security group steps for your launch configuration, or choose Skip to review.

11.    Choose Create launch configuration.

Launch instances from a custom AMI that has CodeDeploy installed

Warning: Follow these steps in order. If you don't configure your instance correctly before creating the AMI, then your deployment will fail.

1.    Confirm that the CodeDeploy agent is installed on your Amazon EC2 instance running Windows.

2.    Connect to your Windows instance.

3.    To set the AWS CodeDeploy agent to a stopped state and to change startup to manual, run the following command:

Set-Service -Name 'codedeployagent' -StartupType 'manual' -Status 'stopped'

4.    To start the AWS CodeDeploy agent and change the startup type to automatic, run the following command in the instance user data:

Warning: The following command must be the last command executed in instance user data.

Set-Service -Name 'codedeployagent' -StartupType 'automatic' -Status 'running'

5.    Create a custom AMI from your instance.

Any instances you launch using this custom AMI will have the CodeDeploy agent already installed.

Did this article help you?

Anything we could improve?

Need more help?