From 69725924583b2275784b0a9b645ac023969e4f6c Mon Sep 17 00:00:00 2001 From: Housni Yakoob Date: Fri, 3 Aug 2018 23:20:01 +0300 Subject: [PATCH 1/2] Simpler way to use your AWS credentials in Docker --- .env | 1 - .gitignore | 6 +++++- Dockerfile | 17 +++++++-------- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++-- docker-compose.yml | 10 --------- env.sample | 14 +++++++++++++ 6 files changed, 77 insertions(+), 23 deletions(-) delete mode 100644 .env delete mode 100644 docker-compose.yml create mode 100644 env.sample diff --git a/.env b/.env deleted file mode 100644 index 20280b2..0000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -COMPOSE_PROJECT_NAME=aws_cli \ No newline at end of file diff --git a/.gitignore b/.gitignore index 51e7c37..d1f4707 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ -aws/ +# Contains environment variables to be passed into Docker. +# Allows `env.sample` to be tracked but not any other file beginning with `env.` +# For example, `env.dev` or `env.prod` won't be tracked but `env.sample` will. +env.* +!env.sample \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 623e857..bd1d0b3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,17 @@ -FROM python:3.7-slim +FROM alpine:3.8 MAINTAINER Yusuf Iqbal -RUN apt-get update -RUN apt-get install --no-install-suggests -y groff-base +RUN apk --update add --no-cache \ + python \ + py-pip \ + && pip install --upgrade awscli \ + && apk -v --purge del py-pip \ + && rm /var/cache/apk/* # Set the application directory WORKDIR /app -# Install AWS CLI -RUN pip install awscli --upgrade --user -RUN pip install --upgrade pip -ENV PATH=~/.local/bin:$PATH - # Copy code from the current folder to /app inside the container ADD . /app -RUN cp -r /app/aws/ ~/.aws +ENTRYPOINT [ "aws" ] \ No newline at end of file diff --git a/README.md b/README.md index 63d5be5..1e79d7f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,50 @@ -- `docker-compose build` -- `docker-compose run aws_cli bash` +# AWS CLI + +Docker image to run AWS CLI commands without having to install it on the host system. + +## Usage +Make a copy of the environment file. +``` +$ cp env.sample env.dev # You can make it `env.prod` or anything you want. +``` + +Edit to add your own AWS credentials. +``` +$ vim env.dev +``` + +Build your Docker image: +``` +$ docker build \ + --rm \ + -t aws_cli \ + . +``` + +Run the `aws_cli_container` container off the `aws_cli` image, loading environment variables from the file `env.dev` into the container and specify your AWS command at the end: +``` +$ docker run \ + --rm \ + -it \ + --name=aws_cli_container \ + -v ${PWD}:/app \ + --env-file ./env.dev \ + aws_cli \ + iam get-user +``` + +You can also create an alias for the Docker run command in your ~/.bashrc by adding this at the end: +``` +alias aws='docker run --rm -it --name=aws_cli_container -v ${PWD}:/app --env-file ./env.dev aws_cli' + +``` + +Then, source the file: +``` +$ . ~/.bashrc +``` + +Then, you can just do: +``` +$ aws iam get-user +``` \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml deleted file mode 100644 index c2ea30f..0000000 --- a/docker-compose.yml +++ /dev/null @@ -1,10 +0,0 @@ -version: "3" - -services: - aws-cli: - container_name: aws_cli - build: . - env_file: - - '.env' - volumes: - - ".:/app" diff --git a/env.sample b/env.sample new file mode 100644 index 0000000..ede7c1c --- /dev/null +++ b/env.sample @@ -0,0 +1,14 @@ +COMPOSE_PROJECT_NAME=aws_cli + +# References: +# 1. https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file +# 2. https://docs.aws.amazon.com/cli/latest/userguide/cli-environment.html +# 3. https://docs.aws.amazon.com/cli/latest/topic/config-vars.html + +# Access and secret key variables override credentials stored in credential and config files. +AWS_ACCESS_KEY_ID=***REMOVED*** +AWS_SECRET_ACCESS_KEY=***REMOVED*** + +# AWS region. This variable overrides the default region of the in-use profile, if set. +# See: https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/ +AWS_DEFAULT_REGION=us-west-2 \ No newline at end of file From dedaac41b2ada3ec08ee6dc85ad92271aff1b4bb Mon Sep 17 00:00:00 2001 From: Housni Yakoob Date: Sat, 4 Aug 2018 00:08:33 +0300 Subject: [PATCH 2/2] Adding a default argument to display user info --- Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index bd1d0b3..676cccb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,4 +14,5 @@ WORKDIR /app # Copy code from the current folder to /app inside the container ADD . /app -ENTRYPOINT [ "aws" ] \ No newline at end of file +ENTRYPOINT [ "aws" ] +CMD [ "iam", "get-user" ]