|
| 1 | +# Provider configuration |
| 2 | +provider "aws" { |
| 3 | + region = "us-east-1" # Change this to your desired region |
| 4 | +} |
| 5 | + |
| 6 | +terraform { |
| 7 | + required_providers { |
| 8 | + aws = { |
| 9 | + source = "hashicorp/aws" |
| 10 | + version = "~> 4.0" |
| 11 | + } |
| 12 | + archive = { |
| 13 | + source = "hashicorp/archive" |
| 14 | + version = "~> 2.0" |
| 15 | + } |
| 16 | + null = { |
| 17 | + source = "hashicorp/null" |
| 18 | + version = "~> 3.0" |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +# Archive the Lambda function code |
| 24 | +data "archive_file" "lambda_zip" { |
| 25 | + type = "zip" |
| 26 | + source_dir = "${path.module}/src" |
| 27 | + output_path = "${path.module}/lambda.zip" |
| 28 | +} |
| 29 | + |
| 30 | +# API Gateway REST API |
| 31 | +resource "aws_api_gateway_rest_api" "main_api" { |
| 32 | + name = "validation-api" |
| 33 | + description = "API Gateway with data validation" |
| 34 | + |
| 35 | + body = jsonencode({ |
| 36 | + openapi = "3.0.1" |
| 37 | + info = { |
| 38 | + title = "validation-api" |
| 39 | + version = "1.0" |
| 40 | + } |
| 41 | + components = { |
| 42 | + schemas = { |
| 43 | + Vehicle = { |
| 44 | + type = "object" |
| 45 | + required = ["make", "model", "year"] |
| 46 | + properties = { |
| 47 | + make = { |
| 48 | + type = "string" |
| 49 | + } |
| 50 | + model = { |
| 51 | + type = "string" |
| 52 | + } |
| 53 | + year = { |
| 54 | + type = "integer" |
| 55 | + minimum = 2010 |
| 56 | + } |
| 57 | + color = { |
| 58 | + type = "string" |
| 59 | + enum = ["green", "red", "blue"] |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +# Lambda Function |
| 69 | +resource "aws_lambda_function" "process_function" { |
| 70 | + filename = data.archive_file.lambda_zip.output_path |
| 71 | + source_code_hash = data.archive_file.lambda_zip.output_base64sha256 |
| 72 | + function_name = "process-function" |
| 73 | + role = aws_iam_role.lambda_role.arn |
| 74 | + handler = "app.lambda_handler" |
| 75 | + runtime = "python3.13" |
| 76 | + architectures = ["arm64"] |
| 77 | + timeout = 3 |
| 78 | + |
| 79 | + depends_on = [ |
| 80 | + data.archive_file.lambda_zip |
| 81 | + ] |
| 82 | +} |
| 83 | + |
| 84 | +# IAM Role for Lambda |
| 85 | +resource "aws_iam_role" "lambda_role" { |
| 86 | + name = "process_function_role" |
| 87 | + |
| 88 | + assume_role_policy = jsonencode({ |
| 89 | + Version = "2012-10-17" |
| 90 | + Statement = [ |
| 91 | + { |
| 92 | + Action = "sts:AssumeRole" |
| 93 | + Effect = "Allow" |
| 94 | + Principal = { |
| 95 | + Service = "lambda.amazonaws.com" |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +# Basic Lambda execution policy |
| 103 | +resource "aws_iam_role_policy_attachment" "lambda_basic" { |
| 104 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 105 | + role = aws_iam_role.lambda_role.name |
| 106 | +} |
| 107 | + |
| 108 | +# API Gateway Resource |
| 109 | +resource "aws_api_gateway_resource" "api_resource" { |
| 110 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 111 | + parent_id = aws_api_gateway_rest_api.main_api.root_resource_id |
| 112 | + path_part = "{id}" |
| 113 | +} |
| 114 | + |
| 115 | +# API Gateway Method |
| 116 | +resource "aws_api_gateway_method" "api_method" { |
| 117 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 118 | + resource_id = aws_api_gateway_resource.api_resource.id |
| 119 | + http_method = "POST" |
| 120 | + authorization = "NONE" |
| 121 | + |
| 122 | + request_parameters = { |
| 123 | + "method.request.querystring.order" = true |
| 124 | + "method.request.header.custom-agent" = true |
| 125 | + } |
| 126 | + |
| 127 | + request_validator_id = aws_api_gateway_request_validator.validator.id |
| 128 | + request_models = { |
| 129 | + "application/json" = aws_api_gateway_model.vehicle_model.name |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +# Request Validator |
| 134 | +resource "aws_api_gateway_request_validator" "validator" { |
| 135 | + name = "validator" |
| 136 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 137 | + validate_request_body = true |
| 138 | + validate_request_parameters = true |
| 139 | +} |
| 140 | + |
| 141 | +# Vehicle Model |
| 142 | +resource "aws_api_gateway_model" "vehicle_model" { |
| 143 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 144 | + name = "Vehicle" |
| 145 | + description = "Vehicle model for validation" |
| 146 | + content_type = "application/json" |
| 147 | + |
| 148 | + schema = jsonencode({ |
| 149 | + type = "object" |
| 150 | + required = ["make", "model", "year"] |
| 151 | + properties = { |
| 152 | + make = { |
| 153 | + type = "string" |
| 154 | + } |
| 155 | + model = { |
| 156 | + type = "string" |
| 157 | + } |
| 158 | + year = { |
| 159 | + type = "integer" |
| 160 | + minimum = 2010 |
| 161 | + } |
| 162 | + color = { |
| 163 | + type = "string" |
| 164 | + enum = ["green", "red", "blue"] |
| 165 | + } |
| 166 | + } |
| 167 | + }) |
| 168 | +} |
| 169 | + |
| 170 | +# Lambda Integration |
| 171 | +resource "aws_api_gateway_integration" "lambda_integration" { |
| 172 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 173 | + resource_id = aws_api_gateway_resource.api_resource.id |
| 174 | + http_method = aws_api_gateway_method.api_method.http_method |
| 175 | + type = "AWS_PROXY" |
| 176 | + integration_http_method = "POST" |
| 177 | + uri = aws_lambda_function.process_function.invoke_arn |
| 178 | +} |
| 179 | + |
| 180 | +# Lambda Permission |
| 181 | +resource "aws_lambda_permission" "api_gateway" { |
| 182 | + statement_id = "AllowAPIGatewayInvoke" |
| 183 | + action = "lambda:InvokeFunction" |
| 184 | + function_name = aws_lambda_function.process_function.function_name |
| 185 | + principal = "apigateway.amazonaws.com" |
| 186 | + source_arn = "${aws_api_gateway_rest_api.main_api.execution_arn}/*/*/*" |
| 187 | +} |
| 188 | + |
| 189 | +# API Gateway Deployment |
| 190 | +resource "aws_api_gateway_deployment" "api_deployment" { |
| 191 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 192 | + depends_on = [aws_api_gateway_integration.lambda_integration] |
| 193 | +} |
| 194 | + |
| 195 | +# API Gateway Stage |
| 196 | +resource "aws_api_gateway_stage" "prod" { |
| 197 | + deployment_id = aws_api_gateway_deployment.api_deployment.id |
| 198 | + rest_api_id = aws_api_gateway_rest_api.main_api.id |
| 199 | + stage_name = "Prod" |
| 200 | +} |
| 201 | + |
| 202 | +# Output |
| 203 | +output "api_endpoint" { |
| 204 | + description = "API Gateway endpoint URL for Prod stage" |
| 205 | + value = "${aws_api_gateway_stage.prod.invoke_url}" |
| 206 | +} |
0 commit comments