代码之家  ›  专栏  ›  技术社区  ›  Joey Yi Zhao

如何在serverless.yml的Resources中使用If条件?

  •  0
  • Joey Yi Zhao  · 技术社区  · 4 年前

    !If resources 但失败。我想控制是否设置 provisionedConcurrency function 部分。

    
    functions:
      getTransactionsHandler:
        ...
    
    resources:
      Conditions:
        CommonPCNotZero: !Not [!Equals [0, '${self:custom.commonPC}']]
      Resources:
        !If 
          - CommonPCNotZero
          - getTransactionsHandler:
            Type: AWS::Lambda::Alias
              Properties:
                FunctionName: !Ref GetTransactionsHandlerLambdaFunction
                FunctionVersion: !Join ['', [!Ref GetTransactionsHandlerLambdaFunction, ':$LATEST']]
                ProvisionedConcurrencyConfig:
                  ProvisionedConcurrentExecutions: '${self:custom.commonPC}'
          - !Ref AWS::NoValue
    

    我跑的时候出错了 sls deploy :

    Error: The CloudFormation template is invalid: Template format error: [/Resources/Fn::If] resource definition is malformed
    

    !if 条件?

    2 回复  |  直到 4 年前
        1
  •  0
  •   Jason Wadsworth    4 年前

    对于资源,只需添加 Condition 包括或排除。

    functions:
      getTransactionsHandler:
        ...
    
    resources:
      Conditions:
        CommonPCNotZero: !Not [!Equals [0, '${self:custom.commonPC}']]
      Resources:
        getTransactionsHandler:
          Type: AWS::Lambda::Alias
          Condition: CommonPCNotZero
          Properties:
            FunctionName: !Ref GetTransactionsHandlerLambdaFunction
            FunctionVersion: !Join ['', [!Ref GetTransactionsHandlerLambdaFunction, ':$LATEST']]
            ProvisionedConcurrencyConfig:
              ProvisionedConcurrentExecutions: '${self:custom.commonPC}'
    

    另外,注意你的压痕。 Type Properties

        2
  •  1
  •   Marcin    4 年前

    不能使用If使整个资源有条件。通常,应使用 Condition

    resources:
      Conditions:
        CommonPCNotZero: !Not [!Equals [0, '${self:custom.commonPC}']]
      Resources:
         
         MyLambdaAlias: 
            Type: AWS::Lambda::Alias
            Condition: CommonPCNotZero
            Properties:
                FunctionName: !Ref GetTransactionsHandlerLambdaFunction
                FunctionVersion: !Join ['', [!Ref GetTransactionsHandlerLambdaFunction, ':$LATEST']]
                ProvisionedConcurrencyConfig:
                  ProvisionedConcurrentExecutions: '${self:custom.commonPC}'