如何使用aws SDK为AWS Lambda函数创建CloudWatch日志触发器?

时间:2019-03-21 05:07:42

标签: amazon-web-services aws-lambda aws-sdk amazon-cloudwatch amazon-cloudwatchlogs

我根据https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudWatchLogs.html#putSubscriptionFilter-property

nodejs

const AWS = require('aws-sdk');
AWS.config = new AWS.Config
    ({
        accessKeyId: "AKIA******",
        secretAccessKey: "6RJf******vy",
    });
const cloudwatchlogs = new AWS.CloudWatchLogs({ region: 'a******1' });

var params = {
    destinationArn: 'arn:aws:lambda:******:function:******', 
    filterName: 'LambdaStream_******', 
    filterPattern: '?Error ?Waring ?error ?"node(1)" ?info ?INFO', 
    logGroupName: '/aws/lambda/******', 
    distribution: 'ByLogStream',
};
cloudwatchlogs.putSubscriptionFilter(params, function (err, data) {
    if (err) console.log( err, err.stack);
    else console.log(data);
});

我将收到以下错误:

 { InvalidParameterException: Could not execute the lambda function. Make sure you have given CloudWatch Logs permission to ex ecute your function.
    at Request.extractError 

......

(/mnt/******/node_modules/aws-sdk/lib/sequential_executor.js:116:18) message: 'Could not execute the lambda function. Make sure you have given CloudWatch Logs permission to execute your function.',   code: 'InvalidParameterException',   time: 2019-03-21T03:05:47.966Z,   requestId: '39c9******3',   statusCode: 400,  retryable: false,   retryDelay: ******86 } InvalidParameterException: Could not execute the lambda function. Make sure you have given CloudWatch Logs permission to execute your function.

补充: enter image description here

我担任了这些执行职务:

  AWSLambdaFullAccess
  CloudWatchFullAccess
  CloudWatchLogsFullAccess
  AmazonVPCFullAccess
  AWSLambdaVPCAccessExecutionRole
  AWSLambdaRole




{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutSubscriptionFilter",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
}

3 个答案:

答案 0 :(得分:0)

var params = {
Action: 'lambda:InvokeFunction', /* required */
FunctionName: 'arn:aws:lambda:******:******:function:******', /* required */
Principal: 'logs.*region*.amazonaws.com', /* required */
StatementId: '******', /* required */
// SourceAccount: '******',
// SourceArn: 'arn:aws:logs:::******:******'
};

lambda.addPermission(params, function (err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data);           // successful response
});

答案 1 :(得分:0)

This is all you need in the role policy for having CloudWatch Full Access.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "logs:*",
            "Resource": "*"
        }
    ]
}

答案 2 :(得分:0)

如文档中所指定,您需要向CloudWatch Logs授予执行Lambda函数的权限。

要执行此任务,可以使用以下CLI命令:

aws lambda add-permission-功能名称“ lamda1”-statement-id “ lamda1”-主要“ logs.us-west-2.amazonaws.com”-操作 “ lambda:InvokeFunction” --source-arn “ arn:aws:logs:us-west-2:xxxxxx047983:log-group:testgroup:*” --source-account“ xxxxxx047983”

请确保将函数名称替换为函数名称,并将xxxxxx替换为您的帐户详细信息。有关更多信息,请参见Amazon CloudWatch Logs Guide