Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 74 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,90 @@ data "aws_ami" "app_ami" {

filter {
name = "name"
values = ["bitnami-tomcat-*-x86_64-hvm-ebs-nami"]
values = [vvar.ami_filter.name]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}

owners = ["979382823631"] # Bitnami
owners = [var.ami_filter.owner]
}

resource "aws_instance" "web" {
ami = data.aws_ami.app_ami.id
instance_type = "t3.nano"
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", "10.0.102.0/24", "var.environment.network_prefix.103.0/24"]

tags = {
Name = "HelloWorld"
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_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"

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"]
}
12 changes: 6 additions & 6 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 42 additions & 4 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
#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"
}

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
}