Test Event
{
"thing_name": "myThingNew",
"access_key": "AKIAXLU4V3LTW4PS6CCH",
"secret_key": "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",
"policy_name": "ESP32_Test_Policy",
"certificate_arn": "arn:aws:iot:us-east-1:506060987111:cert/f1020e6ed117a7c2834f25fc3025f54d6ff5b7662d339c6d4367937a2a0d135d"
}
lambda_function.py
import boto3
def lambda_handler(event, context):
thing_name = event['thing_name']
access_key = event['access_key']
secret_key = event['secret_key']
policy_name= event['policy_name']
certificate_arn = event['certificate_arn']
session = boto3.Session(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key
)
iot_client = session.client('iot')
response = iot_client.create_thing(
thingName=thing_name
)
thing_arn = response['thingArn']
#certificate = iot_client.create_keys_and_certificate(
# setAsActive=True
#)
# Get the certificate ARN and ID
# certificate_arn = certificate['certificateArn']
# certificate_id = certificate['certificateId']
# Attach the policy to the certificate
iot_client.attach_policy(
policyName=policy_name,
target=certificate_arn
)
# Attach the certificate to the thing
iot_client.attach_thing_principal(
thingName=thing_name,
principal=certificate_arn
)
return {
'statusCode': 200,
'body': f"Successfully created IoT thing: {thing_name}"
}
Â
Comments