AWS CLI – Basics
In this post, we will learn how to install, configure and use AWS CLI to manage our AWS footprint to make managing and repeating admin tasks easier.
Join the DZone community and get the full member experience.Join For Free
You might be using the AWS web console for managing your infrastructure. However, the AWS web console is really ideal for once in a while kind of tasks. As you get more serious about building your AWS footprint, you’ll need to automate.
Meet AWS CLI, which makes it relatively easy to launch any AWS action from the command line on your own PC, to make managing and repeating your admin tasks easier.
In this post, we’ll start by installation the AWS CLI. We’ll learn how to configure it and also how to manage if you have multiple AWS accounts.
In upcoming posts in this series, we’ll learn how to squeeze every ounce or milligram of administration goodness out of AWS resources using the CLI.
Installing AWS CLI
Based on your OS, you can install AWS CLI easily following the steps mentioned on the official website on this link. I am using windows, so I used MSI Installer provided by AWS, and once installed you can verify the installation by executing the following command (PowerShell):
Following is the output shown after executing the command:
In the cloud, AWS CLI is pre-installed on AWS-managed images.
Creating an IAM Admin User
To connect AWS CLI and perform various tasks, we need first to create an IAM admin user then use its access-key in AWS CLI configuration that’s how CLI would be able to perform tasks on behalf of that user.
Use AWS Web Console to create an admin user, attach existing security policy “Administrator Access”, enable programmatic access option, and download access-key and secret. Steps are shown below:
Attach Existing Policy (Administrator Access)
Configure AWS CLI
With our IAM Admin user-created, next step, we will configure AWS CLI. Open a PowerShell window and execute the following command:
It will prompt you for Access-Key, Secret information which you will get from the downloaded CSV file of Admin User. Select a default region and output format as shown below:
Let’s test if the CLI is connected to the admin user account and can perform some tasks. We’ll execute the following command which will show all S3 buckets in this account.
aws s3 ls
And here is the output of the command execution:
And now if we check on the web console, we will see that we have exactly the same buckets there:
Configure Named Profiles for the AWS CLI
If you may have a requirement to run more than one AWS account, you can manage it using the Profile option. AWS Keeps config files in a direction (in-home) called .aws. Here is the directory view:
- config: This file contains your format, region preferences, etc.
- credentials: This file holds your access id & key itself.
The following shows the content of the config file:
Here are the contents of the credentials file (I have three different AWS accounts configured):
You can use the profile command to add one or more additional account profiles.
aws configure --profile aws-japan
Here is how you can execute commands on different accounts:
aws s3 ls //(executes for default account)
aws --profile aws-japan s3 ls (using different account)
In this post, we started with a basic introduction of AWS CLI, which helps speed up infrastructure administration tasks with ease. We learned how to install AWS CLI, configure it, and how can we manage multiple AWS accounts using named profiles.
We will resume our journey and explore the power of AWS CLI in upcoming posts. Let me know if you have any questions or comments. Till next time, happy coding.
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.