diff --git a/README.md b/README.md index 8d7dbec..4b194e0 100644 --- a/README.md +++ b/README.md @@ -37,14 +37,14 @@ We recommend protecting the `main` branch and to allow new code pushes only via | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.7.0 | -| [aws](#requirement\_aws) | >= 5.40.0 | +| [terraform](#requirement\_terraform) | >= 1.6.0 | +| [aws](#requirement\_aws) | >= 4.55.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 5.40.0 | +| [aws](#provider\_aws) | >= 4.55.0 | ## Modules @@ -67,7 +67,7 @@ No modules. | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | [cloudfront](#input\_cloudfront) | The name of the s3 bucket containing the cloudfront logs. Creates a db and saved cloudfront queries if set. | `string` | `null` | no | -| [cloudtrail](#input\_cloudtrail) | Configuration for cloudtrail. Creates a db and saved cloudfront queries if bucket\_name is set. Only set prefix if you configured one in your cloudtrail |
object({| `{}` | no | +| [cloudtrail](#input\_cloudtrail) | Configuration for cloudtrail. Creates a db and saved cloudfront queries if bucket\_name is set. Only set prefix if you configured one in your cloudtrail |
bucket_name = optional(string)
prefix = optional(string)
})
object({|
bucket_name = string
prefix = optional(string)
})
{| no | | [query\_bucket\_name](#input\_query\_bucket\_name) | The name of the bucket to save the query into. | `string` | n/a | yes | | [ses](#input\_ses) | The name of the s3 bucket containing the ses logs. Creates a db and saved ses queries if set | `string` | `null` | no | diff --git a/main.tf b/main.tf index 81c93fd..0548103 100644 --- a/main.tf +++ b/main.tf @@ -1,17 +1,17 @@ locals { named_queries = { cloudfront = var.cloudfront != null ? { - cloudfront_create_log_table = "Create the table for CloudFront logs", - cloudfront_log_yesterday_today = "Show requests to CloudFront yesterday and today", - cloudfront_log_yesterday_today_for_ip = "Show requests to CloudFront from a given IP yesterday and today" + cloudfront_create_log_table = "Creates the table for CloudFront logs", + cloudfront_log_yesterday_today = "Shows requests to CloudFront yesterday and today", + cloudfront_log_yesterday_today_for_ip = "Shows requests to CloudFront from a given IP yesterday and today" cloudfront_logs_for_specific_distribution = "Shows requests to a given cloudfront distribution, with optional filter for date, status code or ssl_protocol" } : {} cloudtrail = var.cloudtrail.bucket_name != null ? { - cloudtrail_create_log_table = "Create the table for CloudTrail logs", + cloudtrail_create_log_table = "Creates the table for CloudTrail logs", cloudtrail_console_login = "Displays console login events, ordered by date" } : {} ses = var.ses != null ? { - ses_create_log_table = "Create the table for SES logs" + ses_create_log_table = "Creates the table for SES logs" ses_bounce_ratio = "Displays bounced email as % of send emails" ses_formatted_mails = "Displays only the most relevant information per email, optional filter for event type" ses_daily_not_suppressed_bounces = "Displays amount of bounced emails per day, filtering out bounces caused by addresses being on the suppression list" @@ -66,7 +66,8 @@ resource "aws_athena_named_query" "cloudfront" { } resource "aws_athena_named_query" "cloudtrail" { - for_each = local.named_queries.cloudtrail + for_each = local.named_queries.cloudtrail + name = replace(each.key, "_", "-") database = aws_athena_database.cloudtrail[0].name description = each.value @@ -80,7 +81,8 @@ resource "aws_athena_named_query" "cloudtrail" { } resource "aws_athena_named_query" "ses" { - for_each = local.named_queries.ses + for_each = local.named_queries.ses + name = replace(each.key, "_", "-") database = aws_athena_database.ses[0].name description = each.value diff --git a/tests/test_input_parsing_cloudfront.tftest.hcl b/tests/test_input_parsing_cloudfront.tftest.hcl new file mode 100644 index 0000000..f7f76ce --- /dev/null +++ b/tests/test_input_parsing_cloudfront.tftest.hcl @@ -0,0 +1,39 @@ +mock_provider "aws" {} + +run "dont_create_resources_without_input" { + variables { + query_bucket_name = "test" + } + assert { + condition = length(local.named_queries.cloudfront) == 0 + error_message = "local.named_queries.cloudfront should have 0 elements, but has ${length(local.named_queries.cloudfront)}" + } + assert { + condition = length(aws_athena_database.cloudfront) == 0 + error_message = "aws_athena_database.cloudfront should have 0 instances, but has ${length(aws_athena_database.cloudfront)}" + } + assert { + condition = length(aws_athena_named_query.cloudfront) == 0 + error_message = "aws_athena_named_query.cloudfront should have 0 instances, but has ${length(aws_athena_database.cloudfront)}" + } +} + +run "create_resources_with_correct_input" { + variables { + query_bucket_name = "test" + cloudfront = "cloudfront-bucket" + } + + assert { + condition = length(local.named_queries.cloudfront) != 0 + error_message = "local.named_queries.cloudfront should not have 0 elements" + } + assert { + condition = length(aws_athena_database.cloudfront) == 1 + error_message = "aws_athena_database.cloudfront should have 1 instance, but has ${length(aws_athena_database.cloudfront)}" + } + assert { + condition = length(aws_athena_named_query.cloudfront) != 0 + error_message = "aws_athena_named_query.cloudfront should not have 0 element" + } +} diff --git a/tests/test_input_parsing_cloudtrail.tftest.hcl b/tests/test_input_parsing_cloudtrail.tftest.hcl new file mode 100644 index 0000000..c33bea2 --- /dev/null +++ b/tests/test_input_parsing_cloudtrail.tftest.hcl @@ -0,0 +1,41 @@ +mock_provider "aws" {} + +run "dont_create_resources_without_input" { + variables { + query_bucket_name = "test" + } + assert { + condition = length(local.named_queries.cloudtrail) == 0 + error_message = "local.named_queries.cloudtrail should have 0 elements, but has ${length(local.named_queries.cloudtrail)}" + } + assert { + condition = length(aws_athena_database.cloudtrail) == 0 + error_message = "aws_athena_database.cloudtrail should have 0 instances, but has ${length(aws_athena_database.cloudtrail)}" + } + assert { + condition = length(aws_athena_named_query.cloudtrail) == 0 + error_message = "aws_athena_named_query.cloudtrail should have 0 instances, but has ${length(aws_athena_database.cloudtrail)}" + } +} + +run "create_resources_with_correct_input" { + variables { + query_bucket_name = "test" + cloudtrail = { + bucket_name = "cloudtrail-bucket" + } + } + + assert { + condition = length(local.named_queries.cloudtrail) != 0 + error_message = "local.named_queries.cloudtrail should not have 0 elements" + } + assert { + condition = length(aws_athena_database.cloudtrail) == 1 + error_message = "aws_athena_database.cloudtrail should have 1 instance, but has ${length(aws_athena_database.cloudtrail)}" + } + assert { + condition = length(aws_athena_named_query.cloudtrail) != 0 + error_message = "aws_athena_named_query.cloudtrail should not have 0 element" + } +} diff --git a/tests/test_input_parsing_ses.tftest.hcl b/tests/test_input_parsing_ses.tftest.hcl new file mode 100644 index 0000000..a6030c6 --- /dev/null +++ b/tests/test_input_parsing_ses.tftest.hcl @@ -0,0 +1,39 @@ +mock_provider "aws" {} + +run "dont_create_resources_without_input" { + variables { + query_bucket_name = "test" + } + assert { + condition = length(local.named_queries.ses) == 0 + error_message = "local.named_queries.ses should have 0 elements, but has ${length(local.named_queries.ses)}" + } + assert { + condition = length(aws_athena_database.ses) == 0 + error_message = "aws_athena_database.ses should have 0 instances, but has ${length(aws_athena_database.ses)}" + } + assert { + condition = length(aws_athena_named_query.ses) == 0 + error_message = "aws_athena_named_query.ses should have 0 instances, but has ${length(aws_athena_database.ses)}" + } +} + +run "create_resources_with_correct_input" { + variables { + query_bucket_name = "test" + ses = "ses-bucket" + } + + assert { + condition = length(local.named_queries.ses) != 0 + error_message = "local.named_queries.ses should not have 0 elements" + } + assert { + condition = length(aws_athena_database.ses) == 1 + error_message = "aws_athena_database.ses should have 1 instance, but has ${length(aws_athena_database.ses)}" + } + assert { + condition = length(aws_athena_named_query.ses) != 0 + error_message = "aws_athena_named_query.ses should not have 0 element" + } +} diff --git a/variables.tf b/variables.tf index de8feb4..d8bb5e4 100644 --- a/variables.tf +++ b/variables.tf @@ -12,10 +12,12 @@ variable "cloudfront" { variable "cloudtrail" { description = "Configuration for cloudtrail. Creates a db and saved cloudfront queries if bucket_name is set. Only set prefix if you configured one in your cloudtrail" type = object({ - bucket_name = optional(string) + bucket_name = string prefix = optional(string) }) - default = {} + default = { + bucket_name = null + } } variable "ses" { diff --git a/versions.tf b/versions.tf index f0d7e15..9284a89 100644 --- a/versions.tf +++ b/versions.tf @@ -1,8 +1,8 @@ terraform { - required_version = ">= 1.7.0" + required_version = ">= 1.6.0" required_providers { aws = { - version = ">= 5.40.0" + version = ">= 4.55.0" } } }
"bucket_name": null
}