Skip to content

Commit

Permalink
Allow skipping of the JSON parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jansiwy committed Jan 25, 2024
1 parent 6d0306a commit 5ab2a5d
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 71 deletions.
20 changes: 19 additions & 1 deletion _test/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ provider "aws" {
region = "local"
}

module "sns-to-rollbar" {
module "sns-to-rollbar-with-json-key" {
source = "./.."

name = "example"
Expand All @@ -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"
}
}
203 changes: 134 additions & 69 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ EOS
}

variable "json_key" {
type = string
type = string
default = "-"

description = <<EOS
If the message is JSON, this is the key to use to extract the message used as Rollbar item title.
If the value is "-", the implementation will not attempt to parse the message as JSON.
EOS
}

Expand Down

0 comments on commit 5ab2a5d

Please sign in to comment.