首页 文章

使用AWS Cloudwatch数据输入的Python lambda代码

提问于
浏览
0

所以,我有一个Cloudwatch规则,可以在触发时发送SNS主题 . 现在,我在Lambda中使用python,但是我的代码生成了一些"module errors" . 棘手的部分是我试图获取cloudwatch数据并将其作为变量传递 . 事件变量在这个语法中 event['detail']['resource']['instanceDetails']['instanceId'] 所以在我的python代码中,它看起来像这样......我想尽可能多地学习 . 正如您所看到的,我正在尝试使用此事件变量,然后利用SDK执行相应的ModifyInstanceAttribute来更改与实例关联的安全组(例如使用python):此代码将替换与实例关联的安全组如boto3文档中所述指定:http://boto3.readthedocs.io/en/latest/reference/services/ec2.html#EC2.Client.modify_instance_attribute

我的代码到目前为止......

import boto3
client = boto3.client('ec2')
response = 
client.modify_instance_attribute(InstanceId=event['detail']. ['resource']['instanceDetails']['instanceId'], 
    Groups=['sg-4e499332',]
)

我得到的错误是此模块初始化错误:未定义名称“event”

1 回答

  • 1

    你不会为Lambda编写类似的代码 . 你的处理程序需要是一个带有 eventcontext 参数的函数 .

    Lambda handler docs for Python .

相关问题