问题描述
剧情是这样的
我有一个调用 LAMBDA 函数的微服务,该函数的作用是从 AWS IOT 中删除内容。
是否可以使用 lambda 函数在 AWS IOT 中执行操作?
关于此的任何文章、博客都会有很大帮助,因为我无法在网络上找到任何集成文档。
解决方法
我找到了一种通过 API 使用 lambda 函数在 AWS IOT 中执行多项操作的方法。
上面的链接有关于 API 的描述,这在这种情况下会有所帮助。 从 IOT 中删除事物的示例 lambda 函数脚本是
import json
import boto3
def lambda_handler(event,context):
thing_name = event['thing_name']
delete_thing(thing_name=thing_name)
def delete_thing(thing_name):
c_iot = boto3.client('iot')
print(" DELETING {}".format(thing_name))
try:
r_principals = c_iot.list_thing_principals(thingName=thing_name)
except Exception as e:
print("ERROR listing thing principals: {}".format(e))
r_principals = {'principals': []}
print("r_principals: {}".format(r_principals))
for arn in r_principals['principals']:
cert_id = arn.split('/')[1]
print(" arn: {} cert_id: {}".format(arn,cert_id))
r_detach_thing = c_iot.detach_thing_principal(thingName=thing_name,principal=arn)
print("DETACH THING: {}".format(r_detach_thing))
r_upd_cert = c_iot.update_certificate(certificateId=cert_id,newStatus='INACTIVE')
print("INACTIVE: {}".format(r_upd_cert))
r_del_cert = c_iot.delete_certificate(certificateId=cert_id,forceDelete=True)
print(" DEL CERT: {}".format(r_del_cert))
r_del_thing = c_iot.delete_thing(thingName=thing_name)
print(" DELETE THING: {}\n".format(r_del_thing))
这个 lambda 函数的输入是
{
"thing_name": "MyIotThing"
}