[Fixed] How do i get/reference api gateway restapi id in serverless.yml?

With this document, I can steam cloudwatch logs to a lambda fucntion

    handler: myCloudWatchLog.handler
      - cloudwatchLog: '/aws/lambda/hello'

In this sample, the log group name is hard coded.

But my question is, how do i get current api gateway rest-api id dynamically? Because my lambda function will work on the api gateway log group, which has the name format as:


Seems the resource I am looking for is AWS::ApiGateway::RestApi



Fn::GetAtt returns a value for a specified attribute of this type. This section lists the available attribute and a sample return value.

The root resource ID for a RestApi resource, such as a0bc123d4e.

So it should be

    - ApiGatewayRestApi
    - RootResourceId

Let me test it.


@bill a little late but checkout http://www.goingserverless.com/blog/api-gateway-url where I write about using { "Ref" : "ApiGatewayRestApi" } to set an environment variable to the API GW URL.


Thanks, @buggy

That will be easier, use { "Ref" : "ApiGatewayRestApi" } directly.

1 Like

For anyone else who finds this, note there’s some confusion here about API gateway resources.

  • The RestApiId is the id of the API resource itself, which is returned by a Ref of the implied ApiGatewayRestApi resource that Serverless creates under the hood.

  • The RootResourceId is the id of the REST resource (not to be confused with a CloudFormation resource!) at the root of the API’s path (i.e. “/”). If you are hanging methods or subresources off of root, you’ll need to provide this as the parent resource id. It is available as an attribute of the API resource.

So, for example to define a resource at the path “/mythings”, which needs both the api id and the root resource id, your CF template would look like

  Type: AWS::ApiGateway::Resource
      Ref: ApiGatewayRestApi
        - ApiGatewayRestApi
        - RootResourceId
    PathPart: mythings

Hope this helps clarify.


Thanks for sharing this! It was really helpful.

Thanks! realy what I was looking for.

I have in my template something like this:

        RestApiId: {
          Ref: 'ApiGatewayRestApi',

which worked okay up to now and out-of-the-blue I’ve started to get:

Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [ApiGatewayRestApi] in the Resources block of the template

In the end I was able to pin point the source of the issue to upgrade of serverless-plugin-split-stacks from 1.9.3 to 1.10.0 version. Downgrading fixed the problem.

so where does this go
I am trying to attach ApiGatewayRestApi in a policy Resource
Please help

You can use the CloudFormation built-in Sub to substitute it into a string:

!Sub arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${ApiGatewayRestApi}/*/GET/v1/addresses

Will resolve to something like:


you can use.

and call the value like #{AWS::RestApiId}

Nice, thanks for sharing the information, it really helps student like.
but beginner like me it is little hard to understand fast, can anyone suggest a good AWS classes in Pune.