首页 > 解决方案 > 如何使用 terraform 在 AWS 上启用 CORS

问题描述

我正在尝试在我的 aws 项目上启用 CORS,该项目由API GatewayLambda功能组成。我正在使用GETOPTIONS方法创建一个 API 网关。 OPTIONS旨在成为根据 aws文档启用 CORS 的模拟端点。有一个 lambda 函数 ( aws_lambda_function.app_lambda) 由GET方法调用,并且在响应标头中具有:

"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"

但是,我仍然无法通过 CORS。

resource "aws_api_gateway_rest_api" "rest_api" {
  name        = "appAPIGateway"
  description = "App App App"
}

resource "aws_api_gateway_resource" "rest_api_resource" {
  depends_on = ["aws_api_gateway_rest_api.rest_api"]
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  parent_id = "${aws_api_gateway_rest_api.rest_api.root_resource_id}"
  path_part = "playground"
}

resource "aws_api_gateway_method" "opt" {
  rest_api_id   = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id   = "${aws_api_gateway_resource.rest_api_resource.id}"
  http_method   = "OPTIONS"
  authorization = "NONE"
  api_key_required = true
}

resource "aws_api_gateway_integration" "opt" {
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id = "${aws_api_gateway_resource.rest_api_resource.id}"
  http_method = "${aws_api_gateway_method.opt.http_method}"
  type = "MOCK"
}

resource "aws_api_gateway_integration_response" "opt" {
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id = "${aws_api_gateway_resource.rest_api_resource.id}"
  http_method = "${aws_api_gateway_method.opt.http_method}"
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = "'*'",
    "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Requested-With'",
    "method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'"
  }
  depends_on = ["aws_api_gateway_integration.opt", "aws_api_gateway_method_response.opt"]
}

resource "aws_api_gateway_method_response" "opt" {
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id = "${aws_api_gateway_resource.rest_api_resource.id}"
  http_method = "${aws_api_gateway_method.opt.http_method}"
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = true,
    "method.response.header.Access-Control-Allow-Methods" = true,
    "method.response.header.Access-Control-Allow-Headers" = true
  }
  response_models = {
    "application/json" = "Empty"
  }
  depends_on = ["aws_api_gateway_method.opt"]
}

resource "aws_api_gateway_method" "app_api_gateway_method" {
  rest_api_id      = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id      = "${aws_api_gateway_resource.rest_api_resource.id}"
  http_method      = "GET"
  authorization    = "NONE"
  api_key_required = true
}

resource "aws_api_gateway_method_response" "app_cors_method_response_200" {
    rest_api_id   = "${aws_api_gateway_rest_api.rest_api.id}"
    resource_id   = "${aws_api_gateway_resource.rest_api_resource.id}"
    http_method   = "${aws_api_gateway_method.app_api_gateway_method.http_method}"
    status_code   = "200"
    response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = true,
    "method.response.header.Access-Control-Allow-Methods" = true,
    "method.response.header.Access-Control-Allow-Headers" = true
  }
    depends_on = ["aws_api_gateway_method.app_api_gateway_method"]
}

resource "aws_api_gateway_integration" "app_api_gateway_integration" {
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id = "${aws_api_gateway_method.app_api_gateway_method.resource_id}"
  http_method = "${aws_api_gateway_method.app_api_gateway_method.http_method}"
  integration_http_method = "POST"
  type                    = "AWS_PROXY"
  uri                     = "${aws_lambda_function.app_lambda.invoke_arn}"
  depends_on    = [
    "aws_api_gateway_method.app_api_gateway_method",
    "aws_lambda_function.app_lambda"
    ]
}

resource "aws_api_gateway_integration_response" "app_api_gateway_integration_response" {
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  resource_id = "${aws_api_gateway_resource.rest_api_resource.id}"
  http_method = "${aws_api_gateway_method.app_api_gateway_method.http_method}"
  status_code = 200
  response_parameters = {
    "method.response.header.Access-Control-Allow-Origin" = "'*'",
    "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Requested-With'",
    "method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'"
  }
  depends_on = [
    "aws_api_gateway_integration.app_api_gateway_integration",
    "aws_api_gateway_method_response.app_cors_method_response_200",
  ]
}

resource "aws_api_gateway_deployment" "app_api_gateway_deployment" {
  rest_api_id = "${aws_api_gateway_rest_api.rest_api.id}"
  stage_name  = "app_stage"
  depends_on = [
    "aws_api_gateway_integration_response.app_api_gateway_integration_response",
    "aws_api_gateway_integration_response.opt"
    ]
}

任何帮助将不胜感激。

标签: amazon-web-servicescorsterraformaws-api-gateway

解决方案


对于较新的 HTTP API (v2),您可以使用:

resource "aws_apigatewayv2_api" "lambda" {
  name          = "lambda_gw_api"
  protocol_type = "HTTP"
  cors_configuration {
    allow_origins = ["https://www.mywebsite.fr"]
    allow_methods = ["POST", "GET", "OPTIONS"]
    allow_headers = ["content-type"]
    max_age = 300
  }
}

PS:您可能还需要检查您的 OPTIONS 路由是否有“集成”并且不返回 401。


推荐阅读