簡體   English   中英

我可以在 IoT 規則 SQL 中動態設置條件嗎?

[英]Can I dynamically set the condition in an IoT Rule SQL?

在 yaml 文件的這一部分,我需要更新 sql 語句中的條件。

例如,用戶可以將條件重置為等於 20 而不是 30。

NotifyTemperatureAlarm:
Type: 'AWS::Serverless::Function'
DependsOn: AlertSNSTopic
Properties:
  CodeUri: src/notify_temperature_alarm/
  Handler: app.lambda_handler
  Runtime: python3.9
  Architectures:
    - x86_64
  Events:
    PutImageEvent:
      Type: IoTRule
      Properties:
        Sql: Select * FROM 'device/temperature/alarms' where value > 30
  Policies:
    Version: "2012-10-17"
    Statement:
      - Effect: Allow
        Action: "sns:*"
        Resource: "*"
  Environment:
    Variables:
      alert_sns: !Ref AlertSNSTopic

在參數部分設置您的條件,例如

Parameters:
TemperatureThreshold:
  Type: Number
  Default: '20'

然后更新 NotifyTemperatureAlarm 例如

NotifyTemperatureAlarm:
Type: 'AWS::Serverless::Function'
DependsOn: AlertSNSTopic
Properties:
  CodeUri: src/notify_temperature_alarm/
  Handler: app.lambda_handler
  Runtime: python3.9
  Architectures:
    - x86_64
  Events:
    PutImageEvent:
      Type: IoTRule
      Properties:
        Sql: !Sub "SELECT * FROM 'device/temperature/alarms' where value > ${TemperatureThreshold}"
  Policies:
    Version: "2012-10-17"
    Statement:
      - Effect: Allow
        Action: "sns:*"
        Resource: "*"
  Environment:
    Variables:
      alert_sns: !Ref AlertSNSTopic

要使 TemperatureThreshold 可調,您可以從 cloudformation 更新它,例如

client = boto3.client('cloudformation')  

response = client.update_stack(
    StackName = "your_stack_name",
    UsePreviousTemplate = True,
    Capabilities = ['CAPABILITY_IAM'],
    Parameters=[
        {
            'ParameterKey': 'TemperatureThreshold',
            'ParameterValue': str(new_threshold),
        }
    ]
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM