what is the --rest-api-id and --resource-id and where do i find them?

Viewed 9715

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

It requires these two fields, cant find any docs and were 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?

2 Answers

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

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
Related