generated from cloudposse/terraform-example-module
-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from cloudposse/feature/initial-module-implemen…
…tation
- Loading branch information
Showing
18 changed files
with
351 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
region = "us-east-2" | ||
|
||
namespace = "eg" | ||
|
||
region = "us-east-2" | ||
namespace = "eg" | ||
environment = "ue2" | ||
stage = "test" | ||
|
||
stage = "test" | ||
|
||
name = "example" | ||
|
||
create_sns_topic = true | ||
enabled_standards = ["standards/aws-foundational-security-best-practices/v/1.0.0", "ruleset/cis-aws-foundations-benchmark/v/1.2.0"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
provider "aws" { | ||
region = var.region | ||
} | ||
|
||
module "example" { | ||
source = "../.." | ||
|
||
example = var.example | ||
enabled_standards = var.enabled_standards | ||
create_sns_topic = var.create_sns_topic | ||
|
||
context = module.this.context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
output "id" { | ||
description = "ID of the created example" | ||
value = module.example.id | ||
output "enabled_subscriptions" { | ||
description = "Enabled subscriptions" | ||
value = module.example.enabled_subscriptions | ||
} | ||
|
||
output "example" { | ||
description = "Output \"example\" from example module" | ||
value = module.example.example | ||
} | ||
|
||
output "random" { | ||
description = "Output \"random\" from example module" | ||
value = module.example.random | ||
output "sns_topic" { | ||
value = module.example.sns_topic | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
variable "example" { | ||
#---------------------------------------------------------------------------------------------------------------------- | ||
# REQUIRED VARIABLES | ||
# The module expects values to be supplied for these paramaters. | ||
#---------------------------------------------------------------------------------------------------------------------- | ||
variable "region" { | ||
type = string | ||
description = "The value which will be passed to the example module" | ||
description = "AWS region" | ||
} | ||
|
||
variable "enabled_standards" { | ||
description = "A list of standards to enable in the account" | ||
type = list(string) | ||
default = [] | ||
} | ||
|
||
variable "create_sns_topic" { | ||
description = "Flag to indicate whether an SNS topic should be created for notifications." | ||
type = bool | ||
default = false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
terraform { | ||
required_version = ">= 0.12.0, < 0.14" | ||
required_version = ">= 0.13.0" | ||
|
||
required_providers { | ||
aws = { | ||
source = "hashicorp/aws" | ||
version = ">= 2" | ||
} | ||
local = "~> 1.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,80 @@ | ||
resource "random_integer" "example" { | ||
#----------------------------------------------------------------------------------------------------------------------- | ||
# Subscribe the Acccount to Security Hub | ||
#----------------------------------------------------------------------------------------------------------------------- | ||
resource "aws_securityhub_account" "this" { | ||
count = module.this.enabled ? 1 : 0 | ||
} | ||
|
||
#----------------------------------------------------------------------------------------------------------------------- | ||
# Optionally subscribe to Security Hub Standards | ||
# https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-standards.html | ||
#----------------------------------------------------------------------------------------------------------------------- | ||
resource "aws_securityhub_standards_subscription" "this" { | ||
for_each = local.enabled_standards_arns | ||
depends_on = [aws_securityhub_account.this] | ||
standards_arn = each.key | ||
} | ||
|
||
#----------------------------------------------------------------------------------------------------------------------- | ||
# Optionally configure Event Bridge Rules and SNS subscriptions | ||
# https://docs.aws.amazon.com/securityhub/latest/userguide/securityhub-cwe-integration-types.html | ||
#----------------------------------------------------------------------------------------------------------------------- | ||
module "sns_topic" { | ||
source = "cloudposse/sns-topic/aws" | ||
version = "0.9.0" | ||
count = local.create_sns_topic ? 1 : 0 | ||
|
||
attributes = ["securityhub"] | ||
subscribers = {} | ||
sqs_dlq_enabled = false | ||
|
||
context = module.this.context | ||
} | ||
|
||
min = 1 | ||
max = 50000 | ||
keepers = { | ||
example = var.example | ||
} | ||
module "imported_findings_label" { | ||
source = "cloudposse/label/null" | ||
version = "0.21.0" | ||
|
||
attributes = ["securityhub-imported-findings"] | ||
context = module.this.context | ||
} | ||
|
||
resource "aws_cloudwatch_event_rule" "imported_findings" { | ||
count = local.enable_notifications == true ? 1 : 0 | ||
name = module.imported_findings_label.id | ||
description = "SecurityHubEvent - Imported Findings" | ||
tags = module.this.tags | ||
|
||
event_pattern = jsonencode( | ||
{ | ||
"source" : [ | ||
"aws.securityhub" | ||
], | ||
"detail-type" : [ | ||
var.cloudwatch_event_rule_pattern_detail_type | ||
] | ||
} | ||
) | ||
} | ||
|
||
resource "aws_cloudwatch_event_target" "imported_findings" { | ||
count = local.enable_notifications == true ? 1 : 0 | ||
rule = aws_cloudwatch_event_rule.imported_findings[0].name | ||
arn = local.imported_findings_notification_arn | ||
} | ||
|
||
#----------------------------------------------------------------------------------------------------------------------- | ||
# Locals and Data References | ||
#----------------------------------------------------------------------------------------------------------------------- | ||
locals { | ||
example = format("%v %v", var.example, join("", random_integer.example[*].result)) | ||
enable_notifications = module.this.enabled && (var.create_sns_topic || var.imported_findings_notification_arn != null) | ||
create_sns_topic = module.this.enabled && var.create_sns_topic | ||
imported_findings_notification_arn = local.enable_notifications ? (var.imported_findings_notification_arn != null ? var.imported_findings_notification_arn : module.sns_topic[0].sns_topic.arn) : null | ||
enabled_standards_arns = toset([ | ||
for standard in var.enabled_standards : | ||
format("arn:%s:securityhub:%s::%s", data.aws_partition.this.partition, length(regexall("ruleset", standard)) == 0 ? data.aws_region.this.name : "", standard) | ||
]) | ||
} | ||
|
||
data "aws_partition" "this" {} | ||
data "aws_region" "this" {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
output "id" { | ||
description = "ID of the created example" | ||
value = module.this.enabled ? module.this.id : null | ||
output "enabled_subscriptions" { | ||
description = "A list of subscriptions that have been enabled" | ||
value = [ | ||
for standard in aws_securityhub_standards_subscription.this : | ||
standard.id | ||
] | ||
} | ||
|
||
output "example" { | ||
description = "Example output" | ||
value = module.this.enabled ? local.example : null | ||
output "sns_topic" { | ||
description = "The SNS topic that was created" | ||
value = local.create_sns_topic ? module.sns_topic[0].sns_topic : null | ||
} | ||
|
||
output "random" { | ||
description = "Stable random number for this example" | ||
value = module.this.enabled ? join("", random_integer.example[*].result) : null | ||
output "sns_topic_subscriptions" { | ||
description = "The SNS topic that was created" | ||
value = local.create_sns_topic ? module.sns_topic[0].aws_sns_topic_subscriptions : null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.