如何在 `serverless.yml` 的 `Resources` 中使用 `If` 条件?
How can I use `If` condition in `Resources` of `serverless.yml`?
我试图在 resources
部分使用 !If
条件但失败了。我想控制是否在我的 lambda 上设置 provisionedConcurrency
。 lambda 在 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
条件的正确使用方法是什么?
对于资源,您只需添加 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
应该处于同一级别。
您不能使用 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}'
我试图在 resources
部分使用 !If
条件但失败了。我想控制是否在我的 lambda 上设置 provisionedConcurrency
。 lambda 在 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
条件的正确使用方法是什么?
对于资源,您只需添加 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
应该处于同一级别。
您不能使用 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}'