diff --git a/main.tf b/main.tf index 9b32ce06bb..27ca9bb9ff 100644 --- a/main.tf +++ b/main.tf @@ -3,7 +3,7 @@ data "aws_ami" "app_ami" { filter { name = "name" - values = ["bitnami-tomcat-*-x86_64-hvm-ebs-nami"] + values = ["al2023-ami-2023.11.20260413.0-kernel-6.1-x86_64"] } filter { @@ -11,14 +11,144 @@ data "aws_ami" "app_ami" { values = ["hvm"] } - owners = ["979382823631"] # Bitnami + owners = ["137112412989"] # Amazon } -resource "aws_instance" "web" { - ami = data.aws_ami.app_ami.id - instance_type = "t3.nano" +# resource "aws_instance" "blog" { +# ami = data.aws_ami.app_ami.id +# instance_type = var.instance_type +# subnet_id = module.blog_vpc.public_subnets[0] +# vpc_security_group_ids = [module.blog_sg.security_group_id] +# tags = { +# Name = "HelloWorld" +# } +# } + +# data "aws_vpc" "blog" { +# default = true +# } + +module "blog_vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = "dev" + cidr = "10.0.0.0/16" + + azs = ["us-east-1a", "us-east-1b", "us-east-1c"] + private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] + public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] + + enable_nat_gateway = true + + + tags = { + Terraform = "true" + Environment = "dev" + } +} + +module "blog_sg" { +source = "terraform-aws-modules/security-group/aws" +version = "5.3.1" +name = "blog_new" + +# vpc_id = data.aws_vpc.blog.id +vpc_id = module.blog_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"] +} + + +module "blog_alb" { + source = "terraform-aws-modules/alb/aws" + version = "~> 6.0" + name = "blog-alb" + load_balancer_type = "application" + vpc_id = module.blog_vpc.vpc_id + subnets = module.blog_vpc.public_subnets + security_groups = [module.blog_sg.security_group_id] + target_groups = [ + { + name_prefix = "blog-" + backend_protocol = "HTTP" + backend_port = 80 + target_type = "instance" + } + ] + http_tcp_listeners = [ + { + port = 80 + protocol = "HTTP" + target_group_index = 0 + } + ] tags = { - Name = "HelloWorld" + Environment = "dev" } } + +# resource "aws_lb_target_group_attachment" "blog" { +# target_group_arn = aws_lb_target_group.blog.arn +# target_id = aws_instance.blog.id +# port = 80 +# } + +module "blog_autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "6.5.2" + 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] + instance_type = var.instance_type + image_id = data.aws_ami.app_ami.id +} + + +# resource "aws_security_group" "blog" { +# name = "blog" +# description = "Allow http and https in. Allow everything out" + +# tags = { +# terraform = "true" +# } + +# vpc_id = data.aws_vpc.default.id +# } + +# resource "aws_security_group_rule" "blog_http_in" { +# type = "ingress" +# from_port = 80 +# to_port = 80 +# protocol = "tcp" +# cidr_blocks = ["0.0.0.0/0"] + +# security_group_id = aws_security_group.blog.id +# } + +# resource "aws_security_group_rule" "blog_https_in" { +# type = "ingress" +# from_port = 443 +# to_port = 443 +# protocol = "tcp" +# cidr_blocks = ["0.0.0.0/0"] + +# security_group_id = aws_security_group.blog.id +# } + +# resource "aws_security_group_rule" "blog_everything_out" { +# type = "egress" +# from_port = 0 +# to_port = 0 +# protocol = -1 +# cidr_blocks = ["0.0.0.0/0"] + +# security_group_id = aws_security_group.blog.id +# } \ No newline at end of file diff --git a/outputs.tf b/outputs.tf index b35171bef1..5e8a8f2054 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.blog.ami +# } -#output "instance_arn" { -# value = aws_instance.web.arn -#} +# output "instance_arn" { +# value = aws_instance.blog.arn +# } diff --git a/providers.tf b/providers.tf index c41e3650b5..2c9e2f0d37 100644 --- a/providers.tf +++ b/providers.tf @@ -7,5 +7,5 @@ terraform { } provider "aws" { - region = "us-west-2" + region = "us-east-1" } diff --git a/test.tf b/test.tf new file mode 100644 index 0000000000..e69de29bb2 diff --git a/variables.tf b/variables.tf index c750667e0f..57c835fdf0 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.small" +}