123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- # /storeit
- # Returns the event, and the context
- data "archive_file" "lambda_storeit" {
- type = "zip"
- source_file = "${path.module}/lambda_scripts/storeit.py"
- output_path = "${path.module}/lambda_scripts/storeit.zip"
- }
- resource "aws_lambda_function" "lambda_storeit" {
- # 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_storeit.output_path
- function_name = "game_server_storeit"
- role = aws_iam_role.lambda_role.arn
- handler = "storeit.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_storeit.output_base64sha256
- runtime = "python3.9"
- environment {
- variables = {
- REDIS_ENDPOINT = aws_elasticache_cluster.redis.cache_nodes[0]["address"]
- }
- }
- vpc_config {
- subnet_ids = module.vpc.public_subnets # using public for now, to allow for Internets
- security_group_ids = [aws_security_group.allow_all.id]
- }
- tags = local.tags
- }
- resource "aws_lambda_permission" "api_lambda_storeit" {
- statement_id = "AllowExecutionFromAPI"
- action = "lambda:InvokeFunction"
- function_name = aws_lambda_function.lambda_storeit.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_storeit" {
- 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_storeit.function_name}/invocations"
- }
- resource "aws_apigatewayv2_route" "lambda_storeit_POST" {
- api_id = aws_apigatewayv2_api.gateway.id
- route_key = "POST /storeit"
- target = "integrations/${aws_apigatewayv2_integration.lambda_storeit.id}"
- authorization_type = "JWT"
- authorizer_id = aws_apigatewayv2_authorizer.auth.id
- }
|