Apply rules to s3 buckets

I have code to create S3 buckets and then apply the rule, for some reason am getting the error below for the resource ‘aws_s3_bucket_ownership_controls’

resource "aws_s3_bucket" "sftp-bucket" {
 # bucket = var.bucket_name
  for_each = var.bucket_sftp_users
  bucket = each.value["s3_bucket_name"]

resource "aws_s3_bucket_ownership_controls" "sftp-bucket-acl-ownership" {
  for_each = aws_s3_bucket.sftp-bucket
  bucket = each.key
  rule = {
    object_ownership = "BucketOwnerEnforced"

Getting an error as

│ Error: Insufficient rule blocks

│ on line 17, in resource “aws_s3_bucket_ownership_controls” “sftp-bucket-acl-ownership”:
│ 17: resource “aws_s3_bucket_ownership_controls” “sftp-bucket-acl-ownership” {

│ At least 1 “rule” blocks are required.

│ Error: Unsupported argument

│ on line 20, in resource “aws_s3_bucket_ownership_controls” “sftp-bucket-acl-ownership”:
│ 20: rule = {

│ An argument named “rule” is not expected here. Did you mean to define a block of type “rule”?

HCL (HashiCorp Configuration Language) - the language on which Terraform configurations are based - makes a distinction between an attribute and a block.

Here you have written an attribute:

but this resource expects rule to be a block instead.

The difference is that you need to remove the = after rule.

1 Like

Thank you, That was it