8

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

7 Answers 7

2
    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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

4 Comments

Thanks, But still query string for the GET method is not being created in the api gateway.
Is that based on looking in the console? iirc this will not show up in the console but will show up on the mapped request object when calling the api.
Yes, in console it is not showing up! In the method request URL Query String Parameters it is not showing up.
integration_http_method should be "POST". AWS Lambda only uses post
0

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

Comments

0

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

Comments

0

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']

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.