4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[JAWS-UG CLI] Lambda:#6 Lambda関数の変更

Last updated at Posted at 2015-10-26

前提条件

Lambdaへの権限

Lambdaに対してフル権限があること。

AWS CLI

以下のバージョンで動作確認済

  • AWS CLI 1.9.0
コマンド
aws --version
結果(例)
      aws-cli/1.9.0 Python/2.7.5 Darwin/13.4.0 botocore/1.3.0
  1. 準備
    =======

変数の確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       administrator-prjz-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                ap-northeast-1        env    AWS_DEFAULT_REGION

プロファイルが想定のものになっていることを確認します。

  1. 事前作業
    ===========

1.1. Lambda関数名の決定

コマンド
aws lambda list-functions \
        --query 'Functions[].FunctionName'
結果(例)
      [
        "HelloWorld"
      ]
変数の設定
LAMBDA_FUNC_NAME='HelloWorld'

1.2. Lambda関数

変数の設定
FILE_LAMBDA_FUNC="${LAMBDA_FUNC_NAME}.js"
変数の確認
cat << ETX

        FILE_LAMBDA_FUNC: ${FILE_LAMBDA_FUNC}

ETX
コマンド
cat << EOF > ${FILE_LAMBDA_FUNC}
console.log('Loading function');

exports.handler = function(event, context) {
        console.log('value1 =', event.key2);
        console.log('value2 =', event.key3);
        console.log('value3 =', event.key1);
        context.succeed(event.key2);  // Echo back the first key value
        // context.fail('Something went wrong');
};
EOF

cat ${FILE_LAMBDA_FUNC}
コマンド
zip ${LAMBDA_FUNC_NAME}-new.zip ${FILE_LAMBDA_FUNC}
結果
      adding: HelloWorld.js (deflated 43%)
  1. Lambda関数の更新
    ===================

2.1. Lambda関数の更新

変数の設定
FILE_LAMBDA_ZIP="${LAMBDA_FUNC_NAME}-new.zip"
変数の確認
cat << ETX

        LAMBDA_FUNC_NAME:  ${LAMBDA_FUNC_NAME}
        FILE_LAMBDA_ZIP:   ${FILE_LAMBDA_ZIP}

ETX
コマンド
aws lambda update-function-code \
        --function-name ${LAMBDA_FUNC_NAME} \
        --zip-file fileb://${FILE_LAMBDA_ZIP} \
        --publish
結果(例)

      {
        "CodeSha256": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=",
        "FunctionName": "HelloWorld",
        "CodeSize": 349,
        "MemorySize": 128,
        "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloWorld:2",
        "Version": "2",
        "Role": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution",
        "Timeout": 2,
        "LastModified": "2015-10-26T01:23:45.678+0000",
        "Handler": "HelloWorld.handler",
        "Runtime": "nodejs",
        "Description": "Hello World with AWS Lambda function."
      }

2.2. Lambda関数の確認

コマンド
aws lambda get-function \
        --function-name ${LAMBDA_FUNC_NAME}
結果(例)

      {
        "Code": {
          "RepositoryType": "S3",
          "Location": "https://awslambda-ap-ne-1-tasks.s3-ap-northeast-1.amazonaws.com/snapshots/XXXXXXXXXXXX/HelloWorld-38950242-86bb-4cbb-bab7-27dfdec7f362?x-amz-security-token=AQoDYXdzEDwa8AMILEth%2BKa19K9tePesujMLPz9zX2%2Fglxg9xU%2BPwvLP8ZMoogywweINkAhFta2lr88GEKJwk8x077DnoOmYeXmD2XE2bXbYPgNAFJU2JYgpnxckoXt2qrNAQKUpCtWqeCwHm34d%2FRhoVDXIbhHpn4iMkg%2BCFfU5%2BtjYsZQzp6CfcDUd3fZXYgSBjCJdpTIc222P6zcusHt%2FPc9ob016zlAkIuEi5ZZhXwZFouF7wwURvSo0QbZ1e1M2DwqNfL1bpC3oEPGrKM43XtwEvVSpMu%2FwdDQHBi1f8B7xJbmjNXA7zg%2BLybhUHioDdLPlS2NDgSXUO4QP3BMmTDeLN5Z6rx0ddjl1JJpLczIl6Plzln0NMQ14dkkIKriteFL09Ei0DJQ7Y%2BiXPNMStM66vsN%2F9yAHGNxCrFKEH0t9lgV2Od6%2Fny3MmNQiNSaaqCHsnKpLbabLSy82dsQwhW35XPCJPShwbgXPB9RPXjx%2FqBCnyGztXE7s6vsBy3AmJbctcDcgte2cpvcoEkg4H%2BrDTaBRvDYlqxEj71C3WLLI62z6OLS5fxkYmYfDZzhDQ5oLg5eyvXxlwugkvh%2Fo7pKuo%2B48liADEZDyLKHqFPH0j924OYRJrtr2b1iw4nrwyMAE7VnQBLAFfPWnWeiD3lfDKARvUpFeINW2trEF&AWSAccessKeyId=ASIAJDZ6MUQCOK4OEDVQ&Expires=1445835375&Signature=uzNTBfSjf1kwixwHBT5LsRtQowo%3D"
        },
        "Configuration": {
          "Version": "$LATEST",
          "CodeSha256": "+EAYs8jMYFRBdnsMdFUt2bv//KHE2tkovql6ls+gXoM=",
          "FunctionName": "HelloWorld",
          "MemorySize": 128,
          "CodeSize": 349,
          "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloWorld",
          "Handler": "HelloWorld.handler",
          "Role": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution",
          "Timeout": 3,
          "LastModified": "2015-10-26T04:41:36.261+0000",
          "Runtime": "nodejs",
          "Description": "\ |LAMBDA_FUNC_DESC|\ "
        }
      }

