Sign inSign up

deinternetjongens/aws-cdk

By deinternetjongens

Updated 17 days ago

Container to run AWS CDK in local development and/or in CI/CD pipelines

Image
1

10K+

deinternetjongens/aws-cdk repository overview

AWS CDK docker image

Table of contents

Description

This image is inspired from robertd/alpine-aws-cdk but doesn't use a node alpine based image. Instead, I've chosen to use a python image (with debian). This is because alpine needs to install missing glibc libraries to make AWS CLI V2 work, and the debian based python image does not. This means there is less maintenance.

The following packages are available in the container

If you have any questions and/or suggestions, please contact me via [email protected]

How to use

The image is tagged based on the installed version of AWS CDK. You can also use :latest, this will always serve the latest available version. Also, since version 1.130.0 the image contains a user aws-cdk you can use instead of running it as root.

Run a command directly

First of all you need to run pip3 install. You can do this just once, or when you add packages or need to upgrade.

docker run --rm -u aws-cdk -v $(pwd):/app -v pip:/home/aws-cdk/.local -w /app deinternetjongens/aws-cdk:latest /bin/sh -c 'pip3 install -r requirements.txt'

After pip packages are installed, you can run other commands:

docker run --rm -u aws-cdk -v $(pwd):/app -v pip:/home/aws-cdk/.local -w /app deinternetjongens/aws-cdk:latest /bin/sh -c 'cdk synth'

If you want to deploy, add your local default AWS credentials as volume

docker run --rm -u aws-cdk -v $(pwd):/app -v pip:/home/aws-cdk/.local -v ~/.aws/:/root/.aws -w /app deinternetjongens/aws-cdk:latest /bin/sh -c 'cdk deploy --all'
Add as step Bitbucket Pipelines

Use the template below to add a deploy as a step in bitbucket pipelines

    - step: &deploy
        name: Deploy to AWS
        image: deinternetjongens/aws-cdk:latest
        script:
          - pip3 install -r requirements.txt
          - cdk deploy --all --require-approval never

Hint, jq and zip are also available. This can be useful to get data from AWS via the CLI (see example below)

    - step: &deploy
        name: Deploy to AWS
        image: deinternetjongens/aws-cdk:latest
        script:
          - aws configure set default.region eu-central-1
          - DATABASE_SECRET_ARN=$(aws secretsmanager list-secrets --filter "Key=tag-value,Values=DatabaseStack" --query "SecretList[].ARN" --output text)
          - DATABASE_SECRETS=$(aws secretsmanager get-secret-value --secret-id ${DATABASE_SECRET_ARN} --query "SecretString" | jq -r '.')
          - export AWS_DB_PASSWORD=$(echo "${DATABASE_SECRETS}" | jq '.password')
Use for local development

For local development you can use a docker file. Create the following files:

.docker
+ infra
+- Dockerfile
+ docker-compose.yml

Add this to the Dockerfile

FROM deinternetjongens/aws-cdk:latest

USER aws-cdk

RUN ln -s /run/secrets/aws_config ~/.aws/config \
    && ln -s /run/secrets/aws_credentials ~/.aws/credentials

USER root

WORKDIR /app

Add this to the docker-compose.yml

version: '3.1'

services:
  infra:
    container_name: aws_cdk
    build:
      context: ../
      dockerfile: .docker/infra/Dockerfile
    tty: true
    volumes:
      - ../:/app:delegated
      - pip3_data:/home/aws-cdk/.local:delegated
    secrets:
      - aws_config
      - aws_credentials
secrets:
  aws_config:
    file: ~/.aws/config
  aws_credentials:
    file: ~/.aws/credentials
volumes:
  pip3_data:

Next, build and run the docker-compose

docker-compose -f .docker/docker-compose.yml build
docker-compose -f .docker/docker-compose.yml up -d --force-recreate

Now you can login to the container using this command

docker-compose -f .docker/docker-compose.yml exec -u aws-cdk infra bash

This will use the aws credentials stored in your local '~/.aws/' folder

Other useful commands

Run black (code formatter)

docker run --rm -u aws-cdk -v $(pwd):/app -w /app deinternetjongens/aws-cdk:latest /bin/sh -c 'black .'

Run pylint

docker run --rm -u aws-cdk -v $(pwd):/app -w /app deinternetjongens/aws-cdk:latest /bin/sh -c 'pylint ./**/*.py'

Run pycodestyle

docker run --rm -u aws-cdk -v $(pwd):/app -w /app deinternetjongens/aws-cdk:latest /bin/sh -c 'pycodestyle . --exclude .venv,cdk.out'

Tag summary

Content type

Image

Digest

sha256:b884971f8

Size

331.6 MB

Last updated

17 days ago

docker pull deinternetjongens/aws-cdk