-3

How to create folder structure to create public and private subnet in vpc using terraform need to implement the architecture below image? 1.This is the architecture i need to implementProduction Image

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • What folder structure are you talking about? You do not *need* any folders, you *can* da everything in a single file. Generally: identify whatever part of the system you need more than once, create a module / folder for that and use it multiple times. – luk2302 Jun 25 '20 at 10:14
  • Actually i am very new in cloud i need to know how to add all this resources in my .tf file. i have created main.tf file and vars.tf file. – Diwakar Soni Jun 25 '20 at 10:30
  • Do you know how to create all the infrastructure by hand, do you know all resources and connections involved here? Terraform is the second step, you can only take it after you know the actual AWS resources that you want to provision. – luk2302 Jun 25 '20 at 10:36
  • Yes I know how to create all the infrastructure by hand i know the resouces and connection but the thing is i recently start using terraform so i am little confused.where to start. – Diwakar Soni Jun 25 '20 at 10:43

2 Answers2

0

In order to implement a particular design in Terraform you'll need to identify which resource types within the relevant providers correspond with each of the concepts in your design. Your planned architecture seems to exclusively use AWS concepts, so you'll be working with the AWS provider.

To start, I suggest choosing an object from your design diagram and locating the resource type in Terraform's AWS provider that corresponds with it, and then try to write a Terraform configuration for it. For best results, start with the "outermost" objects -- the ones that don't rely on the existence of other objects -- and work inwards.

As an example, your diagram includes a VPC. If I search in the AWS provider documentation for "VPC" I find, amongst other things, the aws_vpc resource type, which is how the AWS provider represents a declaration of a single VPC. So in your main.tf file you can write an aws_vpc resource:

resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

As you move on to other objects you'll find that you need information about objects you already declared. For example, in order to declare a subnet you will need to include the VPC id. You can do that using references, in this case aws_vpc.example.id to use the VPC ID.

Your question is too broad to give a more specific answer than this, so I'd suggest getting started with the above, and then seeing what other challenges you run into along the way. Once you've got some Terraform configuration written and you've experimented with it, you should be able to open new questions here on Stack Overflow that are more concrete, with real code examples and any error messages you saw, and thus folks will hopefully be able to give more specific advice.

Martin Atkins
  • 62,420
  • 8
  • 120
  • 138
0
**( This code is for only creating vpc with public and private subnets)**
resource "aws_vpc" "myvpc" {    
  cidr_block       = "10.10.0.0/16"    
  instance_tenancy = "default"    
    
  tags = {    
    Name = "myvpc"    
  }    
}    
resource "aws_subnet" "public" {
  cidr_block              = "10.10.1.0/24"
  vpc_id                  = aws_vpc.myvpc.id
  map_public_ip_on_launch = "true"
  availability_zone       = "ap-northeast-1d"

  tags = {
    Name = "public"
  }
}
resource "aws_subnet" "private" {
  cidr_block              = "10.10.2.0/24"
  vpc_id                  = aws_vpc.myvpc.id
  map_public_ip_on_launch = "false"
  availability_zone = "ap-northeast-1d"

  tags = {
    Name = "private"
  }
}