AWS Lambda の customer-managed runtimes で AWS CLI を入れた Bash の runtime を試してみた

AWS Lambda の customer-managed runtimes で AWS CLI を入れた Bash の runtime を試してみた

Docker イメージのビルドと ECR への push、`aws lambda-core` での runtime・version・alias の作成までの手順を紹介します。この runtime を使い、ZIP 圧縮したシェルスクリプトで Lambda 関数を作成すると、関数内で AWS CLI を実行できました。
2026.11.20

はじめに

2026年11月18日、AWS Lambda の customer-managed runtimes が発表されました。

https://aws.amazon.com/jp/about-aws/whats-new/2026/11/aws-lambda-customer-managed-runtimes/

customer-managed runtimes は、自分で作った runtime を Lambda 関数から使える機能です。runtime は関数とは別のリソースとして作ります。関数では、python3.14 のような識別子の代わりに、runtime の ARN と alias 名を指定して利用します。

provided:al2023 のイメージに Bash の bootstrap、AWS CLI、jq を入れて、Bash スクリプトで関数を書ける runtime を作りました。

検証環境

  • リージョン:ap-northeast-1
  • アーキテクチャ:arm64(ビルドしたホストは aarch64)
  • ホスト側:AWS CLI 2.41.1、Docker 29.9.3
  • イメージ内:Amazon Linux 2023、aws-cli 2.41.1、jq 1.8.1

runtime の作成

イメージの定義

bootstrap と ECR のリポジトリポリシーは、次のチュートリアルのものを使います。

https://docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html

Dockerfile は、チュートリアルの Dockerfile に unzip、jq、AWS CLI をインストールする処理を加えたものです。AWS CLI は arm64 用のインストーラー(zip)を使うため、展開に unzip が要ります。Dockerfile として保存します。

FROM public.ecr.aws/lambda/provided:al2023

# AWS CLI v2 (official installer zip, latest) and jq
RUN dnf install -y unzip jq \
 && dnf clean all \
 && curl -sS "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o /tmp/awscliv2.zip \
 && unzip -q /tmp/awscliv2.zip -d /tmp \
 && /tmp/aws/install \
 && rm -rf /tmp/aws /tmp/awscliv2.zip

# Install the runtime interface client.
COPY bootstrap /var/runtime/bootstrap
RUN chmod 755 /var/runtime/bootstrap

# Keep the inherited ENTRYPOINT, /lambda-entrypoint.sh. Don't set CMD.

bootstrap は、ハンドラー function.handler をドットで分け、前半から function.sh を読み込み、後半を関数名として呼び出します。Dockerfile と同じディレクトリに bootstrap として保存します。

bootstrap
#!/bin/bash

set -euo pipefail

# Initialization - load function handler
source $LAMBDA_TASK_ROOT/"$(echo $_HANDLER | cut -d. -f1).sh"

# Processing
while true
do
  HEADERS="$(mktemp)"
  # Get an event. The HTTP request will block until one is received
  EVENT_DATA=$(curl -sS -LD "$HEADERS" "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/next")

  # Extract request ID by scraping response headers received above
  REQUEST_ID=$(grep -Fi Lambda-Runtime-Aws-Request-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2)

  # Extract invocation ID for cross-wiring protection, if present
  INVOCATION_ID=$(grep -Fi Lambda-Runtime-Invocation-Id "$HEADERS" | tr -d '[:space:]' | cut -d: -f2 || true)

  # Run the handler function from the script
  RESPONSE=$($(echo "$_HANDLER" | cut -d. -f2) "$EVENT_DATA")

  # Send the response, echoing the invocation ID
  curl -sS "http://${AWS_LAMBDA_RUNTIME_API}/2018-06-01/runtime/invocation/$REQUEST_ID/response"  -d "$RESPONSE" \
    ${INVOCATION_ID:+--header "Lambda-Runtime-Invocation-Id: $INVOCATION_ID"}

  # Remove the temporary headers file
  rm -f "$HEADERS"
done

ビルドと ECR への push

Lambda にイメージの読み取りを許可するリポジトリポリシーを、ecr-policy.json として保存します。

