I have been trying to get this working also. If you define all of your API endpoints in your swagger/openAPI specification, and include the
x-amazon-apigateway-integration properties to associate endpoints with lambda functions as Ashpabb suggests, then you would not have any http event definitions on your functions (in the “functions” section of serverless.yml), because the relationship between the lambda functions and the API endpoints is defined in the swagger/openAPI spec. Indeed, if you do define http events for functions with the same endpoint names as in the swagger spec then you’ll get a
Another resource with the same parent already has this name error from Serverless upon deployment. But if you don’t have any http event definitions, then Serverless does not create an API for you, so you have to do it all yourself in Cloudformation:
- create the API based on the swagger/openAPI spec
- grant the API permission to execute the defined functions
- deploy the API
@buggy you say that Serverless will merge your resources with the one it generates, but it seems to me that it won’t generate any API resources in this scenario. You could add a dummy http event to your functions in order to prompt Serverless to create the API, the permissions and the deployment, but that’d leave a dummy endpoint lying around. So I think @Dragonil was correct when stating that this approach requires the developer to define all the API resources themselves when using a swagger/openAPI specification.
Here is the Cloudformation that I added to serverless.yml to get it working with a swagger specification:
# Create the API based on the OpenAPI specification
# Deploy the API
# Grant permission for the API to call the lambda - note that lambda named "fetchData"
# earlier in this file becomes "FetchDataLambdaFunction" in Cloudformation
And the relevant part of my swagger spec looks like this:
summary: Returns some data
- - "arn:aws:apigateway:"
- Ref: AWS::Region
- Fn::GetAtt: ["FetchDataLambdaFunction", "Arn"]
I appreciate this question was asked a long time ago, and perhaps there is a better approach now. If so, I’d love to hear about it. There doesn’t seem to be a lot of documentation around how to get a swagger/openAPI spec working within a Serverless application.