def lambda_handler()

in lambda-issuer-acmpca/main.py [0:0]


def lambda_handler(event, context):
    # Whoami and Whatami is important for construction region sensitive ARNs
    region = context.invoked_function_arn.split(":")[3]
    account = context.invoked_function_arn.split(":")[4]
    
    csr = base64.b64decode(event['headers']['device-csr'])
    req = load_certificate_request( FILETYPE_PEM, csr )
    device_id = req.get_subject().CN
    response = provision_certificate( csr )

    certificate = response['Certificate']

    # Send the certificate to AWS IoT. We assume the issuing CA has already
    # been registered.

    certificate_arn = deploy_certificate( certificate )
    if ( certificate_arn == None ): return None

    # Create the Thing object and attach to the deployed certificate

    response = deploy_thing( device_id, certificate_arn )
    if ( response == False ): return None

    # Create the Policy if necessary, and attach the created Policy (or
    # existing Policy) to the Thing.

    response = deploy_policy( certificate_arn, region, account )
    if ( response == False ): return None

    # Return the certificate to API Gateway.
    iot = boto3.client('iot')
    endpoint = iot.describe_endpoint(endpointType = 'iot:Data-ATS')
    payload = { 'certificate': certificate,
                'endpoint': endpoint['endpointAddress'] }
    return json.dumps(payload)