Scenario: Create a EC2 machine in the VPC already written in the template
- Refer Here
- In Terraform we can reuse any terraform with modules and also refer here
- Lets convert our vpc into a module
- Also refer here
- Use the following syntax
module "myvpc" {
source = "./modules/myvpc"
vpccidr = "192.168.0.0/16"
}
- Terraform also provides a way of sharing modules to community using terraform registry
- Now lets find the module to create vpc and we found this

- Lets create a new folder with provider.tf and main.tf. Add aws provider to provider.tf and in main.tf add the following
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.38.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
azs = ["us-west-2a", "us-west-2b", "us-west-2c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
tags = {
Terraform = "true"
Environment = "dev"
}
}
- Execute
terraform init
terraform apply .
- Terraform has an Enterprise version Terraform Enterprise and also Refer here
