brikis98 / terraform-up-and-running-code

Code samples for the book "Terraform: Up & Running" by Yevgeniy Brikman
http://www.terraformupandrunning.com/
MIT License
2.87k stars 1.92k forks source link

non default vpc #59

Closed MacFlurry closed 4 years ago

MacFlurry commented 4 years ago

Hello,

This is a question. I’m in chapter 6/Production grade/testable module

trying to wrap up every module, I’m facing a little issue here

I don't have a default vpc ( I'm experimenting it on my company's aws account.) I have my vpc on a separate aws subscription dedicated to lab., so I can retrieve my vpc i. But, for subnet, I have to create it. so this :

data "aws_subnet_ids" "default" { 
  vpc_id = data.aws_vpc.default.id
}

won't work since we don't use default vpc. What is your advice to make it work with my existing vpc ? Earlier in the book I created subnet like this:

resource "aws_subnet" "my_subnet" {
  count             = 2
  vpc_id            = data.aws_vpc.selected.id
  availability_zone = data.aws_availability_zones.available.names[count.index]
  cidr_block        = cidrsubnet(data.aws_vpc.selected.cidr_block, 12, count.index)

  tags = {
    Name = "sb-vpc-${count.index}"
  }
}

so what is your advice and In which module (based on the book's code) should I add it so that the creation and use are reusable?

thanks for your help.

brikis98 commented 4 years ago

If you don't have a default VPC, you can create one: the VPC UI now lets you do it with a few clicks. If your company policy doesn't allow that, you can create a custom VPC and subnets. This takes a decent amount of code and requires thinking about routes, route tables, Internet Gateways, and if the subnet is private, also NAT Gateways.

For a basic VPC that can be used for experimenting / learning, see this module from the Terraform Registry.

As for where to add it, the answer is, it depends on a number of factors. In general though, you'll want to create a VPC module and deploy it separate from the rest of the infrastructure. Then, in the rest of the infrastructure, you can look up the VPC info using, for example, data sources. Here's an example using the same aws_vpc and aws_subnet_ids data sources as in the book, but to look up a custom VPC and subnets:

data "aws_vpc" "my_custom_vpc" {
  default = false

  # Look up a VPC with the Name tag "my-custom-vpc"
  tags {
    Name = "my-custom-vpc"
  }
}

data "aws_subnet_ids" "my_custom_subnets" {
  vpc_id = data.aws_vpc.my_custom_vpc.id

  # Look up subnets in my  custom VPC that have the ForTesting tag set to "True"
  tags {
    ForTesting = True
  }
}
MacFlurry commented 4 years ago

Thanks a lot @brikis98 , gonna try to adapt your code with my custom vpc by using a separate module as suggested. I'm about to finish your beautiful book.😍 Do you have a slack for one-time exchanges or can we continue to ask questions here? Best regards

brikis98 commented 4 years ago

Feel free to ask further questions about the book as GitHub issues! I'll close this one for now as I think it has been resolved.