{"id":18947623,"url":"https://github.com/yousafkhamza/terraform_vpc","last_synced_at":"2025-04-15T23:30:23.983Z","repository":{"id":195965568,"uuid":"368297370","full_name":"yousafkhamza/terraform_vpc","owner":"yousafkhamza","description":"Terraform is a tool for building infrastructure with various technologies including Amazon AWS, Microsoft Azure, Google Cloud, and vSphere.","archived":false,"fork":false,"pushed_at":"2021-07-11T09:10:54.000Z","size":142,"stargazers_count":3,"open_issues_count":0,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2023-09-20T19:52:45.213Z","etag":null,"topics":["automation","aws","bash-script","terraform","vpc"],"latest_commit_sha":null,"homepage":"","language":"HCL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yousafkhamza.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-05-17T19:17:55.000Z","updated_at":"2023-09-20T19:52:50.913Z","dependencies_parsed_at":"2023-09-20T20:13:40.937Z","dependency_job_id":null,"html_url":"https://github.com/yousafkhamza/terraform_vpc","commit_stats":null,"previous_names":["yousafkhamza/terraform_vpc"],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fterraform_vpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fterraform_vpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fterraform_vpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yousafkhamza%2Fterraform_vpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yousafkhamza","download_url":"https://codeload.github.com/yousafkhamza/terraform_vpc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223688711,"owners_count":17186298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automation","aws","bash-script","terraform","vpc"],"created_at":"2024-11-08T13:10:41.290Z","updated_at":"2024-11-08T13:10:42.042Z","avatar_url":"https://github.com/yousafkhamza.png","language":"HCL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Create a VPC Through Terraform (Fully Automated Script)\n[![Builds](https://travis-ci.org/joemccann/dillinger.svg?branch=master)](https://travis-ci.org/joemccann/dillinger)\n\n## Description.\n\nTerraform is a tool for building infrastructure with various technologies including Amazon AWS, Microsoft Azure, Google Cloud, and vSphere.\n\nHere is a simple document on how to use Terraform to build an AWS VPC along with private/public Subnet and Network Gateway's for the VPC. \nWe will be making 1 VPC with 6 Subnets: 3 Private and 3 Public, 1 NAT Gateways, 1 Internet Gateway, and 2 Route Tables all the creation was automated with appending to your values.\n\n### Features\n- Fully Automated (included terraform installation)\n- Easy to customise and use as the Terraform modules are created using variables,allowing the module to be customized without altering the module's own source code, and allowing modules to be shared between different configurations.\n- Each subnet CIDR block created  automatically using cidrsubnet Function (Automated)\n- AWS informations are defined using tfvars file and can easily changed (Automated/Manual)\n- Project name is appended to the resources that are creating which will make easier to identify the resources. \n\n### Prerequisites for this project\n- Need a IAM user access with attached policies for the creation of VPC.\n- Knowledge to the working principles of each AWS services especially VPC, EC2 and IP Subnetting.\n### How to use\n```sh\nyum install git unzip -y\ngit clone https://github.com/ykhsupport/terraform_vpc.git\ncd terraform_vpc\nchmod +x setup.sh\nsh setup.sh\n```\n\u003e setup.sh script sample screenshots\n- _Screenshot 1_\n![alt text](https://i.ibb.co/s571hh7/run.png)\n- _Screenshot 2_\n![alt text](https://i.ibb.co/zFTSxP4/1.jpg)\n- _Screenshot 3_\n![alt text](https://i.ibb.co/MVbwqCt/2.png)\n- _Screenshot 4_\n![alt text](https://i.ibb.co/QfTh64q/3.png)\n\n### Used Languages\n- Bash (scripting)\n- Terraform (iaac)\n\n# Behind the script\n# Terraform Installation and VPC creation code and explanation :\n\u003e ##### please note that you can use the below steps to make a vpc through terraform (_Manually_).\n\nIf you need to download terraform (Manually), then click here [Terraform](https://www.terraform.io/downloads.html) .\n\n### Prerequisites \n- Create an IAM user on your AWS console that have access to create the required resources.\n- Create a dedicated directory where you can create terraform configuration files.\n- Terraform Installation steps, click here [Terraform installation](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started)\n\n\u003e sample installation steps\n```sh\nwget https://releases.hashicorp.com/terraform/0.15.3/terraform_0.15.3_linux_amd64.zip\nunzip terraform_0.15.3_linux_amd64.zip \nls -l\n-rwxr-xr-x 1 root root 79991413 May  6 18:03 terraform  \u003c\u003c=======\n-rw-r--r-- 1 root root 32743141 May  6 18:50 terraform_0.15.3_linux_amd64.zip\nmv terraform /usr/bin/\nwhich terraform \n/usr/bin/terraform\n```\n\nLets create a file for declaring the variables.This is used to declare the variable and the values are passing through the terrafrom.tfvars file.\n\n### Create a variables.tf file\n```sh\nvariable \"region\" {}\nvariable \"access_key\" {}\nvariable \"secret_key\" {}\nvariable \"vpc_cidr\" {}\nvariable \"project\" {}\n```\n### Create a provider.tf file \n```sh\nprovider \"aws\" {\n  region     = var.region\n  access_key = var.access_key\n  secret_key = var.secret_key\n}\n```\n### Create a terraform.tfvars\nBy default terraform.tfvars will load the variables to the the reources.\nYou can modify accordingly as per your requirements.\n\n```sh\nregion     = \"put-your-region-here\"\naccess_key = \"put-your-access-key-here\"\nsecret_key = \"put-your-secret-key-here\"\nproject = \"name-of-your-project\"\nvpc_cidr = \"X.X.X.X/X\"\n```\nGo to the directory that you wish to save your tfstate files.Then Initialize the working directory containing Terraform configuration files using below command.\n```sh\nterraform init\n```\n#### Lets start creating main.tf file with the details below.\n\n\n\u003e To create VPC\n```sh\nresource \"aws_vpc\" \"vpc\" {\n  cidr_block           = var.vpc_cidr\n  instance_tenancy     = \"default\"\n  enable_dns_support   = true\n  enable_dns_hostnames = true\n  tags = {\n    Name = \"${var.project}-vpc\"\n  }\n}\n```\n\u003e To Gather All Subent Name\n```sh\ndata \"aws_availability_zones\" \"available\" {\n  state = \"available\"\n}\n```\n\n\u003e To create InterGateWay For VPC\n```sh\nresource \"aws_internet_gateway\" \"igw\" {\n  vpc_id = aws_vpc.vpc.id\n  tags = {\n    Name = \"${var.project}-igw\"\n  }\n}\n```\nHere in this infrastructre we shall create 3 public and 3 private subnets in the region.This sample was meant for regions having 6 availability zone. I have used \"us-east-1\". Choose your region and modify according to the availability of the AZ. Also we have already provided the CIDR block in our terraform.tfvars you dont need to calculate the subnets, here we use terraform to automate the subnetting in /19.\n\n\u003e Creating public1 Subnet\n```sh\nresource \"aws_subnet\" \"public1\" {\n    \n  vpc_id                   = aws_vpc.vpc.id\n  cidr_block               = cidrsubnet(var.vpc_cidr, 3, 0)\n  availability_zone        = element(data.aws_availability_zones.available.names,0)\n  map_public_ip_on_launch  = true\n  tags = {\n    Name = \"${var.project}-public1\"\n  }\n}\n```\n\u003e Creating public2 Subnet\n```sh\nresource \"aws_subnet\" \"public2\" {\n  vpc_id                   = aws_vpc.vpc.id\n  cidr_block               = cidrsubnet(var.vpc_cidr, 3, 1)\n  availability_zone        = element(data.aws_availability_zones.available.names,1)\n  map_public_ip_on_launch  = true\n  tags = {\n    Name = \"${var.project}-public2\"\n  }\n}\n```\n\u003e Creating public3 Subnet\n```sh\nresource \"aws_subnet\" \"public3\" {\n  vpc_id                   = aws_vpc.vpc.id\n  cidr_block               = cidrsubnet(var.vpc_cidr, 3, 2)\n  availability_zone        = element(data.aws_availability_zones.available.names,2)\n  map_public_ip_on_launch  = true\n  tags = {\n    Name = \"${var.project}-public-3\"\n  }\n}\n```\n\u003e Creating private1 Subnet\n```sh\nresource \"aws_subnet\" \"private1\" {\n  vpc_id                   = aws_vpc.vpc.id\n  cidr_block               = cidrsubnet(var.vpc_cidr, 3, 3)\n  availability_zone        = element(data.aws_availability_zones.available.names,3)\n  map_public_ip_on_launch  = true\n  tags = {\n    Name = \"${var.project}-private1\"\n  }\n}\n```\n\u003e Creating private2 Subnet\n```sh\nresource \"aws_subnet\" \"private2\" {\n  vpc_id                   = aws_vpc.vpc.id\n  cidr_block               = cidrsubnet(var.vpc_cidr, 3, 4)\n  availability_zone        = element(data.aws_availability_zones.available.names,4)\n  map_public_ip_on_launch  = true\n  tags = {\n    Name = \"${var.project}-private2\"\n  }\n}\n```\n\u003e Creating private3 Subnet\n```sh\nresource \"aws_subnet\" \"private3\" {\n  vpc_id                   = aws_vpc.vpc.id\n  cidr_block               = cidrsubnet(var.vpc_cidr, 3, 5)\n  availability_zone        = element(data.aws_availability_zones.available.names,5)\n  map_public_ip_on_launch  = true\n  tags = {\n    Name = \"${var.project}-private3\"\n  }\n}\n```\n\u003e Creating  Elastic IP For Nat Gateway\n```sh\nresource \"aws_eip\" \"eip\" {\n  vpc      = true\n  tags     = {\n    Name = \"${var.project}-eip\"\n  }\n}\n```\n\u003e Attaching Elastic IP to NAT gateway\n```sh\nresource \"aws_nat_gateway\" \"nat\" {\n  allocation_id = aws_eip.eip.id\n  subnet_id     = aws_subnet.public1.id\n  tags = {\n    Name = \"${var.project}-nat\"\n  }\n}\n```\n\u003e  Creating Public Route Table\n```sh\nresource \"aws_route_table\" \"public\" {\n  vpc_id = aws_vpc.vpc.id\n  route {\n    cidr_block = \"0.0.0.0/0\"\n    gateway_id = aws_internet_gateway.igw.id\n  }\n  tags = {\n    Name = \"${var.project}-public\"\n  }\n}\n```\n\u003e  Creating Private Route Table\n```sh\n  resource \"aws_route_table\" \"private\" {\n  vpc_id = aws_vpc.vpc.id\n  route {\n    cidr_block = \"0.0.0.0/0\"\n    nat_gateway_id = aws_nat_gateway.nat.id\n  }\n  tags = {\n    Name = \"${var.project}-private\"\n  }\n}\n```\n\u003e Creating Public Route Table Association\n```sh\nresource \"aws_route_table_association\" \"public1\" {        \n  subnet_id      = aws_subnet.public1.id\n  route_table_id = aws_route_table.public.id\n}\nresource \"aws_route_table_association\" \"public2\" {      \n  subnet_id      = aws_subnet.public2.id\n  route_table_id = aws_route_table.public.id\n}\nresource \"aws_route_table_association\" \"public3\" {       \n  subnet_id      = aws_subnet.public3.id\n  route_table_id = aws_route_table.public.id\n}\n```\n\u003e Creating Private Route Table Association\n```sh\nresource \"aws_route_table_association\" \"private1\" {        \n  subnet_id      = aws_subnet.private1.id\n  route_table_id = aws_route_table.private.id\n}\nresource \"aws_route_table_association\" \"private2\" {      \n  subnet_id      = aws_subnet.private2.id\n  route_table_id = aws_route_table.private.id\n}\nresource \"aws_route_table_association\" \"private3\" {       \n  subnet_id      = aws_subnet.private3.id\n  route_table_id = aws_route_table.private.id\n}\n````\n\u003e Create an output.tf for getting  terrafrom output.\n```sh\noutput \"aws_eip\" {\nvalue = aws_eip.eip.public_ip\n}\noutput \"aws_vpc\" {\nvalue = aws_vpc.vpc.id\n}\noutput \"aws_internet_gateway\" {\nvalue = aws_internet_gateway.igw.id\n}\noutput \"aws_nat_gateway\" {\nvalue = aws_nat_gateway.nat.id\n}\noutput \"aws_route_table_public\" {\nvalue = aws_route_table.public.id\n}\noutput \"aws_route_table_private\" {\nvalue = aws_route_table.private.id\n}\n```\n#### Lets validate the terraform files using\n```sh\nterraform validate\n```\n#### Lets plan the architecture and verify once again.\n```sh\nterraform plan\n```\n#### Lets apply the above architecture to the AWS.\n```sh\nterraform apply\n```\n\n----\n## Conclusion\nHere is a simple document on how to use Terraform to build an AWS VPC along with private/public Subnet and Network Gateway's for the VPC. We will be making 1 VPC with 6 Subnets: 3 Private and 3 Public, 1 NAT Gateways, 1 Internet Gateway, and 2 Route Tables all the creation was automated with appending to your values.\n\n\n### ⚙️ Connect with Me\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"mailto:yousaf.k.hamza@gmail.com\"\u003e\u003cimg src=\"https://img.shields.io/badge/Gmail-D14836?style=for-the-badge\u0026logo=gmail\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://www.linkedin.com/in/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/LinkedIn-0077B5?style=for-the-badge\u0026logo=linkedin\u0026logoColor=white\"/\u003e\u003c/a\u003e \n\u003ca href=\"https://www.instagram.com/yousafkhamza\"\u003e\u003cimg src=\"https://img.shields.io/badge/Instagram-E4405F?style=for-the-badge\u0026logo=instagram\u0026logoColor=white\"/\u003e\u003c/a\u003e\n\u003ca href=\"https://wa.me/%2B917736720639?text=This%20message%20from%20GitHub.\"\u003e\u003cimg src=\"https://img.shields.io/badge/WhatsApp-25D366?style=for-the-badge\u0026logo=whatsapp\u0026logoColor=white\"/\u003e\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fterraform_vpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyousafkhamza%2Fterraform_vpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyousafkhamza%2Fterraform_vpc/lists"}