|
1 | 1 | /**
|
2 |
| - * AWS S3 Terraform Module |
| 2 | + * AWS S3 Bucket Terraform Module |
3 | 3 | * =====================
|
4 | 4 | *
|
5 |
| - * Create AWS S3 bucket and set policy |
| 5 | + * Create multiple AWS S3 buckets and set policies |
6 | 6 | *
|
7 | 7 | * Usage:
|
8 | 8 | * ------
|
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" |
14 | 15 | * }
|
| 16 | + * ''' |
15 | 17 | **/
|
16 | 18 |
|
17 | 19 | # TODO: Allow pass policy via variable. Default empty policy. If can be done, otherwise 2 modules
|
18 | 20 | # 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}" |
24 | 29 | versioning {
|
25 |
| - enabled = true |
| 30 | + enabled = "${var.versioned}" |
26 | 31 | }
|
| 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 {} |
27 | 42 | tags = "${ merge(
|
28 | 43 | var.tags,
|
29 | 44 | 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)) ), |
32 | 47 | map("Environment", var.environment),
|
33 | 48 | map("Terraform", "true") )}"
|
34 | 49 | }
|
| 50 | + |
35 | 51 | /*
|
36 | 52 | 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 |
37 | 55 | template = "${file("${path.module}/files/policy_s3_bucket.json")}"
|
38 | 56 | vars = {
|
39 |
| - name = "${aws_s3_bucket.bucket.bucket}" |
| 57 | + name = "${aws_s3_bucket.this.bucket}" |
40 | 58 | principal = "${var.principal}"
|
41 | 59 | }
|
42 | 60 | }
|
43 | 61 |
|
44 | 62 | resource "aws_s3_bucket_policy" "bucket_policy" {
|
45 |
| - bucket = "${aws_s3_bucket.bucket.id}" |
| 63 | + bucket = "${aws_s3_bucket.this.id}" |
46 | 64 | policy = "${data.template_file.policy_s3_bucket.rendered}"
|
47 | 65 | }
|
48 | 66 | */
|
| 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 | +*/ |
0 commit comments