ハンズオン(簡易版): EventBridge入門

事前作業2.5. Lambda関数の作成 (handson-cli-events-gettings-ebs-function)

目的

Lambda関数"handson-cli-events-gettings-ebs-function"を作成します。

パラメータの指定

作業に必要なパラメータを変数に格納をします。

0. リージョンの指定

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

環境変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

1. Lambda関数名

Lambda関数名を指定します。

変数の設定:

LAMBDA_FUNCTION_NAME='handson-cli-events-gettings-ebs-function'

2. Lambda関数の説明

Lambda関数の説明を指定します。

変数の設定:

LAMBDA_FUNCTION_DESCRIPTION='function for ebs event of handson-cli-events-gettings.'

3. Lambda関数のランタイム名

Lambda関数のランタイム名を指定します。

変数の設定:

LAMBDA_RUNTIME='python3.8'

4. IAMロール名

IAMロール名を指定します。

変数の設定:

IAM_ROLE_NAME='handson-cli-events-gettings-ebs-role'

5. Lambda関数用ソースコードファイル名

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

変数の設定:

DIR_LAMBDA_CODE="${HOME}/environment/conf-handson-cli-events-gettings"

Lambda関数用ソースコードファイル名を指定します。

変数の設定:

LAMBDA_CODE_NAME='handson-cli-events-gettings-ebs-function.py'

Lambda関数が利用するソースコードのファイル名を指定します。

変数の設定:

FILE_LAMBDA_CODE="${DIR_LAMBDA_CODE}/${LAMBDA_CODE_NAME}" \
  && echo ${FILE_LAMBDA_CODE}

結果(例):

${HOME}/environment/conf-handson-cli-events-gettings/handson-cli-events-gettings-ebs-function.py

各変数に正しいパラメータ値が格納されていることを確認します。

変数の確認:

cat << END

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

  # 1. LAMBDA_FUNCTION_NAME:"handson-cli-events-gettings-ebs-function"
       LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME}"
  # 2. LAMBDA_FUNCTION_DESCRIPTION:"function for ebs event of handson-cli-events-gettings."
       LAMBDA_FUNCTION_DESCRIPTION="${LAMBDA_FUNCTION_DESCRIPTION}"
  # 3. LAMBDA_RUNTIME:"python3.8"
       LAMBDA_RUNTIME="${LAMBDA_RUNTIME}"
  # 4. IAM_ROLE_NAME:"handson-cli-events-gettings-ebs-role"
       IAM_ROLE_NAME="${IAM_ROLE_NAME}"
  # 5. FILE_LAMBDA_CODE:"${HOME}/environment/conf-handson-cli-events-gettings/handson-cli-events-gettings-ebs-function.py"
       FILE_LAMBDA_CODE="${FILE_LAMBDA_CODE}"

END

手順

IAMロールのARNを取得します。

コマンド:

IAM_ROLE_ARN=$( \
  aws iam get-role \
    --role-name ${IAM_ROLE_NAME} \
    --query 'Role.Arn' \
    --output text \
) \
  && echo ${IAM_ROLE_ARN}

結果(例):

arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-events-gettings-ebs-role

ZIPファイルを作成します。

変数の設定:

FILE_LAMBDA_ZIP="${DIR_LAMBDA_CODE}/${LAMBDA_FUNCTION_NAME}.zip" \
  && echo ${FILE_LAMBDA_ZIP}

結果(例):

${HOME}/environment/conf-handson-cli-events-gettings/handson-cli-events-gettings-ebs-function.zip

コマンド:

zip -j ${FILE_LAMBDA_ZIP} ${FILE_LAMBDA_CODE}

結果(例):

adding: ${HOME}/environment/conf-handson-cli-events-gettings/handson-cli-events-gettings-ebs-function.py (deflated 43%)

Lambda関数のハンドラ名を指定します。

変数の設定:

LAMBDA_HANDLER="${LAMBDA_FUNCTION_NAME}.lambda_handler" \
  && echo ${LAMBDA_HANDLER}

結果(例):

handson-cli-events-gettings-ebs-function.lambda_handler

Lambda関数を作成します。

変数の確認:

cat << ETX

  # LAMBDA_FUNCTION_NAME:"handson-cli-events-gettings-ebs-function"
    LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME}"
  # LAMBDA_FUNCTION_DESCRIPTION:"function for ebs event of handson-cli-events-gettings."
    LAMBDA_FUNCTION_DESCRIPTION="${LAMBDA_FUNCTION_DESCRIPTION}"
  # LAMBDA_RUNTIME:"python3.8"
    LAMBDA_RUNTIME="${LAMBDA_RUNTIME}"
  # LAMBDA_HANDLER:"handson-cli-events-gettings-ebs-function.lambda_handler"
    LAMBDA_HANDLER="${LAMBDA_HANDLER}"
  # IAM_ROLE_ARN:"arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-events-gettings-ebs-role"
    IAM_ROLE_ARN="${IAM_ROLE_ARN}"
  # FILE_LAMBDA_ZIP:"${HOME}/environment/conf-handson-cli-events-gettings/handson-cli-events-gettings-ebs-function.zip"
    FILE_LAMBDA_ZIP="${FILE_LAMBDA_ZIP}"

ETX

コマンド:

aws lambda create-function \
  --function-name ${LAMBDA_FUNCTION_NAME} \
  --description "${LAMBDA_FUNCTION_DESCRIPTION}" \
  --runtime ${LAMBDA_RUNTIME} \
  --role ${IAM_ROLE_ARN} \
  --handler ${LAMBDA_HANDLER} \
  --zip-file fileb://${FILE_LAMBDA_ZIP}

結果(例):

{
  "LastUpdateStatus": "Successful",
  "CodeSha256": "<>",
  "FunctionName": "handson-cli-events-gettings-ebs-function",
  "RevisionId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
  "CodeSize": <>,
  "MemorySize": 8,
  "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:handson-cli-events-gettings-ebs-function",
  "Version": "$LATEST",
  "Role": "arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-events-gettings-ebs-role",
  "Timeout": 3,
  "LastModified": "2020-09-10T01:23:45.678+0000",
  "Handler": "handson-cli-events-gettings-ebs-function.lambda_handler",
  "Runtime": "python3.8",
  "State": "Active",
  "TracingConfig": {
      "Mode": "PassThrough"
  },
  "Description": "function for ebs event of handson-cli-events-gettings."
}

完了確認

「Lambda関数"handson-cli-events-gettings-ebs-function"が存在する。」ことを確認します。

コマンド:

aws lambda list-functions \
  --query "Functions[?FunctionName == \`${LAMBDA_FUNCTION_NAME}\`].FunctionName" \
  --output text

結果(例):

handson-cli-events-gettings-ebs-function