I have a module that contains the following code.
resource "aws_s3_bucket" "main" {
bucket = var.bucket_name
acl = "private"
tags = var.tags
versioning {
enabled = var.versioning_enabled
}
}
resource "aws_s3_bucket_policy" "mod" {
depends_on = [aws_s3_bucket.main]
count = length(var.bucket_policy) > 0 ? 1 : 0
bucket = aws_s3_bucket.main.id
policy = var.bucket_policy
}
variable "bucket_policy" {
default = ""
}
I call the module using the code below, which i've redacted for security.
module "xxxx-api-s3-firehose" {
source = "git::ssh://[email protected]/xxxx/infra-terraform-modules-s3?ref=v1.0.0"
bucket_name = "reporting-xxxxxx-api-${var.env_suffix}-${var.region}"
bucket_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "xxx Bucket Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "${aws_iam_role.xxxxx-api-firehose-role.arn}"
},
"Action": [
"s3:Get*",
"s3:List*",
"s3:Put*"
],
"Resource": [
"arn:aws:s3:::${module.xxxx-api-s3-firehose.bucket_id}",
"arn:aws:s3:::${module.xxxxx-api-s3-firehose.bucket_id}/*"
]
},
{
"Sid": "xx Bucket Permissions",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::${var.account_id}:role/${var.xxxxx}"
},
"Action": [
"s3:Get*",
"s3:List*",
"s3:Put*"
],
"Resource": [
"arn:aws:s3:::${module.xxx-api-s3-firehose.bucket_id}",
"arn:aws:s3:::${module.xxx-api-s3-firehose.bucket_id}/*"
]
}
]
}
EOF
I receive the below error after running terraform apply.
Error: Invalid count argument
│
│ on xxxxx-backend-dev.xxxx-api-s3-firehose/main.tf line 39, in resource "aws_s3_bucket_policy" "mod":
│ 39: count = length(var.bucket_policy) > 0 ? 1 : 0
│
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the count depends on.
╵
I receive the error in multiple versions of terraform including the latest 1.0.6.
I'm not sure what the issue is. Can someone advise?
As the error msg suggest, you can't do this. Your
bucket_policylength is not constant. You should be able to overcome this using lists:and