Skip to content

Commit

Permalink
added ec2, network modules
Browse files Browse the repository at this point in the history
  • Loading branch information
chefgs committed Aug 16, 2024
1 parent 388e64c commit c8b2b4c
Show file tree
Hide file tree
Showing 17 changed files with 222 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tf-ec2-with-modules/terraform-project/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Local .terraform directories
**/.terraform/*

# .tfstate files
*.tfstate
*.tfstate.*

# Crash log files
crash.log

# Exclude all .tfvars files, which are likely to contain sensitive data, such as passwords
*.tfvars

# Ignore override files as they are usually used to override resources locally and should not be shared
override.tf
override.tf.json
*_override.tf
*_override.tf.json

# Include .terraformrc and terraform.rc files as they are user-specific configuration files
.terraformrc
terraform.rc

# Ignore any plan output files
*.tfplan
24 changes: 24 additions & 0 deletions tf-ec2-with-modules/terraform-project/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions tf-ec2-with-modules/terraform-project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
provider "aws" {
region = "us-west-2"
}

module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
}

module "subnet" {
source = "./modules/subnet"
vpc_id = module.vpc.vpc_id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
}

module "security_group" {
source = "./modules/security-group"
vpc_id = module.vpc.vpc_id
}


data "aws_ami" "example" {
most_recent = true

filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["amazon"]
}

module "ec2" {
source = "./modules/ec2"
ami = data.aws_ami.example.id
instance_type = "t2.micro"
subnet_id = module.subnet.subnet_id
security_group_id = module.security_group.security_group_id
key_name = var.key_name
}
11 changes: 11 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/ec2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "aws_instance" "main" {
ami = var.ami
instance_type = var.instance_type
subnet_id = var.subnet_id
security_groups = [var.security_group_id]
key_name = var.key_name

tags = {
Name = "MyEC2Instance"
}
}
3 changes: 3 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/ec2/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "instance_id" {
value = aws_instance.main.id
}
24 changes: 24 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/ec2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
variable "ami" {
description = "The AMI ID for the EC2 instance"
type = string
}

variable "instance_type" {
description = "The instance type for the EC2 instance"
type = string
}

variable "subnet_id" {
description = "The ID of the subnet"
type = string
}

variable "security_group_id" {
description = "The ID of the security group"
type = string
}

variable "key_name" {
description = "value of the key pair"
type = string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource "aws_security_group" "main" {
vpc_id = var.vpc_id

ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "allow_ssh"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "security_group_id" {
value = aws_security_group.main.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "vpc_id" {
description = "The ID of the VPC"
type = string
}
24 changes: 24 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/subnet/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "aws_subnet" "main" {
vpc_id = var.vpc_id
cidr_block = var.cidr_block
availability_zone = var.availability_zone
map_public_ip_on_launch = true
}

resource "aws_internet_gateway" "main" {
vpc_id = var.vpc_id
}

resource "aws_route_table" "public" {
vpc_id = var.vpc_id

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
}

resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.public.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "subnet_id" {
value = aws_subnet.main.id
}
14 changes: 14 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/subnet/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "vpc_id" {
description = "The ID of the VPC"
type = string
}

variable "cidr_block" {
description = "The CIDR block for the subnet"
type = string
}

variable "availability_zone" {
description = "The availability zone for the subnet"
type = string
}
3 changes: 3 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
}
3 changes: 3 additions & 0 deletions tf-ec2-with-modules/terraform-project/modules/vpc/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "vpc_id" {
value = aws_vpc.main.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
variable "cidr_block" {
description = "The CIDR block for the VPC"
type = string
}
3 changes: 3 additions & 0 deletions tf-ec2-with-modules/terraform-project/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "instance_id" {
value = module.ec2.instance_id
}
7 changes: 7 additions & 0 deletions tf-ec2-with-modules/terraform-project/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Define any global variables if needed
variable "key_name" {
description = "value of the key pair"
type = string
default = "your-pem-key-name"
}

0 comments on commit c8b2b4c

Please sign in to comment.