Skip to content
New issue

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

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

Already on GitHub? # to your account

take care of "each" attribute is empty and has multiple instances #11

Merged

Conversation

nao23
Copy link
Contributor

@nao23 nao23 commented Sep 25, 2020

I'm using ecspresso and faced a problem that tfstate-lookup could not lookup some resouces in my tfstate.

I found that tfstate-lookup could not work for the resouces which have no "each" attribute and have two or more instance.

example resouce
    {
      "module": "module.subnets",
      "mode": "managed",
      "type": "aws_subnet",
      "name": "main",
      "provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
      "instances": [
        {
          "index_key": 0,
          "schema_version": 1,
          "attributes": {
            "arn": "arn:aws:ec2:ap-northeast-1:123456789012:subnet/subnet-01234567890123456",
            "assign_ipv6_address_on_creation": false,
            "availability_zone": "ap-northeast-1a",
            "availability_zone_id": "apne1-az4",
            "cidr_block": "10.11.12.0/22",
            "id": "subnet-01234567890123456",
            "ipv6_cidr_block": "",
            "ipv6_cidr_block_association_id": "",
            "map_public_ip_on_launch": false,
            "outpost_arn": "",
            "owner_id": "012345678901",
            "tags": {},
            "timeouts": null,
            "vpc_id": "vpc-01234567"
          },
          "private": "bnVsbA=="
        },
        {
          "index_key": 1,
          "schema_version": 1,
          "attributes": {
            "arn": "arn:aws:ec2:ap-northeast-1:123456789012:subnet/subnet-90123456789012345",
            "assign_ipv6_address_on_creation": false,
            "availability_zone": "ap-northeast-1a",
            "availability_zone_id": "apne1-az4",
            "cidr_block": "10.11.15.0/22",
            "id": "subnet-90123456789012345",
            "ipv6_cidr_block": "",
            "ipv6_cidr_block_association_id": "",
            "map_public_ip_on_launch": false,
            "outpost_arn": "",
            "owner_id": "012345678901",
            "tags": {},
            "timeouts": null,
            "vpc_id": "vpc-01234567"
          },
          "private": "bnVsbA=="
        }
      ]
    }

In this case, this query does not work.

❯ tfstate-lookup -s tfstate/test/terraform.tfstate 'module.subnets.aws_subnet.main[0].cidr_block'
null

❯ tfstate-lookup -s tfstate/test/terraform.tfstate | grep "module.subnets"
module.subnets.aws_subnet.main

So, I fixed it.

❯ make test
go test ./...
?   	github.com/fujiwara/tfstate-lookup/cmd/tfstate-lookup	[no test files]
ok  	github.com/fujiwara/tfstate-lookup/tfstate	(cached)

❯ go build -o tfstate-lookup cmd/tfstate-lookup/main.go

❯ ./tfstate-lookup -s tfstate/test/terraform.tfstate 'module.subnets.aws_subnet.main[0].cidr_block'
10.11.12.0/22

❯ ./tfstate-lookup -s tfstate/test/terraform.tfstate | grep "module.subnets"
module.subnets.aws_subnet.main[0]
module.subnets.aws_subnet.main[1]

Please review 🙇 🙇

@nao23 nao23 changed the title take care of each is empty and has multiple instances take care of each attribute is empty and has multiple instances Sep 25, 2020
@nao23 nao23 changed the title take care of each attribute is empty and has multiple instances take care of "each" attribute is empty and has multiple instances Sep 25, 2020
@fujiwara fujiwara merged commit c9f9145 into fujiwara:master Sep 28, 2020
@fujiwara
Copy link
Owner

Thanks!

fujiwara added a commit to kayac/ecspresso that referenced this pull request Sep 28, 2020
@fujiwara
Copy link
Owner

fujiwara commented Sep 28, 2020

I've released tfstate-lookup v0.0.13. ecspresso v0.18.1 and v0.99.8 includes this patch.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants