Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TF 0.12 data.aws_iam_policy_document still contains unknown values during apply #22424

Closed
GreenyMcDuff opened this issue Aug 10, 2019 · 6 comments
Labels
bug config core v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@GreenyMcDuff
Copy link

Terraform Version

v0.12.6

Terraform Configuration Files

locals {
  bucket_name_prefix = "${var.service_name}-${var.account_id}"
}

module "app_logs_bucket" {
  source = "../modules-generic/s3-versioned-bucket/"
  
  create      = var.create
  name        = "${local.bucket_name_prefix}-app-logs"
  kms_key_arn = data.terraform_remote_state.this.outputs.kms_key_arn
}

resource "aws_s3_bucket_policy" "logs" {
  count  = var.create ? 1 : 0
  bucket = module.app_logs_bucket[0].name
  policy = data.aws_iam_policy_document.app_logs_bucket[0].json

  depends_on = [module.app_logs_bucket]
}

data "aws_iam_policy_document" "app_logs_bucket" {
  count = var.create ? 1 : 0

  statement {
    sid = "Admin access"

    principals {
      type        = "AWS"
      identifiers = ["arn:aws:iam::${var.account_id}:root"]
    }

    resources = [
      "arn:aws:s3:::${module.app_logs_bucket[0].name}",
      "arn:aws:s3:::${module.app_logs_bucket[0].name}/*"
    ]

    actions = ["s3:*"]
  }
}

// s3-versioned-bucket module code

resource "aws_s3_bucket" "this" {
  count  = var.create ? 1 : 0
  bucket = var.name
  acl    = "private"
  
  versioning {
    enabled = true
  }

  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        kms_master_key_id = var.kms_key_arn
        sse_algorithm     = "aws:kms"
      }
    }
  }

  lifecycle_rule {
    prefix  = "*"
    enabled = true

    noncurrent_version_expiration {
      days = var.version_expiration_days
    }
  }
}

resource "aws_s3_bucket_public_access_block" "this" {
  count  = var.create ? 1 : 0
  
  bucket = aws_s3_bucket.this[0].id

  block_public_acls       = var.block_public_acls
  block_public_policy     = var.block_public_policy
  ignore_public_acls      = var.ignore_public_acls
  restrict_public_buckets = var.restrict_public_buckets
}

Expected Behavior

The Bucket Policy should have been created and attached to the S3 Bucket.

Actual Behavior

Terraform throws the following error on apply

Error: configuration for data.aws_iam_policy_document.app_logs_bucket[0] still contains unknown values during apply (this is a bug in Terraform; please report it!)

Steps to Reproduce

  1. terraform init
  2. terraform plan
  3. terraform apply

Additional Context

This worked with v0.11.14

Hardcoding the bucket name in the aws_s3_bucket_policy resource and the aws_iam_policy_document data resolves the issue:

resource "aws_s3_bucket_policy" "logs" {
  count  = var.create ? 1 : 0
  bucket = "service-111111111111-app-logs" #module.app_logs_bucket[0].name
  policy = data.aws_iam_policy_document.app_logs_bucket[0].json

  depends_on = [module.app_logs_bucket]
}

data "aws_iam_policy_document" "app_logs_bucket" {
  count = var.create ? 1 : 0

  statement {
    sid = "Admin access"

    principals {
      type        = "AWS"
      identifiers = ["arn:aws:iam::111111111111:root"] #["arn:aws:iam::${var.account_id}:root"]
    }

    resources = [
      "arn:aws:s3:::service-111111111111-app-logs",
      "arn:aws:s3:::service-111111111111-app-logs/*"
    ]

    # resources = [
    #   "arn:aws:s3:::${module.app_logs_bucket[0].name}",
    #   "arn:aws:s3:::${module.app_logs_bucket[0].name}/*"
    # ]

    actions = ["s3:*"]
  }
}

References

Possibly related to

@ghost
Copy link

ghost commented Aug 15, 2019

Same issue here.

Terraform apply:

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
 <= read (data resources)

