Skip to content

Commit 6157ad5

Browse files
committed
infra: deploy postgres primary and monitor
1 parent a2dcb5f commit 6157ad5

File tree

10 files changed

+281
-0
lines changed

10 files changed

+281
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#cloud-config
2+
hostname: ${hostname}
3+
fqdn: ${hostname}
4+
manage_etc_hosts: true
5+
6+
users:
7+
- name: app
8+
shell: /bin/bash
9+
ssh_authorized_keys:
10+
- ${ssh_public_key}
11+
- name: admin
12+
shell: /bin/bash
13+
sudo: ALL=(ALL) NOPASSWD:ALL
14+
ssh_authorized_keys:
15+
- ${ssh_public_key}
16+
17+
package_update: true
18+
package_upgrade: true
19+
20+
packages:
21+
- ca-certificates
22+
- curl
23+
- wget
24+
- gnupg
25+
- vim
26+
- git
27+
- zip
28+
- unzip
29+
- openssl
30+
- libssl-dev
31+
- build-essential
32+
- rsyslog
33+
- htop
34+
- rsync
35+
- pkg-config
36+
37+
runcmd:
38+
- loginctl enable-linger app
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
provider "aws" {
2+
region = "us-east-2"
3+
}
4+
5+
6+
module "postgres_monitor" {
7+
source = "./postgres_monitor"
8+
}
9+
10+
module "postgres_primary" {
11+
source = "./postgres_primary"
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "postgres_monitor_instance_hostname" {
2+
description = "Private DNS name of the EC2 instance."
3+
value = module.postgres_monitor.instance_hostname
4+
}
5+
6+
output "postgres_monitor_public_ip" {
7+
description = "Public IP address of the EC2 instance."
8+
value = module.postgres_monitor.public_ip
9+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "instance_hostname" {
2+
description = "Private DNS name of the EC2 instance."
3+
value = aws_instance.postgres_monitor.private_dns
4+
}
5+
6+
output "public_ip" {
7+
description = "Public IP address of the EC2 instance."
8+
value = aws_instance.postgres_monitor.public_ip
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
variable "ssh_key_name" {
2+
description = "The name of the SSH key pair to use for the EC2 instance."
3+
type = string
4+
default = "postgres-monitor-key"
5+
}
6+
7+
variable "ssh_public_key_path" {
8+
description = "Path to the SSH public key file to upload to AWS."
9+
type = string
10+
default = "~/.ssh/aws.pub"
11+
}
12+
13+
variable "instance_name" {
14+
description = "Value of the EC2 instance's Name tag."
15+
type = string
16+
default = "postgres-monitor"
17+
}
18+
19+
variable "instance_type" {
20+
description = "The EC2 instance's type."
21+
type = string
22+
default = "t2.micro"
23+
}
24+
25+
variable "hostname" {
26+
description = "The hostname to set for the EC2 instance."
27+
type = string
28+
default = "postgres-monitor"
29+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
terraform {
2+
required_providers {
3+
scaleway = {
4+
source = "scaleway/scaleway"
5+
}
6+
}
7+
}
8+
9+
# Get available bare metal offer
10+
data "scaleway_baremetal_offer" "offer" {
11+
zone = var.zone
12+
name = var.offer_name
13+
subscription_period = "hourly"
14+
}
15+
16+
# Get Debian 12 OS
17+
data "scaleway_baremetal_os" "debian12" {
18+
# name = "Debian"
19+
# version = "12"
20+
os_id = "83640d93-a0b8-45ad-9c9f-30cae48380a4"
21+
}
22+
23+
# Upload SSH key
24+
resource "scaleway_iam_ssh_key" "main" {
25+
name = var.ssh_key_name
26+
public_key = file(var.ssh_public_key_path)
27+
}
28+
29+
# Create bare metal server with hourly billing
30+
resource "scaleway_baremetal_server" "postgres_primary" {
31+
name = var.server_name
32+
offer = data.scaleway_baremetal_offer.offer.offer_id
33+
zone = var.zone
34+
description = var.description
35+
36+
# Install OS
37+
os = data.scaleway_baremetal_os.debian12.os_id
38+
39+
# Attach SSH key
40+
ssh_key_ids = [scaleway_iam_ssh_key.main.id]
41+
42+
# Cloud-init configuration
43+
# cloud_init = templatefile("${path.module}/../cloudinit/scaleway-cloud-init.yaml", {
44+
# hostname = var.hostname
45+
# ssh_public_key = trimspace(file(var.ssh_public_key_path))
46+
# })
47+
48+
tags = var.tags
49+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
variable "zone" {
2+
description = "Scaleway zone"
3+
type = string
4+
default = "fr-par-2"
5+
}
6+
7+
variable "offer_name" {
8+
description = "Name of the bare metal server offer"
9+
type = string
10+
default = "EM-A116X-SSD"
11+
}
12+
13+
variable "server_name" {
14+
description = "Name of the bare metal server"
15+
type = string
16+
default = "postgres-primary"
17+
}
18+
19+
variable "hostname" {
20+
description = "Hostname for the server"
21+
type = string
22+
default = "postgres-primary"
23+
}
24+
25+
variable "description" {
26+
description = "Description of the server"
27+
type = string
28+
default = "PostgreSQL primary server"
29+
}
30+
31+
variable "ssh_key_name" {
32+
description = "Name for the SSH key in Scaleway"
33+
type = string
34+
default = "postgres-primary-key"
35+
}
36+
37+
variable "ssh_public_key_path" {
38+
description = "Path to the SSH public key file"
39+
type = string
40+
default = "~/.ssh/scaleway.pem.pub"
41+
}
42+
43+
variable "tags" {
44+
description = "Tags to apply to the server"
45+
type = list(string)
46+
default = ["postgres", "primary"]
47+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.92"
6+
}
7+
scaleway = {
8+
source = "scaleway/scaleway"
9+
version = "2.64.0"
10+
}
11+
}
12+
13+
required_version = ">= 1.2"
14+
}

infra/aggregation_mode/terraform/variables.tf

Whitespace-only changes.

0 commit comments

Comments
 (0)