|
| 1 | +### Lambda function |
| 2 | +locals { |
| 3 | + architecture = var.architecture == "x86_64" ? "amd64" : "arm64" |
| 4 | +} |
| 5 | + |
| 6 | +resource "aws_iam_role" "lambda_role" { |
| 7 | + name = "lambda_execution_role" |
| 8 | + assume_role_policy = jsonencode({ |
| 9 | + Version = "2012-10-17", |
| 10 | + Statement = [{ |
| 11 | + Action = "sts:AssumeRole", |
| 12 | + Effect = "Allow", |
| 13 | + Principal = { Service = "lambda.amazonaws.com" } |
| 14 | + }] |
| 15 | + }) |
| 16 | +} |
| 17 | + |
| 18 | +resource "aws_iam_policy" "s3_access" { |
| 19 | + name = "S3ListBucketsPolicy" |
| 20 | + description = "Allow Lambda to list S3 buckets" |
| 21 | + policy = jsonencode({ |
| 22 | + Version = "2012-10-17", |
| 23 | + Statement = [{ |
| 24 | + Effect = "Allow", |
| 25 | + Action = ["s3:ListAllMyBuckets"], |
| 26 | + Resource = "*" |
| 27 | + }] |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_iam_role_policy_attachment" "attachBasicExecutionRolePolicy" { |
| 32 | + role = aws_iam_role.lambda_role.name |
| 33 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 34 | +} |
| 35 | + |
| 36 | +resource "aws_iam_role_policy_attachment" "attach_s3_policy" { |
| 37 | + role = aws_iam_role.lambda_role.name |
| 38 | + policy_arn = aws_iam_policy.s3_access.arn |
| 39 | +} |
| 40 | + |
| 41 | +resource "aws_iam_role_policy_attachment" "attach_xray_policy" { |
| 42 | + role = aws_iam_role.lambda_role.name |
| 43 | + policy_arn = "arn:aws:iam::aws:policy/AWSXRayDaemonWriteAccess" |
| 44 | +} |
| 45 | + |
| 46 | +resource "aws_lambda_function" "sampleLambdaFunction" { |
| 47 | + function_name = var.function_name |
| 48 | + runtime = var.runtime |
| 49 | + timeout = 300 |
| 50 | + handler = "com.amazon.sampleapp.LambdaHandler::handleRequest" |
| 51 | + role = aws_iam_role.lambda_role.arn |
| 52 | + filename = "${path.module}/../build/distributions/lambda-function.zip" |
| 53 | + source_code_hash = filebase64sha256("${path.module}/../build/distributions/lambda-function.zip") |
| 54 | + tracing_config { |
| 55 | + mode = var.lambda_tracing_mode |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +### API Gateway proxy to Lambda function |
| 60 | +resource "aws_api_gateway_rest_api" "apigw_lambda_api" { |
| 61 | + name = var.api_gateway_name |
| 62 | +} |
| 63 | + |
| 64 | +resource "aws_api_gateway_resource" "apigw_lambda_resource" { |
| 65 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 66 | + parent_id = aws_api_gateway_rest_api.apigw_lambda_api.root_resource_id |
| 67 | + path_part = "lambda" |
| 68 | +} |
| 69 | + |
| 70 | +resource "aws_api_gateway_method" "apigw_lambda_method" { |
| 71 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 72 | + resource_id = aws_api_gateway_resource.apigw_lambda_resource.id |
| 73 | + http_method = "ANY" |
| 74 | + authorization = "NONE" |
| 75 | +} |
| 76 | + |
| 77 | +resource "aws_api_gateway_integration" "apigw_lambda_integration" { |
| 78 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 79 | + resource_id = aws_api_gateway_resource.apigw_lambda_resource.id |
| 80 | + http_method = aws_api_gateway_method.apigw_lambda_method.http_method |
| 81 | + integration_http_method = "POST" |
| 82 | + type = "AWS_PROXY" |
| 83 | + uri = aws_lambda_function.sampleLambdaFunction.invoke_arn |
| 84 | +} |
| 85 | + |
| 86 | +# resource "aws_api_gateway_method" "lambda_api_proxy_root_nodejs" { |
| 87 | +# rest_api_id = aws_api_gateway_rest_api.lambda_api_proxy.id |
| 88 | +# resource_id = aws_api_gateway_rest_api.lambda_api_proxy.root_resource_id |
| 89 | +# http_method = "ANY" |
| 90 | +# authorization = "NONE" |
| 91 | +# } |
| 92 | +# |
| 93 | +# resource "aws_api_gateway_integration" "lambda_api_root_nodejs" { |
| 94 | +# rest_api_id = aws_api_gateway_rest_api.lambda_api_proxy.id |
| 95 | +# resource_id = aws_api_gateway_method.lambda_api_proxy_root_nodejs.resource_id |
| 96 | +# http_method = aws_api_gateway_method.lambda_api_proxy_root_nodejs.http_method |
| 97 | +# |
| 98 | +# integration_http_method = "POST" |
| 99 | +# type = "AWS_PROXY" |
| 100 | +# uri = var.function_invoke_arn |
| 101 | +# } |
| 102 | + |
| 103 | +resource "aws_api_gateway_deployment" "apigw_lambda_deployment" { |
| 104 | + depends_on = [ |
| 105 | + aws_api_gateway_integration.apigw_lambda_integration |
| 106 | + ] |
| 107 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 108 | +} |
| 109 | + |
| 110 | +resource "aws_api_gateway_stage" "test" { |
| 111 | + stage_name = "default" |
| 112 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 113 | + deployment_id = aws_api_gateway_deployment.apigw_lambda_deployment.id |
| 114 | + xray_tracing_enabled = var.apigw_tracing_enabled |
| 115 | +} |
| 116 | + |
| 117 | +resource "aws_lambda_permission" "apigw_lambda_invoke" { |
| 118 | + statement_id = "AllowAPIGatewayInvoke" |
| 119 | + action = "lambda:InvokeFunction" |
| 120 | + function_name = aws_lambda_function.sampleLambdaFunction.function_name |
| 121 | + principal = "apigateway.amazonaws.com" |
| 122 | + source_arn = "${aws_api_gateway_rest_api.apigw_lambda_api.execution_arn}/*/*" |
| 123 | +} |
| 124 | + |
| 125 | +# Output the API Gateway URL |
| 126 | +output "invoke_url" { |
| 127 | + value = "${aws_api_gateway_stage.test.invoke_url}/lambda" |
| 128 | +} |
0 commit comments