Start messing around with ASGs

This commit is contained in:
Radon Rosborough 2021-06-18 07:02:49 +00:00
parent 4ff9d13f55
commit 81e78c18e1
1 changed files with 108 additions and 36 deletions

View File

@ -11,26 +11,28 @@ terraform {
} }
} }
locals {
tags = {
Terraform = "Managed by Terraform"
BillingCategory = "Riju"
}
}
data "external" "env" { data "external" "env" {
program = ["jq", "-n", "env"] program = ["jq", "-n", "env"]
} }
provider "aws" { provider "aws" {
region = "us-west-1" region = "us-west-1"
default_tags {
tags = {
Terraform = "Managed by Terraform"
BillingCategory = "Riju"
}
}
} }
data "aws_region" "current" {} data "aws_region" "current" {}
data "aws_vpc" "default" {
default = true
}
resource "aws_iam_user" "deploy" { resource "aws_iam_user" "deploy" {
name = "riju-deploy" name = "riju-deploy"
tags = local.tags
} }
resource "aws_iam_access_key" "deploy" { resource "aws_iam_access_key" "deploy" {
@ -104,7 +106,6 @@ data "aws_iam_policy_document" "riju" {
resource "aws_s3_bucket" "riju" { resource "aws_s3_bucket" "riju" {
bucket = data.external.env.result.S3_BUCKET bucket = data.external.env.result.S3_BUCKET
tags = local.tags
} }
resource "aws_s3_bucket_public_access_block" "riju" { resource "aws_s3_bucket_public_access_block" "riju" {
@ -169,42 +170,113 @@ resource "aws_security_group" "server" {
protocol = "-1" protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] cidr_blocks = ["0.0.0.0/0"]
} }
tags = local.tags
} }
resource "aws_instance" "server" { resource "aws_security_group" "alb" {
instance_type = "t3.small" name = "riju-alb"
ami = data.aws_ami.server.id description = "Security group for Riju application load balancer"
availability_zone = "${data.aws_region.current.name}b"
security_groups = [aws_security_group.server.name] ingress {
tags = merge(local.tags, { description = "HTTP"
Name = "Riju server" from_port = 80
}) to_port = 80
root_block_device { protocol = "tcp"
tags = merge(local.tags, { cidr_blocks = ["0.0.0.0/0"]
Name = "Riju server root volume" }
})
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_ebs_volume" "data" { resource "aws_launch_template" "server" {
availability_zone = "${data.aws_region.current.name}b" name = "riju-server"
size = 125 image_id = data.aws_ami.server.id
type = "gp3" instance_type = "t3.small"
tags = merge(local.tags, { security_group_names = [aws_security_group.server.name]
Name = "Riju Docker data"
}) block_device_mappings {
device_name = "/dev/sdh"
ebs {
volume_type = "gp3"
volume_size = 125
}
}
tags = {
Name = "Riju server"
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "Riju server"
}
}
} }
resource "aws_volume_attachment" "data" { resource "aws_autoscaling_group" "server" {
device_name = "/dev/sdh" availability_zones = [
volume_id = aws_ebs_volume.data.id "${data.aws_region.current.name}b",
instance_id = aws_instance.server.id "${data.aws_region.current.name}c",
]
desired_capacity = 1
min_size = 1
max_size = 3
launch_template {
id = aws_launch_template.server.id
}
tag {
key = "Name"
value = "Riju server"
propagate_at_launch = false
}
} }
output "server_ip_address" { resource "aws_lb" "server" {
value = aws_instance.server.public_ip name = "riju-server"
security_groups = [aws_security_group.alb.name]
}
resource "aws_lb_target_group" "server_http" {
name = "riju-server-http"
port = 80
protocol = "HTTP"
vpc_id = data.aws_vpc.default.id
}
resource "aws_autoscaling_attachment" "server_http" {
autoscaling_group_name = aws_autoscaling_group.server.id
alb_target_group_arn = aws_lb_target_group.server_http.arn
}
resource "aws_lb_target_group" "server_https" {
name = "riju-server-https"
port = 443
protocol = "HTTPS"
vpc_id = data.aws_vpc.default.id
}
resource "aws_autoscaling_attachment" "server_https" {
autoscaling_group_name = aws_autoscaling_group.server.id
alb_target_group_arn = aws_lb_target_group.server_https.arn
}
output "alb_dns_name" {
value = aws_lb.server
} }
output "deploy_aws_access_key_id" { output "deploy_aws_access_key_id" {