[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

functions:
  myCloudWatchLog:
    handler: myCloudWatchLog.handler
    events:
      - 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:

API-Gateway-Execution-Logs_{rest-api-id}/{stage_name}
1 Like

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

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-restapi.html

Fn::GetAtt

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

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

So it should be

  RestapiId:
    Fn::GetAtt:
    - ApiGatewayRestApi
    - RootResourceId

Let me test it.

2 Likes

@bill a little late but checkout How to I get my API Gateway URL with the Serverless Framework where I write about using { "Ref" : "ApiGatewayRestApi" } to set an environment variable to the API GW URL.

6 Likes

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

ThingsResource:
  Type: AWS::ApiGateway::Resource
  Properties:
    RestApiId:
      Ref: ApiGatewayRestApi
    ParentId:
      Fn::GetAtt:
        - ApiGatewayRestApi
        - RootResourceId
    PathPart: mythings

Hope this helps clarify.

5 Likes

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:

arn:aws:execute-api:us-east-1:123456789:xHjflv456f/*/GET/v1/addresses

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.

  APIGatewayExecutionLogs: !Join
    - ""
    - - "API-Gateway-Execution-Logs_"
      - !Ref ApiGatewayRestApi
      - "/"
      - ${self:provider.environment.STAGE}

This approach is if you need value in Serverless config/setup.
In other cases { “Ref” : “ApiGatewayRestApi” } will be a references, not a string and that will be a problem. With this approach you can construct string how you want and apply