2.3. バージョン番号の取得

変数の設定
LAMBDA_FUNC_VER=$( \
        hash=$( \
          aws lambda list-versions-by-function \
            --function-name ${LAMBDA_FUNC_NAME} \
            --query 'Versions[?Version == `$LATEST`].CodeSha256' \
            --output text \
        ) \
        && aws lambda list-versions-by-function \
          --function-name ${LAMBDA_FUNC_NAME} \
          --query "Versions[?CodeSha256 == \`${hash}\` && Version != \`\$LATEST\`].Version[]" \
          --output text \
      ) \
        && echo ${LAMBDA_FUNC_VER}
結果(例)
2
  1. Lambda関数の動作確認
    =======================

3.1. サンプルデータの作成

変数の設定
FILE_DATA_LAMBDA="${LAMBDA_FUNC_NAME}-data.json" \
        && echo ${FILE_DATA_LAMBDA}
サンプルデータ
cat << EOF > ${FILE_DATA_LAMBDA}
{
          "key1": "val1",
          "key2": "val2",
          "key3": "val3"
}
EOF

cat ${FILE_DATA_LAMBDA}
コマンド
jsonlint -q ${FILE_DATA_LAMBDA}
変数の設定
FILE_OUTPUT_LAMBDA="${LAMBDA_FUNC_NAME}-out.txt" \
        && echo ${FILE_OUTPUT_LAMBDA}
変数の確認
cat << ETX

        LAMBDA_FUNC_NAME:   ${LAMBDA_FUNC_NAME}
        FILE_DATA_LAMBDA:   ${FILE_DATA_LAMBDA}
        FILE_OUTPUT_LAMBDA: ${FILE_OUTPUT_LAMBDA}

ETX
コマンド
FILE_LOG_LAMBDA="${LAMBDA_FUNC_NAME}-$(date +%Y%m%d%H%M%S).log" \
        && echo ${FILE_LOG_LAMBDA}
コマンド
aws lambda invoke \
        --function-name ${LAMBDA_FUNC_NAME} \
        --log-type Tail \
        --payload file://${FILE_DATA_LAMBDA} \
        ${FILE_OUTPUT_LAMBDA} \
        > ${FILE_LOG_LAMBDA}
コマンド
cat ${FILE_LOG_LAMBDA} \
  | jp.py 'StatusCode'
結果(例)
      200
コマンド
cat ${FILE_OUTPUT_LAMBDA}
結果
      val2
コマンド
cat ${FILE_LOG_LAMBDA} \
        | jp.py 'LogResult' \
        | sed 's/"//' \
        | base64 --decode
結果
      START RequestId: 1dc6ae3a-7b9d-11e5-a29a-b93d2ad7eab9 Version: $LATEST
      2015-10-26T04:50:45.532Z    1dc6ae3a-7b9d-11e5-a29a-b93d2ad7eab9    value1 = val2
      2015-10-26T04:50:45.532Z    1dc6ae3a-7b9d-11e5-a29a-b93d2ad7eab9    value2 = val3
      2015-10-26T04:50:45.532Z    1dc6ae3a-7b9d-11e5-a29a-b93d2ad7eab9    value3 = val1
      END RequestId: 1dc6ae3a-7b9d-11e5-a29a-b93d2ad7eab9
      REPORT RequestId: 1dc6ae3a-7b9d-11e5-a29a-b93d2ad7eab9      Duration: 6.34 ms       Billed Duration: 100 ms         Memory Size: 128 MB     Max Memory Used: 27 MB
  1. エイリアスの更新
    ===================

4.1. エイリアスの確認

変数の設定
LAMBDA_ALIAS_NAME='product'
コマンド
aws lambda get-alias \
        --function-name ${LAMBDA_FUNC_NAME} \
        --name ${LAMBDA_ALIAS_NAME}
結果(例)
      {
        "AliasArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloWorld:product",
        "FunctionVersion": "1",
        "Name": "product",
        "Description": "Released version on product env."
      }

4.2. エイリアスの更新

コマンド
aws lambda update-alias \
        --function-name ${LAMBDA_FUNC_NAME} \
        --function-version ${LAMBDA_FUNC_VER} \
        --name ${LAMBDA_ALIAS_NAME}
結果(例)
      {
        "AliasArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloWorld:product",
        "FunctionVersion": "2",
        "Name": "product",
        "Description": "Released version on product env."
      }

4.3. エイリアスの確認

コマンド
aws lambda get-alias \
        --function-name ${LAMBDA_FUNC_NAME} \
        --name ${LAMBDA_ALIAS_NAME}
結果(例)
      {
        "AliasArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloWorld:product",
        "FunctionVersion": "2",
        "Name": "product",
        "Description": "Released version on product env."
      }

完了

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?