diff --git a/_test/main.tf b/_test/main.tf index ad22081..a3f93ee 100644 --- a/_test/main.tf +++ b/_test/main.tf @@ -2,7 +2,7 @@ provider "aws" { region = "local" } -module "sns-to-rollbar" { +module "sns-to-rollbar-with-json-key" { source = "./.." name = "example" @@ -21,3 +21,21 @@ module "sns-to-rollbar" { env = "test" } } + +module "sns-to-rollbar-without-json-key" { + source = "./.." + + name = "example" + + rollbar_project_access_token = { + access_token = "some-token" + } + + environment = "test" + level = "debug" + + tags = { + app = "some-service" + env = "test" + } +} diff --git a/main.tf b/main.tf index 269ae0d..36b073e 100644 --- a/main.tf +++ b/main.tf @@ -115,109 +115,174 @@ resource "aws_sfn_state_machine" "this" { name = var.name role_arn = aws_iam_role.sfn-state-machine.arn - definition = jsonencode({ - StartAt = "PostItems" + definition = ( + var.json_key != "-" ? - States = { - PostItems = { - Type = "Map" + # If `json_key` is specified, the state machine will attempt to parse `message` as JSON. + jsonencode({ + StartAt = "PostItems" - ItemProcessor = { - StartAt = "IsJSON" + States = { + PostItems = { + Type = "Map" - States = { - IsJSON = { - Type = "Choice" + ItemProcessor = { + StartAt = "IsJSON" - Choices = [ - { - Variable = "$.message" - StringMatches = "{*}" - Next = "ParseJSON" - }, - ] + States = { + IsJSON = { + Type = "Choice" - Default = "DontParseJSON" - } - - ParseJSON = { - Type = "Pass" + Choices = [ + { + Variable = "$.message" + StringMatches = "{*}" + Next = "ParseJSON" + }, + ] - Parameters = { - "message.$" = "States.StringToJson($.message)" + Default = "DontParseJSON" } - Next = "FindBody" - } + ParseJSON = { + Type = "Pass" - FindBody = { - Type = "Pass" + Parameters = { + "message.$" = "States.StringToJson($.message)" + } - Parameters = { - "body.$" = "$.message['${var.json_key}']" + Next = "FindBody" } - ResultPath = "$.overrides" - Next = "MergeBody" - } + FindBody = { + Type = "Pass" - MergeBody = { - Type = "Pass" + Parameters = { + "body.$" = "$.message['${var.json_key}']" + } + ResultPath = "$.overrides" - Parameters = { - "message.$" = "States.JsonMerge($.message, $.overrides, false)" + Next = "MergeBody" } - Next = "PostItem" - } + MergeBody = { + Type = "Pass" + + Parameters = { + "message.$" = "States.JsonMerge($.message, $.overrides, false)" + } + + Next = "PostItem" + } - DontParseJSON = { - Type = "Pass" + DontParseJSON = { + Type = "Pass" - Parameters = { - "message" = { - "body.$" = "$.message" + Parameters = { + "message" = { + "body.$" = "$.message" + } } + + Next = "PostItem" } - Next = "PostItem" + PostItem = { + Type = "Task" + + Resource = "arn:aws:states:::http:invoke" + + Parameters = { + Method = "POST" + ApiEndpoint = "https://api.rollbar.com/api/1/item/" + Headers = { + "Accept" = "application/json" + "Content-Type" = "application/json" + } + Authentication = { + ConnectionArn = aws_cloudwatch_event_connection.this.arn + } + RequestBody = { + data = { + environment = var.environment + level = var.level + body = { + "message.$" = "$.message" + } + } + } + } + + End = true + } } + } + + End = true + } + } + }) : - PostItem = { - Type = "Task" + # If `json_key` is not specified (i.e. `-`), the state machine will not attempt to parse `message` as JSON. + jsonencode({ + StartAt = "PostItems" - Resource = "arn:aws:states:::http:invoke" + States = { + PostItems = { + Type = "Map" - Parameters = { - Method = "POST" - ApiEndpoint = "https://api.rollbar.com/api/1/item/" - Headers = { - "Accept" = "application/json" - "Content-Type" = "application/json" - } - Authentication = { - ConnectionArn = aws_cloudwatch_event_connection.this.arn + ItemProcessor = { + StartAt = "DontParseJSON" + + States = { + DontParseJSON = { + Type = "Pass" + + Parameters = { + "message" = { + "body.$" = "$.message" + } } - RequestBody = { - data = { - environment = var.environment - level = var.level - body = { - "message.$" = "$.message" + + Next = "PostItem" + } + + PostItem = { + Type = "Task" + + Resource = "arn:aws:states:::http:invoke" + + Parameters = { + Method = "POST" + ApiEndpoint = "https://api.rollbar.com/api/1/item/" + Headers = { + "Accept" = "application/json" + "Content-Type" = "application/json" + } + Authentication = { + ConnectionArn = aws_cloudwatch_event_connection.this.arn + } + RequestBody = { + data = { + environment = var.environment + level = var.level + body = { + "message.$" = "$.message" + } } } } - } - End = true + End = true + } } } - } - End = true + End = true + } } - } - }) + }) + ) tags = var.tags } diff --git a/variables.tf b/variables.tf index a5020df..503b6e1 100644 --- a/variables.tf +++ b/variables.tf @@ -7,10 +7,12 @@ EOS } variable "json_key" { - type = string + type = string + default = "-" description = <