1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Proposal] Let AWS CLI run on Docker container

Posted at

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] (https://hub.docker.com/r/mesosphere/aws-cli/)
-[AWS CLI in Docker environment] (https://qiita.com/ABCompany1/items/c92ac5087519c103f056)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?