Skip to content

Commit cc1cdd7

Browse files
committed
Change to manage multiple S3 buckets
1 parent b9649c5 commit cc1cdd7

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-38
lines changed

main.tf

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,78 @@
11
/**
2-
* AWS S3 Terraform Module
2+
* AWS S3 Bucket Terraform Module
33
* =====================
44
*
5-
* Create AWS S3 bucket and set policy
5+
* Create multiple AWS S3 buckets and set policies
66
*
77
* Usage:
88
* ------
9-
*
10-
* module "s3" {
11-
* source = "../tf_s3"
12-
* name = "apps"
13-
* environment = "dev01"
9+
* '''hcl
10+
* module "s3-bucket" {
11+
* source = "../s3-bucket"
12+
* names = ["images","thumbnails"]
13+
* environment = "dev"
14+
* org = "corp"
1415
* }
16+
* '''
1517
**/
1618

1719
# TODO: Allow pass policy via variable. Default empty policy. If can be done, otherwise 2 modules
1820
# create s3 bucket and set policy
19-
resource "aws_s3_bucket" "bucket" {
20-
#bucket = "dmp-rpns-${var.s3_env_map[var.env]}"
21-
# TODO: Setup namespaced condition
22-
bucket = "${format("%s-%s", var.environment, var.name)}"
23-
acl = "private"
21+
# TODO: setup encryption
22+
23+
resource "aws_s3_bucket" "this" {
24+
count = "${length(var.names)}"
25+
bucket = "${var.namespaced ?
26+
format("%s-%s-%s", var.org, var.environment, element(var.names, count.index)) :
27+
format("%s-%s", var.org, element(var.names, count.index))}"
28+
acl = "${var.public ? public-read : private}"
2429
versioning {
25-
enabled = true
30+
enabled = "${var.versioned}"
2631
}
32+
#acceleration_status
33+
#force_destroy = true
34+
#lifecycle_rule {}
35+
#logging {
36+
# target_bucket
37+
# target_prefix
38+
#}
39+
#region
40+
#request_payer
41+
#replication_configuration {}
2742
tags = "${ merge(
2843
var.tags,
2944
map("Name", var.namespaced ?
30-
format("%s-%s-s3-bucket", var.environment, var.name) :
31-
format("%s-s3-bucket", var.name) ),
45+
format("%s-%s-s3-bucket", var.environment, element(var.names, count.index)) :
46+
format("%s-s3-bucket", element(var.names, count.index)) ),
3247
map("Environment", var.environment),
3348
map("Terraform", "true") )}"
3449
}
50+
3551
/*
3652
data "template_file" "policy_s3_bucket" {
53+
# TODO: add condition to select public or private template
54+
# or 2 data and condition in policy for which data to use
3755
template = "${file("${path.module}/files/policy_s3_bucket.json")}"
3856
vars = {
39-
name = "${aws_s3_bucket.bucket.bucket}"
57+
name = "${aws_s3_bucket.this.bucket}"
4058
principal = "${var.principal}"
4159
}
4260
}
4361
4462
resource "aws_s3_bucket_policy" "bucket_policy" {
45-
bucket = "${aws_s3_bucket.bucket.id}"
63+
bucket = "${aws_s3_bucket.this.id}"
4664
policy = "${data.template_file.policy_s3_bucket.rendered}"
4765
}
4866
*/
67+
68+
#resource "aws_s3_bucket_notification"
69+
70+
/*
71+
resource "aws_s3_bucket_object" "this" {
72+
count = "${length(var.files)}"
73+
bucket = "${aws_s3_bucket.this.id}"
74+
key = "${element(keys(var.files), count.index)}"
75+
source = "${lookup(var.files, element(keys(var.files), count.index))}"
76+
etag = "${md5(file("${lookup(var.files, element(keys(var.files), count.index))}"))}"
77+
}
78+
*/

outputs.tf

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
// AWS S3 Bucket Name
2-
output "s3_bucket_name" {
3-
value = "${aws_s3_bucket.bucket.id}"
4-
}
51

6-
// AWS S3 Bucket ARN
7-
output "s3_bucket_arn" {
8-
value = "${aws_s3_bucket.bucket.arn}"
2+
output "arn" {
3+
description = "List of AWS S3 Bucket ARNs"
4+
value = "${aws_s3_bucket.this.*.arn}"
5+
}
6+
output "domain_name" {
7+
description = "List of AWS S3 Bucket Domain Names"
8+
value = "${aws_s3_bucket.this.*.bucket_domain_name}"
99
}
10-
// AWS S3 Bucket Domain Name
11-
output "s3_bucket_domain_name" {
12-
value = "${aws_s3_bucket.bucket.bucket_domain_name}"
10+
output "hosted_zone_id" {
11+
description = "List of AWS S3 Bucket Hosted Zone IDs"
12+
value = "${aws_s3_bucket.this.*.hosted_zone_id}"
1313
}
14-
// AWS S3 Bucket Region
15-
output "s3_bucket_region" {
16-
value = "${aws_s3_bucket.bucket.region}"
14+
output "id" {
15+
description = "List of AWS S3 Bucket IDs"
16+
value = "${aws_s3_bucket.this.*.id}"
1717
}
18-
// AWS S3 Bucket ID
19-
output "s3_bucket_id" {
20-
value = "${aws_s3_bucket.bucket.id}"
18+
output "name" {
19+
description = "List of AWS S3 Bucket Names"
20+
value = "${aws_s3_bucket.this.*.id}"
2121
}
22-
// AWS S3 Bucket Hosted Zone ID
23-
output "s3_bucket_hosted_zone_id" {
24-
value = "${aws_s3_bucket.bucket.hosted_zone_id}"
22+
output "region" {
23+
description = "List of AWS S3 Bucket Regions"
24+
value = "${aws_s3_bucket.this.*.region}"
2525
}
26+
27+
#aws_s3_bucket_object.this.id
28+
#aws_s3_bucket_object.this.etag
29+
#aws_s3_bucket_object.this.version_id

variables.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11

2+
23
// Standard Variables
34

4-
variable "name" {
5-
description = "Name"
5+
variable "names" {
6+
description = "List of S3 bucket names"
7+
type = "list"
68
}
79
variable "environment" {
810
description = "Environment (ex: dev, qa, stage, prod)"
@@ -16,8 +18,21 @@ variable "tags" {
1618
default = {}
1719
}
1820

21+
variable "org" {
22+
description = "Organization name to prefix S3 buckets with"
23+
}
24+
1925
// Module specific Variables
2026

2127
variable "principal" {
2228
description = "principal"
29+
default = "*"
30+
}
31+
variable "public" {
32+
description = "Allow public read access to bucket"
33+
default = false
34+
}
35+
variable "versioned" {
36+
description = "Version the bucket"
37+
default = false
2338
}

0 commit comments

Comments
 (0)