Newb question: create URI to invoke lambda from API gateway


#1

This is my first post here, so please excuse the total newb question. I have a serverless project with several lambda functions, and the next step is to create an API Gateway API to invoke them from. In the sample below I’ve got toProcessFunction which is the lambda and then the API, a resource, and a method. The API, resource and method could be total nonsense so far. In the method I need to give it a URI for the lambda, which includes the lambda’s ARN. However I cannot explain to serverless that I want the ARN there. I’ve read the serverless variables doc many times, but there doesn’t seem to be a relevant option, except maybe cf.

So what I’m wondering is whether variable interpolation happens too early in the process for this to be possible? In that case, my guess is that there’s a better way to do it. If so, please point me to the docs.

Thank you.

  toProcessFunction:
    handler: functions.toProcess
    environment:
      mysqlHost: ${self:custom.secret.mysqlHost}
      mysqlUsername: ${self:custom.secret.mysqlUsername}
      mysqlPassword: ${self:custom.secret.mysqlPassword}
      mysqlDatabase: ${self:custom.secret.mysqlDatabase}
    events:
      - http:
          path: toProcess
          method: post
          cors: true
          integration: lambda
      - http:
          path: toProcess
          method: get
          cors: true
          integration: lambda
    role:
        "doStuffFromInsideVPC"

resources:
  Resources:
    insideAPI:
      Type: "AWS:ApiGateway::RestApi"
      Properties:
        Name: "inside-vpc"
        Description: "API to access services inside the VPC"
        EndpointConfiguration:
          Types:
            - EDGE
    toProcessResource:
      Type: "AWS::ApiGateway::Resource"
      Properties:
        PathPart: "toProcess"
        RestApiId: insideAPI
        ParentId: insideAPI.RootResourceId
    toProcessMethodPOST:
      Type: "AWS::ApiGateway::Method"
      Properties:
        HttpMethod: POST
        ResourceId: toProcessResource
        RestApiId: insideAPI
        Integration:
          Type: AWS_PROXY
          IntegrationHttpMethod: POST
          Uri: "arn:aws:apigateway:#{AWS::Region}:lambda:path/2015-03-31/functions/${cf:toProcessFunction.Arn}/invocations"

#2

Well, after writing out that long screed, I had another think about it, and decided that as my ultimate goal was to present the API through my custom domain, I should search for how to do that. And goodness me, I found serverless-domain-manager plugin, realised that I knew how to configure it, and it worked first time. That’s quite amazing.

So the answer is that events -> http stuff in the lambdas is enough to tell serverless how to construct the API, and serverless-domain-manager is then smart enough to attach the newly constructed API to the custom domain name.