diff --git a/main.tf b/main.tf index 9b32ce0..f10d05e 100644 --- a/main.tf +++ b/main.tf @@ -14,11 +14,86 @@ 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 "blog_vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "dev" + 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 = { - Name = "HelloWorld" + Terraform = "true" + Environment = "dev" } } + +module "autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "8.0.0" + + name = "blog" + min_size = 1 + max_size = 2 + + vpc_zone_identifier = module.blog_vpc.public_subnets + target_group_arns = module.blog_alb.target_group_arns + security_groups = [module.blog_sg.security_group_id] + + image_id = data.aws_ami.app_ami.id + instance_type = var.instance_type +} + +module "blog_alb" { + source = "terraform-aws-modules/alb/aws" + + name = "blog-alb" + vpc_id = module.blog_vpc.vpc_id + subnets = module.blog_vpc.public_subnets + security_groups = [module.blog_sg.security_group_id] + + listeners = { + ex-http-https-redirect = { + port = 80 + protocol = "HTTP" + redirect = { + port = "443" + protocol = "HTTPS" + status_code = "HTTP_301" + } + } + } + + target_groups = { + ex-instance = { + name_prefix = "blog" + protocol = "HTTP" + port = 80 + target_type = "instance" + target_id = aws_instance.blog.id + } + } + + tags = { + Environment = "Development" + Project = "Example" + } +} + +module "blog_sg" { + source = "terraform-aws-modules/security-group/aws" + version = "4.13.0" + + vpc_id = module.blog_vpc.vpc_id + name = "blog" + ingress_rules = ["https-443-tcp","http-80-tcp"] + ingress_cidr_blocks = ["0.0.0.0/0"] + egress_rules = ["all-all"] + egress_cidr_blocks = ["0.0.0.0/0"] +} \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index b35171b..e69de29 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,7 +0,0 @@ -#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 c750667..60856bc 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" +}