diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..a490b614a3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] +} \ No newline at end of file diff --git a/main.tf b/main.tf index 9b32ce06bb..9d9a7bcef7 100644 --- a/main.tf +++ b/main.tf @@ -1,24 +1,184 @@ -data "aws_ami" "app_ami" { +# -------------------------------------------------- +# AMI Bitnami Tomcat +# -------------------------------------------------- +data "aws_ami" "tomcat" { most_recent = true filter { name = "name" values = ["bitnami-tomcat-*-x86_64-hvm-ebs-nami"] } + + + - filter { - name = "virtualization-type" - values = ["hvm"] - } + + - owners = ["979382823631"] # Bitnami + owners = ["979382823631"] + } -resource "aws_instance" "web" { - ami = data.aws_ami.app_ami.id - instance_type = "t3.nano" +# -------------------------------------------------- +# VPC +# -------------------------------------------------- +module "vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "blog-vpc" + cidr = "10.0.0.0/16" + + azs = ["us-west-2a", "us-west-2b"] + public_subnets = ["10.0.1.0/24", "10.0.2.0/24"] + + enable_nat_gateway = false tags = { - Name = "HelloWorld" + + Environment = "dev" + } +} + +# -------------------------------------------------- +# SECURITY GROUP - ALB +# -------------------------------------------------- +module "alb_sg" { + source = "terraform-aws-modules/security-group/aws" + version = "5.3.1" + + name = "alb-sg" + vpc_id = module.vpc.vpc_id + + + + + + + + ingress_rules = ["http-80-tcp", "https-443-tcp"] + ingress_cidr_blocks = ["0.0.0.0/0"] + + egress_rules = ["all-all"] + +} + +# -------------------------------------------------- +# SECURITY GROUP - EC2 +# -------------------------------------------------- +module "ec2_sg" { + source = "terraform-aws-modules/security-group/aws" + version = "5.3.1" + + name = "ec2-sg" + vpc_id = module.vpc.vpc_id + + ingress_with_source_security_group_id = [ + { + rule = "http-8080-tcp" + source_security_group_id = module.alb_sg.security_group_id + } + ] + + egress_rules = ["all-all"] +} + +# -------------------------------------------------- +# APPLICATION LOAD BALANCER +# -------------------------------------------------- +module "alb" { + source = "terraform-aws-modules/alb/aws" + + name = "blog-alb" + vpc_id = module.vpc.vpc_id + subnets = module.vpc.public_subnets + + + security_groups = [module.alb_sg.security_group_id] + + target_groups = { + blog = { + backend_protocol = "HTTP" + backend_port = 8080 + target_type = "instance" + health_check = { + path = "/" + } + } + } + + listeners = { + http = { + port = 80 + protocol = "HTTP" + redirect = { + port = "443" + protocol = "HTTPS" + status_code = "HTTP_301" + } + } + + + + + + + + + + + + + + + + + + + + + + + + https = { + port = 443 + protocol = "HTTPS" + certificate_arn = var.acm_certificate_arn + + forward = { + target_group_key = "blog" + } + } } } + +# -------------------------------------------------- +# AUTO SCALING GROUP +# -------------------------------------------------- +module "asg" { + source = "terraform-aws-modules/autoscaling/aws" + version = "9.0.2" + + name = "blog-asg" + + + min_size = 1 + max_size = 2 + desired_capacity = 1 + + + + + vpc_zone_identifier = module.vpc.public_subnets + security_groups = [module.ec2_sg.security_group_id] + + image_id = data.aws_ami.tomcat.id + instance_type = "t3.micro" + + + + + + target_group_arns = [ + module.alb.target_groups["blog"].arn + ] +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index b35171bef1..8b13789179 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,7 +1 @@ -#output "instance_ami" { -# value = aws_instance.web.ami -#} -#output "instance_arn" { -# value = aws_instance.web.arn -#} diff --git a/variables.tf b/variables.tf index c750667e0f..154a71f121 100644 --- a/variables.tf +++ b/variables.tf @@ -1,4 +1,4 @@ -#variable "instance_type" { -# description = "Type of EC2 instance to provision" -# default = "t3.nano" -#} +variable "instance_type" { +description = "Type of EC2 instance to provision" +default = "t3.micro" +}