-
-
Save jeshan/52cb021fd20d871c56ad5ce6d2654d7b to your computer and use it in GitHub Desktop.
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'; | |
if (event.Records && (event.Records[0].eventSource === 'aws:sqs')) return 'isSqs'; | |
} |
Guys, this slipped under my radar. I don't know why github does not send notifications for gists.
silvav: if it's still relevant for you, then yes you can use it under public domain.
CowChris: thanks, I'll add it
For Python: You can check this out https://gist.github.com/Necromancerx/abed07138690d37d170a6cf15b40d749. This is just based on your gist. Thank you.
Would you kindly add a if statement for a lambda being triggered by application load balancer?
For API Gateway HTTP:
if (event.requestContext && event.requestContext.resourceId) return 'isApiGatewayHttp';
This is probably not the best idea as it relies on AWS implementation details which may change.
Interesting and useful. I have struggled with the best approach to knowing my invoker. Although this may not persist well over time it is better than putting a caller flag in the payload. The only other option I see is multiple similar functions, which leads to logic sprawl but avoids the conditional overhead.
For SQS add this
elif 'Records' in event and len(event['Records']) > 0 and 'eventSource' in event['Records'][0] and event['Records'][0]['eventSource'] == 'aws:sqs':
return 'sqs'
if (event.requestContext.elb.targetGroupArn) return 'is ALBEvent'
if (event.source === 'aws.scheduler') return 'isScheduler';
This is awesome! this should be an NPM module - are you planning to do this? (Or if not, are you ok with me adding it? I would attribute it to you and put a link to this page)
For SQS:
if (event.Records && (event.Records[0].eventSource === 'aws:sqs')) return 'isSqs';