Small, modular Terraform project that creates a VPC, a public subnet, a security group (SSH + HTTP) and a single EC2 instance. The repo intentionally includes only the core infra modules so it is easy to read and reuse.
terraform-aws/
├── main.tf # wires modules together
├── variables.tf # top-level variables
├── outputs.tf # top-level outputs
├── README.md
└── modules/
├── vpc/ # creates VPC + subnet and outputs subnet id
│ ├── main.tf
│ └── variables.tf
├── security_group/# creates the SG allowing SSH/HTTP
│ ├── main.tf
│ └── variables.tf
└── ec2/ # creates an EC2 instance (AMI auto-selected per region)
├── main.tf
└── variables.tf
- Configure AWS credentials (required for Terraform to talk to AWS):
aws configure- Initialize the working directory (downloads providers and modules):
terraform init- (Optional) See the execution plan:
terraform plan- Apply the plan and provision resources:
terraform applyConfirm with yes when prompted.
- To destroy everything provisioned by Terraform:
terraform destroy- The EC2 module will automatically select a recent Amazon Linux 2 AMI for the
region you're deploying to unless you override
ami_id. - The VPC module will auto-select a valid availability zone for the configured
region when
availability_zoneis left empty. - Do NOT commit
terraform.tfstateor the.terraform/directory. The included.gitignorealready ignores those files.
After a successful apply the following outputs are available:
vpc_id— VPC created by the VPC modulesubnet_id— Subnet created in the VPCsecurity_group_id— Security group allowing SSH and HTTPec2_instance_id— The EC2 instance ID
- Change
variables.tfor the module-level variable files to configure region, instance type, CIDR blocks, or to provide a custom AMI.
Satish Choudhary
Feel free to fork or open an issue if you want enhancements.