Amazon Web Services

This guide covers setting up and using the AWS Command Line Interface (CLI) for interacting with various AWS services.

AWS CLI Installation

For fresh Ubuntu instances:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip && sudo ./aws/install && rm -rfv ./awscliv2.zip ./aws

For other operating systems, refer to the official installation guide.

Shell Completion Setup

Set up shell completion for the AWS CLI so you can use tab to autocomplete commands and options.

Bash

Add to your ~/.bashrc:

complete -C '/usr/local/bin/aws_completer' aws

Zsh

Add to your ~/.zshrc:

autoload bashcompinit && bashcompinit
autoload -Uz compinit && compinit
complete -C '/usr/local/bin/aws_completer' aws

Important

If you installed the AWS CLI in a different location, update the path accordingly. For example, if you installed it using homebrew, the path might be /opt/homebrew/bin/aws_completer.

AWS CLI Configuration

Set up your AWS credentials:

aws configure

You'll be prompted for:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region name (e.g., us-east-1)
  • Default output format (json, text, or table)

Tip

You can set up multiple profiles using aws configure --profile profilename

CLI Usage Examples

List S3 Buckets

aws s3 ls

List EC2 Instances

aws ec2 describe-instances

Create an IAM User

aws iam create-user --user-name newuser

Using JSON Parameters

For complex API calls, you can use JSON files:

aws ec2 run-instances --cli-input-json file://ec2-params.json

JMESPath Querying

Use --query to filter output:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name]'

AWS CLI Aliases

Create shortcuts for common commands in ~/.aws/cli/alias:

[toplevel]
todays-instances = ec2 describe-instances --query 'Reservations[].Instances[?LaunchTime>=`2023-01-01`]'

Use with: aws todays-instances

Profiles

You can work with two accounts by creating two profiles on the aws command line. It will prompt you for your AWS Access Key ID, AWS Secret Access Key and desired region, so have them ready.

Source

aws configure --profile profile1
aws configure --profile profile2

Pass the --profile flag to the aws command to use the desired profile.

aws s3 ls --profile profile1 s3://bucket/only/profile1/can/access
aws s3 ls --profile profile2 s3://bucket/only/profile2/can/access

If a profile is not specified, the default profile will be used.

References

Created by Ryan D. Najac for the Palomero Lab at the Institute for Cancer Genetics.
Page last updated on 2025-05-02.