本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Cross-account 使用 Lambda 共享日志数据
在此示例中,日志数据接收者(帐户) 999999999999) 将 Lambda 函数设置为跨账户订阅目标和日志数据发送器(账户) 111111111111) 创建向其传送日志事件的订阅过滤器。
注意
日志组和目标必须在同一个日志组中 Amazon 地区。
跨账户向 Lambda 函数传送日志事件
-
在收款人账户中,创建一个 Amazon Identity and Access Management (IAM) 角色名
CWLtoLambdaRole那个 CloudWatch 日志假定调用该函数。使用以下信任策略,这允许 CloudWatch 记录担任该角色的服务主体:{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "logs.us-east-1.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "ArnLike": { "aws:SourceArn": ["arn:aws:logs:us-east-1:999999999999:*", "arn:aws:logs:us-east-1:111111111111:*"] } } } ] }将以下权限策略附加到该角色,允许该角色调用该函数:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "lambda:InvokeFunction", "Resource": "arn:aws:lambda:us-east-1:999999999999:function:RecipientFunction" } ] } -
在收款人账户中,授予 CloudWatch 记录调用该函数的权限:
aws lambda add-permission \ --function-name "RecipientFunction" \ --statement-id "AllowCWLogsInvoke" \ --principal "logs.us-east-1.amazonaws.com" \ --action "lambda:InvokeFunction" -
在收款人账户中,使用您在第一步中创建的角色创建指向该函数的目标:
aws logs put-destination \ --destination-name "testLambdaDestination" \ --target-arn "arn:aws:lambda:us-east-1:999999999999:function:RecipientFunction" \ --role-arn "arn:aws:iam::999999999999:role/CWLtoLambdaRole" -
在收件人账户中,附加目标访问策略,允许发件人账户在目标上创建订阅筛选器:
aws logs put-destination-policy \ --destination-name "testLambdaDestination" \ --access-policy '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"AWS":"111111111111"},"Action":"logs:PutSubscriptionFilter","Resource":"arn:aws:logs:us-east-1:999999999999:destination:testLambdaDestination"}]}' -
在发件人账户中 (111111111111),创建以目标为目标的订阅过滤器:
aws logs put-subscription-filter \ --log-group-name "myLogGroup" \ --filter-name "testFilter" \ --filter-pattern "" \ --destination-arn "arn:aws:logs:us-east-1:999999999999:destination:testLambdaDestination" -
要验证交付,请将测试日志事件放入源日志组。有关生成测试事件的更多信息,请参阅 PutLogEvents。在您发送事件后,确认已调用接收方 Lambda 函数。该函数的日志显示传送的日志事件。