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
3 changes: 3 additions & 0 deletions environments/dev/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "dev" {
source = "../../modules/blog"
}
24 changes: 24 additions & 0 deletions environments/dev/moved.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
moved {
from = module.blog_vpc
to = module.dev.module.blog_vpc
}

moved {
from = module.blog_sg
to = module.dev.module.blog_sg
}

moved {
from = module.blog_alb
to = module.dev.module.blog_alb
}

moved {
from = aws_lb_target_group.blog
to = module.dev.aws_lb_target_group.blog
}

moved {
from = module.blog_autoscaling
to = module.dev.module.blog_autoscaling
}
File renamed without changes.
11 changes: 11 additions & 0 deletions environments/qa/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module "qa" {
source = "../../modules/blog"

environment = {
name = "qa"
network_prefix = "10.1"
}

min_size = 0
max_size = 0
}
3 changes: 3 additions & 0 deletions environments/qa/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "environment_url" {
value = module.qa.environment_url
}
11 changes: 11 additions & 0 deletions environments/qa/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}

provider "aws" {
region = "us-west-2"
}
24 changes: 0 additions & 24 deletions main.tf

This file was deleted.

110 changes: 110 additions & 0 deletions modules/blog/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
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] # Bitnami
}

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"]
private_subnets = ["${var.environment.network_prefix}.1.0/24", "${var.environment.network_prefix}.2.0/24", "${var.environment.network_prefix}.3.0/24"]
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_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "5.3.1"
name = "${var.environment.name}-blog"

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 = "${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 = "blog-"
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
}
}

resource "aws_lb_target_group" "blog" {
name = "${var.environment.name}-blog"
port = 80
protocol = "HTTP"
vpc_id = module.blog_vpc.vpc_id
}

module "blog_autoscaling" {
source = "terraform-aws-modules/autoscaling/aws"
version = "9.2.1"

name="${var.environment.name}-blog"

min_size = var.min_size
max_size = var.max_size

vpc_zone_identifier = module.blog_vpc.public_subnets

launch_template_name = "blog"
security_groups = [module.blog_sg.security_group_id]
instance_type = var.instance_type
image_id = data.aws_ami.app_ami.id

traffic_source_attachments = {
"${var.environment.name}-blog-alb" ={
traffic_source_identifier = aws_lb_target_group.blog.arn
}
}
}
3 changes: 3 additions & 0 deletions modules/blog/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "environment_url"{
value = module.blog_alb.lb_dns_name
}
41 changes: 41 additions & 0 deletions modules/blog/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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-nginx-*-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 "min_size" {
description = "Minimum number of intances in the ASG"
default = 1
}

variable "max_size" {
description = "Miximum number of intances in the ASG"
default = 2
}

7 changes: 0 additions & 7 deletions outputs.tf

This file was deleted.

4 changes: 0 additions & 4 deletions variables.tf

This file was deleted.