|
@@ -0,0 +1,58 @@
|
|
|
+data "archive_file" "lambda_example" {
|
|
|
+ type = "zip"
|
|
|
+ source_file = "${path.module}/lambda_scripts/example.py"
|
|
|
+ output_path = "${path.module}/lambda_scripts/example.zip"
|
|
|
+}
|
|
|
+
|
|
|
+resource "aws_lambda_function" "lambda_example" {
|
|
|
+ # If the file is not in the current working directory you will need to include a
|
|
|
+ # path.module in the filename.
|
|
|
+ filename = data.archive_file.lambda_example.output_path
|
|
|
+ function_name = "game_server_example"
|
|
|
+ role = aws_iam_role.lambda_role.arn
|
|
|
+ handler = "example.lambda_handler"
|
|
|
+
|
|
|
+ # The filebase64sha256() function is available in Terraform 0.11.12 and later
|
|
|
+ # For Terraform 0.11.11 and earlier, use the base64sha256() function and the file() function:
|
|
|
+ # source_code_hash = "${base64sha256(file("lambda_function_payload.zip"))}"
|
|
|
+ source_code_hash = data.archive_file.lambda_example.output_base64sha256
|
|
|
+
|
|
|
+ runtime = "python3.9"
|
|
|
+
|
|
|
+ environment {
|
|
|
+ variables = {
|
|
|
+ foo = "bar"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tags = local.tags
|
|
|
+}
|
|
|
+
|
|
|
+resource "aws_lambda_permission" "api_lambda_example" {
|
|
|
+ statement_id = "AllowExecutionFromAPI"
|
|
|
+ action = "lambda:InvokeFunction"
|
|
|
+ function_name = aws_lambda_function.lambda_example.function_name
|
|
|
+ principal = "apigateway.amazonaws.com"
|
|
|
+
|
|
|
+ # The /*/*/* part allows invocation from any stage, method and resource path
|
|
|
+ # within API Gateway REST API.
|
|
|
+ #source_arn = "${aws_apigatewayv2_stage.test.execution_arn}/*/*/*"
|
|
|
+ source_arn = "${aws_apigatewayv2_api.gateway.execution_arn}/*/*/*"
|
|
|
+ #qualifier = aws_lambda_alias.test_alias.name
|
|
|
+}
|
|
|
+
|
|
|
+# The API Gateway Route
|
|
|
+resource "aws_apigatewayv2_integration" "lambda_example" {
|
|
|
+ api_id = aws_apigatewayv2_api.gateway.id
|
|
|
+ integration_type = "AWS_PROXY"
|
|
|
+ connection_type = "INTERNET"
|
|
|
+ integration_method = "POST"
|
|
|
+ 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"
|
|
|
+}
|
|
|
+
|
|
|
+resource "aws_apigatewayv2_route" "lambda_example" {
|
|
|
+ api_id = aws_apigatewayv2_api.gateway.id
|
|
|
+ route_key = "GET /example"
|
|
|
+ target = "integrations/${aws_apigatewayv2_integration.lambda_example.id}"
|
|
|
+ authorization_type = "JWT"
|
|
|
+ authorizer_id = aws_apigatewayv2_authorizer.auth.id
|
|
|
+}
|