Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I determine which AWS service triggered my Lambda function?

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)?

like image 882
orome Avatar asked Mar 02 '17 17:03

orome


2 Answers

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';

}
like image 52
Kdroid Avatar answered Sep 19 '22 13:09

Kdroid


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

  1. examine every possible event structure generated by every potential service and
  2. successfully and confidently identify for each service of interest a key or set of keys that is always reliably present in the service's events and unique to them.
like image 45
orome Avatar answered Sep 20 '22 13:09

orome