如何使用terraform在aws api网关中添加URL查询字符串参数?

时间:2017-06-28 11:43:56

标签: api amazon-web-services terraform

我想在API中传递account_id,如下面的https://exampleapi.com/dev?account_id=12345

以下是创建aws api网关的terraform代码段:

resource "aws_api_gateway_method" "example_api_method" {
  rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
  resource_id = "${aws_api_gateway_resource.example_api_resource.id}"
  http_method = "GET"
  authorization = "NONE"

}

resource "aws_api_gateway_integration" "example_api_method-integration" {
  rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
  resource_id = "${aws_api_gateway_resource.example_api_resource.id}"
  http_method = "${aws_api_gateway_method.example_api_method.http_method}"
  type = "AWS"
  uri = "arn:aws:apigateway:${var.aws_region}:lambda:path/functions/${var.lambda_arn}/invocations"
  integration_http_method = "GET"
}

提前致谢。

5 个答案:

答案 0 :(得分:1)

    resource "aws_api_gateway_method" "example_api_method" {
      rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
      resource_id = "${aws_api_gateway_resource.example_api_resource.id}"
      http_method = "GET"
      authorization = "NONE"

      request_parameters = {
        "integration.request.querystring.account_id"=true
      }
    }

有关更多信息,请参见https://www.terraform.io/docs/providers/aws/r/api_gateway_method.html

上的aws_api_gateway_method资源的request_parameters字段

答案 1 :(得分:0)

在此集成资源上,您可以使用request_templates参数并使用input variable。如果您添加了喜欢的内容

resource "aws_api_gateway_integration" "example_api_method-integration" {
  rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
  resource_id = "${aws_api_gateway_resource.example_api_resource.id}"
  http_method = "${aws_api_gateway_method.example_api_method.http_method}"
  type = "AWS"
  uri = "arn:aws:apigateway:${var.aws_region}:lambda:path/functions/${var.lambda_arn}/invocations"
  integration_http_method = "GET"
  request_templates = {
  "application/json" = <<EOF
{
"account_id": "$util.escapeJavaScript($input.params().querystring.get("account_id))"
}
EOF
  }
}

到你的terraform你会得到一个名为account_id的查询字符串添加到你的活动中。 input variable文档中还有一个示例,说明如何在查询字符串键下解压缩所有查询字符串。

答案 2 :(得分:0)

request_parameters = { "integration.request.querystring.var1"="'value'" "integration.request.querystring.var2"="'value'" }

请在var 1和var 2中输入所需的值,并将它们各自的值保持为值。

答案 3 :(得分:0)

您可以使用aws_caller_identity获取account_id

data "aws_caller_identity" "current" {} 

resource "aws_api_gateway_method" "example_api_method" {
  rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
  resource_id = "${aws_api_gateway_resource.example_api_resource.id}"
  http_method = "GET"
  authorization = "NONE"

}

resource "aws_api_gateway_integration" "example_api_method-integration" {
  rest_api_id = "${aws_api_gateway_rest_api.example_api.id}"
  resource_id = "${aws_api_gateway_resource.example_api_resource.id}"
  http_method = "${aws_api_gateway_method.example_api_method.http_method}"
  type = "AWS"
  uri  = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${data.aws_caller_identity.current.account_id}:function:${var.lambda_function_name}/invocations
  integration_http_method = "GET"
}

答案 4 :(得分:0)

如果您尝试执行这样的查询字符串:

url.com/some-path?queryStringHere=value

那么您不必在 Terraform 中指定任何内容。您可以向 URL 添加任何查询,例如 ?queryStringHere=value 并访问它,例如在 lambda 中通过 event 对象和以下之一:

queryStringParameters: { start_time: '1' },
multiValueQueryStringParameters: { start_time: [ '1' ] }