-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
191 additions
and
166 deletions.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
ch8.production-grade-infrastructure/modules/cluster/asg-rolling-deploy/main.tf
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,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"] | ||
} |
43 changes: 43 additions & 0 deletions
43
ch8.production-grade-infrastructure/modules/cluster/asg-rolling-deploy/variables.tf
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,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" | ||
} |
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
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