lambda_list.tf 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # /list
  2. # Returns the event, and the context
  3. data "archive_file" "lambda_list" {
  4. type = "zip"
  5. source_file = "${path.module}/scripts/list.py"
  6. output_path = "${path.module}/tmp/list.zip"
  7. }
  8. resource "aws_lambda_function" "lambda_list" {
  9. # If the file is not in the current working directory you will need to include a
  10. # path.module in the filename.
  11. filename = data.archive_file.lambda_list.output_path
  12. function_name = "game_server_list"
  13. role = aws_iam_role.lambda_role.arn
  14. handler = "list.lambda_handler"
  15. # The filebase64sha256() function is available in Terraform 0.11.12 and later
  16. # For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
  17. # source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
  18. source_code_hash = data.archive_file.lambda_list.output_base64sha256
  19. runtime = "python3.9"
  20. environment {
  21. variables = {
  22. foo = "bar"
  23. }
  24. }
  25. vpc_config {
  26. subnet_ids = module.vpc.public_subnets # using public for now, to allow for Internets
  27. security_group_ids = [aws_security_group.allow_all.id]
  28. }
  29. tags = local.tags
  30. }
  31. resource "aws_lambda_permission" "api_lambda_list" {
  32. statement_id = "AllowExecutionFromAPI"
  33. action = "lambda:InvokeFunction"
  34. function_name = aws_lambda_function.lambda_list.function_name
  35. principal = "apigateway.amazonaws.com"
  36. # The /*/*/* part allows invocation from any stage, method and resource path
  37. # within API Gateway REST API.
  38. #source_arn = "${aws_apigatewayv2_stage.test.execution_arn}/*/*/*"
  39. source_arn = "${aws_apigatewayv2_api.gateway.execution_arn}/*/*/*"
  40. #qualifier = aws_lambda_alias.test_alias.name
  41. }
  42. # The API Gateway Route
  43. resource "aws_apigatewayv2_integration" "lambda_list" {
  44. api_id = aws_apigatewayv2_api.gateway.id
  45. integration_type = "AWS_PROXY"
  46. connection_type = "INTERNET"
  47. integration_method = "POST"
  48. integration_uri = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/arn:aws:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.id}:function:${aws_lambda_function.lambda_list.function_name}/invocations"
  49. }
  50. resource "aws_apigatewayv2_route" "lambda_list_GET" {
  51. api_id = aws_apigatewayv2_api.gateway.id
  52. route_key = "GET /list"
  53. target = "integrations/${aws_apigatewayv2_integration.lambda_list.id}"
  54. authorization_type = "JWT"
  55. authorizer_id = aws_apigatewayv2_authorizer.auth.id
  56. }
  57. resource "aws_apigatewayv2_route" "lambda_list_POST" {
  58. api_id = aws_apigatewayv2_api.gateway.id
  59. route_key = "POST /list"
  60. target = "integrations/${aws_apigatewayv2_integration.lambda_list.id}"
  61. authorization_type = "JWT"
  62. authorizer_id = aws_apigatewayv2_authorizer.auth.id
  63. }