diff --git a/dev/main.tf b/dev/main.tf new file mode 100644 index 0000000..4df5b9a --- /dev/null +++ b/dev/main.tf @@ -0,0 +1,3 @@ +module "dev" { + source = "../modules/blog" +} diff --git a/dev/outputs.tf b/dev/outputs.tf new file mode 100644 index 0000000..24ca406 --- /dev/null +++ b/dev/outputs.tf @@ -0,0 +1,3 @@ +output "environment_url" { + value = module.dev.environment_url +} diff --git a/providers.tf b/dev/providers.tf similarity index 100% rename from providers.tf rename to dev/providers.tf diff --git a/main.tf b/main.tf deleted file mode 100644 index 9b32ce0..0000000 --- a/main.tf +++ /dev/null @@ -1,24 +0,0 @@ -data "aws_ami" "app_ami" { - most_recent = true - - filter { - name = "name" - values = ["bitnami-tomcat-*-x86_64-hvm-ebs-nami"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["979382823631"] # Bitnami -} - -resource "aws_instance" "web" { - ami = data.aws_ami.app_ami.id - instance_type = "t3.nano" - - tags = { - Name = "HelloWorld" - } -} diff --git a/modules/blog/main.tf b/modules/blog/main.tf new file mode 100644 index 0000000..81c5654 --- /dev/null +++ b/modules/blog/main.tf @@ -0,0 +1,95 @@ +data "aws_ami" "app_ami" { + most_recent = true + + filter { + name = "name" + values = [var.ami_filter.name] + } + + filter { + name = "virtualization-type" + values = ["hvm"] + } + + owners = [var.ami_filter.owner] +} + + +module "blog_vpc" { + source = "terraform-aws-modules/vpc/aws" + + name = var.environment.name + cidr = "${var.environment.network_prefix}.0.0/16" + + azs = ["us-west-2a","us-west-2b","us-west-2c"] + public_subnets = ["${var.environment.network_prefix}.101.0/24", "${var.environment.network_prefix}.102.0/24", "${var.environment.network_prefix}.103.0/24"] + + enable_nat_gateway = true + + tags = { + Terraform = "true" + Environment = var.environment.name + } +} + + +module "blog_autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "6.5.2" + + name = "${var.environment.name}-blog" + + min_size = var.asg_min + max_size = var.asg_max + 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 +} + +module "blog_alb" { + source = "terraform-aws-modules/alb/aws" + version = "~> 6.0" + + name = "${var.environment.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 = "${var.environment.name}-" + backend_protocol = "HTTP" + backend_port = 80 + target_type = "instance" + } + ] + + http_tcp_listeners = [ + { + port = 80 + protocol = "HTTP" + target_group_index = 0 + } + ] + + tags = { + Environment = var.environment.name + } +} + +module "blog_sg" { + source = "terraform-aws-modules/security-group/aws" + version = "4.13.0" + + vpc_id = module.blog_vpc.vpc_id + name = "${var.environment.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"] +} diff --git a/modules/blog/outputs.tf b/modules/blog/outputs.tf new file mode 100644 index 0000000..f00b1fe --- /dev/null +++ b/modules/blog/outputs.tf @@ -0,0 +1,3 @@ +output "environment_url" { + value = module.blog_alb.lb_dns_name +} diff --git a/modules/blog/variables.tf b/modules/blog/variables.tf new file mode 100644 index 0000000..c9dbbce --- /dev/null +++ b/modules/blog/variables.tf @@ -0,0 +1,42 @@ +variable "instance_type" { + description = "Type of EC2 instance to provision" + default = "t3.nano" +} + +variable "ami_filter" { + description = "Name filter and owner for AMI" + + type = object ({ + name = string + owner = string + }) + + default = { + name = "bitnami-tomcat-*-x86_64-hvm-ebs-nami" + owner = "979382823631" # Bitnami + } +} + +variable "environment" { + description = "Deployment environment" + + type = object ({ + name = string + network_prefix = string + }) + default = { + name = "dev" + network_prefix = "10.0" + } +} + + +variable "asg_min" { + description = "Minimum instance count for the ASG" + default = 1 +} + +variable "asg_max" { + description = "Maximum instance count for the ASG" + default = 2 +} diff --git a/outputs.tf b/outputs.tf deleted file mode 100644 index b35171b..0000000 --- a/outputs.tf +++ /dev/null @@ -1,7 +0,0 @@ -#output "instance_ami" { -# value = aws_instance.web.ami -#} - -#output "instance_arn" { -# value = aws_instance.web.arn -#} diff --git a/prod/main.tf b/prod/main.tf new file mode 100644 index 0000000..60928d6 --- /dev/null +++ b/prod/main.tf @@ -0,0 +1,9 @@ +module "prod" { + source = "../modules/blog" + environment = { + name = "prod" + network_prefix = "10.2" + } + asg_min = 1 + asg_max = 10 +} diff --git a/prod/outputs.tf b/prod/outputs.tf new file mode 100644 index 0000000..9309dc1 --- /dev/null +++ b/prod/outputs.tf @@ -0,0 +1,3 @@ +output "environment_url" { + value = module.prod.environment_url +} diff --git a/prod/providers.tf b/prod/providers.tf new file mode 100644 index 0000000..c41e365 --- /dev/null +++ b/prod/providers.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} + +provider "aws" { + region = "us-west-2" +} diff --git a/qa/main.tf b/qa/main.tf new file mode 100644 index 0000000..eeee717 --- /dev/null +++ b/qa/main.tf @@ -0,0 +1,9 @@ +module "qa" { + source = "../modules/blog" + environment = { + name = "qa" + network_prefix = "10.1" + } + asg_min = 0 + asg_max = 0 +} diff --git a/qa/outputs.tf b/qa/outputs.tf new file mode 100644 index 0000000..4e77e70 --- /dev/null +++ b/qa/outputs.tf @@ -0,0 +1,3 @@ +output "environment_url" { + value = module.qa.environment_url +} diff --git a/qa/providers.tf b/qa/providers.tf new file mode 100644 index 0000000..c41e365 --- /dev/null +++ b/qa/providers.tf @@ -0,0 +1,11 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + } + } +} + +provider "aws" { + region = "us-west-2" +} diff --git a/variables.tf b/variables.tf deleted file mode 100644 index c750667..0000000 --- a/variables.tf +++ /dev/null @@ -1,4 +0,0 @@ -#variable "instance_type" { -# description = "Type of EC2 instance to provision" -# default = "t3.nano" -#}