|
| 1 | + |
| 2 | +# Create deployment package for Lambda |
| 3 | +data "archive_file" "lambda_zip" { |
| 4 | + type = "zip" |
| 5 | + source_dir = "build/lambda" |
| 6 | + output_path = "build/lambda.zip" |
| 7 | +} |
| 8 | + |
| 9 | +# IAM role for Lambda function |
| 10 | +resource "aws_iam_role" "lambda_role" { |
| 11 | + name = "snowflake-counter-lambda-role" |
| 12 | + |
| 13 | + assume_role_policy = jsonencode({ |
| 14 | + Version = "2012-10-17" |
| 15 | + Statement = [ |
| 16 | + { |
| 17 | + Action = "sts:AssumeRole" |
| 18 | + Effect = "Allow" |
| 19 | + Principal = { |
| 20 | + Service = "lambda.amazonaws.com" |
| 21 | + } |
| 22 | + } |
| 23 | + ] |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +# Lambda function |
| 28 | +resource "aws_lambda_function" "user_lambda" { |
| 29 | + filename = data.archive_file.lambda_zip.output_path |
| 30 | + function_name = "user-service" |
| 31 | + role = aws_iam_role.lambda_role.arn |
| 32 | + handler = "handler.handler" |
| 33 | + source_code_hash = data.archive_file.lambda_zip.output_base64sha256 |
| 34 | + runtime = "python3.11" |
| 35 | + timeout = 30 |
| 36 | +} |
| 37 | + |
| 38 | +# API Gateway |
| 39 | +resource "aws_api_gateway_rest_api" "user_api" { |
| 40 | + name = "user-service-api" |
| 41 | + description = "User Service API" |
| 42 | + tags = { |
| 43 | + _custom_id_ = "users-api" |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +# API Gateway resource 'create' |
| 48 | +resource "aws_api_gateway_resource" "user_resource" { |
| 49 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 50 | + parent_id = aws_api_gateway_rest_api.user_api.root_resource_id |
| 51 | + path_part = "users" |
| 52 | +} |
| 53 | + |
| 54 | +# API Gateway method 'create' |
| 55 | +resource "aws_api_gateway_method" "create_user_method" { |
| 56 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 57 | + resource_id = aws_api_gateway_resource.user_resource.id |
| 58 | + http_method = "POST" |
| 59 | + authorization = "NONE" |
| 60 | +} |
| 61 | + |
| 62 | +# API Gateway integration 'create' |
| 63 | +resource "aws_api_gateway_integration" "lambda_integration" { |
| 64 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 65 | + resource_id = aws_api_gateway_resource.user_resource.id |
| 66 | + http_method = aws_api_gateway_method.create_user_method.http_method |
| 67 | + |
| 68 | + integration_http_method = "POST" |
| 69 | + type = "AWS_PROXY" |
| 70 | + uri = aws_lambda_function.user_lambda.invoke_arn |
| 71 | +} |
| 72 | + |
| 73 | +# API Gateway method 'list' |
| 74 | +resource "aws_api_gateway_method" "list_users_method" { |
| 75 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 76 | + resource_id = aws_api_gateway_resource.user_resource.id |
| 77 | + http_method = "GET" |
| 78 | + authorization = "NONE" |
| 79 | +} |
| 80 | + |
| 81 | +# API Gateway integration 'list' |
| 82 | +resource "aws_api_gateway_integration" "lambda_list_integration" { |
| 83 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 84 | + resource_id = aws_api_gateway_resource.user_resource.id |
| 85 | + http_method = aws_api_gateway_method.list_users_method.http_method |
| 86 | + |
| 87 | + integration_http_method = "POST" |
| 88 | + type = "AWS_PROXY" |
| 89 | + uri = aws_lambda_function.user_lambda.invoke_arn |
| 90 | +} |
| 91 | + |
| 92 | +# Lambda permission for API Gateway |
| 93 | +resource "aws_lambda_permission" "api_gateway_lambda" { |
| 94 | + statement_id = "AllowExecutionFromAPIGateway" |
| 95 | + action = "lambda:InvokeFunction" |
| 96 | + function_name = aws_lambda_function.user_lambda.function_name |
| 97 | + principal = "apigateway.amazonaws.com" |
| 98 | + source_arn = "${aws_api_gateway_rest_api.user_api.execution_arn}/*/*" |
| 99 | +} |
| 100 | + |
| 101 | +# API Gateway deployment |
| 102 | +resource "aws_api_gateway_deployment" "user_api_deployment" { |
| 103 | + depends_on = [ |
| 104 | + aws_api_gateway_integration.lambda_integration, |
| 105 | + ] |
| 106 | + |
| 107 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 108 | + |
| 109 | + lifecycle { |
| 110 | + create_before_destroy = true |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +# Stage |
| 115 | +resource "aws_api_gateway_stage" "stage" { |
| 116 | + deployment_id = aws_api_gateway_deployment.user_api_deployment.id |
| 117 | + rest_api_id = aws_api_gateway_rest_api.user_api.id |
| 118 | + stage_name = "test" |
| 119 | +} |
| 120 | + |
| 121 | +# Output the API Gateway ID |
| 122 | +output "api_endpoint" { |
| 123 | + value = "http://${aws_api_gateway_rest_api.user_api.id}.execute-api.localhost.localstack.cloud:4566/test/users" |
| 124 | + description = "API Gateway ID" |
| 125 | +} |
0 commit comments