0
votes

I'm trying to provision an EC2 instance in an existing and specific VPC by providing an AWS region to Terraform.

I want to be able to automatically choose a specific vpc using regex or some other method, this is my tf file.

can anybody help me?

the VPC I want to be chosen automatically has a prefix "digital". so instead of providing its name in here -> name = "tag:${local.env_profile}-vpc"

I want to provide only the region and then to get this specific VPC using regex.

provider "aws" {
  region                  = "eu-west-3"
  shared_credentials_file = "${var.shared_credentials_file}"
  profile                 = "${var.profile}"
}

data "aws_vpc" "selected" {

  filter {
    name = "tag:${local.env_profile}-vpc"
    values = ["${local.env_profile}-vpc"]
  }
}

resource "aws_instance" "general" {
  ami = "ami-00035f41c82244dab"
  instance_type = "${var.type}"
  vpc = "${data.aws_vpc.selected.id}"
  key_name = "${var.key_name}"
  tags {
    Name = "empty"
  }
}
1
How have you tagged your VPCs?ydaetskcoR
I have tags and i also have a general prefix which will help me with choosing those vpc's in each regionOz Giat
Can you edit your question to show the tags of your VPC? Either the output of aws ec2 describe-vpcs or if you created them via Terraform the code used to create them (in a separate code block).ydaetskcoR
I'm not really sure I follow exactly what you want here. Can you edit your question to show how you identify the VPC you want to put the instance in and explain in more detail what result you want?ydaetskcoR
yes one moment i will edit my questionOz Giat

1 Answers

0
votes

I don't think there is a way to select a random VPC via regex in terraform. You can check the data source for VPC here https://www.terraform.io/docs/providers/aws/d/vpc.html

You can refer to a VPC . by using the ID and the name.