Skip to content

Commit 6aeca6c

Browse files
updated api integration response templates
1 parent abff678 commit 6aeca6c

File tree

4 files changed

+108
-13
lines changed

4 files changed

+108
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Messaging as API
22
This terraform module will create a REST API to send Emails, SMS and Push Notifications using Amazon Pinpoint. You just provide the list of Projects, domain/email identities, this module will create projects and channels in Amazon Pinpoint and will generate REST API endpoints accordingly.
33

4+
## Pipoint is Regional
5+
The Amazon Pinpoint API is available in several AWS Regions and it provides an endpoint for each of these Regions. Also some of the Pinpoint service are not available in few regions. See [Amazon Pinpoint endpoints and quotas in the Amazon Web Services General Reference](https://docs.aws.amazon.com/general/latest/gr/pinpoint.html)
6+
47

58
# Links
69

apig-email.tf

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,25 @@ resource "aws_api_gateway_integration" "send_email_int" {
2323
resource_id = aws_api_gateway_resource.send_email[each.key].id
2424
http_method = each.value.http_method
2525
integration_http_method = local.http_methods.POST
26-
type = local.integration_types.AWS_PROXY
26+
type = local.integration_types.AWS
2727
uri = aws_lambda_function.send_email[each.key].invoke_arn
28+
29+
request_templates = {
30+
"application/json" = <<EOF
31+
#set( $bodyStr = $util.parseJson($input.body) )
32+
#if( $bodyStr != "" )
33+
{
34+
"httpMethod": "$context.httpMethod",
35+
"body": $input.body
36+
}
37+
#else
38+
{
39+
"httpMethod": "ERROR-BODY"
40+
}
41+
#end
42+
EOF
43+
}
44+
2845
}
2946

3047
resource "aws_api_gateway_method_response" "send_email_post_res_200" {
@@ -34,25 +51,100 @@ resource "aws_api_gateway_method_response" "send_email_post_res_200" {
3451
resource_id = aws_api_gateway_resource.send_email[each.key].id
3552
http_method = each.value.http_method
3653
status_code = "200"
54+
55+
//cors section
56+
response_parameters = {
57+
"method.response.header.Access-Control-Allow-Headers" = true,
58+
"method.response.header.Access-Control-Allow-Methods" = true,
59+
"method.response.header.Access-Control-Allow-Origin" = true
60+
}
3761
}
3862

39-
resource "aws_api_gateway_integration_response" "send_email_int_res" {
63+
resource "aws_api_gateway_integration_response" "send_email_int_res_200" {
4064
for_each = aws_api_gateway_method_response.send_email_post_res_200
4165

4266
rest_api_id = aws_api_gateway_rest_api.messaging[0].id
4367
resource_id = aws_api_gateway_resource.send_email[each.key].id
4468
http_method = each.value.http_method
4569
status_code = aws_api_gateway_method_response.send_email_post_res_200[each.key].status_code
4670

47-
# Transforms the backend JSON response to XML
71+
//cors
72+
response_parameters = {
73+
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
74+
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'",
75+
"method.response.header.Access-Control-Allow-Origin" = "'*'"
76+
}
77+
78+
# response_templates = {
79+
# "application/json" = <<EOF
80+
# #set($inputRoot = $input.path('$'))
81+
# {
82+
# errorMessage: $inputRoot.body;
83+
# }
84+
# EOF
85+
# }
86+
87+
depends_on = [
88+
aws_api_gateway_method.send_email_post,
89+
aws_api_gateway_integration.send_email_int
90+
]
91+
}
92+
93+
resource "aws_api_gateway_method_response" "send_email_post_res_400" {
94+
for_each = aws_api_gateway_integration.send_email_int
95+
96+
rest_api_id = aws_api_gateway_rest_api.messaging[0].id
97+
resource_id = aws_api_gateway_resource.send_email[each.key].id
98+
http_method = each.value.http_method
99+
status_code = "400"
100+
}
101+
102+
resource "aws_api_gateway_integration_response" "send_email_int_res_400" {
103+
for_each = aws_api_gateway_method_response.send_email_post_res_400
104+
105+
selection_pattern = ".*\"errorCode\":400.*"
106+
rest_api_id = aws_api_gateway_rest_api.messaging[0].id
107+
resource_id = aws_api_gateway_resource.send_email[each.key].id
108+
http_method = each.value.http_method
109+
status_code = aws_api_gateway_method_response.send_email_post_res_400[each.key].status_code
110+
48111
response_templates = {
49-
"application/json" = <<EOF
50-
#set($inputRoot = $input.path('$'))
51-
{
52-
errorMessage: $inputRoot.body;
112+
"application/json" = "$input.path('$.errorMessage')"
113+
}
114+
115+
depends_on = [
116+
aws_api_gateway_method.send_email_post,
117+
aws_api_gateway_integration.send_email_int
118+
]
53119
}
54-
EOF
120+
121+
122+
resource "aws_api_gateway_method_response" "send_email_post_res_500" {
123+
for_each = aws_api_gateway_integration.send_email_int
124+
125+
rest_api_id = aws_api_gateway_rest_api.messaging[0].id
126+
resource_id = aws_api_gateway_resource.send_email[each.key].id
127+
http_method = each.value.http_method
128+
status_code = "500"
129+
}
130+
131+
resource "aws_api_gateway_integration_response" "send_email_int_res_500" {
132+
for_each = aws_api_gateway_method_response.send_email_post_res_500
133+
134+
selection_pattern = ".*\"errorCode\":500.*"
135+
rest_api_id = aws_api_gateway_rest_api.messaging[0].id
136+
resource_id = aws_api_gateway_resource.send_email[each.key].id
137+
http_method = each.value.http_method
138+
status_code = aws_api_gateway_method_response.send_email_post_res_500[each.key].status_code
139+
140+
response_templates = {
141+
"application/json" = "$input.path('$.errorMessage')"
55142
}
143+
144+
depends_on = [
145+
aws_api_gateway_method.send_email_post,
146+
aws_api_gateway_integration.send_email_int
147+
]
56148
}
57149

58150
resource "aws_lambda_permission" "send_email_post" {

lambda.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ resource "aws_lambda_function" "send_email" {
1919

2020
environment {
2121
variables = {
22+
"CURRENT_REGION": data.aws_region.current.name,
2223
"PINPOINT_APP_ID" : aws_pinpoint_app.project[each.key].application_id,
2324
"FROM_EMAIL_ID" : each.value.from_email
2425
}

send_email.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
const { PinpointClient, SendMessagesCommand } = require("@aws-sdk/client-pinpoint");
33

44
async function sendEmail(toAddress, params) {
5-
const REGION = "us-east-1";
6-
const pinClient = new PinpointClient({ region: REGION });
5+
const pinClient = new PinpointClient({ region: process.env.CURRENT_REGION });
76

87
const { MessageResponse } = await pinClient.send(
98
new SendMessagesCommand(params),
@@ -89,7 +88,8 @@ exports.handler = async function (event, context, callback) {
8988
}
9089

9190
// get values from payload
92-
var payload = JSON.parse(event.body);
91+
// var payload = JSON.parse(event.body);
92+
var payload = getParamValue(event, "body", true, errorCallback);
9393
var toAddress = getParamValue(payload, "recipient", true, errorCallback);
9494
var subject = getParamValue(payload, "subject", true, errorCallback);
9595
var body = getParamValue(payload, "body", true, errorCallback);
@@ -130,7 +130,6 @@ exports.handler = async function (event, context, callback) {
130130
const res = await sendEmail(toAddress, params);
131131
successCallback(res);
132132
} catch (err) {
133-
errorCallback(err);
133+
errorCallback(err, 500);
134134
}
135-
return;
136135
};

0 commit comments

Comments
 (0)