DEV Community

Ishraque Bin Shafique
Ishraque Bin Shafique

Posted on • Updated on

Cloud on Your Terms: Running AWS CLI as a Docker Container

AWS CLI Using Docker

What Is AWS CLI?

AWS CLI is a versatile command-line interface designed for interacting with and effectively managing AWS resources. Virtually any action that can be performed through the AWS Management Console by calling AWS APIs can also be accomplished from your terminal using the AWS CLI.

One of the main strengths of AWS CLI lies in its ability to automate repetitive tasks through scripting. Instead of manually clicking through the console multiple times to achieve the same outcome, you can write scripts that efficiently handle tasks like listing all S3 buckets in your AWS account. This automation streamlines operations and saves time, making cloud management more efficient and convenient.

Why Use Docker For AWS CLI?

On February 10, 2020, AWS CLI version 2 made its debut, bringing a host of fresh capabilities. Among its notable additions was the ability to install the AWS CLI as a Docker container. Docker, an open-source containerization platform, empowers developers to encapsulate applications within containers, providing a consistent environment regardless of the underlying system. With this integration, users gained the advantage of running the AWS CLI seamlessly within a Docker container, offering enhanced portability and flexibility in managing AWS resources.

Scope Of This Article

This article will focus on:

  1. How to download and run the AWS CLI v2 docker image
  2. How to share host credentials for programmatic access to AWS
  3. How to shorten the Docker command
  4. How to update the AWS CLI Docker Container

Installing Docker As A Prerequisite

Installing Docker is very easy. This guide will consider installing docker in Ubuntu Ubuntu 22.04.2 LTS. Other Linux distros and OSes might vary a bit which can be found in Docker Official Guide.

sudo apt-get update -y; sudo apt-get install ca-certificates curl gnupg -y
sudo install -m 0755 -d /etc/apt/keyrings -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y; sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
sudo groupadd docker
sudo usermod -aG docker $USER
sudo reboot
Enter fullscreen mode Exit fullscreen mode

Copy and paste the above commands into a Linux terminal to have the latest version of Docker installed in the machine.
The machine will take a reboot after installation in complete in order to bring up all the services properly.

After that the following command is run to check if docker has been installed properly and a similar output will be shown:

ishraque@testvm:~$ docker --version
Docker version 24.0.5, build ced0996
Enter fullscreen mode Exit fullscreen mode

Getting Started With AWS CLI Container

The official AWS CLI version 2 Docker image is hosted on DockerHub within the amazon/aws-cli repository. DockerHub serves as a public online repository, enabling the storage and sharing of Docker images.

To install the AWS CLI on your local computer, you can utilize the docker run command.

sh docker run --rm -it amazon/awc-cli --version

The initial download from DockerHub occurs only once during the first execution. Subsequent docker run commands will directly access a copy from the local docker image cache on your computer, eliminating the need for additional downloads.

ishraque@vtestvm:~$ docker run --rm -it amazon/aws-cli --version
Unable to find image 'amazon/aws-cli:latest' locally
latest: Pulling from amazon/aws-cli
c0184eb4a5d5: Pull complete 
a541274d7cb2: Pull complete 
bd947c838e14: Pull complete 
33971762a989: Pull complete 
ec2d4ca4f5a9: Pull complete 
Digest: sha256:cebe51ef1440f573184340e0cded7c86b42fd47352e6bda6179ef56bc173a25a
Status: Downloaded newer image for amazon/aws-cli:latest
aws-cli/2.13.7 Python/3.11.4 Linux/5.19.0-1029-aws docker/x86_64.amzn.2 prompt/off
Enter fullscreen mode Exit fullscreen mode

At the very bottom of the output shown above, it states that aws-cli version 2.13.7 is running with the Docker container.

Getting Access Keys For AWS CLI

Getting the Access Keys are very simple and requires following these steps:

  • Log into AWS console.
  • Click on the username on the upper right corner of AWS Console.
  • Click Security Credentials.
  • Scroll down and find the Access Keys section.
  • Click Create access keys
  • On the next page select Command Line Interface (CLI) and select the checkbox at the bottom of the page.

Select CLI and the checkbox

  • Give any name to the Access Key

Giving Name To Access Key

  • Download the Access Key

Download the Access Keys

PS. The Access Key shown in this Article has been deleted long before the Article has been published online!!

Saving The Credentials For AWS CLI Docker

  • Make a folder in home directory with:

mkdir ~/.aws

  • Make two files named config and credentials

touch config credentials

  • The config file should have similar contents (change accordingly)
ishraque@testvm:~$ cat ~/.aws/config 
[default]
region = us-east-1
output = json

Enter fullscreen mode Exit fullscreen mode
  • The credentials file should have similar contents (change accordingly)
ishraque@testvm:~$ cat ~/.aws/credentials 
[default]
aws_access_key_id = AKIA4KP3BMTILMRLHZVF
aws_secret_access_key = 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
Enter fullscreen mode Exit fullscreen mode

Using Container AWS CLI

Now any AWS CLI commands can be run using:

docker run --rm -it -v ~/.aws:/root/.aws amazon/aws-cli command
Enter fullscreen mode Exit fullscreen mode

💥 One bonus tip is to make an alias of the docker run command so that it can be called with a much shorter command: 💥

alias awsd='docker run --rm -it -v ~/.aws:/root/.aws -v $(pwd):/aws amazon/aws-cli'
Enter fullscreen mode Exit fullscreen mode

Updating The Docker Container

By default, when running the AWS CLI Docker image, it automatically downloads the latest version as we did not specify a specific tag. The latest version is always tagged as "latest." Consequently, when we use the docker run command again, it will use the existing image with the "latest" tag from the local cache. It won't attempt to download the most recent image from DockerHub unless we explicitly instruct it to do so.

To pull the latest version explicitly, you can use the following command:

docker pull amazon/aws-cli:latest
Enter fullscreen mode Exit fullscreen mode

Creating A Bucket With AWS CLI Container

A S3 bucket can be made with the following command:

awsd s3 mb s3://<globally-unique-bucket-name>
Enter fullscreen mode Exit fullscreen mode

S3 buckets can be listed with:

awsd s3 ls
Enter fullscreen mode Exit fullscreen mode

A S3 bucket can be removed with the following command:

awsd s3 mb s3://<name-of-your-bucket>
Enter fullscreen mode Exit fullscreen mode

Here is an example from the terminal:

ishraque@ishraque-laptop:~/Desktop/GitProjects$ awsd s3 ls
ishraque@ishraque-laptop:~/Desktop/GitProjects$ awsd s3 mb s3://ibshafique-test-bucket
make_bucket: ibshafique-test-bucket
ishraque@ishraque-laptop:~/Desktop/GitProjects$ awsd s3 ls
2023-08-05 05:35:37 ibshafique-test-bucket
Enter fullscreen mode Exit fullscreen mode

Conclusion

Numerous companies have embraced container-based deployment tools like Docker, leveraging their advantages in application development and deployment. Running the AWS CLI from within a container harnesses the benefits of containers, such as enhanced portability, isolation, and security. If you have anything to share, please feel free to comment.

References:

  1. https://docs.aws.amazon.com/cli/index.html
  2. https://hub.docker.com/r/amazon/aws-cli
  3. https://docs.docker.com/engine/install/

Top comments (0)