0
votes

I am trying automate attaching a lambda function to an already existing Api Gateway(that was not deployed in a stack). It looks like the AWS::ApiGateway::Method is the best way of going about doing this, but I can seem to figure out how to get the specific Api Gateway RestApiId and ResourceId. How can I go about "fetching" that information? Is there a cloudformation way or do I need to use some type of lambda function?

Thanks for helping me with frustrating problem!

2

2 Answers

0
votes

How can I go about "fetching" that information?

There is no such functionality as "fetching" in CFN. You have to provide everything manually as input parameters or hard-code the values that you want. Alternatively, you have to develop a custom resource to get that info for you. The custom resource would use a lambda function that you need to write yourself.

0
votes

The quickest and easiest way to do this would be using a CFN parameter. I've put a small example of how this can be done below. The example uses a SAM serverless lambda and an HTTP API (API gateway v2), but the solution for a REST API (API gateway v1) would be very similar.

Parameters:
  HttpApi:
    Type: String

Resources:
  SourceFunction:
    Type: AWS::Serverless::Function
    Properties:
      [...]
      Events:
        GetEndpoint:
          Type: HttpApi
          Properties:
            Path: /endpoint
            Method: GET
            ApiId: !Ref HttpApi

More information about cloudformation parameters can be found here. More information about the serverless function (in case you're not familiar with AWS SAM) can be found here.