lambda_example.tf 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # /example
  2. # always returns {"a": "b"}
  3. data "archive_file" "lambda_example" {
  4. type = "zip"
  5. source_file = "${path.module}/scripts/example.py"
  6. output_path = "${path.module}/tmp/example.zip"
  7. }
  8. resource "aws_lambda_function" "lambda_example" {
  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_example.output_path
  12. function_name = "game_server_example"
  13. role = aws_iam_role.lambda_role.arn
  14. handler = "example.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_example.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_example" {
  32. statement_id = "AllowExecutionFromAPI"
  33. action = "lambda:InvokeFunction"
  34. function_name = aws_lambda_function.lambda_example.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_example" {
  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_example.function_name}/invocations"
  49. }
  50. resource "aws_apigatewayv2_route" "lambda_example" {
  51. api_id = aws_apigatewayv2_api.gateway.id
  52. route_key = "GET /example"
  53. target = "integrations/${aws_apigatewayv2_integration.lambda_example.id}"
  54. authorization_type = "JWT"
  55. authorizer_id = aws_apigatewayv2_authorizer.auth.id
  56. }