【问题标题】:How do I create an API Proxy using Terraform and AWS API Gateway如何使用 Terraform 和 AWS API Gateway 创建 API 代理
【发布时间】:2017-06-23 12:52:42
【问题描述】:

我正在尝试使用 Terraform 在 AWS 上的 API Gateway 中建立一个简单的 API 代理。基本上,我想包装根并将请求代理回另一个端点。它可能是最简单的设置,我似乎无法让它在 Terraform 中工作。

您将在下面找到脚本。此时我可以创建 REST API、定义资源、创建方法,但似乎没有任何方法可以将其定义为端点。

provider "aws" {
    region = "us-east-1"
}
resource "aws_api_gateway_rest_api" "TerraTest" {
  name = "TerraTest"
  description = "This is my API for demonstration purposes"
}

resource "aws_api_gateway_resource" "TerraProxyResource" {
  rest_api_id = "${aws_api_gateway_rest_api.TerraTest.id}"
  parent_id = "${aws_api_gateway_rest_api.TerraTest.root_resource_id}"
  path_part = "{proxy+}"
}

resource "aws_api_gateway_integration" "integration" {
    rest_api_id = "${aws_api_gateway_rest_api.TerraTest.id}"
    resource_id = "${aws_api_gateway_resource.TerraProxyResource.id}"
    http_method = "${aws_api_gateway_method.mymethod.http_method}"

    type = "HTTP_PROXY"
    uri = "http://api.endpoint.com/{proxy+}"
}

这里我将类型设置为代理,但我认为 URI 不是设置端点的正确属性。

resource "aws_api_gateway_method" "mymethod" {
  rest_api_id = "${aws_api_gateway_rest_api.TerraTest.id}"
  resource_id = "${aws_api_gateway_resource.TerraProxyResource.id}"
  http_method = "ANY"
  authorization = "NONE"
}

我希望这里的某个地方能够创建到其他端点的映射,但似乎没有任何属性。 (https://github.com/hashicorp/terraform/blob/master/builtin/providers/aws/resource_aws_api_gateway_method.go)

resource "aws_api_gateway_api_key" "TerraTestKey" {
  name = "Terra_Test_Key"

  stage_key {
    rest_api_id = "${aws_api_gateway_rest_api.TerraTest.id}"
    stage_name = "${aws_api_gateway_deployment.TerraTestDeployment.stage_name}"
  }
}


resource "aws_api_gateway_deployment" "TerraTestDeployment" {
  rest_api_id = "${aws_api_gateway_rest_api.TerraTest.id}"
  stage_name = "dev"
}

我扫描了源代码,但没有看到任何可以设置的属性。

任何人都可以分享任何建议/片段吗?

提姆

附言。如果你想尝试自己运行脚本,我把它放在这里:http://textuploader.com/d14sx

【问题讨论】:

  • 我在这里找到了我的问题的答案:stackoverflow.com/questions/39040739/…
  • 您能用详细代码回答您自己的问题吗?我需要参考。
  • @BMW 我在下面添加了它。祝你好运。

标签: amazon-web-services aws-api-gateway terraform


【解决方案1】:

这是显示有效解决方案的相关模块。它并不独立,因为它依赖于其他地方定义的一些变量,但它应该足以帮助任何努力获得 AWS 代理设置的人,并且还显示 L​​ambda 授权方集成作为奖励。

provider "aws" {
  region  = "${var.region}"
  profile = "${var.profile}"
}

data "aws_iam_role" "api_user" {
  role_name = "api_user"
}

module "authorizer_lambda" {
  source   = "../lambda"
  name     = "${var.api_name}-authorizer_lambda"
  filename = "authorizer_lambda"
  runtime  = "nodejs4.3"
  role     = "${data.aws_iam_role.api_user.arn}"
}

resource "aws_api_gateway_authorizer" "custom_authorizer" {
  name                   = "${var.api_name}-custom_authorizer"
  rest_api_id            = "${aws_api_gateway_rest_api.ApiGateway.id}"
  authorizer_uri         = "${module.authorizer_lambda.uri}"
  authorizer_credentials = "${data.aws_iam_role.api_user.arn}"
  authorizer_result_ttl_in_seconds = 1
}

resource "aws_api_gateway_rest_api" "ApiGateway" {
  name        = "${var.api_name}"
  description = "${var.api_description}"
}

resource "aws_api_gateway_resource" "ApiProxyResource" {
  rest_api_id = "${aws_api_gateway_rest_api.ApiGateway.id}"
  parent_id   = "${aws_api_gateway_rest_api.ApiGateway.root_resource_id}"
  path_part   = "{proxy+}"
}

resource "aws_api_gateway_integration" "ApiProxyIntegration" {
  rest_api_id              = "${aws_api_gateway_rest_api.ApiGateway.id}"
  resource_id              = "${aws_api_gateway_resource.ApiProxyResource.id}"
    http_method              = "${aws_api_gateway_method.ApiProxyMethod.http_method}"
    type                     = "HTTP_PROXY"
    integration_http_method  = "ANY"
    uri                      = "${format("%s/{proxy}", "${var.base_url}")}"
    passthrough_behavior     = "WHEN_NO_MATCH"
    request_parameters       = "${var.aws_api_gateway_integration_request_parameters}"
}

resource "aws_api_gateway_method" "ApiProxyMethod" {
  rest_api_id                   = "${aws_api_gateway_rest_api.ApiGateway.id}"
  resource_id                   = "${aws_api_gateway_resource.ApiProxyResource.id}"
  http_method                   = "ANY"
  authorization                 = "CUSTOM"
  authorizer_id                 = "${aws_api_gateway_authorizer.custom_authorizer.id}"  
  request_parameters            = {"method.request.path.proxy" = true}
}

resource "aws_api_gateway_deployment" "ApiDeployment" {
  depends_on = ["aws_api_gateway_method.ApiProxyMethod"]
  rest_api_id = "${aws_api_gateway_rest_api.ApiGateway.id}"
  stage_name = "${var.stage_name}"
}

【讨论】:

  • ${format(...)} 是一个地形函数:terraform.io/docs/configuration/functions/format.html
  • 为其他人指出,这里很容易错过的一大关键是aws_api_gateway_method 上的request_parameters 属性。如果没有这个,它将无法将 proxy 识别为集成 uri 中的参数。
  • 跟进@rayepps 的评论,查看用于填充 ApiProxyIntegration.request_parameters 的变量会非常有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 2016-12-07
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2019-02-03
  • 2018-05-25
相关资源
最近更新 更多