Skip to content

Commit

Permalink
add terraform 0.12 support for 04 example
Browse files Browse the repository at this point in the history
  • Loading branch information
diodonfrost committed Sep 20, 2019
1 parent a6798f1 commit 09bcd35
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 89 deletions.
9 changes: 5 additions & 4 deletions 04-instance-with-loadbalancer/00-params.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ variable "vpc_cidr" {
#### HTTP PARAMS
variable "network_http" {
default = {
subnet_name = "subnet_http"
cidr = "192.168.1.0/24"
subnet_name = "subnet_http"
cidr = "192.168.1.0/24"
}
}

Expand All @@ -29,12 +29,13 @@ variable "desired_capacity_http" {
#### DB PARAMS
variable "network_db" {
default = {
subnet_name = "subnet_db"
cidr = "192.168.2.0/24"
subnet_name = "subnet_db"
cidr = "192.168.2.0/24"
}
}

# Set number of instance
variable "desired_capacity_db" {
default = 3
}

1 change: 1 addition & 0 deletions 04-instance-with-loadbalancer/010-ssh-key.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ resource "aws_key_pair" "user_key" {
key_name = "user-key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 [email protected]"
}

31 changes: 16 additions & 15 deletions 04-instance-with-loadbalancer/020-network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@

# VPC creation
resource "aws_vpc" "terraform" {
cidr_block = "${var.vpc_cidr}"
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = {
Name = "vpc-terraform"
tags = {
Name = "vpc-terraform"
}
}

# http subnet configuration
resource "aws_subnet" "http" {
vpc_id = "${aws_vpc.terraform.id}"
cidr_block = "${var.network_http["cidr"]}"
tags = {
Name = "subnet-http"
vpc_id = aws_vpc.terraform.id
cidr_block = var.network_http["cidr"]
tags = {
Name = "subnet-http"
}
depends_on = ["aws_internet_gateway.gw"]
depends_on = [aws_internet_gateway.gw]
}

# db subnet configuration
resource "aws_subnet" "db" {
vpc_id = "${aws_vpc.terraform.id}"
cidr_block = "${var.network_db["cidr"]}"
tags = {
Name = "subnet-db"
vpc_id = aws_vpc.terraform.id
cidr_block = var.network_db["cidr"]
tags = {
Name = "subnet-db"
}
depends_on = ["aws_internet_gateway.gw"]
depends_on = [aws_internet_gateway.gw]
}

# External gateway configuration
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.terraform.id}"
tags = {
vpc_id = aws_vpc.terraform.id
tags = {
Name = "internet-gateway"
}
}

19 changes: 10 additions & 9 deletions 04-instance-with-loadbalancer/030-security-group.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
resource "aws_security_group" "administration" {
name = "administration"
description = "Allow default administration service"
vpc_id = "${aws_vpc.terraform.id}"
tags = {
Name = "administration"
vpc_id = aws_vpc.terraform.id
tags = {
Name = "administration"
}

# Open ssh port
Expand Down Expand Up @@ -38,9 +38,9 @@ resource "aws_security_group" "administration" {
resource "aws_security_group" "web" {
name = "web"
description = "Allow web incgress trafic"
vpc_id = "${aws_vpc.terraform.id}"
tags = {
Name = "web"
vpc_id = aws_vpc.terraform.id
tags = {
Name = "web"
}

# http port
Expand Down Expand Up @@ -72,9 +72,9 @@ resource "aws_security_group" "web" {
resource "aws_security_group" "db" {
name = "db"
description = "Allow db incgress trafic"
vpc_id = "${aws_vpc.terraform.id}"
tags = {
Name = "db"
vpc_id = aws_vpc.terraform.id
tags = {
Name = "db"
}

# db port
Expand All @@ -93,3 +93,4 @@ resource "aws_security_group" "db" {
cidr_blocks = ["0.0.0.0/0"]
}
}

33 changes: 18 additions & 15 deletions 04-instance-with-loadbalancer/060-instance-http.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

# Create instance
resource "aws_instance" "http" {
count = "${var.desired_capacity_http}"
ami = "${var.ami}"
instance_type = "t2.micro"
key_name = "${aws_key_pair.user_key.key_name}"
vpc_security_group_ids = ["${aws_security_group.administration.id}",
"${aws_security_group.web.id}"]
subnet_id = "${aws_subnet.http.id}"
user_data = "${file("scripts/first-boot-http.sh")}"
tags = {
Name = "http-instance${count.index}"
count = var.desired_capacity_http
ami = var.ami
instance_type = "t2.micro"
key_name = aws_key_pair.user_key.key_name
vpc_security_group_ids = [
aws_security_group.administration.id,
aws_security_group.web.id,
]
subnet_id = aws_subnet.http.id
user_data = file("scripts/first-boot-http.sh")
tags = {
Name = "http-instance${count.index}"
}
}

# Attach floating ip on instance http
resource "aws_eip" "public_http" {
count = "${var.desired_capacity_http}"
count = var.desired_capacity_http
vpc = true
instance = "${element(aws_instance.http.*.id, count.index)}"
depends_on = ["aws_internet_gateway.gw"]
tags = {
Name = "public-http${count.index}"
instance = element(aws_instance.http.*.id, count.index)
depends_on = [aws_internet_gateway.gw]
tags = {
Name = "public-http${count.index}"
}
}

23 changes: 13 additions & 10 deletions 04-instance-with-loadbalancer/061-instance-db.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

# Create instance
resource "aws_instance" "db" {
count = "${var.desired_capacity_db}"
ami = "${var.ami}"
instance_type = "t2.micro"
key_name = "${aws_key_pair.user_key.key_name}"
vpc_security_group_ids = ["${aws_security_group.administration.id}",
"${aws_security_group.db.id}"]
subnet_id = "${aws_subnet.db.id}"
user_data = "${file("scripts/first-boot-db.sh")}"
tags = {
Name = "db-instance${count.index}"
count = var.desired_capacity_db
ami = var.ami
instance_type = "t2.micro"
key_name = aws_key_pair.user_key.key_name
vpc_security_group_ids = [
aws_security_group.administration.id,
aws_security_group.db.id,
]
subnet_id = aws_subnet.db.id
user_data = file("scripts/first-boot-db.sh")
tags = {
Name = "db-instance${count.index}"
}
}

53 changes: 27 additions & 26 deletions 04-instance-with-loadbalancer/070-loadbalancer.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
# Create network load balancer
resource "aws_lb" "http" {
name = "http-lb"
subnets = ["${aws_subnet.http.*.id}"]
subnets = aws_subnet.http.*.id
load_balancer_type = "network"
tags {
Name = "http-lb"
tags = {
Name = "http-lb"
}
}

# Create listener for load balancer http
resource "aws_lb_listener" "http" {
load_balancer_arn = "${aws_lb.http.id}"
port = "80"
protocol = "TCP"
load_balancer_arn = aws_lb.http.id
port = "80"
protocol = "TCP"
default_action {
target_group_arn = "${aws_lb_target_group.http.id}"
target_group_arn = aws_lb_target_group.http.id
type = "forward"
}
}
Expand All @@ -28,17 +28,17 @@ resource "aws_lb_target_group" "http" {
name = "http-lb-target-group"
port = 80
protocol = "TCP"
vpc_id = "${aws_vpc.terraform.id}"
tags {
Name = "http-lb-target-group"
vpc_id = aws_vpc.terraform.id
tags = {
Name = "http-lb-target-group"
}
}

# Add instance to load balancer
resource "aws_lb_target_group_attachment" "http" {
count = "${var.desired_capacity_http}"
target_group_arn = "${aws_lb_target_group.http.arn}"
target_id = "${element(aws_instance.http.*.id, count.index)}"
count = var.desired_capacity_http
target_group_arn = aws_lb_target_group.http.arn
target_id = element(aws_instance.http.*.id, count.index)
port = 80
}

Expand All @@ -47,21 +47,21 @@ resource "aws_lb_target_group_attachment" "http" {
# Create a new load balancer for db instance
resource "aws_lb" "db" {
name = "db-lb"
subnets = ["${aws_subnet.db.*.id}"]
subnets = aws_subnet.db.*.id
load_balancer_type = "network"
internal = "true"
tags {
Name = "db-lb"
tags = {
Name = "db-lb"
}
}

# Create listener for load balancer http
resource "aws_lb_listener" "db" {
load_balancer_arn = "${aws_lb.db.id}"
port = "3306"
protocol = "TCP"
load_balancer_arn = aws_lb.db.id
port = "3306"
protocol = "TCP"
default_action {
target_group_arn = "${aws_lb_target_group.db.id}"
target_group_arn = aws_lb_target_group.db.id
type = "forward"
}
}
Expand All @@ -71,16 +71,17 @@ resource "aws_lb_target_group" "db" {
name = "db-lb-target-group"
port = 3306
protocol = "TCP"
vpc_id = "${aws_vpc.terraform.id}"
tags {
Name = "db-lb-target-group"
vpc_id = aws_vpc.terraform.id
tags = {
Name = "db-lb-target-group"
}
}

# Add instance to load balancer
resource "aws_lb_target_group_attachment" "db" {
count = "${var.desired_capacity_db}"
target_group_arn = "${aws_lb_target_group.db.arn}"
target_id = "${element(aws_instance.db.*.id, count.index)}"
count = var.desired_capacity_db
target_group_arn = aws_lb_target_group.db.arn
target_id = element(aws_instance.db.*.id, count.index)
port = 80
}

13 changes: 7 additions & 6 deletions 04-instance-with-loadbalancer/071-routing-table.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@

# Routing table configuration
resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.terraform.id}"
vpc_id = aws_vpc.terraform.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
gateway_id = aws_internet_gateway.gw.id
}
}

# Associate http route
resource "aws_route_table_association" "http" {
subnet_id = "${aws_subnet.http.id}"
route_table_id = "${aws_route_table.public.id}"
subnet_id = aws_subnet.http.id
route_table_id = aws_route_table.public.id
}

# Associate db route
resource "aws_route_table_association" "db" {
subnet_id = "${aws_subnet.db.id}"
route_table_id = "${aws_route_table.public.id}"
subnet_id = aws_subnet.db.id
route_table_id = aws_route_table.public.id
}

9 changes: 5 additions & 4 deletions 04-instance-with-loadbalancer/100-outputs.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# Display dns information

output "http_ip" {
value = "${aws_eip.public_http.*.public_ip}"
value = aws_eip.public_http.*.public_ip
}

output "db_hostname" {
value = "${aws_instance.db.*.private_dns}"
value = aws_instance.db.*.private_dns
}

output "lb_hostname_http" {
value = "${aws_lb.http.dns_name}"
value = aws_lb.http.dns_name
}

output "lb_hostname_db" {
value = "${aws_lb.db.dns_name}"
value = aws_lb.db.dns_name
}

4 changes: 4 additions & 0 deletions 04-instance-with-loadbalancer/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}

0 comments on commit 09bcd35

Please sign in to comment.