ecr-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "LambdaReadAccess",
      "Effect": "Allow",
      "Principal": {
        "Service": "lambda.amazonaws.com"
      },
      "Action": [
        "ecr:BatchGetImage",
        "ecr:DescribeImages",
        "ecr:GetDownloadUrlForLayer"
      ]
    }
  ]
}
export ACCOUNT_ID=111122223333
export AWS_REGION=ap-northeast-1
export REPO_URI=${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com/devio-bash-runtime-1119

docker build --platform linux/arm64 --tag ${REPO_URI}:v1 .
aws ecr create-repository --repository-name devio-bash-runtime-1119
aws ecr set-repository-policy --repository-name devio-bash-runtime-1119 --policy-text file://ecr-policy.json
aws ecr get-login-password | docker login --username AWS --password-stdin ${ACCOUNT_ID}.dkr.ecr.${AWS_REGION}.amazonaws.com
docker push ${REPO_URI}:v1

runtime、version、alias

runtime、version、alias は aws lambda-core で作ります。version と alias は wait で ACTIVE になるのを待ちます。

aws lambda-core create-runtime --name devio-bash-1119 --description "Bash runtime with AWS CLI and jq"
aws lambda-core create-runtime-version --runtime-name devio-bash-1119 --name v1 --image-uri ${REPO_URI}:v1
aws lambda-core wait runtime-version-active --runtime-name devio-bash-1119 --name v1
aws lambda-core create-runtime-alias --runtime-name devio-bash-1119 --name prod --runtime-version v1
aws lambda-core wait runtime-alias-active --runtime-name devio-bash-1119 --name prod

aws lambda-core get-runtime-version --runtime-name devio-bash-1119 --name v1 の応答は次のとおりです(抜粋)。

{
    "Name": "v1",
    ...
    "State": "ACTIVE",
    "Platforms": [
        {
            "Architecture": "arm64",
            "Os": "linux",
            ...
        }
    ],
    ...
}

Platforms には arm64 の1件が含まれています。

関数の作成と呼び出し

関数の作成

ここからは runtime を使う利用側の操作です。

実行ロールには AWSLambdaBasicExecutionRole だけを付けます。

実行ロールの作成
aws iam create-role --role-name devio-bash-1119-role --assume-role-policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws iam attach-role-policy --role-name devio-bash-1119-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

関数のコードは function.sh に書きます。イベントを stderr に出し、イベントの command を jq で取り出して bash -c で実行します。その出力は stderr に出してから、関数の戻り値として返します。

function handler () {
  EVENT_DATA=$1
  echo "$EVENT_DATA" 1>&2

  COMMAND=$(echo "$EVENT_DATA" | jq -r '.command')
  RESULT=$(bash -c "$COMMAND")
  echo "$RESULT" 1>&2
  echo "$RESULT"
}

function.sh に実行権限を付けて、.zip にします。

chmod 755 function.sh
zip function.zip function.sh

--runtime custom を指定し、--runtime-config で runtime の ARN と alias を渡して、関数を作成します。

aws lambda create-function --function-name devio-bash-1119-fn \
  --runtime custom \
  --runtime-config RuntimeArn=arn:aws:lambda:${AWS_REGION}:${ACCOUNT_ID}:runtime/devio-bash-1119,RuntimeAlias=prod \
  --handler function.handler \
  --timeout 30 \
  --zip-file fileb://function.zip \
  --role arn:aws:iam::${ACCOUNT_ID}:role/devio-bash-1119-role \
  --architectures arm64
aws lambda wait function-active-v2 --function-name devio-bash-1119-fn

create-function の応答は次のとおりです(抜粋)。RuntimeVersionConfig を見ると、関数が alias prod の指す version v1 に紐づいていることが分かります。

{
    ...
    "Runtime": "custom",
    "Handler": "function.handler",
    "Timeout": 30,
    "MemorySize": 128,
    ...
    "Architectures": [
        "arm64"
    ],
    ...
    "RuntimeVersionConfig": {
        "RuntimeVersionArn": "arn:aws:lambda:ap-northeast-1:111122223333:runtime/devio-bash-1119/version/v1"
    },
    ...
    "RuntimeConfig": {
        "RuntimeArn": "arn:aws:lambda:ap-northeast-1:111122223333:runtime/devio-bash-1119",
        "RuntimeAlias": "prod"
    }
}

呼び出しの結果

aws lambda invoke --function-name devio-bash-1119-fn --payload '{"command":"aws sts get-caller-identity"}' --cli-binary-format raw-in-base64-out response.json

command に実行したいコマンドを渡します。response.json は次の内容でした。

{
    "UserId": "AROAXXXXXXXXXXXXXXXXX:devio-bash-1119-fn",
    "Account": "111122223333",
    "Arn": "arn:aws:sts::111122223333:assumed-role/devio-bash-1119-role/devio-bash-1119-fn"
}

Arn は実行ロール devio-bash-1119-role の assumed-role です。実行ロールの認証情報が関数内の aws コマンドに渡ったことが分かります。実行ロールの権限は AWSLambdaBasicExecutionRole だけで、aws sts get-caller-identity に追加の権限は要りませんでした。関数側で HOME などの環境変数を設定しなくても、aws コマンドは動作しました。

この呼び出しで CloudWatch Logs に出力されたログは次のとおりです。{"command":"aws sts get-caller-identity"} は、--payload で渡したイベントを、function.sh の echo "$EVENT_DATA" 1>&2 が出した行です。その次の { から } まで(UserId、Account、Arn)は、実行したコマンドの出力を echo "$RESULT" 1>&2 が出したものです。{"status":"OK"} は、bootstrap が応答を返す curl の出力で、Runtime API が返した本文です。

INIT_START Runtime Version: devio-bash-1119:v1	Runtime Version ARN: arn:aws:lambda:ap-northeast-1:111122223333:runtime/devio-bash-1119/version/v1
START RequestId: 10a0ab8f-80ac-4576-b12f-256de700f193 Version: $LATEST
{"command":"aws sts get-caller-identity"}
{
"UserId": "AROAXXXXXXXXXXXXXXXXX:devio-bash-1119-fn",
"Account": "111122223333",
"Arn": "arn:aws:sts::111122223333:assumed-role/devio-bash-1119-role/devio-bash-1119-fn"
}
{"status":"OK"}
END RequestId: 10a0ab8f-80ac-4576-b12f-256de700f193
REPORT RequestId: 10a0ab8f-80ac-4576-b12f-256de700f193	Duration: 14503.90 ms	Billed Duration: 14562 ms	Memory Size: 128 MB	Max Memory Used: 112 MB	Init Duration: 57.30 ms	

INIT_START 行は、コールドスタートのたびに出力されます。関数がどの runtime version で動いたかが分かります。

メモリごとの実行時間

メモリを 128 MB、512 MB、1024 MB に変え、Duration を REPORT 行から読み取りました。測定は、aws sts get-caller-identity を実行して Account と Arn を jq で絞る関数で行いました。

コールドスタートの結果です。

メモリ Duration Init Duration
128 MB 14628.38 ms 59.51 ms
512 MB 3328.66 ms 59.96 ms
1024 MB 1779.73 ms 56.00 ms

ウォームスタートの結果です。

メモリ Duration
128 MB 12476.68 ms
512 MB 3028.41 ms
1024 MB 1460.02 ms

各値は1回の測定です。Max Memory Used は、128 MB 構成で 114 MB、512 MB と 1024 MB 構成で 119 MB でした。

まとめ

AWS CLI を使うシェルスクリプトを実行する Bash の runtime を、provided:al2023 のイメージに bootstrap、AWS CLI、jq を入れて作成できました。

利用側は、--runtime custom と --runtime-config を指定して、シェルスクリプトを .zip に格納した関数を作成し、この runtime で動かせました。

AWS 提供の runtime に無い言語やバージョンを Lambda で実行するには、これまで layer や Docker イメージを用意する必要がありました。今回追加された customer-managed runtimes もお試しください。

この記事をシェアする

AWSのお困り事はクラスメソッドへ

関連記事