|
| 1 | +# API Gateway REST API for PDM Mock |
| 2 | +resource "aws_api_gateway_rest_api" "pdm_mock" { |
| 3 | + name = "${var.project}-${var.environment}-pdm-mock-api" |
| 4 | + description = "PDM Mock API for testing integration with Patient Data Manager" |
| 5 | + |
| 6 | + endpoint_configuration { |
| 7 | + types = ["REGIONAL"] |
| 8 | + } |
| 9 | + |
| 10 | + tags = { |
| 11 | + Name = "${var.project}-${var.environment}-pdm-mock-api" |
| 12 | + Project = var.project |
| 13 | + Environment = var.environment |
| 14 | + Component = local.component |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +# /resource path |
| 19 | +resource "aws_api_gateway_resource" "resource" { |
| 20 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 21 | + parent_id = aws_api_gateway_rest_api.pdm_mock.root_resource_id |
| 22 | + path_part = "resource" |
| 23 | +} |
| 24 | + |
| 25 | +# /resource/{id} path |
| 26 | +resource "aws_api_gateway_resource" "resource_id" { |
| 27 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 28 | + parent_id = aws_api_gateway_resource.resource.id |
| 29 | + path_part = "{id}" |
| 30 | +} |
| 31 | + |
| 32 | +# POST /resource - Create resource |
| 33 | +resource "aws_api_gateway_method" "create_resource" { |
| 34 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 35 | + resource_id = aws_api_gateway_resource.resource.id |
| 36 | + http_method = "POST" |
| 37 | + authorization = "NONE" |
| 38 | +} |
| 39 | + |
| 40 | +resource "aws_api_gateway_integration" "create_resource" { |
| 41 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 42 | + resource_id = aws_api_gateway_resource.resource.id |
| 43 | + http_method = aws_api_gateway_method.create_resource.http_method |
| 44 | + |
| 45 | + integration_http_method = "POST" |
| 46 | + type = "AWS_PROXY" |
| 47 | + uri = module.pdm_mock_api.lambda_invoke_arn |
| 48 | +} |
| 49 | + |
| 50 | +# GET /resource/{id} - Get resource |
| 51 | +resource "aws_api_gateway_method" "get_resource" { |
| 52 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 53 | + resource_id = aws_api_gateway_resource.resource_id.id |
| 54 | + http_method = "GET" |
| 55 | + authorization = "NONE" |
| 56 | +} |
| 57 | + |
| 58 | +resource "aws_api_gateway_integration" "get_resource" { |
| 59 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 60 | + resource_id = aws_api_gateway_resource.resource_id.id |
| 61 | + http_method = aws_api_gateway_method.get_resource.http_method |
| 62 | + |
| 63 | + integration_http_method = "POST" |
| 64 | + type = "AWS_PROXY" |
| 65 | + uri = module.pdm_mock_api.lambda_invoke_arn |
| 66 | +} |
| 67 | + |
| 68 | +# Lambda permission for API Gateway |
| 69 | +resource "aws_lambda_permission" "pdm_mock_api_gateway" { |
| 70 | + statement_id = "AllowAPIGatewayInvoke" |
| 71 | + action = "lambda:InvokeFunction" |
| 72 | + function_name = module.pdm_mock_api.lambda_function_name |
| 73 | + principal = "apigateway.amazonaws.com" |
| 74 | + |
| 75 | + # More specific source ARN for better security |
| 76 | + source_arn = "${aws_api_gateway_rest_api.pdm_mock.execution_arn}/*/*" |
| 77 | +} |
| 78 | + |
| 79 | +# Deployment |
| 80 | +resource "aws_api_gateway_deployment" "pdm_mock" { |
| 81 | + depends_on = [ |
| 82 | + aws_api_gateway_integration.create_resource, |
| 83 | + aws_api_gateway_integration.get_resource, |
| 84 | + ] |
| 85 | + |
| 86 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 87 | + |
| 88 | + triggers = { |
| 89 | + redeployment = sha1(jsonencode([ |
| 90 | + aws_api_gateway_resource.resource.id, |
| 91 | + aws_api_gateway_resource.resource_id.id, |
| 92 | + aws_api_gateway_method.create_resource.id, |
| 93 | + aws_api_gateway_method.get_resource.id, |
| 94 | + aws_api_gateway_integration.create_resource.id, |
| 95 | + aws_api_gateway_integration.get_resource.id, |
| 96 | + ])) |
| 97 | + } |
| 98 | + |
| 99 | + lifecycle { |
| 100 | + create_before_destroy = true |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +# Stage |
| 105 | +resource "aws_api_gateway_stage" "pdm_mock" { |
| 106 | + deployment_id = aws_api_gateway_deployment.pdm_mock.id |
| 107 | + rest_api_id = aws_api_gateway_rest_api.pdm_mock.id |
| 108 | + stage_name = var.environment |
| 109 | + |
| 110 | + xray_tracing_enabled = true |
| 111 | + |
| 112 | + access_log_settings { |
| 113 | + destination_arn = aws_cloudwatch_log_group.pdm_mock_api_gateway.arn |
| 114 | + format = jsonencode({ |
| 115 | + requestId = "$context.requestId" |
| 116 | + ip = "$context.identity.sourceIp" |
| 117 | + caller = "$context.identity.caller" |
| 118 | + user = "$context.identity.user" |
| 119 | + requestTime = "$context.requestTime" |
| 120 | + httpMethod = "$context.httpMethod" |
| 121 | + resourcePath = "$context.resourcePath" |
| 122 | + status = "$context.status" |
| 123 | + protocol = "$context.protocol" |
| 124 | + responseLength = "$context.responseLength" |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + tags = { |
| 129 | + Name = "${var.project}-${var.environment}-pdm-mock-api-stage" |
| 130 | + Project = var.project |
| 131 | + Environment = var.environment |
| 132 | + Component = local.component |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +# CloudWatch Log Group for API Gateway |
| 137 | +resource "aws_cloudwatch_log_group" "pdm_mock_api_gateway" { |
| 138 | + name = "/aws/apigateway/${var.project}-${var.environment}-pdm-mock-api" |
| 139 | + retention_in_days = var.log_retention_in_days |
| 140 | + kms_key_id = module.kms.key_arn |
| 141 | + |
| 142 | + tags = { |
| 143 | + Name = "${var.project}-${var.environment}-pdm-mock-api-logs" |
| 144 | + Project = var.project |
| 145 | + Environment = var.environment |
| 146 | + Component = local.component |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +# Outputs |
| 151 | +output "pdm_mock_api_endpoint" { |
| 152 | + description = "The base URL of the PDM Mock API" |
| 153 | + value = aws_api_gateway_stage.pdm_mock.invoke_url |
| 154 | +} |
| 155 | + |
| 156 | +output "pdm_mock_api_id" { |
| 157 | + description = "The ID of the PDM Mock API Gateway" |
| 158 | + value = aws_api_gateway_rest_api.pdm_mock.id |
| 159 | +} |
0 commit comments