Skip to content

Commit

Permalink
Chore: Refactor module (#5)
Browse files Browse the repository at this point in the history
* move vpc to module

* Feature: Terraform CI (#4)

* add tf workflow

* update ci

* update exit command

* update return code

* update exit code

* fmt

* update readme

* add module

* update vpc module

* add vm module

* add main

* fmt

* refactor

* fmt

* simplify vpc route table

* fmt

* Revert "simplify vpc route table"

This reverts commit a8f22db.

* update vars

* update output
  • Loading branch information
guyzsarun authored Dec 18, 2023
1 parent 89c4946 commit 5b968eb
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# .tfstate files
*.tfstate
*.tfstate.*
kubeconfig*

# Crash log files
crash.log
Expand Down
42 changes: 0 additions & 42 deletions bastion.tf

This file was deleted.

27 changes: 27 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module "bastion" {
source = "./modules/vm"
ssh_key_pair = var.ssh_key_pair

vm_name = "bastion_vm"
subnet_id = aws_subnet.main-vpc-subnet-public[0].id

init_script = "./helper/init.sh"

security_group_ids = [
aws_security_group.allow_ssh.id,
aws_security_group.allow_egress.id
]
}

module "private_bastion" {
source = "./modules/vm"
ssh_key_pair = var.ssh_key_pair

vm_name = "private_vm"
subnet_id = aws_subnet.main-vpc-subnet-private[0].id

security_group_ids = [
aws_security_group.allow_ssh.id,
aws_security_group.allow_egress.id
]
}
20 changes: 20 additions & 0 deletions modules/vm/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "aws_key_pair" "ssh_key_pair" {
key_name = "${var.vm_name}-key"
public_key = var.ssh_key_pair
}

resource "aws_instance" "vm" {
ami = var.vm_ami
instance_type = var.instance_type
key_name = aws_key_pair.ssh_key_pair.key_name
monitoring = true

subnet_id = var.subnet_id

user_data = var.init_script != null ? file(var.init_script) : null

vpc_security_group_ids = var.security_group_ids
tags = {
Name = var.vm_name
}
}
11 changes: 11 additions & 0 deletions modules/vm/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "id" {
value = aws_instance.vm.id
}

output "public_ip" {
value = aws_instance.vm.public_ip
}

output "private_ip" {
value = aws_instance.vm.private_ip
}
29 changes: 29 additions & 0 deletions modules/vm/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
variable "ssh_key_pair" {
type = string
sensitive = true
}

variable "vm_ami" {
default = "ami-02453f5468b897e31" #amazon linux
}

variable "instance_type" {
default = "t2.micro"
}

variable "vm_name" {
type = string
}

variable "subnet_id" {
type = string
}

variable "security_group_ids" {
type = list(string)
}

variable "init_script" {
type = string
default = null
}
31 changes: 0 additions & 31 deletions output.tf

This file was deleted.

30 changes: 30 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
output "bastion-vm" {
value = {
id = module.bastion.id
private_ip = module.bastion.private_ip
public_ip = module.bastion.public_ip
}
}

output "private-bastion-vm" {
value = {
id = module.private_bastion.id
private_ip = module.private_bastion.private_ip
}
}


output "nat-gateway" {
value = {
public_ip = aws_nat_gateway.nat-gw.public_ip
private_ip = aws_nat_gateway.nat-gw.private_ip
}
}

output "eks" {
value = {
cluster_name = module.eks.cluster_name
cluster_endpoint = module.eks.cluster_endpoint
get_kubeconfig_command = "aws eks update-kubeconfig --name ${module.eks.cluster_name}"
}
}
5 changes: 3 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
variable "vm_ami" {
default = "ami-02453f5468b897e31"
variable "vpc_name" {
type = string
default = "default-vpc"
}

variable "aws_credentials" {
Expand Down
16 changes: 8 additions & 8 deletions vpc.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resource "aws_vpc" "main-vpc" {
enable_dns_hostnames = true

tags = {
Name = "main-vpc"
Name = var.vpc_name
}
}

Expand All @@ -20,7 +20,7 @@ resource "aws_subnet" "main-vpc-subnet-private" {
map_public_ip_on_launch = false

tags = {
Name = "main-vpc-${data.aws_availability_zones.available.names[count.index]}-private"
Name = "${var.vpc_name}-${data.aws_availability_zones.available.names[count.index]}-private"
Type = "private"
}
}
Expand All @@ -33,7 +33,7 @@ resource "aws_subnet" "main-vpc-subnet-public" {
map_public_ip_on_launch = true

tags = {
Name = "main-vpc-${data.aws_availability_zones.available.names[count.index]}-public"
Name = "${var.vpc_name}-${data.aws_availability_zones.available.names[count.index]}-public"
Type = "public"
}
}
Expand All @@ -47,7 +47,7 @@ resource "aws_route_table" "main-vpc-public-routetable" {
}

tags = {
Name = "main-vpc-public-routetable"
Name = "${var.vpc_name}-public-routetable"
}
}

Expand All @@ -60,7 +60,7 @@ resource "aws_route_table" "main-vpc-private-routetable" {
}

tags = {
Name = "main-vpc-private-routetable"
Name = "${var.vpc_name}-private-routetable"
}
}

Expand All @@ -86,14 +86,14 @@ resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.main-vpc.id

tags = {
Name = "internet-gw"
Name = "${var.vpc_name}-internet-gw"
}
}

resource "aws_eip" "nat" {
domain = "vpc"
tags = {
Name = "nat"
Name = "${var.vpc_name}-nat"
}
}

Expand All @@ -103,7 +103,7 @@ resource "aws_nat_gateway" "nat-gw" {
subnet_id = aws_subnet.main-vpc-subnet-public[0].id

tags = {
Name = "nat-gw"
Name = "${var.vpc_name}-nat-gw"
}

depends_on = [aws_internet_gateway.gw]
Expand Down

0 comments on commit 5b968eb

Please sign in to comment.