Skip to content

Commit

Permalink
Initial commit (#2)
Browse files Browse the repository at this point in the history
* Initial commit

* Update variables.tf

* Update variables.tf

* First version of the module

* First version of the module
  • Loading branch information
jnonino authored May 20, 2022
1 parent bc68394 commit 558e237
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 52 deletions.
19 changes: 19 additions & 0 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,43 @@ In order to run all checks at any point run the following command:

## Providers

No providers.
| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.15.0 |
| <a name="provider_random"></a> [random](#provider\_random) | 3.2.0 |

## Modules

No modules.

## Resources

No resources.
| Name | Type |
|------|------|
| [aws_s3_bucket.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |
| [aws_s3_bucket_acl.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_acl) | resource |
| [aws_s3_bucket_policy.logs_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource |
| [aws_s3_bucket_public_access_block.logs_block_public_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_public_access_block) | resource |
| [aws_s3_bucket_server_side_encryption_configuration.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_server_side_encryption_configuration) | resource |
| [random_string.random](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string) | resource |
| [aws_iam_policy_document.logs_access_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_aws_principals_identifiers"></a> [aws\_principals\_identifiers](#input\_aws\_principals\_identifiers) | List of identifiers for AWS principals with access to write in the logs bucket | `list(string)` | n/a | yes |
| <a name="input_block_s3_bucket_public_access"></a> [block\_s3\_bucket\_public\_access](#input\_block\_s3\_bucket\_public\_access) | (Optional) If true, public access to the S3 bucket will be blocked. | `bool` | `true` | no |
| <a name="input_enable_s3_bucket_server_side_encryption"></a> [enable\_s3\_bucket\_server\_side\_encryption](#input\_enable\_s3\_bucket\_server\_side\_encryption) | (Optional) If true, server side encryption will be applied. | `bool` | `true` | no |
| <a name="input_name_prefix"></a> [name\_prefix](#input\_name\_prefix) | Name prefix for resources on AWS | `string` | n/a | yes |
| <a name="input_s3_bucket_server_side_encryption_key"></a> [s3\_bucket\_server\_side\_encryption\_key](#input\_s3\_bucket\_server\_side\_encryption\_key) | (Optional) The AWS KMS master key ID used for the SSE-KMS encryption. This can only be used when you set the value of sse\_algorithm as aws:kms. The default aws/s3 AWS KMS master key is used if this element is absent while the sse\_algorithm is aws:kms. | `string` | `"aws/s3"` | no |
| <a name="input_s3_bucket_server_side_encryption_sse_algorithm"></a> [s3\_bucket\_server\_side\_encryption\_sse\_algorithm](#input\_s3\_bucket\_server\_side\_encryption\_sse\_algorithm) | (Optional) The server-side encryption algorithm to use. Valid values are AES256 and aws:kms | `string` | `"aws:kms"` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Resource tags | `map(string)` | `{}` | no |

## Outputs

No outputs.
| Name | Description |
|------|-------------|
| <a name="output_lb_logs_s3_bucket_arn"></a> [lb\_logs\_s3\_bucket\_arn](#output\_lb\_logs\_s3\_bucket\_arn) | LB Logging S3 Bucket ARN |
| <a name="output_lb_logs_s3_bucket_id"></a> [lb\_logs\_s3\_bucket\_id](#output\_lb\_logs\_s3\_bucket\_id) | LB Logging S3 Bucket ID |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4 changes: 0 additions & 4 deletions examples/disabled/main.tf

This file was deleted.

22 changes: 0 additions & 22 deletions examples/enabled/.terraform.lock.hcl

This file was deleted.

4 changes: 0 additions & 4 deletions examples/enabled/main.tf

This file was deleted.

19 changes: 0 additions & 19 deletions examples/enabled/mock_provider.tf

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module "logs_bucket" {
source = "../../"

name_prefix = "test"
aws_principals_identifiers = ["test-user-arn"]
block_s3_bucket_public_access = true
enable_s3_bucket_server_side_encryption = true
s3_bucket_server_side_encryption_sse_algorithm = "aws:kms"
s3_bucket_server_side_encryption_key = "aws/s3"
}
File renamed without changes.
88 changes: 88 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#------------------------------------------------------------------------------
# S3 BUCKET - For access logs
#------------------------------------------------------------------------------
resource "random_string" "random" {
length = 7
lower = true
number = false
upper = false
special = false
keepers = {
name_prefix = var.name_prefix
}
}

resource "aws_s3_bucket" "logs" {
bucket = lower("${random_string.random.keepers.name_prefix}-logs-${random_string.random.result}")
tags = merge(
var.tags,
{
Name = lower("${random_string.random.keepers.name_prefix}-logs-${random_string.random.result}")
},
)
}

resource "aws_s3_bucket_acl" "logs" {
bucket = aws_s3_bucket.logs.id
acl = "log-delivery-write"
}

resource "aws_s3_bucket_server_side_encryption_configuration" "logs" {
count = var.enable_s3_bucket_server_side_encryption ? 1 : 0

bucket = aws_s3_bucket.logs.id

rule {
apply_server_side_encryption_by_default {
sse_algorithm = var.s3_bucket_server_side_encryption_sse_algorithm
kms_master_key_id = var.s3_bucket_server_side_encryption_sse_algorithm == "aws:kms" ? var.s3_bucket_server_side_encryption_key : null

}
}
}

#------------------------------------------------------------------------------
# IAM POLICY DOCUMENT - For access logs to the S3 bucket
#------------------------------------------------------------------------------
data "aws_iam_policy_document" "logs_access_policy_document" {
statement {
effect = "Allow"

principals {
type = "AWS"
identifiers = var.aws_principals_identifiers
}

actions = [
"s3:PutObject",
]

resources = [
"${aws_s3_bucket.logs.arn}/*",
]
}
}

#------------------------------------------------------------------------------
# IAM POLICY - For access logs to the s3 bucket
#------------------------------------------------------------------------------
resource "aws_s3_bucket_policy" "logs_access_policy" {
bucket = aws_s3_bucket.logs.id
policy = data.aws_iam_policy_document.logs_access_policy_document.json
}

#------------------------------------------------------------------------------
# S3 bucket block public access
#------------------------------------------------------------------------------
resource "aws_s3_bucket_public_access_block" "logs_block_public_access" {
count = var.block_s3_bucket_public_access ? 1 : 0

bucket = aws_s3_bucket.logs.id

block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true

depends_on = [aws_s3_bucket_policy.logs_access_policy]
}
12 changes: 12 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#------------------------------------------------------------------------------
# S3 Bucket
#------------------------------------------------------------------------------
output "lb_logs_s3_bucket_id" {
description = "LB Logging S3 Bucket ID"
value = aws_s3_bucket.logs.id
}

output "lb_logs_s3_bucket_arn" {
description = "LB Logging S3 Bucket ARN"
value = aws_s3_bucket.logs.arn
}
35 changes: 35 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,38 @@ variable "tags" {
default = {}
description = "Resource tags"
}

#------------------------------------------------------------------------------
# IAM
#------------------------------------------------------------------------------
variable "aws_principals_identifiers" {
type = list(string)
description = "List of identifiers for AWS principals with access to write in the logs bucket"
}

#------------------------------------------------------------------------------
# S3 bucket
#------------------------------------------------------------------------------
variable "block_s3_bucket_public_access" {
description = "(Optional) If true, public access to the S3 bucket will be blocked."
type = bool
default = true
}

variable "enable_s3_bucket_server_side_encryption" {
description = "(Optional) If true, server side encryption will be applied."
type = bool
default = true
}

variable "s3_bucket_server_side_encryption_sse_algorithm" {
description = "(Optional) The server-side encryption algorithm to use. Valid values are AES256 and aws:kms"
type = string
default = "aws:kms"
}

variable "s3_bucket_server_side_encryption_key" {
description = "(Optional) The AWS KMS master key ID used for the SSE-KMS encryption. This can only be used when you set the value of sse_algorithm as aws:kms. The default aws/s3 AWS KMS master key is used if this element is absent while the sse_algorithm is aws:kms."
type = string
default = "aws/s3"
}

2 comments on commit 558e237

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’° Infracost estimate: monthly cost will not change

Project Previous New Diff
cn-terraform/terraform-aws-logs-s3-bucket $0 $0 $0
Infracost output
Project: cn-terraform/terraform-aws-logs-s3-bucket

+ module.logs_bucket.aws_s3_bucket.logs
  Monthly cost depends on usage

    + Standard
    
        + Storage
          Monthly cost depends on usage
            +$0.023 per GB
    
        + PUT, COPY, POST, LIST requests
          Monthly cost depends on usage
            +$0.005 per 1k requests
    
        + GET, SELECT, and all other requests
          Monthly cost depends on usage
            +$0.0004 per 1k requests
    
        + Select data scanned
          Monthly cost depends on usage
            +$0.002 per GB
    
        + Select data returned
          Monthly cost depends on usage
            +$0.0007 per GB

Monthly cost change for cn-terraform/terraform-aws-logs-s3-bucket
Amount:  $0.00 ($0.00 β†’ $0.00)

──────────────────────────────────
Key: ~ changed, + added, - removed

5 cloud resources were detected:
βˆ™ 1 was estimated, it includes usage-based costs, see https://infracost.io/usage-file
βˆ™ 3 were free, rerun with --show-skipped to see details
βˆ™ 1 is not supported yet, rerun with --show-skipped to see details
Is this comment useful? Yes, No

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ’° Infracost estimate: monthly cost will not change

Project Previous New Diff
cn-terraform/terraform-aws-logs-s3-bucket $0 $0 $0
Infracost output
Project: cn-terraform/terraform-aws-logs-s3-bucket

+ module.logs_bucket.aws_s3_bucket.logs
  Monthly cost depends on usage

    + Standard
    
        + Storage
          Monthly cost depends on usage
            +$0.023 per GB
    
        + PUT, COPY, POST, LIST requests
          Monthly cost depends on usage
            +$0.005 per 1k requests
    
        + GET, SELECT, and all other requests
          Monthly cost depends on usage
            +$0.0004 per 1k requests
    
        + Select data scanned
          Monthly cost depends on usage
            +$0.002 per GB
    
        + Select data returned
          Monthly cost depends on usage
            +$0.0007 per GB

Monthly cost change for cn-terraform/terraform-aws-logs-s3-bucket
Amount:  $0.00 ($0.00 β†’ $0.00)

──────────────────────────────────
Key: ~ changed, + added, - removed

5 cloud resources were detected:
βˆ™ 1 was estimated, it includes usage-based costs, see https://infracost.io/usage-file
βˆ™ 3 were free, rerun with --show-skipped to see details
βˆ™ 1 is not supported yet, rerun with --show-skipped to see details
Is this comment useful? Yes, No

Please sign in to comment.