Skip to content

Aws t26 s3 #21

Merged
merged 49 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
acf6a52
update variables
ashle001 Sep 14, 2020
4029f0c
add-main-to-branch
ashle001 Sep 14, 2020
92da786
prevent_destroy=false
ashle001 Sep 14, 2020
5c7b7fa
disable versioning
ashle001 Sep 14, 2020
eefc2ce
force_destroy=true
ashle001 Sep 14, 2020
7ad2acc
remove-Prevent_destroy
ashle001 Sep 14, 2020
60bc56c
disable-main
ashle001 Sep 14, 2020
6100615
new_main
ashle001 Sep 14, 2020
74a119b
add tag
ashle001 Sep 14, 2020
bae2f55
update logging prefix
ashle001 Sep 14, 2020
4c4d67c
fix access_log prefix
ashle001 Sep 15, 2020
f8ea902
attach bucket policy
ashle001 Sep 15, 2020
350db34
Added KMS CMK key
Sep 21, 2020
bb1f4d8
Merge branch 't26_s3-feat' of github.e.it.census.gov:terraform-module…
ashle001 Sep 21, 2020
a96c00d
Modified KMS CMK key and local file
Sep 22, 2020
359df1a
Merge branch 't26_s3-feat' of github.e.it.census.gov:terraform-module…
ashle001 Sep 22, 2020
17bc30d
add create kms key
ashle001 Sep 22, 2020
0f68a2c
remove deuplicate key alias
ashle001 Sep 22, 2020
fd25974
fix key name
ashle001 Sep 22, 2020
ff8bb31
bucket_name
ashle001 Sep 22, 2020
10824aa
prefixes
ashle001 Sep 22, 2020
094fbee
key policy
ashle001 Sep 22, 2020
ab1b7ce
locals
ashle001 Sep 22, 2020
2b54983
locals2
ashle001 Sep 22, 2020
7b618ad
key-id
ashle001 Sep 22, 2020
d68bb70
key-id2
ashle001 Sep 22, 2020
6c2f069
key-id3
ashle001 Sep 22, 2020
b84d639
destroy true
ashle001 Sep 22, 2020
de678d7
add bucket policy
ashle001 Sep 24, 2020
873e16c
remove delay
ashle001 Sep 24, 2020
61bc94d
add ssl enforcement policy
ashle001 Sep 24, 2020
70aa3c6
add ssl enforcement policy2
ashle001 Sep 24, 2020
712b61d
fix logging prefix
ashle001 Sep 24, 2020
5ab18c2
fix statement
ashle001 Sep 24, 2020
8aba073
fix kms key description
ashle001 Sep 24, 2020
394d221
fix brackets
badra001 Sep 24, 2020
64c0fc3
update doc
badra001 Sep 24, 2020
e3009dc
add policy delay
ashle001 Sep 24, 2020
49f36d7
fix policy
ashle001 Sep 24, 2020
ad4058e
remove delay
ashle001 Sep 24, 2020
9fea093
fix delay
ashle001 Sep 24, 2020
c606874
fix delay2
ashle001 Sep 24, 2020
cd0a123
add delay on block access
ashle001 Sep 25, 2020
454e497
add delay on block access
ashle001 Sep 25, 2020
bd146f9
remove old kms block
ashle001 Oct 1, 2020
93bd3e8
remove main.tf.bak
ashle001 Oct 1, 2020
7ce5322
run pre-commit
badra001 Oct 1, 2020
a356b49
remove locals.tf
ashle001 Oct 1, 2020
25f23dc
Merge branch 'aws-t26-s3' of github.e.it.census.gov:terraform-modules…
ashle001 Oct 1, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
= Usage =
module "mybucket" {
# About aws-t26-s3

# Usage

```hcl
module "mybucket" {
source = "git@github.e.it.census.gov:terraform-modules/aws-t26-s3.git"
bucket\_name = "myt26bucket"
bucket_name = "myt26bucket"
}
```

## Requirements

Expand All @@ -20,6 +25,7 @@ No requirements.

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| access\_log\_bucket | Server Access Logging Bucket ID | `string` | n/a | yes |
| access\_log\_bucket\_prefix | Access log bucket prefix, to which the bucket name will be appended to make the target\_prefix | `string` | `"s3"` | no |
| bucket\_folders | List of folders (keys) to create after creation of bucket | `list(string)` | `[]` | no |
| bucket\_name | AWS Bucket Name | `string` | n/a | yes |
Expand Down
104 changes: 89 additions & 15 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
/* = About =
* = Usage =
* module "mybucket" {
* source = "git@github.e.it.census.gov:terraform-modules/aws-t26-s3.git"
*
* bucket_name = "myt26bucket"
* }
*
*/
/*
* # About aws-t26-s3
*
* # Usage
*
* ```hcl
* module "mybucket" {
* source = "git@github.e.it.census.gov:terraform-modules/aws-t26-s3.git"
*
* bucket_name = "myt26bucket"
* }
* ```
*
*/

locals {
enforced_tags = {
"boc:safeguard" = "title26"
}
#account_id = data.aws_caller_identity.current.account_id
#aws_region = data.aws_region.current.name
#partition = data.aws_arn.current.partition
#name = (var.name != "" && var.name != null) ? var.name : format("k-kms-%v-%v", var.bucket_name, local.aws_region)
name = var.bucket_name

# kms_key_arn_exists = var.kms_key_arn != "" && var.kms_key_arn != null
kms_key_arn = aws_kms_key.key.arn
kms_key_name = format("%s%s", local._prefixes["kms"], var.bucket_name)

base_tags = {
# "boc:tf_module_version" = var._module_version
"boc:created_by" = "terraform"
}
}

#---
Expand All @@ -24,8 +43,10 @@ resource "aws_s3_bucket" "this" {
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_key_id
sse_algorithm = "aws:kms"
kms_master_key_id = aws_kms_key.key.key_id
#kms_master_key_id = var.kms_key_id
#kms_master_key_id = "k-kms-", var.bucket_name
sse_algorithm = "aws:kms"
}
}
}
Expand All @@ -36,11 +57,11 @@ resource "aws_s3_bucket" "this" {

logging {
target_bucket = var.access_log_bucket
target_prefix = format("%s/%s/", var.access_log_bucket_prefix, var.access_log_bucket)
target_prefix = format("%s/%s/", var.access_log_bucket_prefix, var.bucket_name)
}

lifecycle {
prevent_destroy = true
prevent_destroy = false
}

tags = merge(
Expand Down Expand Up @@ -81,9 +102,42 @@ data "aws_iam_policy_document" "this" {
values = ["true"]
}
}
statement {
sid = "enforceSSL"
effect = "Deny"
actions = ["s3:*"]
principals {
type = "AWS"
identifiers = ["*"]
}
resources = [aws_s3_bucket.this.arn, "${aws_s3_bucket.this.arn}/*"]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}

resource "null_resource" "s3_create_wait" {
#---
# apply policy to bucket and public access block policy to bucket
#---
resource "aws_s3_bucket_policy" "policy" {
bucket = aws_s3_bucket.this.bucket
policy = data.aws_iam_policy_document.this.json
depends_on = [null_resource.policy_delay]
}

resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
depends_on = [aws_s3_bucket_policy.policy]
}

resource "null_resource" "policy_delay" {
triggers = {
bucket = aws_s3_bucket.this.id
}
Expand All @@ -99,5 +153,25 @@ resource "aws_s3_bucket_object" "this_objects" {
key = format("%s/", element(var.bucket_folders, count.index))
source = "/dev/null"

depends_on = [null_resource.s3_create_wait]
depends_on = [null_resource.policy_delay]
}

#---
# create a key and alias if not specified
#---
resource "aws_kms_key" "key" {
description = "KMS CMK for S3 bucket ${local.name}"
enable_key_rotation = true
#policy = data.aws_iam_policy_document.key.json

tags = merge(
local.base_tags,
{ "Name" = local.kms_key_name },
var.tags
)
}

resource "aws_kms_alias" "key" {
name = "alias/${local.kms_key_name}"
target_key_id = aws_kms_key.key.key_id
}
12 changes: 12 additions & 0 deletions prefixes.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
locals {
_prefixes = {
"efs" = "v-efs-"
"s3" = "v-s3-"
"ebs" = "v-ebs-"
"kms" = "k-kms-"
"role" = "r-"
"policy" = "p-"
"security-group" = ""
# "security-group" = "sg-"
}
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ variable "access_log_bucket_prefix" {
type = string
default = "s3"
}

variable "access_log_bucket" {
description = "Server Access Logging Bucket ID"
type = string
# default = null
}