LoginSignup
2
2

More than 5 years have passed since last update.

Serverless Frameworkで複数ステージにデプロイされているLambdaをInvokeする

Posted at

Serverless Frameworkでは、$ sls deploy --stage devとか$ sls deploy --stage prodみたいにステージを分けてデプロイできます。

でServerless FrameworkでデプロイされるLambdaは、service-name-stage-function-nameという名前になります。
なのでLambdaからinvokeする場合は以下のように書きます。

const query = {
  FunctionName: 'serverless-fw-name-development-examplefunction',
  Payload: JSON.stringify({
    'username': username
  }),
  InvocationType: 'Event',
  LogType: 'None'
}
const client = new aws.Lambda({apiVersion: '2015-03-31'})
client.invoke(query).promise()

ただこの書き方だと、stage名決め打ちになってしまいます。
で、どうもServerless FWのステージ名はeventcontext内にも入っていない様子。

ということで今のところserverless.ymlで環境変数としてステージ名を突っ込んでおくのが無難かなと思います。

serverless.yml
service: sample

provider:
  name: aws
  runtime: nodejs4.3
  stage: dev
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "lambda:InvokeFunction"
      Resource:
        - "*" #サンプルなので全許可にしてます。
functions:
  example:
    handler: hello.handler
    environment:
      STAGE: ${opt:stage}
hello.js
// 前略

const query = {
  FunctionName: 'sample-' + process.env.STAGE + '-example',
  Payload: JSON.stringify({
    'username': username
  }),
  InvocationType: 'Event',
  LogType: 'None'
}
const client = new aws.Lambda({apiVersion: '2015-03-31'})
client.invoke(query).promise()

他にいい方法あればコメントか編集リクエスト貰えるとめっちゃ助かります。

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