This course introduces you to the AWS Command Line Interface.
- Install and configure the AWS Command Line Interface
- Manage CLI credential profiles
- Structure CLI commands
- Better control the outputted response data
- Use input functionality to make working with the CLI easier
- Those looking to get more familiar with the AWS Command Line Interface
- Have a good understanding of command line basics
- Understand some of the fundamental AWS services, such as Amazon EC2, Amazon DynamoDB, Amazon S3, and IAM
- Familiarity with JSON and YAML will help to better understand some of the practical parts of the course.
- For more information on some of these services, check out the following courseware titled:
After you have the AWS CLI installed, you will then need credentials to be able to perform commands. These credentials usually come in the form of either an access key and secret access key, or temporary credentials through a single-sign-on service.
You can set your access keys in several ways.
The first way is by using environment variables. The syntax for how you set an environment variable will be dependent on your operating system. For example, I’m using my MacOS terminal, so the syntax would be “export AWS_ACCESS_KEY_ID=” and then I can paste in my access key. Then I can do the same for the secret access key, using the command “export AWS_SECRET_ACCESS_KEY=” and then paste it in. Then I can run a command to see if it works, say “aws s3 ls”. And as you can see, it’s listing my s3 buckets in my account, so now I know that the credentials are working.
For every command I run, it will then use these credentials for authorization, overriding any other credentials I’ve configured, until the end of my shell session. However, if you want these credentials to last beyond the end of the shell session, it’s recommended you set up your default profile instead. If you’re using your own personal AWS account, setting up the default profile is the most common approach of setting up your credentials.
I can do this by running aws configure, and it will prompt me for four pieces of information: my access key, which I will paste in, my secret access key, which I will also paste in here. My default region, which is where you want to send your requests to by default. I mainly work out of the US Oregon region, so I can specify us-west-2. And then it will ask for a default output, which you can specify as json, yaml, yaml-stream, text, or table. I’m going to specify yaml as my default output.
Once I set these configurations, I can view them in the .aws directory. Let’s go to this directory by running the command cd ~/.aws/. From here I’ll list the contents of the directory using ls. This directory has a few files in it. The important ones for this lecture are the AWS credentials file and the AWS Config file. If I use the cat command to display the config file, you can see it contains the less sensitive information I selected, such as the default region and the default output.
I’ll go ahead and do the same with the credentials file, using cat credentials. You can see it contains the access keys that I copy and pasted when I ran aws configure.
Now, the issue that most companies have with this approach is that the access keys may not be changed regularly, so companies often choose to integrate an SSO service with the AWS CLI so that credentials can be changed dynamically. This approach is most commonly used if you’re using a company-provided AWS account.
Most SSO services follow a similar sign-in flow, so for ease, I’ll show you the login flow using the AWS SSO service. I’ve already set the service up in the AWS console and integrated users from an Active Directory setup. After that is complete, I’m going to clear my screen here and then run the command ‘aws configure sso’. The first thing it asks for is the start URL, which your administrator will provide to you. So, I’ll paste in my start URL here.
And then select the region my AWS SSO is set up in, which is us-east-1.
Then, I am redirected to a browser that asks me to log in. I’ll log in using my username and password for my active directory user. After I’ve successfully logged in, I’ll go back to the terminal. Here, I can see which accounts and roles I have available for me to use, and I just have one role, called ViewOnly that I can access in one account. I can then start to run commands using this role.
In summary, use the aws configure command to set up your default profile. This command will modify two files: the config and the credentials file. Additionally, you can use SSO services with the AWS CLI to source credentials. That’s it for this one - see you next time!
Alana Layton is an experienced technical trainer, technical content developer, and cloud engineer living out of Seattle, Washington. Her career has included teaching about AWS all over the world, creating AWS content that is fun, and working in consulting. She currently holds six AWS certifications. Outside of Cloud Academy, you can find her testing her knowledge in bar trivia, reading, or training for a marathon.