-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
204 lines (184 loc) · 5.21 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
provider "aws" {
region = "us-east-1"
}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.5.1"
name = "PrivateS3Vpc"
azs = ["us-east-1a"]
cidr = "10.0.0.0/16"
private_subnets = ["10.0.0.0/20"]
}
resource "aws_s3_bucket" "default" {
for_each = toset(["localonly", "vpcendpointonly", "both"])
bucket_prefix = each.key
}
resource "aws_vpc_endpoint" "s3" {
vpc_endpoint_type = "Gateway"
vpc_id = module.vpc.vpc_id
service_name = "com.amazonaws.us-east-1.s3"
# The route to the VPC endpoint is automatically added
# to the route table when the endpoint is created.
route_table_ids = [module.vpc.private_route_table_ids[0]]
}
resource "aws_vpc_endpoint_policy" "name" {
vpc_endpoint_id = aws_vpc_endpoint.s3.id
policy = <<EOF
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal": "*",
"Action": "s3:*",
"Resource":[
"${aws_s3_bucket.default["vpcendpointonly"].arn}/*",
"${aws_s3_bucket.default["both"].arn}/*"
]
},
{
"Effect" : "Allow",
"Principal" : "*",
"Action" : ["s3:ListBucket", "s3:DeleteBucketPolicy"],
"Resource" : [
"${aws_s3_bucket.default["vpcendpointonly"].arn}",
"${aws_s3_bucket.default["both"].arn}"
]
},
{
"Effect" : "Allow",
"Principal": "*",
"Action":[
"s3:ListAllMyBuckets",
"s3:GetBucketLocation"
],
"Resource" : "*"
}
]
}
EOF
}
module "instance_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "5.1.0"
vpc_id = module.vpc.vpc_id
name = "InstanceSg"
use_name_prefix = true
ingress_with_cidr_blocks = [
{
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = "0.0.0.0/0"
},
]
egress_with_cidr_blocks = [
{
protocol = "-1"
from_port = 0
to_port = 0
cidr_blocks = "0.0.0.0/0"
ipv6_cidr_blocks = "::0/0"
}
]
}
resource "aws_iam_role" "instance_role" {
name_prefix = "S3ReadOnlyInstanceRole"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
resource "aws_iam_role_policy_attachment" "s3_full_access" {
role = aws_iam_role.instance_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
}
# Allow the EC2 instance to delete S3 bucket policy
# to allow `terraform destroy` to succeed.
resource "aws_iam_role_policy" "s3_readonly" {
name_prefix = "S3DeleteBucketPolicy"
role = aws_iam_role.instance_role.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow",
Action = [
"s3:DeleteBucketPolicy",
],
Resource = ["*"]
}]
})
}
resource "aws_iam_instance_profile" "default" {
name_prefix = "S3ReadOnlyInstanceProfile"
role = aws_iam_role.instance_role.id
}
resource "aws_instance" "default" {
ami = "ami-0230bd60aa48260c6" #Amazon Linux 2023
instance_type = "t2.micro"
subnet_id = module.vpc.private_subnets[0]
vpc_security_group_ids = [module.instance_sg.security_group_id]
iam_instance_profile = aws_iam_instance_profile.default.name
}
resource "aws_ec2_instance_connect_endpoint" "default" {
subnet_id = module.vpc.private_subnets[0]
security_group_ids = [module.instance_sg.security_group_id]
}
# Prevent s3:ListBucket action from anywhere apart from
# the VPC gateway endpoint.
#
# This one is a bit tricky because after you applied the policy,
# you can't run Terraform destroy due to the lack of ListBucket
# access. To destroy successfully, run outputs.runBeforeDestroyUsingEC2Instance
# before running `terraform destroy`.
resource "aws_s3_bucket_policy" "vpc_endpoint_only" {
bucket = aws_s3_bucket.default["vpcendpointonly"].id
policy = <<EOF
{
"Version": "2012-10-17",
"Id": "Policy1415115909152",
"Statement": [
{
"Sid": "Access-to-specific-VPCE-only",
"Principal": "*",
"Action": "s3:ListBucket",
"Effect": "Deny",
"Resource": [
"${aws_s3_bucket.default["vpcendpointonly"].arn}"
],
"Condition": {
"StringNotEquals": {
"aws:sourceVpce": "${aws_vpc_endpoint.s3.id}"
}
}
}
]
}
EOF
}
output "s3_buckets" {
value = {
vpcEndpointOnly = aws_s3_bucket.default["vpcendpointonly"].bucket
localOnly = aws_s3_bucket.default["localonly"].bucket
both = aws_s3_bucket.default["both"].bucket
}
}
output "commands" {
value = {
runBeforeDestroyUsingEC2Instance = "aws s3api delete-bucket-policy --bucket ${aws_s3_bucket.default["vpcendpointonly"].bucket}"
}
}
# TEST:
#
# - Run `aws s3 ls s3://{bucketName}` for each of the provided buckets locally and inside
# provided EC2 instance. You will see that `vpcendpointonly*` bucket can only be listed
# on the EC2 instance, `localonly*` bucket can only be listed locally and `both*` can be
# listed on... well, both.