aws - 调用 PutMetricData 操作时发生错误 (AccessDenied)

时间:2021-07-19 19:56:40

标签: amazon-web-services terraform

我在尝试运行 Lambda 函数时收到以下错误。如何解决这个问题。

{
  "errorMessage": "An error occurred (AccessDenied) when calling the PutMetricData 
operation: User: arn:aws:sts::12345678:assumed-role/someRole/role is not authorized 
to perform: cloudwatch:PutMetricData",

1 个答案:

答案 0 :(得分:2)

您需要允许 someRole(由 lambda 函数假定)将指标写入 cloudwatch。该政策应如下所示:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["cloudwatch:PutMetricData"], # add more actions if needed
      "Resource": "*"
    }
  ]
}

编辑 关于关于你应该使用什么作为资源而不是“”*

的OP评论

PutMetricData 没有资源级权限,所以必须是“*”。如果您使用命名空间,则可以使用条件键来限制对命名空间的访问。

例如:

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Resource": "*",
        "Action": "cloudwatch:PutMetricData",
        "Condition": {
            "StringEquals": {
                "cloudwatch:namespace": "MyNamespace"
            }
        }
    }
}