ハンズオン(簡易版): Lambda基礎 レイヤー

5.2. Lambda関数のコード更新 (ZIP: handson-cli-lambda-basic-layer-function)

手順の目的 [why]

Lambda関数"handson-cli-lambda-basic-layer-function"のコードを更新します。

設定値の指定

設定値の指定

手順に必要な設定値を変数に格納をします。

0. リージョンの指定

リージョンを指定します。

環境変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

1. Lambda関数名

Lambda関数名を指定します。

変数の設定:

LAMBDA_FUNCTION_NAME='handson-cli-lambda-basic-layer-function'

2. Lambda関数コードZIPファイル

Lambda関数コードZIPファイル用ディレクトリを指定します。

変数の設定:

DIR_LAMBDA_FUNCTION_ZIP="${HOME}/environment/artifact-handson-cli-lambda"

Lambda関数コードZIPファイルを指定します。

変数の設定:

FILE_LAMBDA_FUNCTION_ZIP="${DIR_LAMBDA_FUNCTION_ZIP}/${LAMBDA_FUNCTION_NAME}.zip" \
  && echo ${FILE_LAMBDA_FUNCTION_ZIP}

結果(例):

${HOME}/environment/artifact-handson-cli-lambda/handson-cli-lambda-basic-layer-function.zip

設定値の確認

各変数に正しい設定値が格納されていることを確認しながら保存します。

変数の確認:

cat << END

  # 0. AWS_DEFAULT_REGION:"ap-northeast-1"
       AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 1. LAMBDA_FUNCTION_NAME:"handson-cli-lambda-basic-layer-function"
       LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME}"
  # 2. FILE_LAMBDA_FUNCTION_ZIP:"${HOME}/environment/artifact-handson-cli-lambda/handson-cli-lambda-basic-layer-function.zip"
       FILE_LAMBDA_FUNCTION_ZIP="${FILE_LAMBDA_FUNCTION_ZIP}"

END

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

処理の実行

Lambda関数のコードを更新します。

変数の確認:

cat << END

  # LAMBDA_FUNCTION_NAME:"handson-cli-lambda-basic-layer-function"
    LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME}"
  # FILE_LAMBDA_FUNCTION_ZIP:"${HOME}/environment/artifact-handson-cli-lambda/handson-cli-lambda-basic-layer-function.zip"
    FILE_LAMBDA_FUNCTION_ZIP="${FILE_LAMBDA_FUNCTION_ZIP}"

END

コマンド:

aws lambda update-function-code \
  --function-name ${LAMBDA_FUNCTION_NAME} \
  --zip-file fileb://${FILE_LAMBDA_FUNCTION_ZIP}

結果(例):

{
    "Layers": [
        {
            "CodeSize": 259,
            "Arn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:layer:handson-cli-lambda-layer:1"
        }
    ],
    "FunctionName": "handson-cli-lambda-basic-layer-function",
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:handson-cli-lambda-basic-layer-function",
    "Runtime": "python3.8",
    "Role": "arn:aws:iam::XXXXXXXXXXXX:role/handson-cli/handson-cli-lambda-role",
    "Handler": "handson-cli-lambda-basic-layer-function.handler",
    "CodeSize": 256,
    "Description": "function for handson-cli-lambda."
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2021-02-18T01:23:45.678+0000",
    "CodeSha256": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Version": "$LATEST",
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "State": "Active",
    "LastUpdateStatus": "Successful",
    "PackageType": "Zip"
}

ZIPファイルのハッシュ値を取得し、Base64エンコードします。

コマンド:

LAMBDA_FUNCTION_CODE_SHA256_ZIP=$( \
  openssl dgst -binary -sha256 ${FILE_LAMBDA_FUNCTION_ZIP} \
    | base64 \
) \
  && echo ${LAMBDA_FUNCTION_CODE_SHA256_ZIP}

結果(例):

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

完了確認

「Lambda関数"handson-cli-lambda-basic-layer-function"の更新に成功している。」ことを確認します。

コマンド:

aws lambda get-function \
  --function-name ${LAMBDA_FUNCTION_NAME} \
  --query 'Configuration.LastUpdateStatus' \
  --output text

結果(例):

Successful

「Lambda関数"handson-cli-lambda-basic-layer-function"のコードハッシュ値が"ZIPファイルのハッシュをBase64エンコードした値"と同じである。」ことを確認します。

コマンド:

LAMBDA_FUNCTION_CODE_SHA256=$( \
  aws lambda get-function \
    --function-name ${LAMBDA_FUNCTION_NAME} \
    --query 'Configuration.CodeSha256' \
    --output text \
) \
  && echo ${LAMBDA_FUNCTION_CODE_SHA256}

結果(例):

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

コマンド:

if [ "${LAMBDA_FUNCTION_CODE_SHA256_ZIP}" = "${LAMBDA_FUNCTION_CODE_SHA256}" ]; then
  (echo "same"; exit 0)
else
  (echo "different"; exit 1)
fi

結果(例):

same

手順の完了