@@ -20,9 +20,43 @@ data "archive_file" "pack_calc" {
2020 output_path = " ${ path . module } /.terraform/archives/gymshark-pack-calc.zip"
2121}
2222
23+ data "archive_file" "token_authorizer" {
24+ type = " zip"
25+ source_file = " ${ path . module } /files/token_authoriser.js"
26+ output_path = " ${ path . module } /.terraform/archives/token_authorizer.zip"
27+ }
28+
2329resource "aws_lambda_permission" "apigateway" {
2430 action = " lambda:InvokeFunction"
2531 function_name = aws_lambda_function. pack_calc . function_name
2632 principal = " apigateway.amazonaws.com"
2733 source_arn = " ${ aws_apigatewayv2_api . pack_calc . execution_arn } /*/*/*"
34+ }
35+
36+
37+ resource "aws_lambda_function" "token_authoriser" {
38+ depends_on = [aws_cloudwatch_log_group . token_authoriser ]
39+ function_name = " gymshark-pack-calc-authorizer"
40+ handler = " token_authoriser.handler"
41+ role = aws_iam_role. lambda . arn
42+ runtime = " nodejs12.x"
43+ environment {
44+ variables = {
45+ SECRET_KEY = aws_ssm_parameter.api_token.name
46+ }
47+ }
48+ filename = data. archive_file . token_authorizer . output_path
49+ source_code_hash = data. archive_file . token_authorizer . output_base64sha256
50+
51+ }
52+ resource "aws_cloudwatch_log_group" "token_authoriser" {
53+ name = " /aws/lambda/gymshark-pack-calc-authorizer"
54+ retention_in_days = 14
55+ }
56+
57+ resource "aws_lambda_permission" "apigateway_auth" {
58+ action = " lambda:InvokeFunction"
59+ function_name = aws_lambda_function. token_authoriser . function_name
60+ principal = " apigateway.amazonaws.com"
61+ source_arn = " ${ aws_apigatewayv2_api . pack_calc . execution_arn } /authorizers/${ aws_apigatewayv2_authorizer . authoriser . id } "
2862}
0 commit comments