diff --git a/infra/main.tf b/infra/main.tf new file mode 100644 index 0000000..00496d1 --- /dev/null +++ b/infra/main.tf @@ -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 + } +} diff --git a/infra/output.tf b/infra/output.tf new file mode 100644 index 0000000..bf70504 --- /dev/null +++ b/infra/output.tf @@ -0,0 +1,4 @@ +# Output for the load balancer DNS name +output "elb_dns_name" { + value = aws_lb.example.dns_name +} diff --git a/infra/provider.tf b/infra/provider.tf new file mode 100644 index 0000000..58c2cfa --- /dev/null +++ b/infra/provider.tf @@ -0,0 +1,14 @@ +terraform { + required_version = ">= 1.0" + + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 5.0" + } + } +} + +provider "aws" { + region = var.REGION +} diff --git a/infra/terraform.tfvars b/infra/terraform.tfvars new file mode 100644 index 0000000..265f81e --- /dev/null +++ b/infra/terraform.tfvars @@ -0,0 +1 @@ +REGION = "ap-southeast-2" diff --git a/infra/variables.tf b/infra/variables.tf new file mode 100644 index 0000000..6d88088 --- /dev/null +++ b/infra/variables.tf @@ -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 +}