动态更改 aws cloudformation 模板上的事件属性 [英] Dynamically change event properties on aws cloudformation templates

查看:23
本文介绍了动态更改 aws cloudformation 模板上的事件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 aws 构建无服务器应用程序,我们希望仅在生产环境中启用 lambda 加温器.

We are building a serverless app using aws and we want to enable lambda warmers only on the production environment.

我们的 cloudformation 参数:

Our cloudformation parameters:

Parameters:
  Environment:
    Description: Environment name
    Type: String

  EnableWarmer:
    Description: Flag to enable/disable warmup Events
    Default: DISABLED
    Type: String

我们的 lambdas yaml 文件如下所示:

Our lambdas yaml file looks like this:

MyLambda:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub '${Environment}-my-lambda'
      CodeUri: src
      Handler: lambda.handler
      Runtime: nodejs12.x
      MemorySize: 128
      Timeout: 100
      Description: Creates a new something
      Layers:
        - !Ref InternalDependencyLayer
        - !Ref ExternalDependencyLayer
      Role: !Ref LambdaRoleArn
      Events:
        Api:
          Type: Api
          Properties:
            Path: /url
            Method: POST
            RestApiId: !Ref ApiGateway
        WarmingSchedule:
          Type: Schedule
          Properties:
            Enabled: !Ref EnableWarmer
            Schedule: rate(5 minutes)
            Input: '{ "warmer":true,  "concurrency": 2 }'

然后我们使用这些参数部署开发环境:

And then we deploy the dev environment with these params:

- Key: Environment
  Value: dev

- Key: EnableWarmer
  Value: DISABLED

类似于我们使用这些参数部署的生产环境:

Similarly for the production environment we deploy with these params:

- Key: Environment
  Value: production

- Key: EnableWarmer
  Value: ENABLED

根据 aws 文档,参数不能是布尔类型,这是调度事件的启用属性的必需类型.

According to aws documentation parameters can't be of type boolean which is the required type of the enabled attribute of the schedule event.

幸运的是亚马逊声明:

Enabled 表示规则是否启用.

Enabled Indicates whether the rule is enabled.

要禁用规则,请将此属性设置为 False.

To disable the rule, set this property to False.

类型:布尔值

必填:否

AWS CloudFormation 兼容性:此属性类似于AWS::Events::Rule 资源的状态属性.如果这个属性是设置为 True 然后 AWS SAM 通过 ENABLED,否则通过 DISABLED

AWS CloudFormation compatibility: This property is similar to the State property of an AWS::Events::Rule resource. If this property is set to True then AWS SAM passes ENABLED, otherwise it passes DISABLED

https:///docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedule.html

但是,当我们部署到开发环境时,会启用预热器.

However when we deploy to the dev environment the warmers are enabled.

推荐答案

根据文档,Enabled 必须是一个布尔变量.

According to the documentation, Enabled must a Boolean variable.

您仍然可以使用 String 参数并将其转换为 CloudFormation 中的布尔值.

You can still have a String parameter and convert it to Boolean inside the CloudFormation.

WarmingSchedule:
  Type: Schedule
    Properties:
      Enabled: !Equals [!Ref EnableWarmer, ENABLED] 
      Schedule: rate(5 minutes)
      Input: '{ "warmer":true,  "concurrency": 2 }'

这样你仍然可以发送 ENABLED 或 DISABLED 作为参数,但 WarmingSchedule.Enabled 的输入将是一个布尔值.

This way you still can send ENABLED or DISABLED as a parameter but the input of WarmingSchedule.Enabled will be a Boolean.

这篇关于动态更改 aws cloudformation 模板上的事件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