-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_backing.tf
82 lines (75 loc) · 2.03 KB
/
data_backing.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
################################################################################
# Resources to allow reading backing data from data prep into mongo database
################################################################################
# Look up bucket
data "aws_s3_bucket" "backing" {
bucket=var.bucket_name
}
# Create policy & role to permit reading from the backing data bucket.
resource "aws_iam_policy" "s3_read_only" {
name = "s3_read_${random_pet.app.id}"
path = "/"
description = "Allow EC2 instance to read a specified 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.backing.arn}",
"${data.aws_s3_bucket.backing.arn}/*"
]
},
{
"Effect":"Allow",
"Action":[
"s3:ListBucket",
"s3:GetObject"
],
"Resource":[
"${data.aws_s3_bucket.backing.arn}",
"${data.aws_s3_bucket.backing.arn}/*"
]
}
]
}
)
}
# Role that the EC2 instance can assume
resource "aws_iam_role" "s3_read_only" {
name = "s3_read_${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_read_only" {
role = aws_iam_role.s3_read_only.name
policy_arn = aws_iam_policy.s3_read_only.arn
}
resource "aws_iam_instance_profile" "s3_read_bucket" {
name = "s3_reading_${random_pet.app.id}"
role = aws_iam_role.s3_read_only.name
}