从部署到特定阶段排除Lambda函数

时间:2017-12-08 16:03:17

标签: aws-lambda serverless-framework

我试图将Lambda函数排除在通过无服务器部署到AWS中的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?

4 个答案:

答案 0 :(得分:7)

您可以将这些定义放在不同的属性上,并使用变量来选择要使用的定义。

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})。

答案 1 :(得分:0)

我正在使用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

答案 2 :(得分:0)

如果您使用的是无服务器框架,则可以使用无服务器插件

<块引用>

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

答案 3 :(得分:0)

例如创建一个文件

<块引用>

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