-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathmain.tf
More file actions
105 lines (88 loc) · 2.19 KB
/
main.tf
File metadata and controls
105 lines (88 loc) · 2.19 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
tailscale = {
source = "tailscale/tailscale"
}
}
}
# Create Tailscale auth key
resource "tailscale_tailnet_key" "this" {
reusable = false
ephemeral = false
preauthorized = true
expiry = var.ec2_tailscale_key_expiry
description = "Auth key for ${var.ec2_instance_name}"
tags = var.ec2_tailscale_tags
}
# Upload existing SSH public key to AWS
resource "aws_key_pair" "ssh_key" {
key_name = var.ec2_ssh_key_name
public_key = file(var.ec2_ssh_public_key_path)
}
# Debian 12 AMI
data "aws_ami" "debian12" {
most_recent = true
filter {
name = "name"
values = ["debian-12-amd64-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
owners = ["136693071363"] # https://wiki.debian.org/Cloud/AmazonEC2Image/
}
# Security group
resource "aws_security_group" "ssh_access" {
name = "${var.ec2_instance_name}-ssh-access"
description = "Allow SSH inbound traffic for ${var.ec2_instance_name}"
ingress {
description = "SSH from anywhere"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.ec2_instance_name}-ssh-access"
}
}
# EC2 Instance
resource "aws_instance" "this" {
ami = data.aws_ami.debian12.id
instance_type = var.ec2_instance_type
key_name = var.ec2_ssh_key_name
vpc_security_group_ids = [aws_security_group.ssh_access.id]
user_data = templatefile(var.ec2_cloud_init_template_path, {
hostname = var.ec2_hostname
ssh_public_key = trimspace(file(var.ec2_ssh_public_key_path))
tailscale_auth_key = tailscale_tailnet_key.this.key
})
user_data_replace_on_change = true
tags = merge(
{
Name = var.ec2_instance_name
},
var.ec2_tags
)
lifecycle {
ignore_changes = [ami]
}
root_block_device {
volume_size = var.ec2_root_volume_size
}
}