-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_backup.tf
84 lines (77 loc) · 2.04 KB
/
data_backup.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
################################################################################
# Resources for backup/restore of mongo database from S3
################################################################################
# Look up bucket for mongo backups
data "aws_s3_bucket" "backup" {
bucket=var.bucket_name
}
# Create policy to permit IO with backup bucket.
resource "aws_iam_policy" "s3_backup" {
name = "s3_backup_${random_pet.app.id}"
path = "/"
description = "Allow EC2 instance to read/write backup bucket"
policy = jsonencode(
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"s3:ListBucket",
"s3:ListAllMyBuckets"
],
"Resource":"arn:aws:s3:::*"
},
{
"Effect":"Deny",
"Action":[
"s3:ListBucket"
],
"NotResource":[
"${data.aws_s3_bucket.backup.arn}",
"${data.aws_s3_bucket.backup.arn}/*"
]
},
{
"Effect":"Allow",
"Action":[
"s3:ListBucket",
"s3:GetObject",
"s3:DeleteObject",
"s3:PutObject"
],
"Resource":[
"${data.aws_s3_bucket.backup.arn}",
"${data.aws_s3_bucket.backup.arn}/*"
]
}
]
}
)
}
# Role for EC2 instance to assume
resource "aws_iam_role" "s3_backup" {
name = "s3_backup_${random_pet.app.id}"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
# Attach managed policy to role
resource "aws_iam_role_policy_attachment" "s3_backup" {
role = aws_iam_role.s3_backup.name
policy_arn = aws_iam_policy.s3_backup.arn
}
resource "aws_iam_instance_profile" "s3_backup" {
name = "s3_mongo_backup_${random_pet.app.id}"
role = aws_iam_role.s3_backup.name
}