LoginSignup
1
0

More than 1 year has passed since last update.

AWS CDK / CLI を試せるコンテナを amd64 / arm64 の両方で動くようにする

Last updated at Posted at 2023-04-08

Docker を利用して、AWS CDK / CLI を実行できるコンテナを作ります。
Windows(x86-64 ≒ amd64)、Mac(Apple silicon ≒ arm64)の両方で動くようにするための工夫もしています。

この記事でわかること

  • AWS CDK / CLI を実行できる Dockerfile の記述方法
  • amd64 / arm64 のアーキテクチャで分岐した Dockerfile の記述方法の1つ例
  • この記事内のソースは以下で公開しています。

コンテナでの AWS CDK / CLI 利用

各ファイルの説明と実行方法

TARGETARCH 変数を利用と FROM句・platform 指定を組み合わて、各アーキテクチャ事の固有処理を記載したステージを作っています。

最後に各アーキテクチャごとのステージ名を利用して、共通の処理を行います。

Dockerfile
FROM python:3.11.0-bullseye as base

ARG TARGETARCH
RUN echo "TARGETARCH for ${TARGETARCH}"

# amd64 系の処理
FROM --platform=linux/amd64 base as stage-amd64

RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
RUN curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb"

# arm64 系の処理
FROM --platform=linux/arm64 base as stage-arm64

RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
RUN curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_arm64/session-manager-plugin.deb" -o "session-manager-plugin.deb"

FROM stage-${TARGETARCH:-amd64} as final

# install the latest nodejs & npm & other
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
RUN apt update \
    && apt-get install -y nodejs \
    && apt clean

# install the latest AWS CDK
RUN npm install -g aws-cdk

# install the latest AWSCLI
RUN apt update \
    && apt-get install -y groff \
    && apt-get install -y less \
    && apt clean
RUN unzip awscliv2.zip \
    && ./aws/install

# install the latest SessionManagerPlugin
RUN dpkg -i session-manager-plugin.deb

上記の Dockerfile を以下のような docker-compose.yml で起動できるようにしました。

docker-compose.yml
version: "3.5"

services:
  aws-cdk:
    build:
      context: .
      dockerfile: ./Dockerfile
    container_name: aws-cdk
    tty: true
    working_dir: /usr/src/app
    volumes:
      - ./:/usr/src/app
      - ./.aws:/root/.aws

これで、以下のようにコンテナを起動して、コンテナにアクセスするこで、 AWS CDK / CLI が利用できるようになります。

docker-compose up -d
docker exec -it aws-cdk /bin/bash

コンテナにアクセスしたら、初期設定を行いましょう。

aws configure

注意点

./.aws 配下に AWS の credentials が作成されています。
git のリポジトリへ Push しないように必ず .gitignore で除外対象へ加えてください。

.gitignore
.aws

AWS CDK / CLI のバージョンが上がったとき

AWS CDK / CLI を最新バージョンとしたいときは以下のコマンドでコンテナをリビルドしてください。

docker-compose build --no-cache

ChatGPT に聞いてみた結果は、、、

ChatGPT へ amd64 と arm64 でインストールするプログラムを切り替え方法を聞いたところ、以下のように if で分岐することを教えてくれました。

この if で分岐するほうが一般的なんだと思いますが、この記事で記載した内容も参考にしてください。
どちらがわかりやすいと思うかかなっと思います。

# Base image
FROM ubuntu:latest

ARG TARGETARCH

# Install packages via apt-get
RUN apt-get update -y && \
    if [ "${TARGETARCH}" = "amd64" ]; then \
      apt-get install -y package-amd64; \
    elif [ "${TARGETARCH}" = "arm64" ]; then \
      apt-get install -y package-arm64; \
    else \
      echo "Unknown architecture: ${TARGETARCH}"; \
      exit 1; \
    fi

# Set the command to be executed when a container is started
CMD ["echo", "Hello, World!"]
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