Help us understand the problem. What is going on with this article?

[JAWS-UG CLI] Lambda #24 StepFunctions用関数 (Hello World)

More than 3 years have passed since last update.

前提条件

Lambdaへの権限

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

AWS CLI

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

  • AWS CLI 1.10.63
コマンド
aws --version

結果(例):

  aws-cli/1.11.63 Python/2.7.10 Darwin/15.6.0 botocore/1.5.26

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

IAM Role

'lambdaBasicExecution'ロールが存在すること。

0. 準備

まず変数の確認をします。

変数の確認
cat << ETX

        AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:  (0.2) ${AWS_DEFAULT_REGION}
        IAM_ROLE_ARN:        (0.3) ${IAM_ROLE_ARN}

ETX

結果(例):

  AWS_DEFAULT_PROFILE: (0.1) lambdaFull-prjz-mbp13
  AWS_DEFAULT_REGION:  (0.2) ap-northeast-1
  IAM_ROLE_ARN:        (0.3) arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution

変数が入っていない、適切でない場合は、それぞれの手順番号について作業を
行います。

0.1. プロファイルの指定

プロファイルの一覧を確認します。

コマンド
cat ~/.aws/credentials \
       | grep '\[' \
       | sed 's/\[//g' | sed 's/\]//g'

結果(例):

  iamFull-prjz-mbpr13
  lambdaFull-prjz-mbp13
変数の設定
export AWS_DEFAULT_PROFILE='lambdaFull-prjz-mbp13'

0.2. リージョンの指定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.3. IAMロールの指定

変数の設定
IAM_ROLE_NAME='lambdaBasicExecution'
コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}

結果(例):

  {
      "Role": {
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Action": "sts:AssumeRole",
                    "Principal": {
                        "Service": "lambda.amazonaws.com"
                    },
                    "Effect": "Allow",
                    "Sid": ""
                }
            ]
        },
        "RoleId": "AROAXXXXXXXXXXXXXXXXX",
        "CreateDate": "2017-03-18T01:23:45Z",
        "RoleName": "lambdaBasicExecution",
        "Path": "/",
        "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution"
      }
  }
コマンド
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/lambdaBasicExecution

1. 事前作業

1.1. Lambda関数名の決定

変数の設定
LAMBDA_FUNC_NAME='HelloFunction'

同名のLambda関数の不存在確認

コマンド
aws lambda get-function \
        --function-name ${LAMBDA_FUNC_NAME}

結果(例):

  A client error (ResourceNotFoundException) occurred when calling the GetFunction operation: Function not found: arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloFunction

1.2. Lambda関数

変数の設定
FILE_LAMBDA_FUNC='HelloFunction.js'
コマンド
cat << EOF > ${FILE_LAMBDA_FUNC}
exports.handler = (event, context, callback) => {
    callback(null, "Hello, " + event.who + "!");
};
EOF

cat ${FILE_LAMBDA_FUNC}

結果(例):

  exports.handler = (event, context, callback) => {
      callback(null, "Hello, " + event.who + "!");
  };
コマンド
zip ${LAMBDA_FUNC_NAME}.zip ${FILE_LAMBDA_FUNC}

結果:

  adding: HelloFunction.js (deflated 43%)

2. Lambda関数の作成

変数の設定
LAMBDA_FUNC_DESC='Say Hello to someone'
LAMBDA_RUNTIME='nodejs4.3'
LAMBDA_HANDLER="${LAMBDA_FUNC_NAME}.handler"
FILE_LAMBDA_ZIP="${LAMBDA_FUNC_NAME}.zip"
変数の確認
cat << ETX

        LAMBDA_FUNC_NAME:  ${LAMBDA_FUNC_NAME}
        LAMBDA_FUNC_DESC: "${LAMBDA_FUNC_DESC}"
        LAMBDA_RUNTIME:    ${LAMBDA_RUNTIME}
        FILE_LAMBDA_ZIP    ${FILE_LAMBDA_ZIP}
        IAM_ROLE_ARN:      ${IAM_ROLE_ARN}
        LAMBDA_HANDLER:    ${LAMBDA_HANDLER}

ETX
コマンド
aws lambda create-function \
        --function-name ${LAMBDA_FUNC_NAME} \
        --description "${LAMBDA_FUNC_DESC}" \
        --zip-file fileb://${FILE_LAMBDA_ZIP} \
        --runtime ${LAMBDA_RUNTIME} \
        --role ${IAM_ROLE_ARN} \
        --handler ${LAMBDA_HANDLER}

