Skip to content

cloud-bees/vpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS VPC with Terraform

GitHub CI

This repository contains code to create an AWS VPC using Terraform. It includes detailed instructions for initializing, deploying, and managing the infrastructure.

Get Started

1. Run Devcontainer

Open the project in a Devcontainer to ensure you have a consistent development environment.

2. Update Configuration

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.

3. Initialize Terraform

Navigate to the Terraform directory and initialize the project:

cd terraform && terraform init

4. Deploy Infrastructure

Generate a plan and apply it to deploy your infrastructure:

terraform plan -out=deploy.tfplan
terraform apply -auto-approve deploy.tfplan

5. SSH into EC2 Instance

After deployment, SSH into your EC2 instance:

ssh -i ~/.ssh/my_key_pair.pem ec2-user@<ec2-public-ip>

6. Destroy Infrastructure

When you’re done, you can destroy the infrastructure:

terraform plan -destroy -out=destroy.tfplan
terraform apply destroy.tfplan

Developer Guide

1. Initialize Terraform

Make sure to initialize the Terraform project before making changes:

cd terraform && terraform init

2. Format and Validate Configuration

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.

3. Deploy Infrastructure

Create a plan and deploy the infrastructure:

terraform plan -out=deploy.tfplan
terraform apply -auto-approve deploy.tfplan

4. Destroy Infrastructure

To destroy the infrastructure, run:

terraform plan -destroy -out=destroy.tfplan
terraform apply destroy.tfplan

5. Generate Documentation

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

6. Test and Debug

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

7. Lint Your Code

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

Requirements

Name Version
terraform >= 1.8.3
aws >= 5.30
local >= 2.5.0
tls >= 4.0.0

Providers

Name Version
aws >= 5.30
local >= 2.5.0
tls >= 4.0.0

Modules

No modules.

Resources

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

Inputs

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

Outputs

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

License

This project is released under the Mozilla Public License by @Nguyen Tri Man (@cloud-bees)

© 2024 Nguyen Tri Man. All rights reserved.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published