Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 77116ea

Browse files
committed
terraform: s3: enable S3 Glacier backups
This will replicate any incoming data to S3 bucket into a backup bucket with "Glacier Deep Archive" storage type [1]. This storage type costs $ 0.00099 / GB / month. It's dirt cheap and it's made for cases where rare access is required, like once or twice a year. Only drawback of this storage type is the retriving time. It might take up to 12 hours to retrive a file storage in Deep Archive mode. Storing files in standard S3 storage already has a redundancy, so having a backup is a fall back of a fall back if data ever gets lost at some point. [1] https://aws.amazon.com/blogs/aws/new-amazon-s3-storage-class-glacier-deep-archive/
1 parent 192449f commit 77116ea

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

terraform/roles.tf

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,67 @@ data "template_file" "qareports_role_policy_s3" {
3131
environment = "${var.environment}"
3232
}
3333
}
34+
35+
#
36+
# Role needed so S3 can replicate objects in backup bucket
37+
#
38+
resource "aws_iam_role" "qareports_s3_replication_role" {
39+
name = "QAREPORTS_${title(var.environment)}S3ReplicationRole"
40+
41+
assume_role_policy = <<POLICY
42+
{
43+
"Version": "2012-10-17",
44+
"Statement": [{
45+
"Action": "sts:AssumeRole",
46+
"Principal": {
47+
"Service": "s3.amazonaws.com"
48+
},
49+
"Effect": "Allow",
50+
"Sid": ""
51+
}]
52+
}
53+
POLICY
54+
}
55+
56+
resource "aws_iam_policy" "qareports_s3_replication_role_policy" {
57+
name = "QAREPORTS_${title(var.environment)}S3ReplicationRolePolicy"
58+
59+
policy = <<POLICY
60+
{
61+
"Version": "2012-10-17",
62+
"Statement": [{
63+
"Action": [
64+
"s3:GetReplicationConfiguration",
65+
"s3:ListBucket"
66+
],
67+
"Effect": "Allow",
68+
"Resource": [
69+
"${aws_s3_bucket.qareports_s3_bucket.arn}"
70+
]
71+
},
72+
{
73+
"Action": [
74+
"s3:GetObjectVersion",
75+
"s3:GetObjectVersionAcl"
76+
],
77+
"Effect": "Allow",
78+
"Resource": [
79+
"${aws_s3_bucket.qareports_s3_bucket.arn}/*"
80+
]
81+
},
82+
{
83+
"Action": [
84+
"s3:ReplicateObject",
85+
"s3:ReplicateDelete"
86+
],
87+
"Effect": "Allow",
88+
"Resource": "${aws_s3_bucket.qareports_s3_bucket_backup.arn}/*"
89+
}]
90+
}
91+
POLICY
92+
}
93+
94+
resource "aws_iam_role_policy_attachment" "qareports_s3_replication_role_policy_attachment" {
95+
role = "${aws_iam_role.qareports_s3_replication_role.name}"
96+
policy_arn = "${aws_iam_policy.qareports_s3_replication_role_policy.arn}"
97+
}

terraform/s3.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,33 @@ resource "aws_s3_bucket" "qareports_s3_bucket" {
33
tags = {
44
Name = "${var.environment}-qareports-storage"
55
}
6+
7+
versioning {
8+
enabled = true
9+
}
10+
11+
replication_configuration {
12+
role = "${aws_iam_role.qareports_s3_replication_role.arn}"
13+
14+
rules {
15+
id = "${var.environment}-qareports-s3-replication-rule"
16+
status = "Enabled"
17+
18+
destination {
19+
bucket = "${aws_s3_bucket.qareports_s3_bucket_backup.arn}"
20+
21+
# DEEP_ARCHIVE means using AWS S3 Glacier Deep Archive
22+
# ref: https://aws.amazon.com/blogs/aws/new-amazon-s3-storage-class-glacier-deep-archive/
23+
storage_class = "DEEP_ARCHIVE"
24+
}
25+
}
26+
}
27+
}
28+
29+
resource "aws_s3_bucket" "qareports_s3_bucket_backup" {
30+
bucket = "${var.environment}-qareports-storage-backup"
31+
32+
versioning {
33+
enabled = true
34+
}
635
}

terraform/templates/role_policy.json.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
],
1515
"Resource": [
1616
"arn:aws:s3:::${environment}-qareports-storage",
17-
"arn:aws:s3:::${environment}-qareports-storage/*"
17+
"arn:aws:s3:::${environment}-qareports-storage/*",
18+
"arn:aws:s3:::${environment}-qareports-storage-backup",
19+
"arn:aws:s3:::${environment}-qareports-storage-backup/*"
1820
]
1921
}
2022
]

0 commit comments

Comments
 (0)