Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
claranet-barney committed Apr 18, 2020
0 parents commit 4b36f08
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.tfstate
*.backup
.terraform
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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"
})
}
}
]
}
```
42 changes: 42 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
@@ -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"
})
}
}
]
}
62 changes: 62 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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}",
]
}
}
28 changes: 28 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -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 = []
}

0 comments on commit 4b36f08

Please sign in to comment.