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のステージ名はevent
やcontext
内にも入っていない様子。
ということで今のところ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()
他にいい方法あればコメントか編集リクエスト貰えるとめっちゃ助かります。