Skip to content

Commit

Permalink
feat: ASG ローリングデプロイをモジュール化
Browse files Browse the repository at this point in the history
  • Loading branch information
tkdn committed Mar 3, 2024
1 parent cb8bbc9 commit 4c1de5f
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
terraform {
required_version = ">= 1.0.0, < 2.0.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}

resource "aws_launch_configuration" "example" {
image_id = var.ami
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]

user_data = templatefile("${path.module}/user-data.sh", {
server_text = var.server_text
server_port = var.server_port
db_address = data.terraform_remote_state.db.outputs.address
db_port = data.terraform_remote_state.db.outputs.port
})

# ASG がある起動設定を使う場合は必須
lifecycle {
create_before_destroy = true
}
}

resource "aws_autoscaling_group" "example" {
name = var.cluster_name

launch_configuration = aws_launch_configuration.example.name
vpc_zone_identifier = data.aws_subnets.default.ids

target_group_arns = [aws_lb_target_group.asg.arn]
health_check_type = "ELB"

min_size = var.min_size
max_size = var.max_size

# ASG の変更を展開する歳にはインスタンスを更新する
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
}

tag {
key = "Name"
value = var.cluster_name
propagate_at_launch = true
}

dynamic "tag" {
for_each = {
for key, value in var.custom_tags :
key => upper(value)
if key != "Name"
}

content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}

resource "aws_autoscaling_schedule" "scale_out_during_buisiness_hours" {
count = var.enable_autoscaling ? 1 : 0

scheduled_action_name = "${var.cluster_name}-scale-out-during-buisiness-hours"
min_size = 2
max_size = 10
desired_capacity = 10
recurrence = "0 9 * * *"

autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_autoscaling_schedule" "scale_in_at_night" {
count = var.enable_autoscaling ? 1 : 0

scheduled_action_name = "${var.cluster_name}-scale-in-at-night"
min_size = 2
max_size = 2
desired_capacity = 2
recurrence = "0 17 * * *"

autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_security_group" "instance" {
name = "${var.cluster_name}-instance"
}

resource "aws_security_group_rule" "name" {
type = "ingress"
security_group_id = aws_security_group.instance.id

from_port = var.server_port
to_port = var.server_port
protocol = local.tcp_protocol
cidr_blocks = local.all_ips
}

resource "aws_cloudwatch_metric_alarm" "high_cpu_utilization" {
alarm_name = "${var.cluster_name}-high-cpu-utilization"
namespace = "AWS/EC2"
metric_name = "CPUUtilization"

dimensions = {
AutoScalingGroupName = aws_autoscaling_group.example.name
}

comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
period = 300
statistic = "Average"
threshold = 90
unit = "Percent"
}

resource "aws_cloudwatch_metric_alarm" "low_cpu_credit_balance" {
count = format("%.1s", var.instance_type) == "t" ? 1 : 0

alarm_name = "${var.cluster_name}-low-cpu-credit-balance"
namespace = "AWS/EC2"
metric_name = "CPUCreditBalance"

dimensions = {
AutoScalingGroupName = aws_autoscaling_group.example.name
}

comparison_operator = "LessThanThreshold"
evaluation_periods = 1
period = 300
statistic = "Minimum"
threshold = 10
unit = "Count"
}

