diff --git a/main.tf b/main.tf index 9b32ce06bb..a72500f632 100644 --- a/main.tf +++ b/main.tf @@ -14,11 +14,108 @@ data "aws_ami" "app_ami" { owners = ["979382823631"] # Bitnami } -resource "aws_instance" "web" { - ami = data.aws_ami.app_ami.id - instance_type = "t3.nano" +data "aws_vpc" "default"{ + default = true +} + +module "module_dev_vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "dev_vpc" + cidr = "10.0.0.0/16" + + azs = ["us-west-2a", "us-west-2b", "us-west-2c"] + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] + + tags = { + Terraform = "true" + Environment = "dev" + } +} + +module "autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "9.0.2" + name = "autoscaling" + + min_size = 1 + max_size = 2 + + vpc_zone_identifier = module.dev_vpc.public_subnets + target_group_arns = module.dev_alb.target_group_arns + security_groups = [module.module_security_group.security_group_id] + + image_id = data.aws_ami.app_ami.id + instance_type = var.instance_type +} + +module "dev_alb" { + source = "terraform-aws-modules/alb/aws" + load_balancer_type ="application" + + name = "dev-alb" + vpc_id = module.module_dev_vpc.vpc_id + subnets = module.module_dev_vpc.public_subnets + security_groups = [module.module_security_group.security_group_id] + + resource "aws_lb_listener_rule" "health_check" { + listener_arn = aws_lb_listener.front_end.arn + + action { + type = "fixed-response" + + fixed_response { + content_type = "text/plain" + message_body = "HEALTHY" + status_code = "200" + } + } + + condition { + query_string { + key = "health" + value = "check" + } + + query_string { + value = "bar" + } + } +} + listeners = { + http_tcp_listeners = { + port = 80 + protocol = "HTTP" + target_group_index = 0 + } + } + + + target_groups = { + ex-instance = { + name_prefix = "blog" + protocol = "HTTP" + port = 80 + target_type = "instance" + target_id = aws_instance.web.id + } + } tags = { - Name = "HelloWorld" + Environment = "dev" } } + +module "module_security_group"{ + name = "module_security_group" + source = "terraform-aws-modules/security-group/aws" + version = "5.3.1" + + vpc_id = module.module_dev_vpc.vpc_id + + ingress_rules = ["http-80-tcp", "https-443-tcp"] + ingress_cidr_blocks = ["0.0.0.0/0"] + + egress_rules = ["all-all"] + egress_cidr_blocks = ["0.0.0.0/0"] +} diff --git a/outputs.tf b/outputs.tf index b35171bef1..c429b19b48 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,7 +1,7 @@ -#output "instance_ami" { -# value = aws_instance.web.ami -#} +output "instance_ami" { + value = aws_instance.web.ami +} -#output "instance_arn" { -# value = aws_instance.web.arn -#} +output "instance_arn" { + value = aws_instance.web.arn +} diff --git a/variables.tf b/variables.tf index c750667e0f..60856bc925 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.nano" +}