結果(例):

  {
    "CodeSha256": "aNNHoAV+4ERLB3aO2i72HiCZzsDDNF/22V4HSO+7Gk0=",
    "FunctionName": "HelloFunction",
    "CodeSize": 270,
    "MemorySize": 128,
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloFunction",
    "Version": "$LATEST",
    "Role": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution",
    "Timeout": 3,
    "LastModified": "2017-03-18T01:23:45.678+0000",
    "Handler": "HelloFunction.handler",
    "Runtime": "nodejs4.3",
    "Description": "Say Hello to someone"
  }
コマンド
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/HelloFunction-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx?x-amz-security-token=AQxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&AWSAccessKeyId=ASIAXXXXXXXXXXXXXXXX&Expires=xxxxxxxxxx&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  },
  "Configuration": {
      "Version": "$LATEST",
      "CodeSha256": "aNNHoAV+4ERLB3aO2i72HiCZzsDDNF/22V4HSO+7Gk0=",
      "FunctionName": "HelloFunction",
      "MemorySize": 128,
      "CodeSize": 270,
      "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloFunction",
      "Handler": "HelloFunction.handler",
      "Role": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution",
      "Timeout": 3,
      "LastModified": "2017-03-18T01:23:45.678+0000",
      "Runtime": "nodejs4.3",
      "Description": "Say Hello to someone"
    }
  }
コマンド
aws lambda get-function-configuration \
        --function-name ${LAMBDA_FUNC_NAME}

結果(例):

  {
    "CodeSha256": "aNNHoAV+4ERLB3aO2i72HiCZzsDDNF/22V4HSO+7Gk0=",
    "FunctionName": "HelloFunction",
    "CodeSize": 270,
    "MemorySize": 128,
    "FunctionArn": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloFunction",
    "Version": "$LATEST",
    "Role": "arn:aws:iam::XXXXXXXXXXXX:role/lambdaBasicExecution",
    "Timeout": 3,
    "LastModified": "2017-03-18T01:23:45.678+0000",
    "Handler": "HelloFunction.handler",
    "Runtime": "nodejs4.3",
    "Description": "Say Hello to someone"
  }

3. Lambda関数の動作確認

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

変数の設定
FILE_INPUT="${LAMBDA_FUNC_NAME}-data.json" \
          && echo ${FILE_INPUT}
サンプルデータ
cat << EOF > ${FILE_INPUT}
{
        "who": "AWS Step Functions"
}
EOF

cat ${FILE_INPUT}

JSONファイルを作成したら、フォーマットが壊れてないか必ず確認します。

コマンド
jsonlint -q ${FILE_INPUT}

エラーが出力されなければOKです。

3.2. lambda関数の手動実行

変数の設定
FILE_OUTPUT_LAMBDA="${LAMBDA_FUNC_NAME}-out.txt"
FILE_LOG_LAMBDA="${LAMBDA_FUNC_NAME}-$(date +%Y%m%d%H%M%S).log"
変数の確認
cat << ETX

        LAMBDA_FUNC_NAME:   ${LAMBDA_FUNC_NAME}
        FILE_INPUT:         ${FILE_INPUT}
        FILE_OUTPUT_LAMBDA: ${FILE_OUTPUT_LAMBDA}
        FILE_LOG_LAMBDA:    ${FILE_LOG_LAMBDA}

ETX
コマンド
aws lambda invoke \
        --function-name ${LAMBDA_FUNC_NAME} \
        --log-type Tail \
        --payload file://${FILE_INPUT} \
        ${FILE_OUTPUT_LAMBDA} \
        > ${FILE_LOG_LAMBDA}
コマンド
cat ${FILE_LOG_LAMBDA} \
        | jp.py 'StatusCode'

結果(例):

  200

3.3. lambda関数の実行結果の確認

コマンド
cat ${FILE_OUTPUT_LAMBDA}

結果:

  "Hello, AWS Step Functions!"

3.4. lambda関数のログの確認

コマンド
cat ${FILE_LOG_LAMBDA} \
        | jp.py 'LogResult' \
        | sed 's/\"//g' \
        | base64 --decode

結果(例):

  START RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Version: $LATEST
  END RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  REPORT RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx      Duration: 2.08 ms       Billed Duration: 100 ms         Memory Size: 128 MB        Max Memory Used: 11 MB

完了

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away