I am trying to create bucket policy to grants a CloudFront origin access identity (OAI) permission to get (read) all objects in your Amazon S3 bucket. But I am facing this error as "An error occurred:
Error: error creating IAM policy example_policy: MalformedPolicyDocument: Policy document should not specify a principal.
│ status code: 400, request id: 95044f55-e4bf-403e-8233-95964ffe09d1
│
│ with module.iam.aws_iam_policy.s3Frontend,
│ on ..\modules\iam\resources.tf line 65, in resource "aws_iam_policy" "s3Frontend":
│ 65: resource "aws_iam_policy" "s3Frontend" {
data "aws_iam_policy_document" "s3Frontend" {
version = "2012-10-17"
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:ListBucket"
]
principals {
type = "AWS"
identifiers = ["arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity $MYID"] #
}
resources = [ "arn:aws:s3:::WebSitefrontend/*" ]
}
}
resource "aws_iam_policy" "s3Frontend" {
name = "example_policy"
path = "/"
policy = data.aws_iam_policy_document.s3Frontend.json
}
output "s3FrontendId" {
description = "IDs of frontend deploy artifect on s3"
value = aws_iam_policy.s3-Frontend.id
}
Thanks for helping.
Error MalformedPolicyDocument: Policy document should not specify a principal
refers to IAM identity-based policy created in resource "aws_iam_policy"
, which cannot contain principal.
You are trying to create S3 Bucket resource-based policy, which can be used in the aws_s3_bucket_policy Terraform resource. Example usage:
resource "aws_s3_bucket_policy" "s3Frontend" {
bucket = aws_s3_bucket.WebSitefrontend.id
policy = data.aws_iam_policy_document.s3Frontend.json
}