-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.tf
More file actions
146 lines (116 loc) · 3.09 KB
/
instance.tf
File metadata and controls
146 lines (116 loc) · 3.09 KB
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
data "aws_ec2_instance_types" "this" {
filter {
name = "burstable-performance-supported"
values = ["true"]
}
filter {
name = "current-generation"
values = ["true"]
}
filter {
name = "memory-info.size-in-mib"
values = ["512"]
}
filter {
name = "processor-info.supported-architecture"
values = ["arm64"]
}
}
data "aws_ec2_instance_type" "this" {
instance_type = coalesce(var.instance_type, data.aws_ec2_instance_types.this.instance_types.0)
}
data "aws_ssm_parameter" "this" {
name = "/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-${data.aws_ec2_instance_type.this.supported_architectures.0}"
with_decryption = false
}
resource "tls_private_key" "this" {
algorithm = "ED25519"
}
resource "random_pet" "this" {}
resource "local_file" "this" {
content = tls_private_key.this.public_key_openssh
filename = "${path.module}/${random_pet.this.id}.pub"
}
resource "local_sensitive_file" "this" {
content = tls_private_key.this.private_key_openssh
filename = "${path.module}/${random_pet.this.id}"
}
resource "aws_key_pair" "this" {
key_name = random_pet.this.id
public_key = tls_private_key.this.public_key_openssh
}
data "aws_iam_policy_document" "this" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
resource "aws_iam_role" "this" {
assume_role_policy = data.aws_iam_policy_document.this.json
name = replace(var.name, " ", "-")
}
data "aws_iam_policy" "this" {
for_each = toset(
[
"ReadOnlyAccess",
"AmazonSSMManagedInstanceCore",
]
)
name = each.key
}
resource "aws_iam_role_policy_attachment" "this" {
for_each = data.aws_iam_policy.this
role = aws_iam_role.this.name
policy_arn = each.value.arn
}
resource "aws_iam_instance_profile" "this" {
name = replace(var.name, " ", "-")
role = aws_iam_role.this.name
}
locals {
user_data = {
runcmd : [
"grubby --args selinux=0 --update-kernel ALL",
],
power_state : {
mode : "reboot",
timeout : 0,
condition : true,
},
}
}
data "cloudinit_config" "this" {
gzip = false
base64_encode = false
part {
content = yamlencode(local.user_data)
content_type = "text/cloud-config"
}
}
resource "aws_instance" "this" {
ami = data.aws_ssm_parameter.this.value
iam_instance_profile = aws_iam_role.this.name
instance_type = data.aws_ec2_instance_type.this.id
key_name = aws_key_pair.this.key_name
subnet_id = aws_subnet.this.id
vpc_security_group_ids = [aws_vpc.this.default_security_group_id]
user_data = data.cloudinit_config.this.rendered
user_data_replace_on_change = true
root_block_device {
encrypted = true
}
lifecycle {
ignore_changes = [ami]
}
}
resource "aws_eip" "this" {
domain = "vpc"
depends_on = [aws_internet_gateway.this]
}
resource "aws_eip_association" "this" {
instance_id = aws_instance.this.id
allocation_id = aws_eip.this.id
}