【问题标题】:Can we Integrate AWS lambda with AWS IOT and perform operations in the IOT using this lambda function?我们可以将 AWS lambda 与 AWS IOT 集成并使用此 lambda 函数在 IOT 中执行操作吗?
【发布时间】:2021-02-05 17:06:51
【问题描述】:

剧情是这样的

我有一个微服务,它调用 LAMBDA 函数,其作用是从 AWS IOT 中删除内容。

有没有一种方法可以使用 lambda 函数在 AWS IOT 中执行操作?

任何与此相关的文章、博客都会有很大帮助,因为我无法在网络上找到任何集成文档。

【问题讨论】:

    标签: amazon-web-services microservices aws-iot-core aws-iot-events


    【解决方案1】:

    我找到了一种通过 API 使用 lambda 函数在 AWS IOT 中执行多项操作的方法。

    https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/iot.html#IoT.Client.delete_thing_group

    上面的链接有关于 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"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 2017-06-10
      • 1970-01-01
      • 2017-02-22
      相关资源
      最近更新 更多