Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow skipping of the JSON parsing #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
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"
}
}
143 changes: 76 additions & 67 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "-" ?
tomap({
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"
}
}) :
tomap({})
),

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
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