diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml new file mode 100644 index 0000000..076392f --- /dev/null +++ b/.github/workflows/validate.yml @@ -0,0 +1,33 @@ +name: Validate + +on: pull_request + +jobs: + validate_terraform_versions: + runs-on: ubuntu-22.04 + + strategy: + matrix: + terraform_version: + - "1.6.6" + + defaults: + run: + working-directory: _test + + steps: + - uses: actions/checkout@v4 + - uses: hashicorp/setup-terraform@v3 + with: + terraform_version: ${{ matrix.terraform_version }} + - run: terraform init + - run: terraform validate + + validate: # this is a workaround, see https://github.community/t/status-check-for-a-matrix-jobs/127354/6 + if: ${{ always() }} + needs: [validate_terraform_versions] + runs-on: ubuntu-22.04 + steps: + - name: Check build status of all needed jobs + if: ${{ needs.validate_terraform_versions.result != 'success' }} + run: exit 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..576c848 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/_test/.terraform +/_test/.terraform.lock.hcl diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..32add6f --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +# CHANGELOG.md + +## v1.0.0 + +- [Initial version](https://github.com/babbel/terraform-aws-sns-to-rollbar/pull/1) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f661a1d --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2024 Babbel GmbH + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 837ed0c..cd7c7a6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ -# terraform-aws-sns-to-rollbar +# AWS SNS to Rollbar + Terraform module which allows posting SNS messages to Rollbar + +## Usage + +```tf +module "sns-to-rollbar" { + source = "babbel/sns-to-rollbar/aws" + version = "~> 1.0" + + name = "example" + + json_key = "message" + + rollbar_project_access_token = { + access_token = "some-token" + } + + environment = "test" + level = "debug" + + tags = { + app = "some-service" + env = "test" + } +} +``` diff --git a/_test/.github/dependabot.yml b/_test/.github/dependabot.yml new file mode 100644 index 0000000..df44be6 --- /dev/null +++ b/_test/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 + +updates: +- package-ecosystem: github-actions + directory: / + schedule: + interval: weekly + day: saturday + time: "01:00" + timezone: Europe/Berlin diff --git a/_test/main.tf b/_test/main.tf new file mode 100644 index 0000000..ad22081 --- /dev/null +++ b/_test/main.tf @@ -0,0 +1,23 @@ +provider "aws" { + region = "local" +} + +module "sns-to-rollbar" { + source = "./.." + + name = "example" + + json_key = "message" + + 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 new file mode 100644 index 0000000..269ae0d --- /dev/null +++ b/main.tf @@ -0,0 +1,304 @@ +# SNS topic (forwards messages to SQS queue) + +resource "aws_sns_topic" "this" { + name = var.name + tags = var.tags +} + +resource "aws_sns_topic_subscription" "sqs-queue" { + topic_arn = aws_sns_topic.this.arn + protocol = "sqs" + endpoint = aws_sqs_queue.this.arn +} + +# SQS queue + +resource "aws_sqs_queue" "this" { + name = var.name + tags = var.tags +} + +data "aws_iam_policy_document" "sqs-queue-consume" { + statement { + actions = [ + "sqs:DeleteMessage", + "sqs:GetQueueAttributes", + "sqs:ReceiveMessage", + ] + + resources = [aws_sqs_queue.this.arn] + } +} + +resource "aws_sqs_queue_policy" "this" { + queue_url = aws_sqs_queue.this.url + policy = data.aws_iam_policy_document.sqs-queue.json +} + +data "aws_iam_policy_document" "sqs-queue" { + statement { + principals { + type = "Service" + identifiers = ["sns.amazonaws.com"] + } + + actions = ["sqs:SendMessage"] + resources = [aws_sqs_queue.this.arn] + + condition { + variable = "aws:SourceArn" + test = "ArnEquals" + values = [aws_sns_topic.this.arn] + } + } +} + +# EventBridge Pipe (pipes messages from SQS queue to Step Function) + +resource "aws_pipes_pipe" "this" { + name = var.name + role_arn = aws_iam_role.pipes-pipe.arn + + source = aws_sqs_queue.this.arn + + target = aws_sfn_state_machine.this.arn + + target_parameters { + input_template = < } + EOF + + step_function_state_machine_parameters { + invocation_type = "FIRE_AND_FORGET" + } + } + + tags = var.tags + + depends_on = [ + aws_iam_role_policy.pipes-pipe-sqs-queue-consume, + aws_iam_role_policy.pipes-pipe-sfn-state-machine-start-execution, + ] +} + +resource "aws_iam_role" "pipes-pipe" { + name = "pipes-${var.name}" + assume_role_policy = data.aws_iam_policy_document.pipes-assume-role.json + tags = var.tags +} + +data "aws_iam_policy_document" "pipes-assume-role" { + statement { + principals { + type = "Service" + identifiers = ["pipes.amazonaws.com"] + } + + actions = ["sts:AssumeRole"] + } +} +resource "aws_iam_role_policy" "pipes-pipe-sqs-queue-consume" { + role = aws_iam_role.pipes-pipe.name + name = "sqs-queue-consume" + policy = data.aws_iam_policy_document.sqs-queue-consume.json +} + +resource "aws_iam_role_policy" "pipes-pipe-sfn-state-machine-start-execution" { + role = aws_iam_role.pipes-pipe.name + name = "sfn-state-machine-start-execution" + policy = data.aws_iam_policy_document.sfn-state-machine-start-execution.json +} + +# Step Function (invokes Rollbar API) + +resource "aws_sfn_state_machine" "this" { + name = var.name + role_arn = aws_iam_role.sfn-state-machine.arn + + definition = jsonencode({ + StartAt = "PostItems" + + States = { + PostItems = { + Type = "Map" + + ItemProcessor = { + StartAt = "IsJSON" + + States = { + IsJSON = { + Type = "Choice" + + Choices = [ + { + Variable = "$.message" + StringMatches = "{*}" + Next = "ParseJSON" + }, + ] + + Default = "DontParseJSON" + } + + ParseJSON = { + Type = "Pass" + + Parameters = { + "message.$" = "States.StringToJson($.message)" + } + + Next = "FindBody" + } + + FindBody = { + Type = "Pass" + + Parameters = { + "body.$" = "$.message['${var.json_key}']" + } + ResultPath = "$.overrides" + + Next = "MergeBody" + } + + MergeBody = { + Type = "Pass" + + Parameters = { + "message.$" = "States.JsonMerge($.message, $.overrides, false)" + } + + Next = "PostItem" + } + + DontParseJSON = { + Type = "Pass" + + Parameters = { + "message" = { + "body.$" = "$.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 + } + } + }) + + tags = var.tags +} + +data "aws_iam_policy_document" "sfn-state-machine-start-execution" { + statement { + actions = ["states:StartExecution"] + resources = [aws_sfn_state_machine.this.arn] + } +} + +resource "aws_iam_role" "sfn-state-machine" { + name = "step-function-${var.name}" + assume_role_policy = data.aws_iam_policy_document.states.json + tags = var.tags +} + +data "aws_iam_policy_document" "states" { + statement { + principals { + type = "Service" + identifiers = ["states.amazonaws.com"] + } + + actions = ["sts:AssumeRole"] + } +} + +resource "aws_iam_role_policy" "sfn-state-machine-invoke-http-endpoint" { + role = aws_iam_role.sfn-state-machine.name + name = "invoke-http-endpoint" + policy = data.aws_iam_policy_document.invoke-http-endpoint.json +} + +# Rollbar + +resource "aws_cloudwatch_event_connection" "this" { + name = "${var.name}-rollbar" + description = "Posts items to Rollbar" + authorization_type = "API_KEY" + + auth_parameters { + api_key { + key = "X-Rollbar-Access-Token" + value = var.rollbar_project_access_token.access_token + } + } +} + +data "aws_iam_policy_document" "invoke-http-endpoint" { + statement { + actions = [ + "states:InvokeHTTPEndpoint", + ] + resources = ["*"] + + condition { + variable = "states:HTTPMethod" + test = "StringEquals" + values = ["POST"] + } + + condition { + variable = "states:HTTPEndpoint" + test = "StringEquals" + values = ["https://api.rollbar.com/api/1/item/"] + } + } + + statement { + actions = [ + "events:RetrieveConnectionCredentials", + ] + resources = [aws_cloudwatch_event_connection.this.arn] + } + + statement { + actions = [ + "secretsmanager:GetSecretValue", + "secretsmanager:DescribeSecret", + ] + resources = [aws_cloudwatch_event_connection.this.secret_arn] + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..e6f4239 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,7 @@ +output "sns_topic" { + value = aws_sns_topic.this + + description = <