Install AWS.Tools module for PowerShell on AWS EC2 using user data and Terraform

I was under the impression that all AWS EC2 instances have the latest version of AWS CLI installed. So, I was in for a pretty shock when I discovered that is not always the case. However, I was required to use the AWS CLI, so I automated the installation with the EC2 user data script and Terraform. Since this was an EC2 with Windows OS, I proceeded with installing the AWS.Tools module for PowerShell.

Note: I wrote this note as part of a series on EC2 user data script. Although it is unnecessary to read all the earlier notes if you are interested ONLY in the steps involved to install AWS.Tools module for PowerShell on EC2, I’d still recommend that because you get the complete picture and maybe learn something new. I have the links to the articles at the bottom.

If you are new to the AWS EC2 user data script, I have a separate note to discuss the steps to start using the user data script  –working with AWS EC2 user data and Terraform.

User data could be set such that it’s run only once when the AWS EC2 instance is provisioned or multiple times with each restart. That is managed via the <persist> flag in the user data script. I prefer having the flag set to <persist>true</persist> since specific provisioning requirements involve restarts. That implies that the user data script needs to be idempotent -run as many times, but the end state is the same.

To install AWS.Tools module for PowerShell, I also required the NuGet package to be installed, and that too had to be idempotent. So the algorithm was:
Step 1: Get a list of all the modules installed.
Step 2: Check if AWS.Tools.Installer module exists.
Step 3: If the module exists, exit out since the desired state exists. Else, move to Step 4.
Step 4: Check the list of packages installed since the AWS.Tools.Installer module requires NuGet package version 2.8.5.201 or above.
Step 5: If the correct version of the NuGet package is installed, move to Step 6. Else, install the correct version of the package.
Step 6: Install the AWS.Tools.Installer module.

I converted the logic associated with the algorithm in the user data script(user_data\user_data.tpl) and stored it in my Github repo: add-awscli-to-userdata

And that is how I installed the AWS.Tools module and ran a few AWS CLI commands from the EC2 instance. If you want to know more, I have two more related notes on the user data script that you will find interesting – How to attach an IAM role to an AWS EC2 instance and how to manage sensitive variables in the user data script.

I hope you found this note useful. Please do not hesitate to reach out with your suggestion or comments.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s