From 56f6ff7f348e55399faec122be911dd89dd4e7dc Mon Sep 17 00:00:00 2001 From: Josh Samuelson Date: Tue, 6 Sep 2022 21:50:00 -0700 Subject: [PATCH 1/2] 03_04 solutions --- main.tf | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- outputs.tf | 12 ++++++------ variables.tf | 8 ++++---- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/main.tf b/main.tf index 9b32ce06bb..8429eccef9 100644 --- a/main.tf +++ b/main.tf @@ -14,11 +14,53 @@ 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 +} + +resource "aws_instance" "blog" { + ami = data.aws_ami.app_ami.id + instance_type = var.instance_type + vpc_security_group_ids = [aws_security_group.blog.id] tags = { - Name = "HelloWorld" + Name = "Learning Terraform" } } + +resource "aws_security_group" "blog" { + name = "blog" + 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 +} diff --git a/outputs.tf b/outputs.tf index b35171bef1..7e9410b55c 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/variables.tf b/variables.tf index c750667e0f..48b7a75904 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" +} \ No newline at end of file From d80bea33da56998a1c63700a1e4248db4b149bd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ram=C3=B3n=20Yuri=20Danzos=20Garc=C3=ADa?= <89027050+danzosr@users.noreply.github.com> Date: Wed, 24 Dec 2025 14:36:49 -0700 Subject: [PATCH 2/2] Refactor to use variables --- main.tf | 102 ++++++++++++++++++++++++++++++++------------------- variables.tf | 40 +++++++++++++++++++- 2 files changed, 103 insertions(+), 39 deletions(-) diff --git a/main.tf b/main.tf index 8429eccef9..68c8e01165 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 = [vvar.ami_filter.name] } filter { @@ -11,56 +11,82 @@ data "aws_ami" "app_ami" { values = ["hvm"] } - owners = ["979382823631"] # Bitnami + owners = [var.ami_filter.owner] } -data "aws_vpc" "default" { - default = true -} +module "blog_vpc" { + source = "terraform-aws-modules/vpc/aws" -resource "aws_instance" "blog" { - ami = data.aws_ami.app_ami.id - instance_type = var.instance_type - vpc_security_group_ids = [aws_security_group.blog.id] + name = var.environment.name + cidr = "${var.environment.network_prefix}.0.0/16" - tags = { - Name = "Learning Terraform" - } -} + azs = ["us-west-2a","us-west-2b","us-west-2c"] + public_subnets = ["var.environment.network_prefix.101.0/24", "10.0.102.0/24", "var.environment.network_prefix.103.0/24"] -resource "aws_security_group" "blog" { - name = "blog" tags = { Terraform = "true" + Environment = var.environment.name } - 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 -} +module "blog_autoscaling" { + source = "terraform-aws-modules/autoscaling/aws" + version = "6.5.2" + + name = "${var.environment.name}-blog" -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 + min_size = var.asg_min_size + max_size = var.asg_max_size + 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 = "blog-alb" + + load_balancer_type = "application" -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 + 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"] +} \ No newline at end of file diff --git a/variables.tf b/variables.tf index 48b7a75904..f3ee59ae1c 100644 --- a/variables.tf +++ b/variables.tf @@ -1,4 +1,42 @@ variable "instance_type" { description = "Type of EC2 instance to provision" default = "t3.nano" -} \ No newline at end of file +} + +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" + owners = "979382823631" # Bitnami + } +} + +variable "environment" + description = "Development Environment" + + type = object({ + name = string + network_prefix = string + }) + + default = { + name = "dev" + network_prefix = "10.0" + } + +variable "asg_min_size" { + description = "Minimun number of instances in the ASG" + default = 1 +} + +variable "asg_max_size" { + description = "Maximum number of instances in the ASG" + default = 2 +} +