Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Terraform
terraform.tfstate
terraform.tfstate.*
.terraform/

15 changes: 15 additions & 0 deletions terraform/aws/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = ">= 1.3.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

provider "aws" {
region = "ap-southeast-2"
}

58 changes: 58 additions & 0 deletions terraform/aws/security_groups.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -----------------------
# Security Group for ALB
# -----------------------
resource "aws_security_group" "alb_sg" {
name = "alb-security-group"
description = "Allow HTTP inbound traffic"
vpc_id = aws_vpc.main.id

ingress {
description = "Allow HTTP from Internet"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "alb-sg"
}
}

# -----------------------
# Security Group for ECS Tasks
# -----------------------
resource "aws_security_group" "ecs_sg" {
name = "ecs-security-group"
description = "Allow traffic from ALB only"
vpc_id = aws_vpc.main.id

ingress {
description = "Allow traffic from ALB"
from_port = 8000
to_port = 8000
protocol = "tcp"
security_groups = [aws_security_group.alb_sg.id]
}

egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "ecs-sg"
}
}

47 changes: 47 additions & 0 deletions terraform/aws/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "aws_region" {
description = "AWS region"
type = string
default = "ap-southeast-2"
}

variable "vpc_cidr" {
description = "CIDR block for VPC"
type = string
default = "10.0.0.0/16"
}

variable "app_name" {
description = "Application name"
type = string
default = "devops-backend"
}

variable "container_port" {
description = "Container port"
type = number
default = 8000
}

variable "container_cpu" {
description = "CPU units for container"
type = number
default = 256
}

variable "container_memory" {
description = "Memory for container (MB)"
type = number
default = 512
}

variable "alert_email" {
description = "Email address to receive CloudWatch alerts"
type = string
}

variable "app_secret_value" {
description = "Secret value for backend (example)"
type = string
sensitive = true
}

81 changes: 81 additions & 0 deletions terraform/aws/vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -----------------------
# VPC
# -----------------------
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = "devops-vpc"
}
}

# -----------------------
# Internet Gateway
# -----------------------
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.main.id

tags = {
Name = "devops-igw"
}
}

# -----------------------
# Public Subnet 1
# -----------------------
resource "aws_subnet" "public_1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "ap-southeast-2a"
map_public_ip_on_launch = true

tags = {
Name = "public-subnet-1"
}
}

# -----------------------
# Public Subnet 2
# -----------------------
resource "aws_subnet" "public_2" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "ap-southeast-2b"
map_public_ip_on_launch = true

tags = {
Name = "public-subnet-2"
}
}

# -----------------------
# Route Table
# -----------------------
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id

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

tags = {
Name = "public-route-table"
}
}

# -----------------------
# Route Table Associations
# -----------------------
resource "aws_route_table_association" "public_1" {
subnet_id = aws_subnet.public_1.id
route_table_id = aws_route_table.public.id
}

resource "aws_route_table_association" "public_2" {
subnet_id = aws_subnet.public_2.id
route_table_id = aws_route_table.public.id
}