11
7

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.

AWS Lambdaの関数から別のLambda関数を呼び出すときのNode.js実装とServerless設定

Posted at

毎回調べてる気がするのでメモ。

前提

AWSアカウントがあってaws configure でアカウントキーの設定済み。

> node -v
v10.11.0

> npm -v
6.9.0

> aws --version
aws-cli/1.16.153 Python/3.6.6 Darwin/18.6.0 botocore/1.12.143

> sls -v
1.43.0

環境構築

serverlessで環境を作成します。

> sls create -t aws-nodejs \
  -p invoke-lambda-fcn-on-lambda

> cd invoke-lambda-fcn-on-lambda

> npm init

# ローカルでテストするならインストールする
> npm i -d --save-dev aws-sdk

実装と設定

handler.js
'use strict';

module.exports.hello = async (event) => {

  let AWS = require('aws-sdk');
  let lambda = new AWS.Lambda();

  let result = await lambda.invoke({
    FunctionName: 'invoke-lambda-fcn-on-lambda-dev-hoge',
    InvocationType: 'RequestResponse',
    Payload: JSON.stringify({"hoge": "hoge"})
  }).promise();
  console.log('invoke function', result);

  return {
    statusCode: 200,
    body: JSON.stringify({
      message: result,
      input: event,
    }, null, 2),
  };
};


module.exports.hoge = async (event) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: event.Body,
      input: event,
    }, null, 2),
  };
};

iamRoleStatements になに設定するんだっけなーって毎回悩みます。

serverless.yml
service: invoke-lambda-fcn-on-lambda

provider:
  name: aws
  runtime: nodejs10.x
  stage: dev
  region: us-east-1

  iamRoleStatements:
    - Effect: 'Allow'
      Action:
        - 'lambda:InvokeFunction'
      Resource:
        - '*'

functions:
  hello:
    handler: handler.hello

  hoge:
    handler: handler.hoge

デプロイと動作確認

sls invoke local でローカル実行させたいのですが、lambda.invoke メソッドで実行できるのはデプロイ済みの関数だけっぽいです。(未調査)

> sls deploy

(略)
Serverless: Stack update finished...
Service Information
service: invoke-lambda-fcn-on-lambda
stage: dev
region: us-east-1
stack: invoke-lambda-fcn-on-lambda-dev
resources: 8
api keys:
  None
endpoints:
  None
functions:
  hello: invoke-lambda-fcn-on-lambda-dev-hello
  hoge: invoke-lambda-fcn-on-lambda-dev-hoge
layers:
  None

> sls invoke -f hello -log

{
    "statusCode": 200,
    "body": "{\n  \"message\": {\n    \"StatusCode\": 200,\n    \"ExecutedVersion\": \"$LATEST\",\n    \"Payload\": \"{\\\"statusCode\\\":200,\\\"body\\\":\\\"{\\\\n  \\\\\\\"input\\\\\\\": {\\\\n    \\\\\\\"hoge\\\\\\\": \\\\\\\"hoge\\\\\\\"\\\\n  }\\\\n}\\\"}\"\n  },\n  \"input\": {}\n}"
}
--------------------------------------------------------------------
START RequestId: f83cec93-032a-4159-90a5-a4345fa68672 Version: $LATEST
   '{"statusCode":200,"body":"{\\n  \\"input\\": {\\n    \\"hoge\\": \\"hoge\\"\\n  }\\n}"}' } function { StatusCode: 200,
END RequestId: f83cec93-032a-4159-90a5-a4345fa68672
REPORT RequestId: f83cec93-032a-4159-90a5-a4345fa68672  Duration: 34.77 ms      Billed Duration: 100 ms         Memory Size: 1024 MB    Max Memory Used: 93 MB

はい。
環境のお片付けもお忘れなく。

> sls remove

参考

amazon web services - AccessDeniedException: User is not authorized to perform: lambda:InvokeFunction - Stack Overflow
https://stackoverflow.com/questions/37498124/accessdeniedexception-user-is-not-authorized-to-perform-lambdainvokefunction

11
7
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
11
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?