Often while working on automation, I am required to interact with AWS cloud resources. For example, when using my laptop, I use the AWS CLI; I have the latest version installed. However, I prefer AWS Tools for Powershell if I automate the process into a build pipeline. If you are new to AWS Tools for Powershell, I’d recommend you read about that at AWS-Docs.
In this note, I list the four items to remember while using AWS Tools for Powershell with the Azure DevOps pipeline. At the end of this note, I have a link to my Git repository with all the necessary artifacts.
Step 1: Install the necessary modules
As you have read in the AWS-Docs link I shared above; the first step is to install
AWS.Tools.Installer module, which is followed by installing the
AWS.Tools.Common module. Installing these two modules lays the foundation for installing any other specific module. For example, if I were required to interact with an Amazon S3 bucket, I would install
AWS.Tools.EC2 module for the Amazon EC2 resources.
Below is a code snippet from the
InstallAWSTools.ps1 script I have in my GitHub repository.
As you can see, any extra modules required would go into the
$RequiredModules list, and the
foreach loop would take care of it.
If you are interested, here is a link to all the currently available
AWS.Tools modules. Once you identify a module you require, click on the Package Details and go through the list of Cmdlets. The cmdlets are the list of available functions that you can use.
Step 2: Create a profile
However, creating a profile would help before using any specific cmdlets in a module. I created a profile using the
Set-AWSCredential cmdlet. Below is the code snippet from the
azure-pipelines.yaml file I have in my GitHub repository.
I also wanted to set the
DefaultAWSRegion value using the cmdlet
Set-DefaultAWSRegion. However, I learned that the setting persists only for the current session. And since each task in
azure-pipelines.yaml was a session, setting the value was not very useful to me.
If you are interested, you can read more at StackOverflow-question.
Step 3: Use specific commands
This step is where you will use specific cmdlets depending on what you wish to automate. It could be to list S3 buckets or create a new ECR repository, as I have specified in this
Step 4: Delete the profile
Finally, close the session using the
Remove-AWSCredentialProfile cmdlet once you have used the other cmdlets. Note that I have the setting
condition: always() which states that even if there were certain failures in the above steps or tasks in the
azure-pipelines.yaml file, this step would still be executed. That way, I do not leave any lingering open sessions.
I have used and continue to use this approach, so if you face any challenges, do not hesitate to reach out and share the error message. I will get back to you at the earliest. Here is a link to my GitHub repository with the artifacts. You may also look at the build associated with this repository at install-aws-tools-powershell-build.
2 thoughts on “Install and use AWS tools for PowerShell on Azure DevOps Pipelines YAML”