The AWS Command Line Interface (CLI) provides power users command-line access to manage AWS services and automate infrastructure tasks. This comprehensive guide will cover installing, configuring, and using the AWS CLI on Ubuntu 22.04.

Benefits of the AWS CLI

While the AWS Management Console provides a graphical interface, the AWS CLI allows managing infrastructure programatically via the command line. Key benefits include:

  • Automation – Script commands to perform infrastructure tasks
  • Efficiency – Manage higher volumes faster with scripts vs. console clicks
  • Control – Fine grained control for advanced use cases
  • Agility – Integrates well with CI/CD pipelines and DevOps tools like Ansible, Terraform, and Jenkins

The AWS CLI makes administrators and developers more productive. Authenticated requests can be scripted eliminating repetitive console workflows.

Common use cases include data transfers, batch processing, automating deployments, and instance management. Organizations often utilize the CLI for scalable, secure infrastructure-as-code.

Installing the AWS CLI on Ubuntu

As Ubuntu includes AWS CLI in its base repositories, installation only takes a single apt command:

sudo apt update
sudo apt install awscli -y  

Note: This installs the latest version 2 of the AWS CLI which is the recommended version going forward due to usability improvements over version 1.

With apt keeping the CLI updated automatically, re-running the install command periodically ensures you have the latest features and fixes.

You can check the version with:

aws --version

Sample output:

aws-cli/2.9.19 Python/3.10.6 Linux/5.19.0-32-generic exe/x86_64.ubuntu.22 prompt/off

Configuring the AWS CLI

Before using the CLI, initial configuration with API credentials is required so the CLI can authenticate requests.

Run aws configure to set up your access keys and default region:

aws configure

You will be prompted for:

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

The credentials and settings are stored in ~/.aws/credentials and ~/.aws/config.

For security best practices, avoid coding credentials directly into scripts. Use aws configure instead with restricted file permissions.

Alternatively, you can set credentials via environment variables:

export AWS_ACCESS_KEY_ID="YOUR_KEY" 
export AWS_SECRET_ACCESS_KEY="YOUR_SECRET"

Using CLI Profiles

Named profiles allow switching between different credentials and settings per account:

Configure a profile named "admin":

aws configure --profile admin

Use the profile:

aws s3 ls --profile admin

This keeps credentials separate for multi-account users.

Authentication Methods

The primary methods used to authenticate with AWS are:

  • Access keys – Recommended for production use
  • Environment variables – Useful for local development
  • EC2 instance metadata role – Authenticates from EC2 instance
  • ECS task role – Access for containers running on ECS
  • Web identity token – Allow users authenticated via OpenID
  • SAML assertion – Integrate CLI with single sign-on

Using AWS CLI environment authentication makes managing secrets easier.

Common AWS CLI Commands

The CLI manages services using subcommands like ec2, s3, and cloudformation. Common examples include:

S3 Buckets

aws s3 ls 
aws s3 cp
aws s3 sync
aws s3 rb

EC2 Instance Management

aws ec2 describe-instances
aws ec2 run-instances  
aws ec2 start-instances
aws ec2 stop-instances 

AutoScaling Groups

aws autoscaling suspend-processes
aws autoscaling resume-processes

CloudFormation Stacks

aws cloudformation create-stack
aws cloudformation update-stack
aws cloudformation delete-stack  

The CLI can perform most operations available in the console. Review the full CLI reference for additional capabilities.

Formatting CLI Output

By default JSON is used for formatting responses. For human readability, switch formats:

aws configure set default.output table
# OR
aws s3 ls --output table

Common formats include:

  • json – Good for programmatic processing
  • text – Concise rows/columns
  • table – Readable in terminal with headers
  • yaml – Streamlined, structured text output

CSV, XML, and other formats are available. Set your preferred default or override per command.

CLI Pagination

When a response contains a large number of results, the CLI will paginate output. Further results can be accessed by specifying –page-size and a token:

aws s3api list-objects --bucket mybucket \
   --page-size 500 --starting-token TOKEN

The token indicates the next page of results. Omitting the token returns the first page.

Adjusting page size allows efficiently exporting large datasets from APIs like S3 object listings.

Upgrading the AWS CLI Version

As new versions bring bug fixes, features, and improvements, upgrade periodically:

sudo apt update
sudo apt install awscli -y

This distributes upgrades alongside regular system updates.

For major version shifts like v1 to v2, uninstall then reinstall:

sudo apt remove awscli
sudo apt install awscli

Always check aws –version after upgrades to verify.

AWS CLI Troubleshooting

If encountering issues, start troubleshooting with:

Permissions Errors

Verify your access keys have necessary IAM permissions for the requested service.

S3 Bucket Errors

Check the bucket name, region, and your account‘s access. Enabling default encryption can cause access failures too.

API Errors

Service APIs could be experiencing issues. Check the Service Health Dashboard in the console.

For additional troubleshooting, consult the AWS CLI documentation, forums, or AWS Support.

Conclusion

The AWS CLI streamlines infrastructure management via the command line while unlocking automation capabilities. Get started installing on Ubuntu 22.04 via a simple apt command along with configuring your credentials and default region.

With robust tooling support and broad service coverage, the AWS CLI is invaluable for administrators and developers using AWS cloud infrastructure.

Similar Posts