ハンズオン(簡易版): Lambda基礎 Boto3 (Python SDK)

2.2.10. Lambda関数設定(環境変数)の更新 (handson-cli-lambda-boto3-ec2-revoke-function)

手順の目的 [why]

Lambda関数"handson-cli-lambda-boto3-ec2-revoke-function"の設定(環境変数)を更新します。

設定値の指定

設定値の指定

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

0. リージョンの指定

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

環境変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

1. Lambda関数名

Lambda関数名を指定します。

変数の設定:

LAMBDA_FUNCTION_NAME='handson-cli-lambda-boto3-ec2-revoke-function'

2. Lambda関数の環境変数の設定文字列

VPCのタグ名を指定します。

変数の設定:

EC2_VPC_TAG_NAME='handson-cli-lambda-vpc'

VPC IDを取得します。

コマンド:

EC2_VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${EC2_VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${EC2_VPC_ID}

結果(例):

vpc-xxxxxxxxxxxxxxxxx

セキュリティグループ名を指定します。

変数の設定:

EC2_SECURITY_GROUP_NAME='handson-cli-lambda-sg'

セキュリティグループIDを取得します。

コマンド:

EC2_SECURITY_GROUP_ID=$( \
  aws ec2 describe-security-groups \
    --filter Name=vpc-id,Values=${EC2_VPC_ID} \
      Name=group-name,Values=${EC2_SECURITY_GROUP_NAME} \
    --query "SecurityGroups[].GroupId" \
    --output text \
) \
  && echo ${EC2_SECURITY_GROUP_ID}

結果(例):

sg-xxxxxxxxxxxxxxxxx

Lambda関数の環境変数の設定文字列を指定します。

変数の設定:

STRING_LAMBDA_FUNCTION_ENVIRONMENT="Variables={REGION=${AWS_DEFAULT_REGION},VPC_ID=${EC2_VPC_ID},SECURITY_GROUP_ID=${EC2_SECURITY_GROUP_ID}}" \
  && echo ${STRING_LAMBDA_FUNCTION_ENVIRONMENT}

結果(例):

Variables={REGION=ap-northeast-1,VPC_ID=vpc-xxxxxxxxxxxxxxxxx,SECURITY_GROUP_ID=sg-xxxxxxxxxxxxxxxxx}

設定値の確認

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

変数の確認:

cat << END

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

  # 1. LAMBDA_FUNCTION_NAME:"handson-cli-lambda-boto3-ec2-revoke-function"
       LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME}"
  # 2. STRING_LAMBDA_FUNCTION_ENVIRONMENT:"Variables={REGION=ap-northeast-1,VPC_ID=vpc-xxxxxxxxxxxxxxxxx,SECURITY_GROUP_ID=sg-xxxxxxxxxxxxxxxxx}"
       STRING_LAMBDA_FUNCTION_ENVIRONMENT="${STRING_LAMBDA_FUNCTION_ENVIRONMENT}"

END

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

処理の実行

Lambda関数の環境変数を変更します。

変数の確認:

cat << ETX

  # LAMBDA_FUNCTION_NAME:"handson-cli-lambda-boto3-ec2-revoke-function"
    LAMBDA_FUNCTION_NAME="${LAMBDA_FUNCTION_NAME}"
  # STRING_LAMBDA_FUNCTION_ENVIRONMENT:"Variables={REGION=ap-northeast-1,VPC_ID=vpc-xxxxxxxxxxxxxxxxx,SECURITY_GROUP_ID=sg-xxxxxxxxxxxxxxxxx}"
    STRING_LAMBDA_FUNCTION_ENVIRONMENT="${STRING_LAMBDA_FUNCTION_ENVIRONMENT}"

ETX

コマンド:

aws lambda update-function-configuration \
  --function-name ${LAMBDA_FUNCTION_NAME} \
  --environment "${STRING_LAMBDA_FUNCTION_ENVIRONMENT}"

結果(例):

{
    "FunctionName": "handson-cli-lambda-boto3-ec2-revoke-function",
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:handson-cli-lambda-boto3-ec2-revoke-function",
    "Runtime": "python3.8",
    "Role": "arn:aws:iam::XXXXXXXXXXXX:role/handson-cli/handson-cli-lambda-boto3-ec2-revoke-role",
    "Handler": "handson-cli-lambda-boto3-ec2-revoke-function.lambda_handler",
    "CodeSize": 256,
    "Description": "function for handson-cli-lambda.",
    "Timeout": 3,
    "MemorySize": 128,
    "LastModified": "2021-03-18T01:23:45.678+0000",
    "CodeSha256": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "Version": "$LATEST",
    "Environment": {
        "Variables": {
            "VPC_ID": "vpc-xxxxxxxxxxxxxxxxx"
            "REGION": "ap-northeast-1",
            "SECURITY_GROUP_ID": "sg-xxxxxxxxxxxxxxxxx"
        }
    },
    "TracingConfig": {
        "Mode": "PassThrough"
    },
    "RevisionId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "State": "Active",
    "LastUpdateStatus": "Successful",
    "PackageType": "Zip"
}

完了確認

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

コマンド:

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

結果(例):

Successful

「Lambda関数"handson-cli-lambda-boto3-ec2-revoke-function"の設定に環境変数が存在する。」ことを確認します。

コマンド:

aws lambda get-function-configuration \
  --function-name ${LAMBDA_FUNCTION_NAME} \
  --query 'Environment.Variables'

結果(例):

{
  "VPC_ID": "vpc-xxxxxxxxxxxxxxxxx",
  "REGION": "ap-northeast-1",
  "SECURITY_GROUP_ID": "sg-xxxxxxxxxxxxxxxxx"
}

手順の完了