使用 AWS CloudFormation 对现有 Lambda 函数启用 Lambda Insights
按照以下步骤操作,使用 AWS CloudFormation 对现有 Lambda 函数启用 Lambda Insights。
步骤 1:安装层
将 Lambda Insights 层添加到 Lambda Insights 层 ARN 中的 Layers
属性。下方示例所示为将该层用于 Lambda Insights 的初始版本。有关 Lambda Insights 扩展程序层的最新版本,请参阅 Lambda Insights 扩展程序的可用版本。
Resources: MyFunction: Type: AWS::Lambda::Function Properties: Layers: - !Sub "arn:aws:lambda:${AWS::Region}:580247275435:layer:LambdaInsightsExtension:14"
步骤 2:添加托管式策略
将 CloudWatchLambdaInsightsExecutionRolePolicy IAM 策略添加到您的函数执行角色。
Resources: MyFunctionExecutionRole: Type: 'AWS::IAM::Role' Properties: ManagedPolicyArns: - 'arn:aws:iam::aws:policy/CloudWatchLambdaInsightsExecutionRolePolicy'
步骤 3:(可选)添加 VPC 终端节点
仅在函数运行于不具有 Internet 访问权限的私有子网中以及您尚未配置 CloudWatch Logs Virtual Private Cloud (VPC) 端点的情况下,才需要执行此步骤。有关更多信息,请参阅将 CloudWatch Logs 与接口 VPC 终端节点结合使用。
Resources: CloudWatchLogsVpcPrivateEndpoint: Type: AWS::EC2::VPCEndpoint Properties: PrivateDnsEnabled: 'true' VpcEndpointType: Interface VpcId: !Ref: VPC ServiceName: !Sub com.amazonaws.${AWS::Region}.logs SecurityGroupIds: - !Ref InterfaceVpcEndpointSecurityGroup SubnetIds: - !Ref PublicSubnet01 - !Ref PublicSubnet02 - !Ref PublicSubnet03