locals {
tcp_protocol = "tcp"
all_ips = ["0.0.0.0/0"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
variable "cluster_name" {
description = "The name to use for all the cluster resorces"
type = string
}

variable "ami" {
description = "The AMI to run in the cluster"
type = string
default = "ami-067983a1f071c98a2"
}

variable "instance_type" {
type = string
}

variable "min_size" {
type = number
}

variable "max_size" {
type = number
}

variable "enable_autoscaling" {
type = bool
}

variable "custom_tags" {
type = map(string)
default = {}
}

variable "server_port" {
description = "The port the server will use for HTTP requests."
type = number
default = 8080
}

variable "server_text" {
description = "The text the web server should return"
type = string
default = "Hello, World"
}
133 changes: 0 additions & 133 deletions ch8.production-grade-infrastructure/modules/webserver-cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,103 +9,6 @@ terraform {
}
}

resource "aws_launch_configuration" "example" {
image_id = var.ami
instance_type = var.instance_type
security_groups = [aws_security_group.instance.id]

user_data = templatefile("${path.module}/user-data.sh", {
server_text = var.server_text
server_port = var.server_port
db_address = data.terraform_remote_state.db.outputs.address
db_port = data.terraform_remote_state.db.outputs.port
})

# ASG がある起動設定を使う場合は必須
lifecycle {
create_before_destroy = true
}
}

resource "aws_autoscaling_group" "example" {
name = var.cluster_name

launch_configuration = aws_launch_configuration.example.name
vpc_zone_identifier = data.aws_subnets.default.ids

target_group_arns = [aws_lb_target_group.asg.arn]
health_check_type = "ELB"

min_size = var.min_size
max_size = var.max_size

# ASG の変更を展開する歳にはインスタンスを更新する
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
}
}

tag {
key = "Name"
value = var.cluster_name
propagate_at_launch = true
}

dynamic "tag" {
for_each = {
for key, value in var.custom_tags :
key => upper(value)
if key != "Name"
}

content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}

resource "aws_autoscaling_schedule" "scale_out_during_buisiness_hours" {
count = var.enable_autoscaling ? 1 : 0

scheduled_action_name = "${var.cluster_name}-scale-out-during-buisiness-hours"
min_size = 2
max_size = 10
desired_capacity = 10
recurrence = "0 9 * * *"

autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_autoscaling_schedule" "scale_in_at_night" {
count = var.enable_autoscaling ? 1 : 0

scheduled_action_name = "${var.cluster_name}-scale-in-at-night"
min_size = 2
max_size = 2
desired_capacity = 2
recurrence = "0 17 * * *"

autoscaling_group_name = aws_autoscaling_group.example.name
}

resource "aws_security_group" "instance" {
name = "${var.cluster_name}-instance"
}

resource "aws_security_group_rule" "name" {
type = "ingress"
security_group_id = aws_security_group.instance.id

from_port = var.server_port
to_port = var.server_port
protocol = local.tcp_protocol
cidr_blocks = local.all_ips
}

resource "aws_alb" "example" {
name = var.cluster_name
load_balancer_type = "application"
Expand Down Expand Up @@ -207,42 +110,6 @@ data "terraform_remote_state" "db" {
}
}

resource "aws_cloudwatch_metric_alarm" "high_cpu_utilization" {
alarm_name = "${var.cluster_name}-high-cpu-utilization"
namespace = "AWS/EC2"
metric_name = "CPUUtilization"

dimensions = {
AutoScalingGroupName = aws_autoscaling_group.example.name
}

comparison_operator = "GreaterThanThreshold"
evaluation_periods = 1
period = 300
statistic = "Average"
threshold = 90
unit = "Percent"
}

resource "aws_cloudwatch_metric_alarm" "low_cpu_credit_balance" {
count = format("%.1s", var.instance_type) == "t" ? 1 : 0

alarm_name = "${var.cluster_name}-low-cpu-credit-balance"
namespace = "AWS/EC2"
metric_name = "CPUCreditBalance"

dimensions = {
AutoScalingGroupName = aws_autoscaling_group.example.name
}

comparison_operator = "LessThanThreshold"
evaluation_periods = 1
period = 300
statistic = "Minimum"
threshold = 10
unit = "Count"
}

locals {
http_port = 80
any_port = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,3 @@ variable "db_remote_state_key" {
description = "The path for database's remote state in S3"
type = string
}

variable "instance_type" {
type = string
}

variable "min_size" {
type = number
}

variable "max_size" {
type = number
}

variable "custom_tags" {
type = map(string)
default = {}
}

variable "enable_autoscaling" {
type = bool
}

variable "ami" {
description = "The AMI to run in the cluster"
type = string
default = "ami-067983a1f071c98a2"
}

variable "server_text" {
description = "The text the web server should return"
type = string
default = "Hello, World"
}

0 comments on commit 4c1de5f

Please sign in to comment.