|
| 1 | +# Upload existing SSH public key to AWS |
| 2 | +resource "aws_key_pair" "ssh_key" { |
| 3 | + key_name = var.ssh_key_name |
| 4 | + public_key = file(var.ssh_public_key_path) |
| 5 | +} |
| 6 | + |
| 7 | +# Debian 12 |
| 8 | +data "aws_ami" "debian12" { |
| 9 | + most_recent = true |
| 10 | + |
| 11 | + filter { |
| 12 | + name = "name" |
| 13 | + values = ["debian-12-amd64-*"] |
| 14 | + } |
| 15 | + |
| 16 | + filter { |
| 17 | + name = "virtualization-type" |
| 18 | + values = ["hvm"] |
| 19 | + } |
| 20 | + |
| 21 | + filter { |
| 22 | + name = "root-device-type" |
| 23 | + values = ["ebs"] |
| 24 | + } |
| 25 | + |
| 26 | + owners = ["136693071363"] # https://wiki.debian.org/Cloud/AmazonEC2Image/ |
| 27 | +} |
| 28 | + |
| 29 | +resource "aws_security_group" "ssh_access" { |
| 30 | + name = "postgres-monitor-ssh-access" |
| 31 | + description = "Allow SSH inbound traffic for postgres monitor" |
| 32 | + |
| 33 | + ingress { |
| 34 | + description = "SSH from anywhere" |
| 35 | + from_port = 22 |
| 36 | + to_port = 22 |
| 37 | + protocol = "tcp" |
| 38 | + cidr_blocks = ["0.0.0.0/0"] |
| 39 | + } |
| 40 | + |
| 41 | + egress { |
| 42 | + description = "Allow all outbound traffic" |
| 43 | + from_port = 0 |
| 44 | + to_port = 0 |
| 45 | + protocol = "-1" |
| 46 | + cidr_blocks = ["0.0.0.0/0"] |
| 47 | + } |
| 48 | + |
| 49 | + tags = { |
| 50 | + Name = "postgres-monitor-ssh-access" |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +resource "aws_instance" "postgres_monitor" { |
| 55 | + ami = data.aws_ami.debian12.id |
| 56 | + instance_type = var.instance_type |
| 57 | + key_name = var.ssh_key_name |
| 58 | + vpc_security_group_ids = [aws_security_group.ssh_access.id] |
| 59 | + |
| 60 | + user_data = templatefile("${path.module}/../cloudinit/postgres-monitor-cloud-init.yaml", { |
| 61 | + hostname = var.hostname |
| 62 | + ssh_public_key = trimspace(file(var.ssh_public_key_path)) |
| 63 | + }) |
| 64 | + |
| 65 | + user_data_replace_on_change = true |
| 66 | + |
| 67 | + tags = { |
| 68 | + Name = var.instance_name |
| 69 | + } |
| 70 | + |
| 71 | + root_block_device { |
| 72 | + volume_size = 32 |
| 73 | + } |
| 74 | +} |
0 commit comments