From 7da6e0a5f711373dcd83110ddd004fcdd684a29c Mon Sep 17 00:00:00 2001 From: Radon Rosborough Date: Sun, 4 Jul 2021 16:19:47 +0000 Subject: [PATCH] Infrastructure up and running --- tf/alb.tf | 41 +++++++++++++++++++++++++++++++++++------ tf/asg.tf | 9 ++------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/tf/alb.tf b/tf/alb.tf index b8a271b..0d125bc 100644 --- a/tf/alb.tf +++ b/tf/alb.tf @@ -32,16 +32,45 @@ resource "aws_lb" "server" { subnets = data.aws_subnet_ids.default.ids } -resource "aws_lb_target_group" "server_http" { +resource "aws_lb_target_group" "server" { name = "riju-server-http" port = 80 protocol = "HTTP" vpc_id = data.aws_vpc.default.id } -resource "aws_lb_target_group" "server_https" { - name = "riju-server-https" - port = 443 - protocol = "HTTPS" - vpc_id = data.aws_vpc.default.id +resource "aws_lb_listener" "server_http" { + load_balancer_arn = aws_lb.server.arn + port = "80" + protocol = "HTTP" + + default_action { + type = "redirect" + + redirect { + port = "443" + protocol = "HTTPS" + status_code = "HTTP_301" + } + } +} + +resource "aws_lb_listener" "server_https" { + load_balancer_arn = aws_lb.server.arn + port = "443" + protocol = "HTTPS" + ssl_policy = "ELBSecurityPolicy-2016-08" + certificate_arn = aws_acm_certificate.riju.arn + + default_action { + type = "forward" + target_group_arn = aws_lb_target_group.server.arn + } +} + +resource "aws_autoscaling_attachment" "server" { + count = local.ami_available ? 1 : 0 + + autoscaling_group_name = aws_autoscaling_group.server[0].name + alb_target_group_arn = aws_lb_target_group.server.arn } diff --git a/tf/asg.tf b/tf/asg.tf index 2b38a6b..e02b089 100644 --- a/tf/asg.tf +++ b/tf/asg.tf @@ -87,19 +87,14 @@ resource "aws_autoscaling_group" "server" { availability_zones = [ for subnet in data.aws_subnet.default : subnet.availability_zone ] - desired_capacity = 0 - min_size = 0 + desired_capacity = 1 + min_size = 1 max_size = 3 launch_template { id = aws_launch_template.server[0].id } - target_group_arns = [ - aws_lb_target_group.server_http.arn, - aws_lb_target_group.server_https.arn, - ] - tags = concat( [ {