|
| 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 = "S3ListBucketPolicy" |
| 20 | + description = "Allow Lambda to check a given S3 bucket exists" |
| 21 | + policy = jsonencode({ |
| 22 | + Version = "2012-10-17", |
| 23 | + Statement = [{ |
| 24 | + Effect = "Allow", |
| 25 | + Action = ["s3:ListBucket"], |
| 26 | + Resource = "*" |
| 27 | + }] |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_iam_role_policy_attachment" "attach_execution_role_policy" { |
| 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 | + architectures = [var.architecture] |
| 55 | + memory_size = 512 |
| 56 | + tracing_config { |
| 57 | + mode = var.lambda_tracing_mode |
| 58 | + } |
| 59 | + layers = var.adot_layer_arn != null && var.adot_layer_arn != "" ? [var.adot_layer_arn] : [] |
| 60 | + environment { |
| 61 | + variables = var.adot_layer_arn != null && var.adot_layer_arn != "" ? { |
| 62 | + AWS_LAMBDA_EXEC_WRAPPER = "/opt/otel-instrument" |
| 63 | + } : {} |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +### API Gateway proxy to Lambda function |
| 68 | +resource "aws_api_gateway_rest_api" "apigw_lambda_api" { |
| 69 | + name = var.api_gateway_name |
| 70 | +} |
| 71 | + |
| 72 | +resource "aws_api_gateway_resource" "apigw_lambda_resource" { |
| 73 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 74 | + parent_id = aws_api_gateway_rest_api.apigw_lambda_api.root_resource_id |
| 75 | + path_part = "lambda" |
| 76 | +} |
| 77 | + |
| 78 | +resource "aws_api_gateway_method" "apigw_lambda_method" { |
| 79 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 80 | + resource_id = aws_api_gateway_resource.apigw_lambda_resource.id |
| 81 | + http_method = "ANY" |
| 82 | + authorization = "NONE" |
| 83 | +} |
| 84 | + |
| 85 | +resource "aws_api_gateway_integration" "apigw_lambda_integration" { |
| 86 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 87 | + resource_id = aws_api_gateway_resource.apigw_lambda_resource.id |
| 88 | + http_method = aws_api_gateway_method.apigw_lambda_method.http_method |
| 89 | + integration_http_method = "POST" |
| 90 | + type = "AWS_PROXY" |
| 91 | + uri = aws_lambda_function.sampleLambdaFunction.invoke_arn |
| 92 | +} |
| 93 | + |
| 94 | +resource "aws_api_gateway_deployment" "apigw_lambda_deployment" { |
| 95 | + depends_on = [ |
| 96 | + aws_api_gateway_integration.apigw_lambda_integration |
| 97 | + ] |
| 98 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 99 | +} |
| 100 | + |
| 101 | +resource "aws_api_gateway_stage" "test" { |
| 102 | + stage_name = "default" |
| 103 | + rest_api_id = aws_api_gateway_rest_api.apigw_lambda_api.id |
| 104 | + deployment_id = aws_api_gateway_deployment.apigw_lambda_deployment.id |
| 105 | + xray_tracing_enabled = var.apigw_tracing_enabled |
| 106 | +} |
| 107 | + |
| 108 | +resource "aws_lambda_permission" "apigw_lambda_invoke" { |
| 109 | + statement_id = "AllowAPIGatewayInvoke" |
| 110 | + action = "lambda:InvokeFunction" |
| 111 | + function_name = aws_lambda_function.sampleLambdaFunction.function_name |
| 112 | + principal = "apigateway.amazonaws.com" |
| 113 | + source_arn = "${aws_api_gateway_rest_api.apigw_lambda_api.execution_arn}/*/*" |
| 114 | +} |
| 115 | + |
| 116 | +# Output the API Gateway URL |
| 117 | +output "invoke_url" { |
| 118 | + value = "${aws_api_gateway_stage.test.invoke_url}/lambda" |
| 119 | +} |
0 commit comments