This repository contains code to create an AWS VPC using Terraform. It includes detailed instructions for initializing, deploying, and managing the infrastructure.
Open the project in a Devcontainer to ensure you have a consistent development environment.
Before deploying the infrastructure, update your VPC settings:
- Modify the VPC name and IP range in
terraform/main.tf
. - Run
aws configure
to set up AWS credentials and region.
Navigate to the Terraform directory and initialize the project:
cd terraform && terraform init
Generate a plan and apply it to deploy your infrastructure:
terraform plan -out=deploy.tfplan
terraform apply -auto-approve deploy.tfplan
After deployment, SSH into your EC2 instance:
ssh -i ~/.ssh/my_key_pair.pem ec2-user@<ec2-public-ip>
When you’re done, you can destroy the infrastructure:
terraform plan -destroy -out=destroy.tfplan
terraform apply destroy.tfplan
Make sure to initialize the Terraform project before making changes:
cd terraform && terraform init
Format and validate the Terraform code:
terraform fmt -check=true -recursive
terraform validate
Note: Skip formatting if you have Visual Studio Code’s format-on-save feature enabled.
Create a plan and deploy the infrastructure:
terraform plan -out=deploy.tfplan
terraform apply -auto-approve deploy.tfplan
To destroy the infrastructure, run:
terraform plan -destroy -out=destroy.tfplan
terraform apply destroy.tfplan
Generate Markdown documentation for your Terraform modules using terraform-docs:
docker run --rm --volume "$(pwd):/terraform-docs" -u $(id -u) \
quay.io/terraform-docs/terraform-docs:0.19.0 markdown table \
/terraform-docs/terraform --recursive --output-file /terraform-docs/README.md
Additionally, create a graph visualization of your Terraform plan:
terraform graph -type=plan | dot -Tpng >graph.png
To test and debug Terraform configurations, use the following commands:
export TF_LOG="trace"
export TF_LOG_PATH="tf.log"
terraform init -test-directory=tests/modules/vpc
terraform test -test-directory=tests/modules/vpc -var aws_region=$AWS_REGION
Before committing your changes, run the linter to ensure consistency:
docker run --rm \
-e LOG_LEVEL=DEBUG \
-e RUN_LOCAL=true \
-e DEFAULT_BRANCH=main \
-e SHELL=/bin/bash \
-e SAVE_SUPER_LINTER_SUMMARY=true \
-e SAVE_SUPER_LINTER_OUTPUT=true \
-e IGNORE_GITIGNORED_FILES=true \
-e FIX_JSON_PRETTIER=true \
-e FIX_MARKDOWN_PRETTIER=true \
-e FIX_YAML_PRETTIER=true \
-e VALIDATE_NATURAL_LANGUAGE=false \
-e CREATE_LOG_FILE=true \
-v "$(pwd)":/tmp/lint \
--platform linux/amd64 \
--name super_linter \
ghcr.io/super-linter/super-linter:v7.1.0
Name | Version |
---|---|
terraform | >= 1.8.3 |
aws | >= 5.30 |
local | >= 2.5.0 |
tls | >= 4.0.0 |
Name | Version |
---|---|
aws | >= 5.30 |
local | >= 2.5.0 |
tls | >= 4.0.0 |
No modules.
Name | Type |
---|---|
aws_default_security_group.default | resource |
aws_flow_log.example | resource |
aws_instance.my_instance | resource |
aws_internet_gateway.my_igw | resource |
aws_key_pair.my_key_pair | resource |
aws_route_table.my_private_route_table | resource |
aws_route_table.my_public_route_table | resource |
aws_route_table_association.my_private_subnet_association | resource |
aws_route_table_association.my_public_subnet_association | resource |
aws_s3_bucket.flow_log | resource |
aws_s3_bucket_versioning.flow_log | resource |
aws_security_group.allow_ssh | resource |
aws_subnet.my_private_subnets | resource |
aws_subnet.my_public_subnets | resource |
aws_vpc.my_vpc | resource |
local_sensitive_file.pem_file | resource |
tls_private_key.ssh | resource |
aws_ami.amazon_linux | data source |
aws_caller_identity.current | data source |
Name | Description | Type | Default | Required |
---|---|---|---|---|
aws_region | The AWS region to use | string |
n/a | yes |
cidr_block | The CIDR block for the VPC | string |
null |
no |
tags | A map of tags to use on all resources | map(string) |
{} |
no |
your_ip_range | Your IP range to allow SSH access | string |
"123.123.123.0/24" |
no |
Name | Description |
---|---|
my_vpc_cidr_block | CIDR block of the VPC |
my_vpc_id | ID of the VPC |
public_subnet_ids | IDs of the public subnets |
tags | Tags of the VPC |
This project is released under the Mozilla Public License by @Nguyen Tri Man (@cloud-bees)
© 2024 Nguyen Tri Man. All rights reserved.