如何知道Lambda函数本身的事件来源 [英] How to know event souce of lambda function in itself
问题描述
我想知道该函数中lambda函数的事件源.
I want to know the event source of lambda function in the function.
我想做的是使用某些AWS服务中的一个lambda函数(CloudWatch,S3,Step函数等),并根据服务更改其行为.
What I want to do is to use one lambda function from some AWS service(CloudWatch, S3, Step functions, etc...) and to change its behavior depending on the service.
上下文对象(函数的参数之一)具有有关lambda函数的信息,但不具有有关事件源的信息.
A context object (one of arguments of the function) has information about the lambda function but not about event source.
有办法知道吗?
推荐答案
如果您已将Kinesis或DynamoDB流标识为使用API的Lambda函数的事件源
If you have identified a Kinesis or DynamoDB stream as an event source for a Lambda function with the API
aws lambda create-event-source-mapping
,那么您可以通过
aws lambda list-event-source-mappings
如果不这样做,则可以使用以下函数进行最佳猜测:
If you don't, then you can make a best guess with a function like the following:
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';
}
我说这是最佳猜测,因为诸如API网关请求之类的事件源可能正在发送任何内容.如果您确定不会遇到这种情况,则上面的功能可以解决问题.
I say that it's a best guess because an event source like an API gateway request can potentially be sending anything. If you're sure that you won't have such a case, then the function above can do the trick.
这篇关于如何知道Lambda函数本身的事件来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!