Obi*_*van 6 amazon-s3 amazon-web-services terraform
我制作了一个由 CloudFront 提供服务的静态网站托管 S3 存储桶。我想仅通过 Origin Access Identity 直接限制来自 CloudFront 的存储桶访问。
我尝试更新 S3 存储桶策略,但显示错误:
Error putting S3 policy: MalformedPolicy: Invalid principal in policy status code: 400, request id
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用以下政策:
resource "aws_s3_bucket_policy" "default" {
bucket = "${aws_s3_bucket.default.id}"
policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${aws_cloudfront_origin_access_identity.origin_access_identity.id}"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::$/*"
}
]
}
EOF
}
Run Code Online (Sandbox Code Playgroud)
正如 [aws_cloudfront_origin_access_identity 文档] 1 中提到的,最好的方法是使用aws_iam_policy_document数据源生成 IAM 策略文档,然后直接附加它。
一个例子看起来像这样:
data "aws_iam_policy_document" "s3_policy" {
statement {
actions = ["s3:GetObject"]
resources = ["${module.names.s3_endpoint_arn_base}/*"]
principals {
type = "AWS"
identifiers = ["${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}"]
}
}
statement {
actions = ["s3:ListBucket"]
resources = ["${module.names.s3_endpoint_arn_base}"]
principals {
type = "AWS"
identifiers = ["${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}"]
}
}
}
resource "aws_s3_bucket" "bucket" {
# ...
policy = "${data.aws_iam_policy_document.s3_policy.json}"
}
Run Code Online (Sandbox Code Playgroud)
如果您真的想按照问题手工制作 IAM 政策,那么您只需要这样的东西:
resource "aws_s3_bucket_policy" "default" {
bucket = "${aws_s3_bucket.default.id}"
policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "2",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn}"
},
"Action": "s3:*",
"Resource": "${aws_s3_bucket.default.arn}""
}
]
}
EOF
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4947 次 |
| 最近记录: |