- Create a custom role with Azure role-based access control (Azure RBAC) using PowerShell - Wed, Jan 20 2021
- Step by step Deploying Docker Container to Azure using Azure CLI - Wed, Sep 2 2020
- Install Docker offline on Windows Server 2016 - Thu, Dec 6 2018
The AWS Tools are also available for Windows PowerShell. However, here I focus on PowerShell Core 6.
Managing AWS with PowerShell
The AWS Tools for PowerShell Core support almost all AWS resources and services. Some of the tools include:
- Compute and Networking: Amazon Elastic Compute Cloud (EC2), Elastic Load Balancing, Route 53, Virtual Private Cloud (VPC)
- Database: Amazon Relational Database Service (RDS), Redshift, ElastiCache
- Storage and Content Delivery: Amazon Simple Storage Service (S3), CloudFront
- Deployment and Management: AWS Elastic Beanstalk, CloudFormation, CloudWatch, Identity and Access Management (IAM)
- App Services: Amazon Simple Queue Service (SQS), Simple Notification Service (SNS), Simple Email Service (SES)
For instance, you can restart all EC2 instances in AWS and check their system status with these PowerShell commands:
Get-EC2Instance | Restart-EC2Instance -Verbose Get-EC2InstanceStatus | ForEach-Object SystemStatus
Advantages of PowerShell AWS tools
AWS also provides a variety of command-line interfaces (AWS CLI), toolkits, and SDKs for all major programming and scripting languages. In my view, PowerShell offers a few advantages over the other options Amazon provides:
- All PowerShell cmdlets follow a verb-noun syntax, which make it easy to construct commands.
- To discover cmdlets in PowerShell, you can use Get-Command with wildcards to list the available cmdlets in AWS Tools for PowerShell Core:
Get-Command *LMFunction -Module AWSPowerShell.NetCore
- PowerShell usually delivers output as objects, which makes it easier to filter and manipulate data than with the text-based output as in the AWS CLI.
- PowerShell Core supports tab completion, pipelines, and remote command execution using PowerShell remoting.
- The AWS Tools for PowerShell Core work on Windows, Linux, and macOS.
- The AWS Tools for PowerShell Core give access to .NET Core classes, which can be very useful in a variety of scenarios.
Install AWS Tools for PowerShell Core
The first step is the AWS access key ID and secret access key. I assume here you already have an AWS account. I recommend you create an IAM user for accessing AWS via PowerShell. With the help of the IAM user's AWS access key and secret access key, you can authenticate your PowerShell session to AWS. The procedure below describes how you can create both keys.
- Launch the IAM console in AWS.
- From the navigation menu at the left of the screen, click Users.
- In the pop-up window, click on Add User.
- Now in the new window, provide a User name, select the Access type as Programmatic Access, and then click Next.
- To set the permissions, choose Attach existing policies directly. In the Policy type filter Administrator, you can choose any permission level. For example, I'll click the checkbox next to AdministratorAccess and then click the Next
- Finally, in the next step after reviewing your user and permission levels, click the Create user button.
- On the next page, you'll find your keys. Note only one is visible. Thus, you have to download and save them at the secure location.
Install the AWSPowerShell.NetCore package
Next, you have to install the AWSPowerShell.NetCore package. The AWS Tools for PowerShell Core are only accessible via the PowerShell Gallery, and no MSI installer is available. You can install the module by using following commands from a PowerShell console running as an administrator:
Install-Module -Scope CurrentUser -Name AWSPowerShell.NetCore -Force
Initialize AWS Tools
All AWS PowerShell Tools cmdlets expect a set of AWS credentials to sign the web service request cryptographically. You can specifically provide credentials whenever you run a command or can provide the credentials at a session level.
To initialize credentials and the region in the current PowerShell Core session, you have to provide the access key ID and the secret key obtained earlier in this article. Then select the region, which should be the nearest to your location to avoid latency issues.
The following commands will import the module and initialize the defaults in your session:
Import-Module AWSPowerShell.NetCore $param = @{ Region = 'ap-south-1' AccessKey = 'YOUR-ACCESS-KEY-FROM-AWS-CONSOLE' SecretKey = 'YOUR-SECRET-KEY-FROM-AWS-CONSOLE' } Initialize-AWSDefaults @param
If you are unsure which regions to choose, you can run the Get-AWSRegion cmdlet to obtain a list of regions.
You can add the above lines to your PowerShell profile. This way, your credentials will be available in every new PowerShell session.
notepad.exe $profile
If you don't want to store your credentials in clear text (recommended), you can create a profile and save the profile in a credential store. Amazon's documentation offers detailed instructions on how to work with profiles and credentials stores.
Updates and versioning
Whenever a new version of AWS Tools for PowerShell core is available, you can update it from the PowerShell console using this command:
Update-Module AWSPowershell.NetCore
And this command reads the version number of the AWS Tools and lists service version information:
Get-AWSPowerShellVersion –ListServiceVersionInfo
Cmdlet discovery
To manage an AWS resource or service with AWS Tools for PowerShell Core, you first need to identify the corresponding cmdlets. You can use the Get-CmdletName cmdlet for this purpose with the -Service and -ApiOpertion parameters to filter out services and their respective cmdlet names:
Get-AWSCmdletName -Service compute -ApiOperation StartInstances Get-AWSCmdletName -Service cloudwatch -ApiOperation list -MatchWithRegex
If you want a more detailed description of the cmdlets, you can also use following code sample:
Subscribe to 4sysops newsletter!
Get-Command set*ec2* -Module AWSPowershell.netcore | ForEach-Object { [PSCustomObject] @{ 'Name' = $_.name 'Description' = (get-help $_.name).description.text } } | Format-Table -Wrap
Conclusion
This article only scratched the surface of the AWS Tools for PowerShell Core. I prefer to manage AWS with PowerShell instead of the other available toolkits and SDKs because I have access to .NET libraries and can work with features such as pipelines and PowerShell remoting. What is your favorite interface for AWS?