Skip to content

Commit

Permalink
feat(tf) Added terraform code
Browse files Browse the repository at this point in the history
  • Loading branch information
Uj5Ghare committed Dec 10, 2024
1 parent 9af00d4 commit a7ecadd
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tf/files/.terraform.lock.hcl

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

9 changes: 9 additions & 0 deletions tf/files/app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

sudo apt update && sudo apt install docker.io -y
sudo usermod -aG docker ubuntu
newgrp docker
docker network create app
docker run -d --name react-app --net app -p 80:3000 uj5ghare/eng-frontend:latest
docker run -d --name node-app --net app -p 8000:8000 uj5ghare/eng-backend:latest
# Now Node.js backend can be accessible with a http://<public-ip>:8000/status API returning status and uptime.
16 changes: 16 additions & 0 deletions tf/files/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terraform {
backend "s3" {
bucket = "devops-engineer-assignment-bucket"
region = "ap-south-2"
key = "app/terraform.tfstate"
dynamodb_table = "Lock-Files"
encrypt = true
}
required_version = ">=0.13.0"
required_providers {
aws = {
version = ">= 2.7.0"
source = "hashicorp/aws"
}
}
}
21 changes: 21 additions & 0 deletions tf/files/instance.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource "aws_instance" "ec2" {
ami = var.ami
instance_type = "t3.micro"
key_name = var.key-name
subnet_id = aws_subnet.public-subnet.id
vpc_security_group_ids = [aws_security_group.security-group.id]
root_block_device {
volume_size = 8
}
user_data = templatefile("./app.sh", {})

tags = {
Name = var.instance-name
}
}

# Output block to get the public IP
output "instance_public_ip" {
value = aws_instance.ec2.public_ip
description = "The public IP address of the EC2 instance"
}
3 changes: 3 additions & 0 deletions tf/files/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "ap-south-2"
}
8 changes: 8 additions & 0 deletions tf/files/vars.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
variable "vpc-name" {}
variable "igw-name" {}
variable "rt-name" {}
variable "subnet-name" {}
variable "sg-name" {}
variable "instance-name" {}
variable "key-name" {}
variable "ami" {}
8 changes: 8 additions & 0 deletions tf/files/vars.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
vpc-name = "app-vpc"
igw-name = "app-igw"
subnet-name = "app-sub"
rt-name = "app-rt"
sg-name = "app-sg"
instance-name = "application-server"
key-name = "Ujwal-Hy"
ami = "ami-03aaeb1f15623d169"
73 changes: 73 additions & 0 deletions tf/files/vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"

tags = {
Name = var.vpc-name
}
}

resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.vpc.id

tags = {
Name = var.igw-name
}
}

resource "aws_subnet" "public-subnet" {
vpc_id = aws_vpc.vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-south-2a"
map_public_ip_on_launch = true

tags = {
Name = var.subnet-name
}
}

resource "aws_route_table" "rt" {
vpc_id = aws_vpc.vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}

tags = {
Name = var.rt-name
}
}

resource "aws_route_table_association" "rt-association" {
route_table_id = aws_route_table.rt.id
subnet_id = aws_subnet.public-subnet.id
}

resource "aws_security_group" "security-group" {
vpc_id = aws_vpc.vpc.id
description = "Allowing HTTP,HTTPS,SSH and Backend Access"

ingress = [
for port in [22, 8000, 443, 80] : {
description = "TLS from VPC"
from_port = port
to_port = port
protocol = "tcp"
ipv6_cidr_blocks = ["::/0"]
self = false
prefix_list_ids = []
security_groups = []
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 = var.sg-name
}
}

0 comments on commit a7ecadd

Please sign in to comment.