-
Notifications
You must be signed in to change notification settings - Fork 1
/
ec2.tf
102 lines (94 loc) · 2.82 KB
/
ec2.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
locals {
ami_name = var.ami_name != "" ? var.ami_name : "al2023-ami-2023.2.20231113.0-kernel-6.1-${data.aws_ec2_instance_type.current.supported_architectures[0]}"
}
data "aws_region" "current" {}
data "aws_ec2_instance_type" "current" {
instance_type = var.instance_type
}
data "aws_ami" "current" {
owners = ["amazon"]
filter {
name = "name"
values = [
# Amazon Linux 2
#
# To lookup name when updating:
#
# aws ec2 describe-images --owners amazon \
# --filters "Name=name,Values=al2023-ami-20*-x86_64" \
# --query 'reverse(sort_by(Images, &CreationDate))[].{Name:Name,ImageId:ImageId}' \
# --region "$region" | jq .[0]
#
local.ami_name
]
}
most_recent = true
include_deprecated = true
}
resource "aws_ssm_parameter" "this" {
count = var.pem_ssm_parameter_name == "" ? 1 : 0
name = "${local.name}-secret"
type = "String"
value = var.pem
tier = "Advanced" # value is over 4kb
}
data "aws_ssm_parameter" "this" {
count = var.pem_ssm_parameter_name != "" ? 1 : 0
name = var.pem_ssm_parameter_name
}
resource "aws_launch_template" "this" {
name_prefix = "${local.name}-"
image_id = data.aws_ami.current.id
instance_type = var.instance_type
iam_instance_profile {
name = aws_iam_instance_profile.this.name
}
network_interfaces {
associate_public_ip_address = var.map_public_ip_on_launch
}
block_device_mappings {
device_name = "/dev/xvda"
ebs {
volume_size = 20
volume_type = "gp3"
delete_on_termination = true
encrypted = true
}
}
user_data = base64encode(
templatefile("${path.module}/user-data.sh.tpl", {
image = var.image,
ssm_parameter_name = var.pem_ssm_parameter_name != "" ? data.aws_ssm_parameter.this[0].name : aws_ssm_parameter.this[0].name
url = var.url
asg_name = local.name
asg_hook_name = "launch"
})
)
tag_specifications {
resource_type = "instance"
tags = merge(local.tags, {
"terraform:altinity:cloud/instance-group" = local.name
"altinity:cloud/version" = var.image
})
}
}
resource "aws_autoscaling_group" "this" {
name = local.name
min_size = 0
desired_capacity = var.replicas
max_size = 3
launch_template {
id = aws_launch_template.this.id
version = "$Latest"
}
initial_lifecycle_hook {
name = "launch"
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
heartbeat_timeout = "420" // 8m
default_result = "ABANDON"
}
wait_for_capacity_timeout = "7m"
vpc_zone_identifier = length(var.subnets) > 0 ? var.subnets : (
var.use_default_subnets ? data.aws_subnets.default[0].ids : aws_subnet.this.*.id
)
}