I have a Python Lambda function that can respond to both an IoT Button and Alexa skill.
Is there a way to read the event
or context
handed to the handler function to identify which service triggered the function (Alexa or IoT)?
I have a Python Lambda function that can respond to both an IoT Button and Alexa skill.
Is there a way to read the event
or context
handed to the handler function to identify which service triggered the function (Alexa or IoT)?
There is no way to reliably accomplish this. The closest you can get is to familiarize yourself with the contents of the various events generated by different services, and (hope to) identify a reliably unique key present in each of the series you are interested in that you can then check for in your code, e.g. with
if 'distinctKey' in event.keys():
# ...
However this is hardly a reliable approach, since it requires that you
Was hoping this has been streamline by AWS by now, but sadly it isn't. There is no one single parameter you can check for to determine the type of event across all AWS services.
However, found this nice articulation online here
function getLambdaEventSource(event) {
if (event.Records && event.Records[0].cf) return 'isCloudfront';
if (event.configRuleId && event.configRuleName && event.configRuleArn) return 'isAwsConfig';
if (event.Records && (event.Records[0].eventSource === 'aws:codecommit')) return 'isCodeCommit';
if (event.authorizationToken === "incoming-client-token") return 'isApiGatewayAuthorizer';
if (event.StackId && event.RequestType && event.ResourceType) return 'isCloudFormation';
if (event.Records && (event.Records[0].eventSource === 'aws:ses')) return 'isSes';
if (event.pathParameters && event.pathParameters.proxy) return 'isApiGatewayAwsProxy';
if (event.source === 'aws.events') return 'isScheduledEvent';
if (event.awslogs && event.awslogs.data) return 'isCloudWatchLogs';
if (event.Records && (event.Records[0].EventSource === 'aws:sns')) return 'isSns';
if (event.Records && (event.Records[0].eventSource === 'aws:dynamodb')) return 'isDynamoDb';
if (event.records && event.records[0].approximateArrivalTimestamp) return 'isKinesisFirehose';
if (event.records && event.deliveryStreamArn && event.deliveryStreamArn.startsWith('arn:aws:kinesis:')) return 'isKinesisFirehose';
if (event.eventType === 'SyncTrigger' && event.identityId && event.identityPoolId) return 'isCognitoSyncTrigger';
if (event.Records && event.Records[0].eventSource === 'aws:kinesis') return 'isKinesis';
if (event.Records && event.Records[0].eventSource === 'aws:s3') return 'isS3';
if (event.operation && event.message) return 'isMobileBackend';
}
There is no way to reliably accomplish this. The closest you can get is to familiarize yourself with the contents of the various events generated by different services, and (hope to) identify a reliably unique key present in each of the series you are interested in that you can then check for in your code, e.g. with
if 'distinctKey' in event.keys():
# ...
However this is hardly a reliable approach, since it requires that you
For Python Dev: You can check this out https://gist.github.com/Necromancerx/abed07138690d37d170a6cf15b40d749.
def get_lambda_event_source(self, event: dict):
if 'pathParameters' in event and 'proxy' in event['pathParameters']:
return 'api_gateway_aws_proxy'
elif 'Records' in event and len(event['Records']) > 0 and 'eventSource' in event['Records'][0] and event['Records'][0]['eventSource'] == 'aws:s3':
return 's3'
elif 'Records' in event and len(event['Records']) > 0 and 'EventSource' in event['Records'][0] and event['Records'][0]['EventSource'] == 'aws:sns':
return 'sns'
elif 'Records' in event and len(event['Records']) > 0 and 'eventSource' in event['Records'][0] and event['Records'][0]['eventSource'] == 'aws:dynamodb':
return 'dynamo_db'
elif 'Records' in event and len(event['Records']) > 0 and 'cf' in event['Records'][0]:
return 'cloudfront'
elif 'source' in event and event['source'] == 'aws.events':
return 'scheduled_event'
elif 'awslogs' in event and 'data' in event['awslogs']:
return 'cloud_watch_logs'
elif 'authorizationToken' in event and event['authorizationToken'] == "incoming-client-token":
return 'api_gateway_authorizer'
elif 'configRuleId' in event and 'configRuleName' in event and 'configRuleArn' in event:
return 'aws_config'
elif 'StackId' in event and 'RequestType' in event and 'ResourceType' in event:
return 'cloud_formation'
elif 'Records' in event and len(event['Records']) > 0 and 'eventSource' in event['Records'][0] and event['Records'][0]['eventSource'] == 'aws:codecommit':
return 'code_commit'
elif 'Records' in event and len(event['Records']) > 0 and 'eventSource' in event['Records'][0] and event['Records'][0]['eventSource'] == 'aws:ses':
return 'ses'
elif 'Records' in event and len(event['Records']) > 0 and 'eventSource' in event['Records'][0] and event['Records'][0]['eventSource'] == 'aws:kinesis':
return 'kinesis'
elif 'records' in event and len(event['Records']) > 0 and 'approximateArrivalTimestamp' in event['records'][0]:
return 'kinesis_firehose'
elif 'records' in event and len(event['Records']) > 0 and 'deliveryStreamArn' in event and event['deliveryStreamArn'] is str and event['deliveryStreamArn'].startswith('arn:aws:kinesis:'):
return 'kinesis_firehose'
elif 'eventType' in event and event['eventType'] == 'SyncTrigger' and 'identityId' in event and 'identityPoolId' in event:
return 'cognito_sync_trigger'
elif 'operation' in event and 'message' in event:
return 'is_mobile_backend'
Based on this javascript gist https://gist.github.com/jeshan/52cb021fd20d871c56ad5ce6d2654d7b
You can read and Log the event in the Cloudwatch like below
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def my_logging_handler(event, context):
logger.info('got event{}'.format(event))
This will log the event data in cloud watch you see and determine which event which triggered the lambda
event
or context
, which I have access to, I can use to determine what derive (IoT or ASK) invoked the function. Or, if (and only if) that's not possible, how I examine some other source (like the logs, perhaps) to do so. –
Zugzwang © 2022 - 2024 — McMap. All rights reserved.
eventSource
field) – Brewhouse