我试图将lambda函数排除在AWS中通过Serverless部署到我的prod
阶段。
我无服务器yaml的摘要看起来像 -
functions:
some-prod-function:
handler: prodFunction.handler
events:
- http:
path: /prod-function
method: post
some-dev-function:
handler: devFunction.handler
events:
- http:
path: /dev-function
method: post
有没有办法将some-dev-function
排除到部署到prod?
您可以将这些定义放在其他属性上,并使用变量以选择要使用的定义。
environment-functions:
prod:
some-prod-function:
handler: prodFunction.handler
events:
- http:
path: /prod-function
method: post
dev:
some-dev-function:
handler: devFunction.handler
events:
- http:
path: /dev-function
method: post
functions: ${self:environment-functions.${opt:stage}}
您可能需要根据如何指定部署的阶段(${opt:stage}
或${env:stage}
)。
我正在使用SLS 1.32.0
我无法让functions: ${self:environment-functions.${opt:stage}}
工作。(不确定为什么)
它返回以下内容:
A valid service attribute to satisfy the declaration 'self:environment-functions.dev' could not be found.
但是,在> dashmug的中使用相同的逻辑, file
为我工作:
serverless.yml :
functions: ${file(serverless-${opt:stage}.yml)}
serverless-dev.yml :
some-dev-function:
handler: devFunction.handler
events:
- http:
path: /dev-function
method: post
serverless-prod.yml :
some-prod-function:
handler: prodFunction.handler
events:
- http:
path: /prod-function
method: post
如果您使用的无服务器框架可以使用无服务器插件
无服务器-plugin-ifelse
然后
plugins:
- serverless-plugin-ifelse
如果您想排除func1
functions:
func1:
name: Function 1
handler: lambda.func1
events:
- http:
path: "path1"
method: "post"
authorizer:
arn: arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_0acCDefgh
func2:
name: Function 2
handler: lambda.func2
events:
- http:
path: "path2"
method: "post"
func3:
name: Function 3
handler: lambda.func2
events:
- http:
path: "path3"
method: "post"
对于us-east-1。然后使用以下代码段
- If: '"${self:provider.region}" == "us-east-1"'
Exclude:
- functions.func1
创建一个文件
env-functions.yml
并添加如下
的内容prod:
some-prod-function:
handler: prodFunction.handler
events:
- http:
path: /prod-function
method: post
dev:
some-dev-function:
handler: devFunction.handler
events:
- http:
path: /dev-function
method: post
在serverless.yml集中之后
functions: ${file(env-functions.yml):${opt:stage, self:provider.stage}}