From 4b36f084e25c9328963aea0b03369e94aafae6a0 Mon Sep 17 00:00:00 2001 From: Barney Parker Date: Sat, 18 Apr 2020 15:13:05 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 3 +++ README.md | 54 ++++++++++++++++++++++++++++++++++++++++++ example/main.tf | 42 +++++++++++++++++++++++++++++++++ main.tf | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ variables.tf | 28 ++++++++++++++++++++++ 5 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 example/main.tf create mode 100644 main.tf create mode 100644 variables.tf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95c72ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.tfstate +*.backup +.terraform diff --git a/README.md b/README.md new file mode 100644 index 0000000..89cd529 --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# terraform-aws-api-sns + +Module to simplify API Gateway SNS service integrations. + +## Compatibility + +This module is HCL2 compantible only. + +## Example + +``` +resource "aws_api_gateway_rest_api" "api" { + name = "api_sns" +} + +resource "aws_sns_topic" "sns" { + name = "api_sns" +} + +module "api-sns" { + source = "../" + + name = "sns" + api_id = aws_api_gateway_rest_api.api.id + resource_id = aws_api_gateway_rest_api.api.root_resource_id + + http_method = "POST" + + topic_arn = aws_sns_topic.sns.arn + + responses = [ + { + status_code = "200" + selection_pattern = "200" + templates = { + "application/json" = jsonencode({ + statusCode = 200 + message = "OK" + }) + } + }, + { + status_code = "400" + selection_pattern = "4\\d{2}" + templates = { + "application/json" = jsonencode({ + statusCode = 400 + message = "Error" + }) + } + } + ] +} +``` diff --git a/example/main.tf b/example/main.tf new file mode 100644 index 0000000..b883c27 --- /dev/null +++ b/example/main.tf @@ -0,0 +1,42 @@ +resource "aws_api_gateway_rest_api" "api" { + name = "api_sns" +} + +resource "aws_sns_topic" "sns" { + name = "api_sns" +} + +module "api-sns" { + source = "../" + + name = "sns" + api_id = aws_api_gateway_rest_api.api.id + resource_id = aws_api_gateway_rest_api.api.root_resource_id + + http_method = "POST" + + topic_arn = aws_sns_topic.sns.arn + + responses = [ + { + status_code = "200" + selection_pattern = "200" + templates = { + "application/json" = jsonencode({ + statusCode = 200 + message = "OK" + }) + } + }, + { + status_code = "400" + selection_pattern = "4\\d{2}" + templates = { + "application/json" = jsonencode({ + statusCode = 400 + message = "Error" + }) + } + } + ] +} \ No newline at end of file diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..109b1af --- /dev/null +++ b/main.tf @@ -0,0 +1,62 @@ +data "aws_region" "current" {} + +module "sns_integration" { + source = "github.com/barneyparker/terraform-aws-api-generic" + + api_id = var.api_id + resource_id = var.resource_id + http_method = var.http_method + authorization = var.authorization + method_request_parameters = var.method_request_parameters + + integration_http_method = "POST" + type = "AWS" + uri = "arn:aws:apigateway:${data.aws_region.current.name}:sns:path//" + credentials = aws_iam_role.sns_publish.arn + + integration_request_parameters = { + "integration.request.header.Content-Type" = "'application/x-www-form-urlencoded'" + } + + request_templates = { + "application/json" = "Action=Publish&TopicArn=$util.urlEncode('${var.topic_arn}')&Message=$util.urlEncode($input.body)" + } + + responses = var.responses +} + +resource "aws_iam_role" "sns_publish" { + name = "${var.name}-sns-publish" + assume_role_policy = data.aws_iam_policy_document.apigw.json +} + +data "aws_iam_policy_document" "apigw" { + statement { + actions = ["sts:AssumeRole"] + + principals { + type = "Service" + identifiers = [ + "apigateway.amazonaws.com" + ] + } + } +} + +resource "aws_iam_role_policy" "sns_publish" { + name = "SNS-Publish" + role = aws_iam_role.sns_publish.id + policy = data.aws_iam_policy_document.sns_publish.json +} + +data "aws_iam_policy_document" "sns_publish" { + statement { + actions = [ + "sns:Publish", + ] + + resources = [ + "${var.topic_arn}", + ] + } +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..7dfbce2 --- /dev/null +++ b/variables.tf @@ -0,0 +1,28 @@ +variable "name" {} + +variable "api_id" {} + +variable "resource_id" {} + +variable "http_method" {} + +variable "topic_arn" {} + +variable "authorization" { + default = "NONE" +} + +variable "method_request_parameters" { + type = map + default = {} +} + +variable "request_templates" { + type = map + default = {} +} + +variable "responses" { + type = list + default = [] +} \ No newline at end of file