-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaws-postgres.tf
More file actions
92 lines (74 loc) · 1.79 KB
/
aws-postgres.tf
File metadata and controls
92 lines (74 loc) · 1.79 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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
variable "region" {
type = string
default = "us-west-2"
}
variable "db_password" {
description = "RDS root user password"
type = string
sensitive = true
default = "passw0rd"
}
variable "vpc_id" {
type = string
}
variable "cidr_block" {
type = string
}
variable "subnet_id" {
type = string
}
# Configure the AWS Provider
provider "aws" {
region = var.region
# access_key = "my-access-key"
# secret_key = "my-secret-key"
}
resource "aws_subnet" "subnet" {
vpc_id = var.vpc_id
availability_zone = "us-west-2b"
# cidr_block = "10.0.2.0/24"
cidr_block = "172.31.16.0/20"
}
resource "aws_db_subnet_group" "default" {
name = "main"
subnet_ids = [var.subnet_id, aws_subnet.subnet.id]
}
resource "aws_security_group" "allow_vpc_only" {
name = "allow_vpc_only"
vpc_id = var.vpc_id
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [var.cidr_block]
}
}
# Create PostgreSQL DB in AWS - Only accessible within the internal VPC
resource "aws_db_instance" "cpln-database-terraform" {
identifier = "cpln-database-terraform"
instance_class = "db.t3.micro"
allocated_storage = 20
engine = "postgres"
engine_version = "17.6"
username = "postgres"
password = var.db_password
db_subnet_group_name = aws_db_subnet_group.default.name
vpc_security_group_ids = [aws_security_group.allow_vpc_only.id]
publicly_accessible = false
skip_final_snapshot = true
apply_immediately = true
}
output "postgres_address" {
value = aws_db_instance.cpln-database-terraform.address
}
output "postgres_port" {
value = aws_db_instance.cpln-database-terraform.port
}