-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.tf
119 lines (101 loc) · 2.92 KB
/
main.tf
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
}
resource "aws_subnet" "secondary" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-west-2b"
}
resource "aws_security_group" "example" {
name = "example"
description = "Example security group"
vpc_id = aws_vpc.main.id
ingress {
description = "TLS from VPC"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.0.0.0/16"]
}
}
resource "aws_rds_cluster" "example" {
cluster_identifier = "aurora-cluster-demo"
engine = "aurora-postgresql"
engine_version = "13.7"
availability_zones = ["us-west-2a", "us-west-2b"]
database_name = "postgres"
master_username = "postgres"
master_password = aws_secretsmanager_secret_version.db_password.secret_string
preferred_backup_window = "07:00-09:00"
vpc_security_group_ids = [aws_security_group.example.id]
db_subnet_group_name = aws_db_subnet_group.example.name
}
resource "aws_db_subnet_group" "example" {
name = "main"
subnet_ids = [aws_subnet.main.id, aws_subnet.secondary.id]
tags = {
Name = "My database subnet group"
}
}
resource "aws_rds_cluster_instance" "cluster_instances" {
count = 2
identifier = "aurora-cluster-demo-${count.index}"
cluster_identifier = aws_rds_cluster.example.id
instance_class = "db.r4.large"
engine = "aurora-postgresql"
engine_version = "13.7"
publicly_accessible = false
}
resource "aws_secretsmanager_secret" "db_password" {
name = "DB_PASSWORD"
}
resource "aws_secretsmanager_secret_version" "db_password" {
secret_id = aws_secretsmanager_secret.db_password.id
secret_string = var.db_password
}
resource "aws_backup_vault" "example" {
name = "my-backup-vault"
}
resource "aws_backup_plan" "example" {
name = "automated-backup-plan"
rule {
rule_name = "rds-rule"
target_vault_name = aws_backup_vault.example.name
schedule = "cron(0 12 * * ? *)"
}
}
resource "aws_backup_selection" "example" {
name = "automated-backup-selection"
iam_role_arn = aws_iam_role.example.arn
plan_id = aws_backup_plan.example.id
resources = [aws_rds_cluster.example.arn]
}
resource "aws_iam_role" "example" {
name = "backup-service-role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "backup.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "example" {
role = aws_iam_role.example.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSBackupServiceRolePolicyForBackup"
}