-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Scott Winkler
authored and
Scott Winkler
committed
Aug 22, 2020
0 parents
commit e108fdd
Showing
10 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Local .terraform directories | ||
**/.terraform/* | ||
|
||
# .tfstate files | ||
*.tfstate | ||
*.tfstate.* | ||
|
||
# Crash log files | ||
crash.log | ||
terraform | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# terraform-bluegreen-aws | ||
a repo for chapter 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# required file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#create instance and autoscaling group and lb listeners | ||
module "iam_instance_profile" { | ||
source = "scottwinkler/iip/aws" | ||
actions = ["logs:*"] | ||
} | ||
|
||
data "aws_ami" "ubuntu" { | ||
most_recent = true | ||
owners = ["099720109477"] | ||
|
||
filter { | ||
name = "name" | ||
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"] | ||
} | ||
} | ||
|
||
locals { | ||
html = templatefile("${path.module}/server/index.html", { NAME = join("-", [var.group, var.app_version]), BG_COLOR = var.group }) | ||
startup = templatefile("${path.module}/server/startup.sh", { HTML = local.html }) | ||
} | ||
|
||
resource "aws_launch_template" "webserver" { | ||
name_prefix = var.base.namespace | ||
image_id = data.aws_ami.ubuntu.id | ||
instance_type = "t3.micro" | ||
user_data = base64encode(local.startup) | ||
key_name = var.ssh_keypair | ||
iam_instance_profile { | ||
name = module.iam_instance_profile.name | ||
} | ||
vpc_security_group_ids = [var.base.sg.webserver] | ||
tags = { | ||
ResourceGroup = var.base.namespace | ||
} | ||
} | ||
|
||
resource "aws_autoscaling_group" "webserver" { | ||
name = "${var.base.namespace}-${var.group}-asg" | ||
min_size = 3 | ||
max_size = 3 | ||
//vpc_zone_identifier = var.base.vpc.private_subnets | ||
vpc_zone_identifier = var.base.vpc.public_subnets | ||
target_group_arns = var.group == "green" ? var.base.target_group_arns.green : var.base.target_group_arns.blue | ||
launch_template { | ||
id = aws_launch_template.webserver.id | ||
version = aws_launch_template.webserver.latest_version | ||
} | ||
tag { | ||
key = "ResourceGroup" | ||
value = var.base.namespace | ||
propagate_at_launch = true | ||
} | ||
tag { | ||
key = "Name" | ||
value = "${var.base.namespace}-${var.group}" | ||
propagate_at_launch = true | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<style> | ||
body { | ||
background-color: ${BG_COLOR}; | ||
color: white; | ||
} | ||
</style> | ||
|
||
<body> | ||
<h1>${NAME}</h1> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
mkdir -p /var/www && cd /var/www | ||
sudo tee /var/www/index.html > /dev/null <<EOF | ||
${HTML} | ||
EOF | ||
python3 -m http.server 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
variable "ssh_keypair" { | ||
default = null | ||
type = string | ||
} | ||
|
||
variable "group" { | ||
type = string | ||
} | ||
|
||
variable "app_version" { | ||
type = string | ||
} | ||
|
||
variable "base" { | ||
type = any | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
resource "random_string" "rand" { | ||
length = 24 | ||
special = false | ||
upper = false | ||
} | ||
|
||
locals { | ||
namespace = var.namespace != "" ? substr(join("-", [var.namespace, random_string.rand.result]), 0, 24) : random_string.rand.result | ||
} | ||
|
||
resource "aws_resourcegroups_group" "resourcegroups_group" { | ||
name = "${local.namespace}-group" | ||
|
||
resource_query { | ||
query = <<-JSON | ||
{ | ||
"ResourceTypeFilters": [ | ||
"AWS::AllSupported" | ||
], | ||
"TagFilters": [ | ||
{ | ||
"Key": "ResourceGroup", | ||
"Values": ["${local.namespace}"] | ||
} | ||
] | ||
} | ||
JSON | ||
} | ||
} | ||
# create vpc and load balancer and resource group | ||
data "aws_availability_zones" "available" {} | ||
|
||
module "vpc" { | ||
source = "terraform-aws-modules/vpc/aws" | ||
version = "2.17.0" | ||
name = "${local.namespace}-vpc" | ||
cidr = "10.0.0.0/16" | ||
azs = data.aws_availability_zones.available.names | ||
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] | ||
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] | ||
enable_nat_gateway = true | ||
single_nat_gateway = true | ||
} | ||
|
||
module "lb_sg" { | ||
source = "scottwinkler/sg/aws" | ||
vpc_id = module.vpc.vpc_id | ||
ingress_rules = [{ | ||
port = 80 | ||
cidr_blocks = ["0.0.0.0/0"] | ||
}] | ||
} | ||
|
||
module "webserver_sg" { | ||
source = "scottwinkler/sg/aws" | ||
vpc_id = module.vpc.vpc_id | ||
ingress_rules = [ | ||
{ | ||
port = 8080 | ||
security_groups = [module.lb_sg.security_group.id] | ||
}, | ||
{ | ||
port = 22 | ||
cidr_blocks = ["10.0.0.0/16"] | ||
} | ||
] | ||
} | ||
|
||
resource "aws_lb" "lb" { | ||
name = "${local.namespace}-lb" | ||
subnets = module.vpc.public_subnets | ||
security_groups = [module.lb_sg.security_group.id] | ||
tags = { | ||
ResourceGroup = local.namespace | ||
} | ||
} | ||
|
||
resource "aws_lb_target_group" "blue_target_group" { | ||
name = "${local.namespace}-blue" | ||
port = 8080 | ||
protocol = "HTTP" | ||
target_type = "instance" | ||
vpc_id = module.vpc.vpc_id | ||
tags = { | ||
ResourceGroup = local.namespace | ||
} | ||
//health_check | ||
} | ||
|
||
resource "aws_lb_target_group" "green_target_group" { | ||
name = "${local.namespace}-green" | ||
port = 8080 | ||
protocol = "HTTP" | ||
target_type = "instance" | ||
vpc_id = module.vpc.vpc_id | ||
tags = { | ||
ResourceGroup = local.namespace | ||
} | ||
//health_check | ||
} | ||
|
||
resource "aws_lb_listener" "lb_listener" { | ||
load_balancer_arn = aws_lb.lb.arn | ||
port = "80" | ||
protocol = "HTTP" | ||
|
||
default_action { | ||
type = "forward" | ||
target_group_arn = var.production == "green" ? aws_lb_target_group.green_target_group.arn : aws_lb_target_group.blue_target_group.arn | ||
} | ||
} | ||
|
||
|
||
resource "aws_lb_listener_rule" "lb_listener_rule" { | ||
listener_arn = aws_lb_listener.lb_listener.arn | ||
priority = 100 | ||
|
||
action { | ||
type = "forward" | ||
target_group_arn = var.production == "green" ? aws_lb_target_group.blue_target_group.arn : aws_lb_target_group.green_target_group.arn | ||
} | ||
|
||
condition { | ||
path_pattern { | ||
values = ["/stg/*"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
output "vpc" { | ||
value = module.vpc | ||
} | ||
|
||
output "namespace" { | ||
value = local.namespace | ||
} | ||
|
||
output "sg" { | ||
value = { | ||
lb = module.lb_sg.security_group.id | ||
webserver = module.webserver_sg.security_group.id | ||
} | ||
} | ||
|
||
output "target_group_arns" { | ||
value = { | ||
blue = [aws_lb_target_group.blue_target_group.arn] | ||
green = [aws_lb_target_group.green_target_group.arn] | ||
} | ||
} | ||
|
||
output "lb_dns_name" { | ||
value = aws_lb.lb.dns_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
variable "namespace" { | ||
default = "terraforminaction" | ||
type = string | ||
} | ||
|
||
variable "production" { | ||
type = string | ||
} |