17

I want to run this command: https://docs.aws.amazon.com/cli/latest/reference/apigateway/test-invoke-method.html

It requires these two fields, but I can't find any docs and where these are:

aws apigateway test-invoke-method --rest-api-id 1234123412 --resource-id avl5sg8fw8 --http-method GET --path-with-query-string '/'

My API gateway endpoint looks like this:

https://abc123.execute-api.us-east-1.amazonaws.com/MyStage/

I only see on unique identifier there - but this command seems to require two IDs. Where do I find them in the API Gateway console?

halfer
  • 19,824
  • 17
  • 99
  • 186
red888
  • 27,709
  • 55
  • 204
  • 392

3 Answers3

31

Your rest-api-id is the identifier before 'execute-api' in your endpoint URL.

In your example URL:

https://abc123.execute-api.us-east-1.amazonaws.com/MyStage/

The rest-api-id is abc123

The resource ID can be obtained with the CLI using the get-resources call and the rest-api-id:

> aws apigateway get-resources --rest-api-id abc123
{
"items": [
    {
        "id": "xxxx1",
        "parentId": "xxxx0",
        "pathPart": "foo",
        "path": "/foo",
        "resourceMethods": {
            "GET": {}
        }
    },
    {
        "id": "xxxx0",
        "path": "/"
    }
]}

Each one of the records in the items attribute is a resource, and its id attribute is a resource ID you can use in your test-invoke-method in conjunction with a method that is associated with the resource.

Both values are visible at the top of the console when you select one of your endpoints/resources: enter image description here

Tres' Bailey
  • 709
  • 7
  • 17
  • 3
    You can obtain a list of all IDs using this command line: `aws apigateway get-rest-apis`. Note: You can provide additional parameters for the region, etc. – Michael Behrens Dec 03 '20 at 02:34
0

Here is a portion of a bash script that you could use, assuming you have jq installed to parse the json and only one API. It gets the first API ID from the items array, then looks up that Resource ID, then invokes the API using POST:

API_ID=`aws apigateway get-rest-apis | jq -r ".items[0].id"` 
RESOURCE_ID=`aws apigateway get-resources --rest-api-id $API_ID | jq -r ".items[].id"`  
aws apigateway test-invoke-method --rest-api-id $API_ID --resource-id $RESOURCE_ID --http-method POST
Michael Behrens
  • 911
  • 10
  • 8
0

Here's a shortcut way to find both rest-api and resource ids from the API Gateway page on the AWS console. Open the console and go to the API Gateway page. Click on the resource and method that you're looking for and look in the URL. This is the pattern you'll see:

https://us-west-1.console.aws.amazon.com/apigateway/home?region=us-west-1#/apis/rest-api-id/resources/resource-id/methods/method

where:

rest-api-id = the rest API id

resource-id = the resource id

method = the method (GET, POST, etc)

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34551941) – user12256545 Jun 20 '23 at 17:07
  • The answer isn't linked to the URL -- the answer IS the link. Note *rest-api-id*, *resource-id* and *method* in the URL -- this is how you find the rest-api-id, resource-id, and method. I updated my response to clarify – Roger Jaffe Jun 21 '23 at 18:16