-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path006_ec2.tf
175 lines (134 loc) · 5.35 KB
/
006_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
## ------------------------------------------------------------------------------------
## AMI
## ------------------------------------------------------------------------------------
data "aws_ami" "amazon_linux_2023" {
most_recent = true
owners = ["amazon"]
# https://docs.aws.amazon.com/linux/al2023/ug/ec2.html#launch-from-ec2-console
filter {
name = "name"
values = ["al2023-ami-minimal-*"]
# values = ["al2023-ami-ecs-hvm-2023.0.20230509-kernel-6.1-x86_64"]
# values = ["al2023-ami-ecs-hvm-2023*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}
## ------------------------------------------------------------------------------------
## EC2 SSH Key
## ------------------------------------------------------------------------------------
resource "tls_private_key" "ec2_ssh_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "aws_key_pair" "generated_key" {
key_name = local.global_prefix
public_key = tls_private_key.ec2_ssh_key.public_key_openssh
}
## ------------------------------------------------------------------------------------
## EC2 Launch Template
## ------------------------------------------------------------------------------------
resource "aws_launch_template" "lt_with_no_ami_lifecycle" {
count = var.ec2_update_ami_if_available == true ? 1 : 0
image_id = data.aws_ami.amazon_linux_2023.id
instance_type = var.ec2_host_instance_type
vpc_security_group_ids = local.ec2_sg_final
key_name = aws_key_pair.generated_key.key_name
iam_instance_profile {
name = data.aws_iam_instance_profile.tower_vm.name
}
metadata_options {
# IMDS token config
http_tokens = var.ec2_require_imds_token == true ? "required" : "optional"
}
user_data = base64encode(local.lt_content_raw)
}
resource "aws_launch_template" "lt_with_ami_lifecycle" {
count = var.ec2_update_ami_if_available == false ? 1 : 0
image_id = data.aws_ami.amazon_linux_2023.id
instance_type = var.ec2_host_instance_type
vpc_security_group_ids = local.ec2_sg_final
key_name = aws_key_pair.generated_key.key_name
iam_instance_profile {
name = data.aws_iam_instance_profile.tower_vm.name
}
metadata_options {
# IMDS token config
http_tokens = var.ec2_require_imds_token == true ? "required" : "optional"
}
user_data = base64encode(local.lt_content_raw)
lifecycle {
ignore_changes = [ image_id ]
}
}
## ------------------------------------------------------------------------------------
## EC2 Instance
## ------------------------------------------------------------------------------------
resource "aws_instance" "ec2" {
depends_on = [module.rds[0]]
subnet_id = local.subnet_ids_ec2[0]
launch_template {
# id = aws_launch_template.lt.id
id = ( var.ec2_update_ami_if_available == true ?
aws_launch_template.lt_with_no_ami_lifecycle[0].id : aws_launch_template.lt_with_ami_lifecycle[0].id
)
version = "$Latest"
}
root_block_device {
encrypted = var.flag_encrypt_ebs
kms_key_id = var.flag_use_kms_key == true ? var.ec2_ebs_kms_key : ""
volume_size = var.ec2_root_volume_size
}
# This is here to stop the EC2 from being updated in place due to perception user data has changed
# when it actually hasn't (triggered by `depends_on` or `data` read). See:
# - https://github.com/hashicorp/terraform-provider-aws/issues/5011
# - https://github.com/hashicorp/terraform/issues/11806
lifecycle {
ignore_changes = [user_data]
}
metadata_options {
# IMDS token config
http_tokens = var.ec2_require_imds_token == true ? "required" : "optional"
}
tags = {
tag-key = local.global_prefix # This tag allows Console InstanceConnect to attach
Name = local.global_prefix
}
}
## ------------------------------------------------------------------------------------
## Elatic IP & Association
## Use so machine termination will not require changes to external DNS.
## ------------------------------------------------------------------------------------
resource "aws_eip" "towerhost" {
count = var.flag_make_instance_public == true ? 1 : 0
domain = "vpc"
}
resource "aws_eip_association" "eip_assoc" {
count = var.flag_make_instance_public == true ? 1 : 0
instance_id = aws_instance.ec2.id
allocation_id = aws_eip.towerhost[0].id
}
## ------------------------------------------------------------------------------------
## Instance Connect Endpoint -- critical for SSH into private subnets
## ------------------------------------------------------------------------------------
# This turned out to be a moot issue since a VPC can only have a single EICE endpoint. Keeping anyways in case folks have more quota.
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/eice-quotas.html
# https://discuss.hashicorp.com/t/the-for-each-value-depends-on-resource-attributes-that-cannot-be-determined-until-apply/25016/2
resource "aws_ec2_instance_connect_endpoint" "example" {
count = var.flag_make_instance_private == true || var.flag_make_instance_private_behind_public_alb == true ? 1 : 0
subnet_id = local.subnet_ids_ec2[0]
security_group_ids = [module.tower_eice_egress_sg.security_group_id]
tags = {
Name = local.global_prefix
}
}