@@ -23,8 +23,25 @@ resource "aws_api_gateway_integration" "send_email_int" {
23
23
resource_id = aws_api_gateway_resource. send_email [each . key ]. id
24
24
http_method = each. value . http_method
25
25
integration_http_method = local. http_methods . POST
26
- type = local. integration_types . AWS_PROXY
26
+ type = local. integration_types . AWS
27
27
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
+
28
45
}
29
46
30
47
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" {
34
51
resource_id = aws_api_gateway_resource. send_email [each . key ]. id
35
52
http_method = each. value . http_method
36
53
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
+ }
37
61
}
38
62
39
- resource "aws_api_gateway_integration_response" "send_email_int_res " {
63
+ resource "aws_api_gateway_integration_response" "send_email_int_res_200 " {
40
64
for_each = aws_api_gateway_method_response. send_email_post_res_200
41
65
42
66
rest_api_id = aws_api_gateway_rest_api. messaging [0 ]. id
43
67
resource_id = aws_api_gateway_resource. send_email [each . key ]. id
44
68
http_method = each. value . http_method
45
69
status_code = aws_api_gateway_method_response. send_email_post_res_200 [each . key ]. status_code
46
70
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
+
48
111
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
+ ]
53
119
}
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')"
55
142
}
143
+
144
+ depends_on = [
145
+ aws_api_gateway_method . send_email_post ,
146
+ aws_api_gateway_integration . send_email_int
147
+ ]
56
148
}
57
149
58
150
resource "aws_lambda_permission" "send_email_post" {
0 commit comments