Posted at

[Proposal] Let AWS CLI run on Docker container

This article is an automatic translation of the article[341404aa6792ad4a9980] below.

https://qiita.com/speaktech/items/341404aa6792ad4a9980


What I did

The AWS CLI is executed on the Docker container, and only the result is returned to the terminal of the host OS.

By aliasing a series of processes including the Docker command, you can use AWS CLI as if it is running on the terminal of the host OS.


Benefits

-It is not necessary to install Python, pip, AWS CLI, credentials to the host OS (do not pollute the host OS)

-Containerized, it is possible to distribute the unified AWS CLI environment to development members


Dockerfile

Alpine and AWS CLI versions are hard-coded, so please change them manually.


Dockerfile

FROM alpine:3.9

ENV AWS_CLI_VERSION 1.16.148
RUN apk -v --update add \
python \
py-pip \
groff \
less \
mailcap \
&& \
pip install --upgrade awscli==$AWS_CLI_VERSION python-magic && \
apk -v --purge del py-pip && \
rm /var/cache/apk/*

WORKDIR /project
ENTRYPOINT ["aws"]



Build

docker build -t aws-cli .


Configuration

Register access key ID, secret access key and default region in variables.

-For Windows (powershell)

$AWS_ACCESS_KEY_ID="<id>"

$AWS_SECRET_ACCESS_KEY="<key>"
$AWS_DEFAULT_REGION="<region>"

-For Linux

export AWS_ACCESS_KEY_ID="<id>"

export AWS_SECRET_ACCESS_KEY="<key>"
export AWS_DEFAULT_REGION="<region>"


Installation

Create an alias to make the AWS CLI appear to be running on your host OS.

It is useful to register to"$profile"on Windows and".bash_profile"on Linux.

※ Mount the current directory to the work directory"/project"on the container.

-For Windows (powershell)

function aws(){

docker run --rm -it `
-e AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID} `
-e AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY} `
-e AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION} `
-v ${pwd}:/project `
speaktech/aws-cli `
$args
}

-For Linux

alias aws='docker run --rm -t $(tty &>/dev/null && echo "-i") \

-e "AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}" \
-e "AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}" \
-e "AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION}" \
-v "$(pwd):/project" \
aws-cli'


How to use

The usage is the same as installing AWS CLI on the host OS. However, please note that command completion can not be performed.


S3上のオブジェクトのリスト表示

aws s3 ls



S3上へローカルのファイルをコピー

aws s3 cp test.txt s3://test-bucket/test-prefix/ 



  • Since the current directory is mounted on the work directory"/project"on the container, files on the current directory can be copied.


Articles I've provided for reference

-AWS CLI in Docker

-AWS CLI in Docker environment