forked from barneyparker/terraform-aws-api-sns
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4b36f08
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.tfstate | ||
*.backup | ||
.terraform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}) | ||
} | ||
} | ||
] | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
}) | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}", | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = [] | ||
} |