From c567f9a00be76e944b2ee5d21520ffe270bb030e Mon Sep 17 00:00:00 2001 From: Jan Sebastian Siwy Date: Thu, 25 Jan 2024 14:29:14 +0100 Subject: [PATCH 1/3] Allow to skip the JSON parsing ... even if the payload is JSON. --- _test/main.tf | 20 ++++++- main.tf | 143 +++++++++++++++++++++++++++----------------------- variables.tf | 4 +- 3 files changed, 98 insertions(+), 69 deletions(-) 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..730ea92 100644 --- a/main.tf +++ b/main.tf @@ -123,95 +123,104 @@ resource "aws_sfn_state_machine" "this" { Type = "Map" ItemProcessor = { - StartAt = "IsJSON" - - States = { - IsJSON = { - Type = "Choice" + StartAt = var.json_key != "-" ? "IsJSON" : "DontParseJSON" + + States = merge( + ( + var.json_key != "-" ? + { + IsJSON = { + Type = "Choice" + + Choices = [ + { + Variable = "$.message" + StringMatches = "{*}" + Next = "ParseJSON" + }, + ] + + Default = "DontParseJSON" + } - Choices = [ - { - Variable = "$.message" - StringMatches = "{*}" - Next = "ParseJSON" - }, - ] + ParseJSON = { + Type = "Pass" - Default = "DontParseJSON" - } + Parameters = { + "message.$" = "States.StringToJson($.message)" + } - ParseJSON = { - Type = "Pass" + Next = "FindBody" + } - Parameters = { - "message.$" = "States.StringToJson($.message)" - } + FindBody = { + Type = "Pass" - Next = "FindBody" - } - - FindBody = { - Type = "Pass" + Parameters = { + "body.$" = "$.message['${var.json_key}']" + } + ResultPath = "$.overrides" - Parameters = { - "body.$" = "$.message['${var.json_key}']" - } - ResultPath = "$.overrides" + Next = "MergeBody" + } - Next = "MergeBody" - } + MergeBody = { + Type = "Pass" - MergeBody = { - Type = "Pass" + Parameters = { + "message.$" = "States.JsonMerge($.message, $.overrides, false)" + } - Parameters = { - "message.$" = "States.JsonMerge($.message, $.overrides, false)" - } + Next = "PostItem" + } + } : + {} + ), - Next = "PostItem" - } + { - DontParseJSON = { - Type = "Pass" + DontParseJSON = { + Type = "Pass" - Parameters = { - "message" = { - "body.$" = "$.message" + Parameters = { + "message" = { + "body.$" = "$.message" + } } - } - Next = "PostItem" - } + Next = "PostItem" + } - PostItem = { - Type = "Task" + PostItem = { + Type = "Task" - Resource = "arn:aws:states:::http:invoke" + 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" + 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 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 = < Date: Thu, 25 Jan 2024 14:31:09 +0100 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32add6f..c4c9a47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG.md +## v1.1.0 + +- [Allow skipping of the JSON parsing](https://github.com/babbel/terraform-aws-sns-to-rollbar/pull/2) + ## v1.0.0 - [Initial version](https://github.com/babbel/terraform-aws-sns-to-rollbar/pull/1) From ca08a09886e1c58f6fdb982facff9a310484360e Mon Sep 17 00:00:00 2001 From: Jan Sebastian Siwy Date: Thu, 25 Jan 2024 14:34:12 +0100 Subject: [PATCH 3/3] Update main.tf --- main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.tf b/main.tf index 730ea92..60a83e8 100644 --- a/main.tf +++ b/main.tf @@ -128,7 +128,7 @@ resource "aws_sfn_state_machine" "this" { States = merge( ( var.json_key != "-" ? - { + tomap({ IsJSON = { Type = "Choice" @@ -173,8 +173,8 @@ resource "aws_sfn_state_machine" "this" { Next = "PostItem" } - } : - {} + }) : + tomap({}) ), {