Skip to content

Commit

Permalink
feat: add terraform iac
Browse files Browse the repository at this point in the history
  • Loading branch information
djnovin committed Jan 15, 2025
1 parent 2167112 commit 1a7467a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 0 deletions.
109 changes: 109 additions & 0 deletions infra/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
terraform {
required_version = ">= 1.0"

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

# Data source for available AZs in the region
data "aws_availability_zones" "available" {}

# Security group for both instances and ELB
resource "aws_security_group" "default" {
name = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-sg"

ingress {
from_port = 80
to_port = 80
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 = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-sg"
Environment = var.ENVIRONMENT
}
}

# Launch configuration for the autoscaling group
resource "aws_launch_configuration" "example" {
image_id = "ami-58d7e821"
instance_type = "t2.micro"
security_groups = [aws_security_group.default.id]

user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 80 &
EOF

lifecycle {
create_before_destroy = true
}
}

# Autoscaling group for high availability
resource "aws_autoscaling_group" "example" {
launch_configuration = aws_launch_configuration.example.id
availability_zones = data.aws_availability_zones.available.names
min_size = 2
max_size = 5
desired_capacity = 3

target_group_arns = [aws_lb_target_group.example.arn]

tag {
key = "Name"
value = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-asg"
propagate_at_launch = true
}
}

# Load balancer for distributing traffic
resource "aws_lb" "example" {
name = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-lb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.default.id]
subnets = data.aws_availability_zones.available.names
enable_deletion_protection = false

tags = {
Name = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-lb"
Environment = var.ENVIRONMENT
}
}

# Target group for the load balancer
resource "aws_lb_target_group" "example" {
name = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-tg"
target_type = "instance"
port = 80
protocol = "HTTP"
vpc_id = var.VPC_ID

health_check {
interval = 30
timeout = 5
healthy_threshold = 2
unhealthy_threshold = 2
path = "/"
protocol = "HTTP"
}

tags = {
Name = "${var.PROJECT_NAME}-${var.ENVIRONMENT}-tg"
Environment = var.ENVIRONMENT
}
}
4 changes: 4 additions & 0 deletions infra/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Output for the load balancer DNS name
output "elb_dns_name" {
value = aws_lb.example.dns_name
}
14 changes: 14 additions & 0 deletions infra/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_version = ">= 1.0"

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

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

# Variables for dynamic naming
variable "PROJECT_NAME" {
description = "Name of the project"
default = "droneshield"
}

variable "ENVIRONMENT" {
description = "Deployment environment (e.g., dev, staging, prod)"
default = "dev"
}

variable "VPC_ID" {
description = "ID of the VPC"
type = string
}

0 comments on commit 1a7467a

Please sign in to comment.