2

我想配置会在特定日期/时间与以下触发lambda函数CloudWatch的规则:AWS terraform CloudWatch的规则为lambda触发

resource "aws_lambda_function" "cleanup_daily" { 
    filename   = "name" 
    function_name  = "name" 
    role    = "arn<removed>" 
    handler   = "snapshotcleanup.lambda_handler" 
    source_code_hash = "${base64sha256(file("file_name"))}" 
    runtime   = "python2.7" 
    timeout   = "20" 
    description  = "desc" 
} 

resource "aws_cloudwatch_event_rule" "daily_rule" { 
    name    = "name" 
    description   = "desc" 
    schedule_expression = "cron(....)" 
} 

resource "aws_cloudwatch_event_target" "daily_target" { 
    rule = "${aws_cloudwatch_event_rule.daily_rule.name}" 
    arn = "${aws_lambda_function.cleanup_daily.arn}" 
} 

然而lambda函数不运行。如果我查看lambda并检查触发器选项卡,那里没有任何内容。如果我查看cloudwatch规则并查看目标下的lambda函数,则会显示出来,如果我点击它,则会重定向到函数本身。任何想法可能在这里错了吗?

对于我点击编辑 - >保存 - >配置详细信息 - >更新而不更改任何内容的一个cloudwatch规则,现在显示在lambda的触发器选项卡下,但仍需要让其他人无法工作W/O这一步,

回答

6

无论何时不同的AWS服务交互,都需要使用AWS IAM授予他们必要的访问权限。

在这种情况下,Cloudwatch Events必须有权执行相关的Lambda函数。

the AWS tutorial的第2步描述了如何使用AWS CLI执行此操作。所述Terraform当量aws lambda add-permission命令的是the aws_lambda_permission resource,其可以与结构例从问题如下使用:

data "aws_caller_identity" "current" { 
    # Retrieves information about the AWS account corresponding to the 
    # access key being used to run Terraform, which we need to populate 
    # the "source_account" on the permission resource. 
} 

resource "aws_lambda_permission" "allow_cloudwatch" { 
    statement_id = "AllowExecutionFromCloudWatch" 
    action   = "lambda:InvokeFunction" 
    function_name = "${aws_lambda_function.cleanup_daily.function_name}" 
    principal  = "events.amazonaws.com" 
    source_account = "${data.aws_caller_identity.current.account_id}" 
    source_arn  = "${aws_cloudwatch_event-rule.daily_rule.arn}" 
} 

AWS LAMBDA权限是在IAM的角色和策略的抽象。有关IAM角色和策略的一些常规背景信息,请参阅需要更多手动配置的my longer answer to another question

相关问题