This document provides a step-by-step guide on how to run the AWS Command Line
Interface (CLI) within a Docker container. The AWS CLI is a powerful tool that allows users to
interact with AWS services from the command line. By using Docker, you can create a
consistent environment for running the AWS CLI without needing to install it directly on your
local machine
Prerequisites
Before you begin, ensure you have the following:
- Docker must be installed on your machine.
- AWS account with appropriate permissions to access the services you intend to
use.
- AWS credentials configured (Access Key ID and Secret Access Key).
Step 1: Pull the AWS CLI Docker Image
You can start by pulling the official AWS CLI Docker image from Docker Hub. Open your
terminal and run the following command:
docker pull amazon/aws-cli
This command downloads the latest version of the AWS CLI image.
Step 2: Run the AWS CLI in Docker
Once the image is downloaded, you can run the AWS CLI commands using Docker. You need
to pass your AWS credentials as environment variables. This is how to do it:
docker run --rm -it \
-e AWS_ACCESS_KEY_ID=your_access_key_id \
-e AWS_SECRET_ACCESS_KEY=your_secret_access_key \
amazon/aws-cli
Replace command with the AWS CLI command you want to execute. For example, to list
your S3 buckets, you would run:
docker run --rm -it \
-e AWS_ACCESS_KEY_ID=your_access_key_id \
-e AWS_SECRET_ACCESS_KEY=your_secret_access_key \
amazon/aws-cli s3 ls
Step 3: Using a Configuration File (Optional)
If you prefer to use a configuration file instead of passing credentials as environment
variables, you can mount your AWS credentials and config files into the Docker container.
First, ensure your AWS credentials are stored in <~/.aws/credentials>
and your configuration in <~/.aws/config>.
You can run the AWS CLI with the following command:
docker run --rm -it \
-v ~/.aws:/root/.aws \
amazon/aws-cli command
This command mounts your local <.aws> directory into the container, allowing the AWS CLI to use your existing configuration.
Step 4: Additional Options
You can also specify the region and output format by adding additional environment
variables or command-line options. For example:
docker run --rm -it \
-e AWS_ACCESS_KEY_ID=your_access_key_id \
-e AWS_SECRET_ACCESS_KEY=your_secret_access_key \
-e AWS_DEFAULT_REGION=us-east-1 \
amazon/aws-cli s3 ls --output json
Conclusion
Running the AWS CLI in Docker provides a flexible and isolated environment for managing
AWS resources. By following the steps outlined in this document, you can easily execute
AWS CLI commands without the need for local installation. This approach is particularly useful
for maintaining consistency across different development environments.