amazon-web-services - 如何知道lambda函数本身的事件源

标签 amazon-web-services aws-lambda

想知道函数中lambda函数的事件源

我想做的是使用某个 AWS 服务(CloudWatch、S3、Step 函数等)中的一个 lambda 函数,并根据服务更改其行为。

上下文对象(函数的参数之一)包含有关 lambda 函数的信息,但不包含有关事件源的信息。

有办法知道吗?

最佳答案

如果您已使用 API 将 Kinesis 或 DynamoDB 流识别为 Lambda 函数的事件源

aws lambda create-event-source-mapping

,然后你可以用

得到它们
aws lambda list-event-source-mappings

如果您不这样做,那么您可以使用如下函数做出最佳猜测:

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 网关请求这样的事件源可能会发送任何内容。如果您确定不会遇到这种情况,那么上面的函数就可以解决问题。

关于amazon-web-services - 如何知道lambda函数本身的事件源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41814750/

相关文章:

windows - .ebignore 不跳过 node_modules (Amazon)

php - 与 Elasticsearch 进行 AJAX 通信的流量和访问控制解决方案?

amazon-web-services - AWS 安全组 - 将 EC2 连接到 RDS

windows - 如何以编程方式/远程执行 EC2 Windows 实例中的程序

amazon-web-services - 是否可以在没有 VPC 的情况下启动 RDS 实例?

amazon-web-services - 在 Lambda 中创建 AmazonS3Client 时出现 OutOfMemoryError

amazon-web-services - 用于 lambda 的亚马逊负载均衡器

node.js - DynamoDB 应用程序架构

aws-lambda - 无服务器 Lambda 和 API 网关的 CORS 问题

node.js - 如何使用 Lambda 启动和停止 EC2 实例