|
| 1 | +terraform { |
| 2 | + required_version = ">= 1.5.0" |
| 3 | + |
| 4 | + required_providers { |
| 5 | + aws = { |
| 6 | + source = "hashicorp/aws" |
| 7 | + version = "~> 5.0" |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +provider "aws" { |
| 13 | + region = var.aws_region |
| 14 | + |
| 15 | + default_tags { |
| 16 | + tags = { |
| 17 | + Project = "postgres-ai-monitoring" |
| 18 | + Environment = var.environment |
| 19 | + ManagedBy = "terraform" |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +# Data sources |
| 25 | +data "aws_ami" "ubuntu" { |
| 26 | + most_recent = true |
| 27 | + owners = ["099720109477"] # Canonical |
| 28 | + |
| 29 | + filter { |
| 30 | + name = "name" |
| 31 | + values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"] |
| 32 | + } |
| 33 | + |
| 34 | + filter { |
| 35 | + name = "virtualization-type" |
| 36 | + values = ["hvm"] |
| 37 | + } |
| 38 | + |
| 39 | + filter { |
| 40 | + name = "root-device-type" |
| 41 | + values = ["ebs"] |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +# VPC (simplified - use default or create minimal) |
| 46 | +resource "aws_vpc" "main" { |
| 47 | + cidr_block = "10.0.0.0/16" |
| 48 | + enable_dns_hostnames = true |
| 49 | + enable_dns_support = true |
| 50 | + |
| 51 | + tags = { |
| 52 | + Name = "${var.environment}-postgres-ai-vpc" |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +resource "aws_subnet" "main" { |
| 57 | + vpc_id = aws_vpc.main.id |
| 58 | + cidr_block = "10.0.1.0/24" |
| 59 | + availability_zone = data.aws_availability_zones.available.names[0] |
| 60 | + map_public_ip_on_launch = true |
| 61 | + |
| 62 | + tags = { |
| 63 | + Name = "${var.environment}-postgres-ai-subnet" |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +data "aws_availability_zones" "available" { |
| 68 | + state = "available" |
| 69 | +} |
| 70 | + |
| 71 | +resource "aws_internet_gateway" "main" { |
| 72 | + vpc_id = aws_vpc.main.id |
| 73 | + |
| 74 | + tags = { |
| 75 | + Name = "${var.environment}-postgres-ai-igw" |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +resource "aws_route_table" "main" { |
| 80 | + vpc_id = aws_vpc.main.id |
| 81 | + |
| 82 | + route { |
| 83 | + cidr_block = "0.0.0.0/0" |
| 84 | + gateway_id = aws_internet_gateway.main.id |
| 85 | + } |
| 86 | + |
| 87 | + tags = { |
| 88 | + Name = "${var.environment}-postgres-ai-rt" |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +resource "aws_route_table_association" "main" { |
| 93 | + subnet_id = aws_subnet.main.id |
| 94 | + route_table_id = aws_route_table.main.id |
| 95 | +} |
| 96 | + |
| 97 | +# Security Group |
| 98 | +resource "aws_security_group" "main" { |
| 99 | + name = "${var.environment}-postgres-ai-sg" |
| 100 | + description = "Security group for postgres_ai monitoring EC2" |
| 101 | + vpc_id = aws_vpc.main.id |
| 102 | + |
| 103 | + # SSH access |
| 104 | + ingress { |
| 105 | + description = "SSH" |
| 106 | + from_port = 22 |
| 107 | + to_port = 22 |
| 108 | + protocol = "tcp" |
| 109 | + cidr_blocks = var.allowed_ssh_cidr |
| 110 | + } |
| 111 | + |
| 112 | + # Grafana |
| 113 | + ingress { |
| 114 | + description = "Grafana" |
| 115 | + from_port = 3000 |
| 116 | + to_port = 3000 |
| 117 | + protocol = "tcp" |
| 118 | + cidr_blocks = var.allowed_cidr_blocks |
| 119 | + } |
| 120 | + |
| 121 | + # Allow all outbound |
| 122 | + egress { |
| 123 | + from_port = 0 |
| 124 | + to_port = 0 |
| 125 | + protocol = "-1" |
| 126 | + cidr_blocks = ["0.0.0.0/0"] |
| 127 | + } |
| 128 | + |
| 129 | + tags = { |
| 130 | + Name = "${var.environment}-postgres-ai-sg" |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +# EBS Volume for data persistence |
| 135 | +resource "aws_ebs_volume" "data" { |
| 136 | + availability_zone = aws_subnet.main.availability_zone |
| 137 | + size = var.data_volume_size |
| 138 | + type = "gp3" |
| 139 | + encrypted = true |
| 140 | + |
| 141 | + tags = { |
| 142 | + Name = "${var.environment}-postgres-ai-data" |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +# EC2 Instance |
| 147 | +resource "aws_instance" "main" { |
| 148 | + ami = data.aws_ami.ubuntu.id |
| 149 | + instance_type = var.instance_type |
| 150 | + subnet_id = aws_subnet.main.id |
| 151 | + |
| 152 | + vpc_security_group_ids = [aws_security_group.main.id] |
| 153 | + |
| 154 | + key_name = var.ssh_key_name |
| 155 | + |
| 156 | + root_block_device { |
| 157 | + volume_size = 30 |
| 158 | + volume_type = "gp3" |
| 159 | + encrypted = true |
| 160 | + } |
| 161 | + |
| 162 | + user_data = templatefile("${path.module}/user_data.sh", { |
| 163 | + grafana_password = var.grafana_password |
| 164 | + postgres_ai_api_key = var.postgres_ai_api_key |
| 165 | + monitoring_instances = var.monitoring_instances |
| 166 | + enable_demo_db = var.enable_demo_db |
| 167 | + }) |
| 168 | + |
| 169 | + tags = { |
| 170 | + Name = "${var.environment}-postgres-ai-monitoring" |
| 171 | + } |
| 172 | + |
| 173 | + lifecycle { |
| 174 | + ignore_changes = [user_data] |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +# Attach EBS volume |
| 179 | +resource "aws_volume_attachment" "data" { |
| 180 | + device_name = "/dev/sdf" |
| 181 | + volume_id = aws_ebs_volume.data.id |
| 182 | + instance_id = aws_instance.main.id |
| 183 | +} |
| 184 | + |
| 185 | +# Elastic IP (optional, for stable IP) |
| 186 | +resource "aws_eip" "main" { |
| 187 | + count = var.use_elastic_ip ? 1 : 0 |
| 188 | + instance = aws_instance.main.id |
| 189 | + domain = "vpc" |
| 190 | + |
| 191 | + tags = { |
| 192 | + Name = "${var.environment}-postgres-ai-eip" |
| 193 | + } |
| 194 | +} |
| 195 | + |
0 commit comments