Skip to content

Commit 38a74db

Browse files
author
Taylor McClure
committed
init commit
1 parent 0b4ed6b commit 38a74db

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# terraform-s3-rbac
2+
3+
## Purpose
4+
5+
Terraform module to allow users to easily create buckets in their own account that gives another account's principal(s) full read/write access to their buckets.
6+
7+
## Usage
8+
9+
```terraform
10+
module "s3_rbac" {
11+
source = "https://github.com:scribd/terraform-s3-rbac.git"
12+
13+
role_name = "remote_s3_rbac"
14+
s3_bucket_names = ["somename-00", "somename-01", "somename-nn"]
15+
remote_principals_arns = ["arn:aws:iam::1234567890:user/someuser", "arn:aws:iam::1234567890:role/somerole"]
16+
17+
tags = {"key": "value"}
18+
}
19+
```

iam_roles.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
resource "aws_iam_role" "s3_rbac" {
2+
name = var.role_name
3+
assume_role_policy = data.aws_iam_policy_document.s3_bucket_access_role.json
4+
5+
tags = var.tags
6+
}
7+
8+
data "aws_iam_policy_document" "s3_bucket_access_role" {
9+
statement {
10+
effect = "Allow"
11+
actions = ["sts:AssumeRole"]
12+
sid = "terraform0"
13+
principals {
14+
type = "AWS"
15+
identifiers = [for a in var.remote_principals_arns : "${a}"]
16+
}
17+
}
18+
}
19+
20+
data "aws_iam_policy_document" "s3_bucket_access" {
21+
statement {
22+
resources = [for s in var.s3_bucket_names : "arn:aws:s3:::${s}"]
23+
effect = "Allow"
24+
actions = ["s3:*"]
25+
sid = "terraform0"
26+
}
27+
28+
statement {
29+
resources = [for s in var.s3_bucket_names : "arn:aws:s3:::${s}/*"]
30+
effect = "Allow"
31+
actions = ["s3:*"]
32+
sid = "terraform1"
33+
}
34+
}
35+
36+
resource "aws_iam_policy" "s3_bucket_access" {
37+
name = "s3_bucket_access"
38+
path = "/"
39+
policy = data.aws_iam_policy_document.s3_bucket_access.json
40+
}
41+
42+
resource "aws_iam_role_policy_attachment" "s3_bucket_access" {
43+
role = aws_iam_role.s3_rbac.name
44+
policy_arn = aws_iam_policy.s3_bucket_access.arn
45+
}

outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "iam_role_arn" {
2+
description = "IAM role arn created to be assumed by remote principal(s)"
3+
value = aws_iam_role.s3_rbac.arn
4+
}
5+
6+
output "s3_bucket_arns" {
7+
description = "Your S3 bucket ARNS"
8+
value = [for v in aws_s3_bucket.s3_buckets : v.arn]
9+
}

provider.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = var.aws_region
3+
}

s3_bucket.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Give an s3 bucket to each bucket name passed to the module
2+
resource "aws_s3_bucket" "s3_buckets" {
3+
for_each = var.s3_bucket_names
4+
5+
bucket = each.value
6+
acl = "private"
7+
tags = var.tags
8+
}
9+
10+
# Make sure no object could ever be public
11+
resource "aws_s3_bucket_public_access_block" "s3_buckets" {
12+
for_each = var.s3_bucket_names
13+
14+
bucket = each.value
15+
16+
block_public_acls = true
17+
block_public_policy = true
18+
restrict_public_buckets = true
19+
ignore_public_acls = true
20+
21+
depends_on = [aws_s3_bucket.s3_buckets]
22+
}

vars.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
variable "aws_region" {
2+
description = "AWS Region"
3+
type = string
4+
default = "us-east-2"
5+
}
6+
7+
variable "s3_bucket_names" {
8+
type = set(string)
9+
description = "one or many of your s3 bucket name(s)"
10+
}
11+
12+
variable "remote_principals_arns" {
13+
type = list(string)
14+
description = "one or many arns for your remote principals"
15+
}
16+
17+
variable "tags" {
18+
type = map(string)
19+
default = { "terraform" : "true" }
20+
description = "custom tags you want to provide for the resources created here"
21+
}
22+
23+
variable "role_name" {
24+
type = string
25+
description = "name to give your role that will be able to be assume by remote principal(s)"
26+
}

0 commit comments

Comments
 (0)