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

With 0.3.1, when tearing down the module, getting an error on terraform_iam_policy output #27

Open
apogrebnyak opened this issue Aug 17, 2020 · 7 comments
Labels
bug Something isn't working cant reproduce

Comments

@apogrebnyak
Copy link

Version: 0.3.1

After this change 7290218#diff-c09d00f135e3672d079ff6e0556d957d deletion of the module fails with

Error: Invalid index

  on .terraform/modules/remote_state/terraform-aws-remote-state-s3-backend-0.3.1/outputs.tf line 23, in output "terraform_iam_policy":
  23:   value       = var.terraform_iam_policy_create ? aws_iam_policy.terraform[0] : null
    |----------------
    | aws_iam_policy.terraform is empty tuple

The given key does not identify an element in this collection value.

Before that change nothing was accessed by index.

Looks like changing output to this fixes the error

output "terraform_iam_policy" {
  description = "The IAM Policy to access remote state environment."
  value       = var.terraform_iam_policy_create ? (
        length(aws_iam_policy.terraform) == 0 ? null : aws_iam_policy.terraform[0]
      ) : null
}
@nozaq nozaq added bug Something isn't working cant reproduce labels Sep 22, 2020
@nozaq
Copy link
Owner

nozaq commented Sep 22, 2020

Thank you for the report.

Hmm, it couldn't be reproduced on my side.
I tried terraform apply then terraform destroy in my testing account, but that was successful without any errors.

@apogrebnyak
Copy link
Author

apogrebnyak commented Sep 22, 2020

The issue arises with using terraform_iam_policy in outputs of calling code.

Also, just verified that the problem still exists in 0.4.0 version

Here is the one-pager that demonstrates the problem

locals {
  common_prefix = "test-deploy"
}

terraform {
  required_version = ">= 0.12.24"
}

provider "aws" {
  version = ">= 2.65.0"
}

provider "aws" {
  version = ">= 2.65.0"

  alias = "replica"
}

module "remote_state" {
  source = "nozaq/remote-state-s3-backend/aws"
  version = "0.4.0"

  providers = {
    aws         = aws
    aws.replica = aws.replica
  }

  dynamodb_table_name = "${local.common_prefix}-lock"

  noncurrent_version_transitions = []
  noncurrent_version_expiration = {
    days = 90
  }

  state_bucket_prefix = "${local.common_prefix}-bucket-"
  replica_bucket_prefix = "${local.common_prefix}-replica-"

  terraform_iam_policy_name_prefix = "test--state-access-"
}

output "terraform_iam_policy" {
  value = module.remote_state.terraform_iam_policy
  description = "The IAM Policy to access remote state environment."
}

If you comment out the last output statement the error is not raised on destroy

@mattwillsher
Copy link
Contributor

I'm using TF 0.13.4 and also can't reproduce, even when outputting module.remote_state.terraform_iam_policy.arn.
From your code, I wonder if your issue is that the replica is in the same region as the source bucket?

@apogrebnyak
Copy link
Author

I'm using TF 0.13.4 and also can't reproduce, even when outputting module.remote_state.terraform_iam_policy.arn.
From your code, I wonder if your issue is that the replica is in the same region as the source bucket?

Is it not supported configuration?

@mattwillsher
Copy link
Contributor

I've not double checked the actual code, but the README says:

Two providers must point to different AWS regions.

@apogrebnyak
Copy link
Author

apogrebnyak commented Oct 19, 2020

Two providers must point to different AWS regions.

What is the issue with pointing to the same region? I think requiring a replica in a different region is an overkill.

@mattwillsher
Copy link
Contributor

Perhaps, and it may be unrelated to your issue. I quite like the level of overkill in this module, given it's storing the state. Losing state files keeps me awake at night :)

Could you test and see if it does solve your issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cant reproduce
Projects
None yet
Development

No branches or pull requests

3 participants