@@ -44,17 +44,25 @@ module "eventbridge" {
4444 orders = {
4545 description = " Capture all order data"
4646 event_pattern = jsonencode ({ " source" : [" myapp.orders" ] })
47- enabled = false
47+ state = " DISABLED " # conflicts with enabled which is deprecated
4848 }
4949 emails = {
5050 description = " Capture all emails data"
5151 event_pattern = jsonencode ({ " source" : [" myapp.emails" ] })
52- enabled = true
52+ state = " ENABLED " # conflicts with enabled which is deprecated
5353 }
5454 crons = {
5555 description = " Trigger for a Lambda"
5656 schedule_expression = " rate(5 minutes)"
5757 }
58+ ecs = {
59+ description = " Capture ECS events"
60+ event_pattern = jsonencode ({
61+ " source" : [" aws.ecs" ]
62+ })
63+ # https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html#eb-service-event-cloudtrail
64+ state = " ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS"
65+ }
5866 }
5967
6068 targets = {
@@ -121,6 +129,13 @@ module "eventbridge" {
121129 input = jsonencode ({ " job" : " crons" })
122130 }
123131 ]
132+
133+ ecs = [
134+ {
135+ name = " something-for-ecs"
136+ arn = module.sns.topic_arn
137+ }
138+ ]
124139 }
125140
126141 # #####################
@@ -370,6 +385,110 @@ resource "null_resource" "download_package" {
370385 }
371386}
372387
388+ # ######
389+ # SNS
390+ # ######
391+
392+ module "sns" {
393+ source = " terraform-aws-modules/sns/aws"
394+ version = " ~> 6.0"
395+
396+ name = " ${ random_pet . this . id } -notifications"
397+ topic_policy_statements = {
398+ events = {
399+ actions = [" sns:publish" ]
400+ principals = [{
401+ type = " Service"
402+ identifiers = [" events.amazonaws.com" ]
403+ }]
404+ }
405+ }
406+ tags = {
407+ name = " ${ random_pet . this . id } -notifications"
408+ }
409+ }
410+
411+ # #############
412+ # CloudTrail
413+ # #############
414+
415+ # required for event rule state of ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS
416+ resource "aws_cloudtrail" "trail" {
417+ name = " ${ random_pet . this . id } -trail"
418+ s3_bucket_name = module. bucket . s3_bucket_id
419+ include_global_service_events = false
420+
421+ event_selector {
422+ exclude_management_event_sources = [
423+ " kms.amazonaws.com" ,
424+ " rdsdata.amazonaws.com"
425+ ]
426+ read_write_type = " ReadOnly"
427+ }
428+ }
429+
430+ # ######
431+ # s3
432+ # ######
433+
434+ data "aws_region" "current" {}
435+
436+ data "aws_caller_identity" "current" {}
437+
438+ module "bucket" {
439+ source = " terraform-aws-modules/s3-bucket/aws"
440+ version = " ~> 3.0"
441+
442+ bucket = " ${ random_pet . this . id } -bucket"
443+ attach_policy = true
444+ policy = data. aws_iam_policy_document . bucket_policy . json
445+
446+ force_destroy = true
447+ }
448+
449+ # https://docs.aws.amazon.com/awscloudtrail/latest/userguide/create-s3-bucket-policy-for-cloudtrail.html
450+ data "aws_iam_policy_document" "bucket_policy" {
451+ statement {
452+ sid = " AWSCloudTrailAclCheck"
453+ principals {
454+ identifiers = [" cloudtrail.amazonaws.com" ]
455+ type = " Service"
456+ }
457+ actions = [" s3:GetBucketAcl" ]
458+ resources = [
459+ " arn:aws:s3:::${ random_pet . this . id } -bucket"
460+ ]
461+ condition {
462+ test = " StringEquals"
463+ values = [" arn:aws:cloudtrail:${ data . aws_region . current . name } :${ data . aws_caller_identity . current . account_id } :trail/${ random_pet . this . id } -trail" ]
464+ variable = " aws:SourceArn"
465+ }
466+ }
467+
468+ statement {
469+ sid = " AWSCloudTrailWrite"
470+ principals {
471+ identifiers = [" cloudtrail.amazonaws.com" ]
472+ type = " Service"
473+ }
474+ actions = [" s3:PutObject" ]
475+ resources = [
476+ " arn:aws:s3:::${ random_pet . this . id } -bucket/*"
477+ ]
478+ condition {
479+ test = " StringEquals"
480+ values = [" bucket-owner-full-control" ]
481+ variable = " s3:x-amz-acl"
482+ }
483+ condition {
484+ test = " StringEquals"
485+ values = [" arn:aws:cloudtrail:${ data . aws_region . current . name } :${ data . aws_caller_identity . current . account_id } :trail/${ random_pet . this . id } -trail" ]
486+ variable = " aws:SourceArn"
487+ }
488+ }
489+
490+ }
491+
373492# ######
374493# # Lambda
375494# ######
0 commit comments