dkakoti
dkakoti

Reputation: 667

How to add URL Query String Parameters in AWS API gateway using terraform?

I want to pass account_id in the API like below https://exampleapi.com/dev?account_id=12345

Here is the terraform snippet to create AWS API gateway:

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"
}

Upvotes: 8

Views: 9445

Answers (7)

ganesh kona
ganesh kona

Reputation: 1

I have tried with below option it is working for me.

request_parameters = {
    "method.request.querystring.bucket" = true
    "method.request.querystring.key" = true
  }

Upvotes: 0

ashraf minhaj
ashraf minhaj

Reputation: 1193

It's even simpler when you use query string parameter and for that you don't have to specify anything on Terraform.

Here's your url with path: www.url/path and you want to pass the value of data, so use like this wwww.url/path?data=1234

And then look up in

event['queryStringParameters']

Upvotes: 0

mkkl
mkkl

Reputation: 263

If you are trying to do query strings like this:

url.com/some-path?queryStringHere=value

Then you don't have to specify anything in Terraform. You can just add any query like ?queryStringHere=value to the URL and access it e.g. in a lambda via the event object and one of these:

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

Upvotes: 0

Nulljack
Nulljack

Reputation: 21

    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
      }
    }

For more info look at request_parameters field of aws_api_gateway_method resource found at https://www.terraform.io/docs/providers/aws/r/api_gateway_method.html

Upvotes: 2

Shanu
Shanu

Reputation: 171

you can use aws_caller_identity to get 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"
}

Upvotes: 0

Rahul Tanniru
Rahul Tanniru

Reputation: 11

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

please put the desired values you wanted in var 1 and var 2 and keep their respective values in value.

Upvotes: 1

khaleesi
khaleesi

Reputation: 983

On this integration resource you could make use of the request_templates parameter and making use of the input variable. If you added something to like

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
  }
}

to your terraform you would get a querystring called account_id added to your event. There is also an example in the input variable docs that shows how to unpack all querystrings under query string key.

Upvotes: 0

Related Questions