Installing the AWS cli for PowerShell Core (pwsh) on your macOS
A walk through of installing all the AWS CLI services in PowerShell Core on your macOS. If you can run pwsh
in a terminal and it starts a PowerShell session, you’re set to get started.
I’ve recently been adapting to the macOS and AWS world. I’ve always previously worked on a Windows machine and used Azure for my professional career. Since I have a heavy PowerShell background, I still want to use those skills so I don’t lose them over time while I continue to build new skills. So I figured why not use PowerShell Core on my mac, or perhaps better known as pwsh
in the mac world.
Prerequisites
If you don’t already have PowerShell Core installed on your mac, you can follow the directions here
If you prefer a homebrew install, you can run:
brew install --cask powershell
To test PowerShell Core is functional, run pwsh
in the terminal and a new PowerShell Core session should start.
Installing the AWS CLI for PowerShell Core on MacOS
AWS does provides a documentation page for our initial install [here](https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up-linux-mac.html
Inside our pwsh
terminal:
Install-Module -Name AWS.Tools.Installer
If you don’t want the prompt for automation purposes, you can add the -Force
flag to the above command.
You may get prompted this is an untrusted repository, if you haven’t Set-PSRepository
for the PS Gallery as a trusted source. The PS Gallery is the place where the public modules are available. This gallery is run by Microsoft. Modules do undergo a security scan and are run through a script analyzer before being published to the gallery. That said, security is everyone’s job so be mindful there is no guarantee the package is not malware.
We can verify our package is published and copyrighted by AWS: https://www.powershellgallery.com/packages/AWS.Tools.Common/4.1.11.0
Now that we have done some quick research, we can agree to install the package by hitting the Y
or A
key.
AWS CLI Tools are installed…but the AWS SERVICE MODULES AREN’T!!
If you’ve been using PowerShell for a long time, you’ve probably gotten into the habit of being able to use the module immediately after it was installed. If you try to run a Get-EC2Instance
command for instance, you’re going to get an error about an unrecognized command after installing the module!
The error message is going to look something like this:
Get-EC2Instance: The term 'Get-EC2Instance' is not recognized as a name of a cmdlet,
function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
To fix this, we need to import the service we are calling. In the AWS documentation, it has an example for importing a few services, including the EC2 service:
Install-AWSToolsModule AWS.Tools.EC2,AWS.Tools.S3 -CleanUp
But what if we want to import all the services?
Just installing the AWS.Tools.Module
did not actually install the modules we need to query AWS.
Import all AWS Services in PowerShell Core
This is different from the Az modules if you’ve worked with them in the past. The Az modules are maintained similarly, but you don’t need to invoke each module to “turn it on” in your terminal.
If you’re wondering just what choice of modules you have, let’s head right to the github source
We can see there are tons of modules here. Sometimes we don’t know exactly what modules we will use in the future, so it is kind of annoying 🙄 to have to remember to import a module if I want to query a new service.
I’d like to have them all ready to go whenever I may need them. There are over 200 services listed! I get it, not everyone wants to load over 200 services, but some people want to set it and forget it. So here’s a powershell code snippet that will do just that, install the 200+ services packaged in the aws CLI
$awsServices = (Get-AWSPowerShellVersion -ListService).'Module Name'
Import-Module AWS.Tools.Installer -Force
foreach ($service in $awsServices) {
Install-Module $awsServices -AllowClobber -Force
}
What’s next?
Once you have the services imported, you can start querying AWS via the cli. The filtering is a bit different compared to the Az cmdlet, but you can still use your PowerShell wizardry to get you to the result you’re looking for.
It took me a while to build a query to return the instance id (ResourceId) with my friendly resource name (value).
(Get-EC2Tag -Filter @{name='tag:Name'; values="dina*"}) | Where-Object { $_.ResourceType -eq 'instance' }
In the above example, the tag Name is referring to my instance name. I’ve got a cluster of AWS resources that start with dina
so that is what I want to find.