From 071ab9973ef309802331c2852d5bcf2a28b70a21 Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Sat, 18 Sep 2021 10:35:20 -0700 Subject: [PATCH] [#113] Remove autoscaling group --- packer/cloudwatch.json | 6 +-- tf/alb.tf | 5 -- tf/asg.tf | 112 ----------------------------------------- tf/cloudwatch.tf | 12 ++--- tf/ec2.tf | 77 ++++++++++++++++++++++++++++ tf/ssm.tf | 6 --- 6 files changed, 86 insertions(+), 132 deletions(-) delete mode 100644 tf/asg.tf diff --git a/packer/cloudwatch.json b/packer/cloudwatch.json index f8b081c..a7397c2 100644 --- a/packer/cloudwatch.json +++ b/packer/cloudwatch.json @@ -5,14 +5,14 @@ }, "metrics": { "append_dimensions": { - "AutoScalingGroupName": "${aws:AutoScalingGroupName}", + "RijuInstanceGroup": "Webserver", "ImageId": "${aws:ImageId}", "InstanceId": "${aws:InstanceId}", "InstanceType": "${aws:InstanceType}" }, "aggregation_dimensions": [ - ["AutoScalingGroupName"], - ["AutoScalingGroupName", "path"] + ["RijuInstanceGroup"], + ["RijuInstanceGroup", "path"] ], "metrics_collected": { "cpu": { diff --git a/tf/alb.tf b/tf/alb.tf index f6a043d..d21bc8a 100644 --- a/tf/alb.tf +++ b/tf/alb.tf @@ -77,8 +77,3 @@ resource "aws_lb_listener" "server_https" { target_group_arn = aws_lb_target_group.server.arn } } - -resource "aws_autoscaling_attachment" "server" { - autoscaling_group_name = aws_autoscaling_group.server.name - alb_target_group_arn = aws_lb_target_group.server.arn -} diff --git a/tf/asg.tf b/tf/asg.tf deleted file mode 100644 index e8fa4df..0000000 --- a/tf/asg.tf +++ /dev/null @@ -1,112 +0,0 @@ -resource "aws_security_group" "server" { - name = "riju-server" - description = "Security group for Riju server" - - ingress { - description = "SSH" - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - description = "HTTP" - from_port = 80 - to_port = 80 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - description = "HTTPS" - from_port = 443 - to_port = 443 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } -} - -resource "aws_launch_template" "server" { - name = "riju-server" - image_id = data.aws_ami.server.id - instance_type = "t3.medium" - - security_group_names = [aws_security_group.server.name] - iam_instance_profile { - name = aws_iam_instance_profile.server.name - } - - update_default_version = true - - block_device_mappings { - device_name = "/dev/sdh" - ebs { - volume_type = "gp3" - volume_size = 256 - } - } - - tags = { - Name = "Riju server" - } - - tag_specifications { - resource_type = "instance" - tags = merge(local.tags, { - Name = "Riju server" - BillingSubcategory = "Riju:EC2:Webserver" - }) - } - - tag_specifications { - resource_type = "volume" - tags = merge(local.tags, { - Name = "Riju server" - BillingSubcategory = "Riju:EBS:Webserver" - }) - } -} - -resource "aws_autoscaling_group" "server" { - name = "riju-server" - - availability_zones = [local.primary_az] - desired_capacity = 1 - min_size = 0 - max_size = 3 - - launch_template { - id = aws_launch_template.server.id - } - - termination_policies = [ - "OldestLaunchTemplate", - "OldestInstance", - ] - - tags = concat( - [ - { - key = "Name" - value = "Riju server" - propagate_at_launch = false - } - ] - ) - - lifecycle { - ignore_changes = [ - desired_capacity, - target_group_arns, - ] - } - -} diff --git a/tf/cloudwatch.tf b/tf/cloudwatch.tf index e789878..130fd9b 100644 --- a/tf/cloudwatch.tf +++ b/tf/cloudwatch.tf @@ -13,7 +13,7 @@ resource "aws_cloudwatch_metric_alarm" "server_cpu" { alarm_actions = [aws_sns_topic.riju.arn] insufficient_data_actions = [aws_sns_topic.riju.arn] dimensions = { - AutoScalingGroupName = aws_autoscaling_group.server.name + RijuInstanceGroup = "Webserver" } tags = { @@ -36,7 +36,7 @@ resource "aws_cloudwatch_metric_alarm" "server_memory" { alarm_actions = [aws_sns_topic.riju.arn] insufficient_data_actions = [aws_sns_topic.riju.arn] dimensions = { - AutoScalingGroupName = aws_autoscaling_group.server.name + RijuInstanceGroup = "Webserver" } tags = { @@ -59,8 +59,8 @@ resource "aws_cloudwatch_metric_alarm" "server_data_volume_disk_space" { alarm_actions = [aws_sns_topic.riju.arn] insufficient_data_actions = [aws_sns_topic.riju.arn] dimensions = { - AutoScalingGroupName = aws_autoscaling_group.server.name - path = "/mnt/riju" + RijuInstanceGroup = "Webserver" + path = "/mnt/riju" } tags = { @@ -83,8 +83,8 @@ resource "aws_cloudwatch_metric_alarm" "server_root_volume_disk_space" { alarm_actions = [aws_sns_topic.riju.arn] insufficient_data_actions = [aws_sns_topic.riju.arn] dimensions = { - AutoScalingGroupName = aws_autoscaling_group.server.name - path = "/" + RijuInstanceGroup = "Webserver" + path = "/" } tags = { diff --git a/tf/ec2.tf b/tf/ec2.tf index a4f20c5..83ccee9 100644 --- a/tf/ec2.tf +++ b/tf/ec2.tf @@ -1,3 +1,80 @@ +resource "aws_security_group" "server" { + name = "riju-server" + description = "Security group for Riju server" + + ingress { + description = "SSH" + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "HTTP" + from_port = 80 + to_port = 80 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + ingress { + description = "HTTPS" + from_port = 443 + to_port = 443 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + } + + egress { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + } +} + +resource "aws_launch_template" "server" { + name = "riju-server" + image_id = data.aws_ami.server.id + instance_type = "t3.medium" + + security_group_names = [aws_security_group.server.name] + iam_instance_profile { + name = aws_iam_instance_profile.server.name + } + + update_default_version = true + + block_device_mappings { + device_name = "/dev/sdh" + ebs { + volume_type = "gp3" + volume_size = 256 + } + } + + tags = { + Name = "Riju server" + } + + tag_specifications { + resource_type = "instance" + tags = merge(local.tags, { + Name = "Riju server" + BillingSubcategory = "Riju:EC2:Webserver" + }) + } + + tag_specifications { + resource_type = "volume" + tags = merge(local.tags, { + Name = "Riju server" + BillingSubcategory = "Riju:EBS:Webserver" + }) + } +} + resource "aws_security_group" "deploy" { name = "riju-deploy" description = "Security group for Riju CI" diff --git a/tf/ssm.tf b/tf/ssm.tf index 2dda2e9..3c67696 100644 --- a/tf/ssm.tf +++ b/tf/ssm.tf @@ -29,9 +29,3 @@ resource "aws_ssm_parameter" "s3_bucket" { type = "String" value = aws_s3_bucket.riju.bucket } - -resource "aws_ssm_parameter" "asg_desired_capacity" { - name = "riju-asg-desired-capacity" - type = "String" - value = aws_autoscaling_group.server.desired_capacity -}