hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.74k stars 9.1k forks source link

Not finding data resources with variables (VPC and Route53) #11140

Open joshghent opened 4 years ago

joshghent commented 4 years ago

Community Note

First off, thank you for an amazing free tool! Seriously, it's awesome.

Terraform Version

➜ terraform version
Terraform v0.12.17
+ provider.aws v2.26.0
+ provider.template v2.1.2

Affected Resource(s)

Terraform Configuration Files

data "aws_vpc" "gizmodo" {
  tags = {
    Name   = "gizmodo-${var.tenant}"
    Tenant = "${var.tenant}"
  }

  state = "available"
}

data "aws_route53_zone" "gizmodo" {
  name         = "gizmodo.${var.tenant}.internal."
  private_zone = true
}

Debug Output

Used TF_LOG=DEBUG. The gist is encrypted with the Terraform Public Key from Keybase https://gist.github.com/joshghent/33217f5dab592d20c698b82cbde72c1c

Panic Output

No panic output

Expected Behavior

It should have found both the VPC and the Route53 Zone

Actual Behavior

It didn't do those things and errors with

Error: no matching VPC found

  on modules/gizmodo-peering/main.tf line 1, in data "aws_vpc" "gizmodo":
   1: data "aws_vpc" "gizmodo" {

Error: no matching Route53Zone found

  on modules/gizmodo-peering/main.tf line 50, in data "aws_route53_zone" "gizmodo":
  50: data "aws_route53_zone" "gizmodo" {

Steps to Reproduce

  1. Create a zone and VPC separately (in my case they were created in a separate Terraform instance) with the zone called "gizmodo.test.internal." and the VPC called "gizmodo-test"
  2. Create a new terraform instance/repo and include the pasted code above with the name of your VPC and Route 53 zone
  3. Run terraform plan

Important Factoids

Nothing of note, the AWS account that I have configured locally has full admin access and the Route53 Full Access policy directly attached so it should find the resources correctly. They can both be viewed in the AWS Console. For the VPC I have tried filtering by both tag and name to no avail.

When running aws route53 list-hosted-zones I can see the route53 zone's in the output When running aws ec2 describe-vpcs I can also see the VPC's

Only slight weirdness is that this module (where the error occurs) is called from another module. It is referenced 33 times from the other module, each time with a different tenant variable passed through.

References

Reviewed all the articles, issues and stackoverflow comments I could find on this. 99% of the time it was because the person did not have access to the route53/VPC in question, for myself that isn't the case. Maybe I've missed something and am being dumb but feel I've exhausted all options at this point.

tovbinm commented 4 years ago

I have the same problem with

Terraform v0.12.28
+ provider.aws v2.70.0
niko-dunixi commented 4 years ago

I'm having the same issue. I can locate my VPC (which I created myself in another stack with the same role) with the CLI:

$ aws ec2 describe-vpcs --filter Name=tag:Name,Values=my-vpc-name
{
    "Vpcs": [
        {
            "CidrBlock": "10.0.0.0/16",
            "DhcpOptionsId": "dopt-********",
            "State": "available",
            "VpcId": "vpc-*****************",
            "OwnerId": "************",
            "InstanceTenancy": "default",
            "CidrBlockAssociationSet": [
                {
                    "AssociationId": "vpc-cidr-assoc-*****************",
                    "CidrBlock": "10.0.0.0/16",
                    "CidrBlockState": {
                        "State": "associated"
                    }
                }
            ],
            "IsDefault": false,
            "Tags": [
                {
                    "Key": "Name",
                    "Value": "my-vpc-name"
                }
            ]
        }
    ]
}

But my equivalent tf doesn't find the it.

data "aws_vpc" "my_vpc" {
  filter {
    name   = "tag:Name"
    values = [local.vpc_name]
  }
}
Error: no matching VPC found

Just like op, I am in an account where the VPC was created separately by another terraform. I used the same role to create that VPC and attempt this particular script as well. Otherwise, I'm not seeing anything out of the ordinary.

One strange thing to note, it seems like changing the tag I filter upon causes things to work magically without complaint. 🤷‍♂️

Terraform v0.12.29
+ provider.aws v2.70.0
debugguru commented 3 years ago

This has been open since 2019. Is this a bug or are we not using it the right way?

tovbinm commented 3 years ago

This is no longer an issue on TF 0.14

debugguru commented 3 years ago

This is what I am using and I am getting above error . I am using TF 0.15

data "aws_vpc" "peer" {
    provider = aws.core-peer
    filter {
      name = "tag:Name"
      values = ["${var.CORE_VPC_NAME}"]
    }
}
provider "aws" {
    region = "${var.AWS_REGION}"
}
provider "aws" {
    alias = "core-peer"
    region = "${var.AWS_REGION_CORE}"
}
debugguru commented 3 years ago

I can confirm this is working now. The issue was with the way I was passing variable in filter values

data "aws_vpc" "peer" {
    provider = aws.core-peer

    filter {
      name = "tag:Name"
      values = [var.CORE_VPC_NAME]
    }
}
divyac1204 commented 3 years ago

I seeing this error "no matching VPC found" even though vpc tagging is correct. any suggestion here data "aws_vpc" "ingeni" { tags = { Application = "CRE-ORA-RDS" environment = "${var.environment}" } }

justinretzolk commented 2 years ago

Hey y'all 👋 Thank you for taking the time to file this issue and for the continued discussion around it. Given that there's been a number of AWS provider releases since the last update, and the mixed results as far as whether this is an issue still or not, can anyone confirm whether you're still experiencing this behavior?

rahulgoyal01 commented 2 years ago

Facing the same issue. Using terraform image in pipeline "cmdlabs/terraform-utils:10.0.0". AWS provider version 3.33.0

Using below syntax

data "aws_vpc" "main" { tags = { Name = local.workspace["vpc_name"] } }

markstuart commented 2 years ago

I was running into this issue with aws_vpc, however it turned out in my case that I needed to ensure that I was assuming the correct role in AWS so that it would find the vpc... in my main.tf file I added:

provider "aws" {
  assume_role {
    role_arn = var.deployer_role
  }
}

then my aws_vpc lookup worked:

data "aws_vpc" "vpc" {
  tags = {
    "Name" = "${terraform.workspace}"
  }
}
Shaltz commented 1 year ago

Hi all, I'm still facing this issue (with aws_vpc AND aws_route53_zone)

here is my code

data "aws_vpc" "my_vpc" {
  filter {
    name = "tag:Name"
    values = [var.vpc_info.name]
  }
}
data "aws_route53_zone" "main_zone" {
  name         = var.r53_zone.name
  private_zone = var.r53_zone.is_private
}

and my tf version: image

I have even tried giving the user I'm deploying with 'AmazonVPCFullAccess' & 'Route53FullAccess', but it doesn't work any better...

Is this still an issue for some of you or am I missing something ?

thanks in advance ;)