Terraform will perform the following actions:

  # data.aws_iam_policy_document.data_dev_bucket will be read during apply
  # (config refers to values not yet known)
 <= data "aws_iam_policy_document" "data_dev_bucket"  {
      + id   = (known after apply)
      + json = (known after apply)

      + statement {
          + actions   = [
              + "*",
            ]
          + effect    = "Deny"
          + resources = [
              + (known after apply),
            ]

          + condition {
              + test     = "Bool"
              + values   = [
                  + "false",
                ]
              + variable = "aws:SecureTransport"
            }

          + principals {
              + identifiers = [
                  + "*",
                ]
              + type        = "*"
            }
        }
    }

Plan: 0 to add, 0 to change, 0 to destroy.

Do you want to perform these actions in workspace "prod-dan"?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes


Error: configuration for data.aws_iam_policy_document.data_dev_bucket still contains unknown values during apply (this is a bug in Terraform; please report it!)


Releasing state lock. This may take a few moments...

My tf template is even simpler, the only dynamic resource is the resource reference:

data "aws_iam_policy_document" "data_dev_bucket" {
  # Only allow TLS communication with the bucket contents
  statement {
    effect = "Deny"
    actions = [
      "*",
    ]
    principals {
      type        = "*"
      identifiers = ["*"]
    }
    resources = [
      "${aws_s3_bucket.data_dev[0].arn}/*",
    ]
    condition {
      test     = "Bool"
      variable = "aws:SecureTransport"
      values   = ["false"]
    }
  }
}

@nozaq
Copy link
Contributor

nozaq commented Aug 17, 2019

Seems terraform returns ... still contains unknown values during apply error when aws_iam_policy_document contains an array element expression with the invalid index.

I was able to reproduce this error with the following code.

provider "aws" {
  region = "us-east-1"
}

resource "null_resource" "foo" {
  count = 0
}

data "aws_iam_policy_document" "bar" {
  statement {
    actions   = ["*"]
    resources = ["${null_resource.foo[0].id}"]
  }
}

null_resource.foo[0] does not exist since its count value is 0.
terraform returns Error: configuration for data.aws_iam_policy_document.bar still contains unknown values during apply (this is a bug in Terraform; please report it!) when applying it. terraform apply succeeds when count parameter in foo resource is set to 1.
The error message is a bit misleading though, the reason it fails to applying the above code is not due to unknown values, but null_resource.foo[0] does not exist.

Not sure it's the root cause of the issue reported here though, I guess it's worthwhile to check if there exist any error in expressions used in data.aws_iam_policy_document?

@rlees85
Copy link

rlees85 commented Aug 18, 2019

Still not working for me either

###############################################################################

data "template_file" "samba_server_setup_script" {
  template = file(format("%s/samba-server-setup.sh.tpl", local.tools))

  vars = {
    "region"      = var.region
    "env_name"    = var.env_name
    "vpc_name"    = var.vpc_name
    "user_name"   = var.samba_user_name
    "efs_mount"   = var.samba_efs_mountpoint
    "extra_init"  = var.client
    "efs_address" = module.commerce_efs.route53_records_name
    "stream_name" = var.stream_name
  }
}

###############################################################################

Using the output of a module as an input to this data source still does not work, was absolutely fine on Terraform 0.11

edit: oddly that output route53_records_name is a join on a splat of one value.

@jbardin jbardin added the core label Aug 19, 2019
@jbardin
Copy link
Member

jbardin commented Aug 20, 2019

Thanks @nozaq, that is indeed the root cause in this situation.
It turns out the data source has some extra validation that is catching the configuration error, though it is unfortunately not mapped back to a useful configuration error and reported as a terraform bug.

@dsnellgrove, since you seem to have the next smallest configuration, can you make a more complete example to confirm if aws_s3_bucket.data_dev[0].arn is a valid reference in your config?

@hashibot hashibot added the v0.12 Issues (primarily bugs) reported against v0.12 releases label Aug 22, 2019
@jbardin
Copy link
Member

jbardin commented Oct 17, 2019

Closed by #22846

@jbardin jbardin closed this as completed Oct 17, 2019
@ghost
Copy link

ghost commented Nov 17, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Nov 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug config core v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants