@@ -52,7 +52,7 @@ resource "aws_ecr_lifecycle_policy" "this" {
52
52
})
53
53
}
54
54
55
- # if any file within lambda/** changes, run docker build locally and push a new image with latest tag to ECR repository
55
+ # run docker build locally and push a new image with latest tag to ECR repository (if any file within lambda/** changed)
56
56
57
57
locals {
58
58
lambda_path = " ${ path . module } /../lambda"
@@ -70,11 +70,13 @@ resource "null_resource" "local_docker_build_tag_push" {
70
70
provisioner "local-exec" {
71
71
working_dir = local. lambda_path
72
72
command = << EOT
73
+ orb
73
74
aws ecr get-login-password --profile ${ var . profile } --region ${ var . region } | docker login --username AWS --password-stdin ${ aws_ecr_repository . this . repository_url }
74
75
75
76
docker build --platform linux/arm64 -t ${ var . image_name } .
76
77
docker tag ${ var . image_name } :latest ${ aws_ecr_repository . this . repository_url } :latest
77
78
docker push ${ aws_ecr_repository . this . repository_url } :latest
79
+ orb stop
78
80
EOT
79
81
}
80
82
}
@@ -86,21 +88,21 @@ data "aws_ecr_image" "latest" {
86
88
image_tag = " latest"
87
89
}
88
90
89
- # lambda
91
+ # lambda - create or force an update if a new image was pushed (image digest of latest tag changed)
90
92
91
93
resource "aws_lambda_function" "this" {
92
94
depends_on = [data . aws_ecr_image . latest , aws_iam_role . lambda_execution ]
93
95
94
96
function_name = var. function_name
95
97
package_type = " Image"
96
- image_uri = " ${ aws_ecr_repository . this . repository_url } @${ data . aws_ecr_image . latest . image_digest } " # include the current image digest of latest tag to force lambda update if a new image was pushed
98
+ image_uri = " ${ aws_ecr_repository . this . repository_url } @${ data . aws_ecr_image . latest . image_digest } "
97
99
role = aws_iam_role. lambda_execution . arn
98
100
timeout = 45
99
101
memory_size = 512
100
102
}
101
103
104
+ # lambda - iam role to be assumed
102
105
103
- # iam role that the lambda will assume
104
106
resource "aws_iam_role" "lambda_execution" {
105
107
name = " ${ var . function_name } _lambda_execution_role"
106
108
@@ -143,3 +145,75 @@ resource "aws_iam_role_policy_attachment" "lambda_policy_attachment" {
143
145
role = aws_iam_role. lambda_execution . name
144
146
policy_arn = aws_iam_policy. lambda_policy . arn
145
147
}
148
+
149
+ # api gateway
150
+
151
+ resource "aws_api_gateway_rest_api" "api" {
152
+ name = var. api_name
153
+ }
154
+
155
+ resource "aws_api_gateway_resource" "proxy" {
156
+ rest_api_id = aws_api_gateway_rest_api. api . id
157
+ parent_id = aws_api_gateway_rest_api. api . root_resource_id
158
+ path_part = " api"
159
+ }
160
+
161
+ resource "aws_api_gateway_method" "proxy_method" {
162
+ rest_api_id = aws_api_gateway_rest_api. api . id
163
+ resource_id = aws_api_gateway_resource. proxy . id
164
+ http_method = " ANY"
165
+ authorization = " NONE"
166
+ }
167
+
168
+ resource "aws_api_gateway_integration" "lambda" {
169
+ rest_api_id = aws_api_gateway_rest_api. api . id
170
+ resource_id = aws_api_gateway_resource. proxy . id
171
+ http_method = aws_api_gateway_method. proxy_method . http_method
172
+ integration_http_method = " ANY"
173
+ type = " AWS_PROXY"
174
+ uri = aws_lambda_function. this . invoke_arn
175
+ timeout_milliseconds = 29000
176
+ }
177
+
178
+ resource "aws_api_gateway_method_response" "cors_response" {
179
+ rest_api_id = aws_api_gateway_rest_api. api . id
180
+ resource_id = aws_api_gateway_resource. proxy . id
181
+ http_method = aws_api_gateway_method. proxy_method . http_method
182
+ status_code = " 200"
183
+
184
+ response_parameters = {
185
+ " method.response.header.Access-Control-Allow-Origin" = true
186
+ " method.response.header.Access-Control-Allow-Methods" = true
187
+ " method.response.header.Access-Control-Allow-Headers" = true
188
+ }
189
+ }
190
+
191
+ resource "aws_api_gateway_integration_response" "cors_integration_response" {
192
+ rest_api_id = aws_api_gateway_rest_api. api . id
193
+ resource_id = aws_api_gateway_resource. proxy . id
194
+ http_method = aws_api_gateway_method. proxy_method . http_method
195
+ status_code = aws_api_gateway_method_response. cors_response . status_code
196
+
197
+ response_parameters = {
198
+ " method.response.header.Access-Control-Allow-Origin" = " '*'"
199
+ " method.response.header.Access-Control-Allow-Methods" = " 'OPTIONS,GET,POST,PUT,DELETE'"
200
+ " method.response.header.Access-Control-Allow-Headers" = " 'Content-Type'"
201
+ }
202
+
203
+ depends_on = [aws_api_gateway_integration . lambda ]
204
+ }
205
+
206
+ resource "aws_api_gateway_deployment" "api_deployment" {
207
+ depends_on = [aws_api_gateway_integration . lambda ]
208
+ rest_api_id = aws_api_gateway_rest_api. api . id
209
+ stage_name = " production"
210
+ }
211
+
212
+ resource "aws_lambda_permission" "apigw_lambda" {
213
+ statement_id = " AllowAPIGatewayInvoke"
214
+ action = " lambda:InvokeFunction"
215
+ function_name = aws_lambda_function. this . function_name
216
+ principal = " apigateway.amazonaws.com"
217
+
218
+ source_arn = " ${ aws_api_gateway_rest_api . api . execution_arn } /*/*"
219
+ }
0 commit comments