View a markdown version of this page

使用条件键限制对 CloudWatch 命名空间的访问 - Amazon CloudWatch

使用条件键限制对 CloudWatch 命名空间的访问

使用 IAM 条件键限制用户仅在指定的 CloudWatch 命名空间中发布指标。本节提供的示例描述了如何允许和排除用户在命名空间中发布指标。

仅允许在一个命名空间中发布

以下策略将用户限制为仅在名为 MyCustomNamespace 的命名空间中发布指标。

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

排除从命名空间发布

以下策略允许用户在除 CustomNamespace2 之外的任何命名空间中发布指标。

JSON
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Resource": "*", "Action": "cloudwatch:PutMetricData" }, { "Effect": "Deny", "Resource": "*", "Action": "cloudwatch:PutMetricData", "Condition": { "StringEquals": { "cloudwatch:namespace": "CustomNamespace2" } } } ] }

控制 OTLP 摄取

以下策略允许用户使用 OTLP API 发布指标:

JSON
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Resource": "*", "Action": "cloudwatch:PutMetricData" } ] }

要禁用双重摄取,即仅使用 PutMetricData 并拒绝任何 OTLP 摄取,可使用以下策略。该策略限制用户只能在 MyCustomNamespace 命名空间中使用 PutMetricData 发布指标,同时由于 StringEquals 条件,隐式拒绝任何 OTLP 摄取:

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

要启用双重摄取,即同时允许 PutMetricData 和 OTLP 摄取,可使用以下策略。该策略限制用户只能在名为 MyCustomNamespace 的命名空间中使用 PutMetricData 发布指标,同时由于 StringEqualsIfExists 条件,允许 OTLP 摄取:

JSON
{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "cloudwatch:PutMetricData", "Resource": "*", "Condition": { "StringEqualsIfExists": { "cloudwatch:namespace": "MyCustomNamespace" } } } ] }