|
| 1 | +# http://development-interview-prep-lb-585997728.us-east-1.elb.amazonaws.com:3000/{proxy} |
| 2 | +# curl -X GET http://development-interview-prep-lb-585997728.us-east-1.elb.amazonaws.com:3000/api/v0/products |
| 3 | +# https://zbubkekd26.execute-api.us-east-1.amazonaws.com/dev |
| 4 | +# curl -X GET https://zbubkekd26.execute-api.us-east-1.amazonaws.com/dev/api/v0/products |
| 5 | + |
| 6 | + |
| 7 | +resource "aws_api_gateway_rest_api" "api" { |
| 8 | + name = "${var.environment}-interview-prep-api" |
| 9 | + description = "API Gateway for Interview Prep ${var.environment} environment" |
| 10 | +} |
| 11 | + |
| 12 | +resource "aws_api_gateway_resource" "proxy" { |
| 13 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 14 | + parent_id = aws_api_gateway_rest_api.api.root_resource_id |
| 15 | + path_part = "{proxy+}" |
| 16 | + |
| 17 | + depends_on = [ aws_api_gateway_rest_api.api ] |
| 18 | +} |
| 19 | + |
| 20 | +resource "aws_api_gateway_method" "proxy_method" { |
| 21 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 22 | + resource_id = aws_api_gateway_resource.proxy.id |
| 23 | + http_method = "ANY" |
| 24 | + authorization = "NONE" |
| 25 | + api_key_required = false |
| 26 | + request_parameters = { |
| 27 | + "method.request.path.proxy" = true |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +resource "aws_api_gateway_method" "proxy_options" { |
| 32 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 33 | + resource_id = aws_api_gateway_resource.proxy.id |
| 34 | + http_method = "OPTIONS" |
| 35 | + authorization = "NONE" |
| 36 | + api_key_required = false |
| 37 | +} |
| 38 | + |
| 39 | +resource "aws_api_gateway_integration" "proxy_integration" { |
| 40 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 41 | + resource_id = aws_api_gateway_resource.proxy.id |
| 42 | + http_method = aws_api_gateway_method.proxy_method.http_method |
| 43 | + type = "HTTP_PROXY" |
| 44 | + integration_http_method = "ANY" |
| 45 | + uri = "http://${var.lb_dns_name}:3000/{proxy}" |
| 46 | + request_parameters = { |
| 47 | + "integration.request.path.proxy" = "method.request.path.proxy" |
| 48 | + } |
| 49 | + timeout_milliseconds = 29000 |
| 50 | +} |
| 51 | + |
| 52 | +resource "aws_api_gateway_integration" "proxy_options_integration" { |
| 53 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 54 | + resource_id = aws_api_gateway_resource.proxy.id |
| 55 | + http_method = aws_api_gateway_method.proxy_options.http_method |
| 56 | + type = "MOCK" |
| 57 | + request_templates = { |
| 58 | + "application/json" = "{\"statusCode\": 200}" |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +resource "aws_api_gateway_integration_response" "proxy_options_integration_response" { |
| 63 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 64 | + resource_id = aws_api_gateway_resource.proxy.id |
| 65 | + http_method = aws_api_gateway_method.proxy_options.http_method |
| 66 | + status_code = "200" |
| 67 | + response_parameters = { |
| 68 | + "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'" |
| 69 | + "method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS'" |
| 70 | + "method.response.header.Access-Control-Allow-Origin" = "'*'" |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +resource "aws_api_gateway_method_response" "proxy_options_response" { |
| 75 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 76 | + resource_id = aws_api_gateway_resource.proxy.id |
| 77 | + http_method = aws_api_gateway_method.proxy_options.http_method |
| 78 | + status_code = "200" |
| 79 | + response_parameters = { |
| 80 | + "method.response.header.Access-Control-Allow-Headers" = true |
| 81 | + "method.response.header.Access-Control-Allow-Methods" = true |
| 82 | + "method.response.header.Access-Control-Allow-Origin" = true |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +resource "aws_api_gateway_deployment" "api_deployment" { |
| 87 | + depends_on = [ |
| 88 | + aws_api_gateway_integration.proxy_integration, |
| 89 | + aws_api_gateway_integration.proxy_options_integration, |
| 90 | + aws_api_gateway_integration_response.proxy_options_integration_response, |
| 91 | + aws_api_gateway_method_response.proxy_options_response |
| 92 | + ] |
| 93 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 94 | +} |
| 95 | + |
| 96 | +resource "aws_api_gateway_stage" "api_stage" { |
| 97 | + deployment_id = aws_api_gateway_deployment.api_deployment.id |
| 98 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 99 | + stage_name = "dev" |
| 100 | + |
| 101 | + access_log_settings { |
| 102 | + destination_arn = aws_cloudwatch_log_group.api_gateway_log_group.arn |
| 103 | + format = "$context.requestId $context.identity.sourceIp $context.identity.userAgent $context.requestTime $context.httpMethod $context.resourcePath $context.status $context.protocol $context.responseLength" |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +resource "aws_api_gateway_method_settings" "api_method_settings" { |
| 108 | + rest_api_id = aws_api_gateway_rest_api.api.id |
| 109 | + stage_name = aws_api_gateway_stage.api_stage.stage_name |
| 110 | + method_path = "*/*" |
| 111 | + settings { |
| 112 | + metrics_enabled = true |
| 113 | + logging_level = "INFO" |
| 114 | + data_trace_enabled = true |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +resource "aws_cloudwatch_log_group" "api_gateway_log_group" { |
| 119 | + name = "/aws/api-gateway/interview-prep/${aws_api_gateway_rest_api.api.id}" |
| 120 | + retention_in_days = 7 |
| 121 | +} |
| 122 | + |
| 123 | +resource "aws_iam_role" "api_gateway_cloudwatch_role" { |
| 124 | + name = "${var.environment}-interview-prep-api-gateway-cloudwatch-role" |
| 125 | + assume_role_policy = jsonencode({ |
| 126 | + Version = "2012-10-17", |
| 127 | + Statement = [ |
| 128 | + { |
| 129 | + Effect = "Allow", |
| 130 | + Principal = { |
| 131 | + Service = "apigateway.amazonaws.com" |
| 132 | + }, |
| 133 | + Action = "sts:AssumeRole" |
| 134 | + } |
| 135 | + ] |
| 136 | + }) |
| 137 | +} |
| 138 | + |
| 139 | +resource "aws_iam_policy" "api_gateway_cloudwatch_policy" { |
| 140 | + name = "${var.environment}-interview-prep-api-gateway-cloudwatch-policy" |
| 141 | + description = "Policy for API Gateway to write logs to CloudWatch" |
| 142 | + policy = jsonencode({ |
| 143 | + Version = "2012-10-17", |
| 144 | + Statement = [ |
| 145 | + { |
| 146 | + Effect = "Allow", |
| 147 | + Action = [ |
| 148 | + "logs:CreateLogGroup", |
| 149 | + "logs:CreateLogStream", |
| 150 | + "logs:PutLogEvents" |
| 151 | + ], |
| 152 | + Resource = "*" |
| 153 | + } |
| 154 | + ] |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +resource "aws_iam_role_policy_attachment" "api_gateway_cloudwatch_policy_attachment" { |
| 159 | + policy_arn = aws_iam_policy.api_gateway_cloudwatch_policy.arn |
| 160 | + role = aws_iam_role.api_gateway_cloudwatch_role.name |
| 161 | +} |
| 162 | + |
| 163 | +# resource "aws_api_gateway_api_key" "api_key" { |
| 164 | +# name = "${var.environment}-interview-prep-api-key" |
| 165 | +# description = "API Key for Interview Prep ${var.environment} environment" |
| 166 | +# enabled = true |
| 167 | +# } |
| 168 | + |
| 169 | +# resource "aws_api_gateway_usage_plan" "usage_plan" { |
| 170 | +# name = "${var.environment}-interview-prep-usage-plan" |
| 171 | +# description = "Usage Plan for Interview Prep ${var.environment} environment" |
| 172 | +# api_stages { |
| 173 | +# api_id = aws_api_gateway_rest_api.api.id |
| 174 | +# stage = aws_api_gateway_stage.api_stage.stage_name |
| 175 | +# } |
| 176 | +# } |
| 177 | + |
| 178 | +# resource "aws_api_gateway_usage_plan_key" "usage_plan_key" { |
| 179 | +# key_id = aws_api_gateway_api_key.api_key.id |
| 180 | +# key_type = "API_KEY" |
| 181 | +# usage_plan_id = aws_api_gateway_usage_plan.usage_plan.id |
| 182 | +# } |
0 commit comments