generated from michaeldeggers/repo_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasg.tf
81 lines (68 loc) · 2.3 KB
/
asg.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] # Retrieves the latest approved image }
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_launch_configuration" "ghost_lc" {
name_prefix = "${local.name_prefix}-lc"
image_id = data.aws_ami.ubuntu.image_id
security_groups = [aws_security_group.ghost_asg_sg.id]
instance_type = var.ec2_instance_type
# path to the user data file
user_data = templatefile("${path.module}/user_data/ghost_init.sh",
{
# This is pulled from the rds resource created in rds.tf
"endpoint" = aws_db_instance.default.address,
"database" = aws_db_instance.default.db_name,
"username" = aws_db_instance.default.username,
"password" = random_password.mysql_password.result,
"admin_url" = "http://blog.${var.route53_hosted_zone_name}", # Different subdomains cause issues
"url" = "http://blog.${var.route53_hosted_zone_name}"
}
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "ghost_asg" {
name = "${aws_launch_configuration.ghost_lc.name}-asg"
launch_configuration = aws_launch_configuration.ghost_lc.name
max_size = var.asg_max_size
min_size = var.asg_min_size
vpc_zone_identifier = [module.vpc.public_subnets[0], module.vpc.public_subnets[1]]
# Associate the ASG with the Application Load Balancer target group.
target_group_arns = [aws_lb_target_group.ghost_lb_tg.arn]
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group" "ghost_asg_sg" {
name = "${local.name_prefix}-asg-sg"
description = "Security group for the ghost instances"
vpc_id = module.vpc.vpc_id
ingress {
description = "Ingress rule for http"
from_port = 80
to_port = 80
protocol = "tcp"
# Security group that will be used by the ALB, see alb.tf
security_groups = [aws_security_group.ghost_lb_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = merge(var.additional_tags,
{
"Name" : "${local.name_prefix}-asg-sg"
})
}