A sample solution taking an API definition from SwaggerHub, crafting a Lambda function from the API definition, and then publishing the function behind AWS Gateway.
- Problem Statement
- Solution Overview
- Prerequisites
- Instructions
- Fork the repo
- Import the Book API into SwaggerHub
- Setup Auto Mock integration in SwaggerHub
- Test the Auto Mock integration
- IAM Role Setup for Deployment to AWS
- Deploy AWS API Gateway and AWS Lambda Function
- Update the SwaggerHub Books API with the AWS API Gateway endpoint
- Calling your AWS API Gateway hosted Books API from SwaggerHub
- Review the AWS API Gateway
- Continue the journey
API design tools and API gateways are two essential components in the development of modern API-based applications. Integrating these tools not only streamlines the API development process but also provides numerous benefits that enhance API functionality, security, and scalability.
SwaggerHub is a multi-spec API design and documentation tool that seamlessly integrates with Amazon Web Services. With this integration, you can establish a relationship between your design/development and your gateway/production environment. Having an integration between the design-time and the run-time allows you to push your API documentation directly from SwaggerHub into your gateway. Every time you update your document in SwaggerHub trigger the required processes to automate the journey towards the API management plane.
Managing your APIs from your gateway becomes a simpler task when you have your API documentation loaded into the tool automatically, there's no need to manually define how your APIs behave when SwaggerHub pushes your API definitions automatically.
By leveraging the seamless integration between SwaggerHub’s API design and documentation capabilities with AWS’s scalable and feature-rich gateway, organizations can confidently deliver innovative, customer-centric services and applications.
In this integrated exercise you will create an end-to-end solution which takes you through the process of creating an OpenAPI Specification (OAS) design definition, and deploying the API to an operational AWS environment.
The main
branch of this repository contains a simple end-to-end solution.
Alongside supporting webinars/workshops, additional scenarios are stored in separate branches.
vuln-api
- This example the process of
- creating an OpenAPI Specification (OAS) design definition
- deploying the API to an operational AWS environment.
- exploring the deployed API to manually identify security vulnerabilities.
- This example the process of
vuln-api-hardened
- This example follows on the
vuln-api
branch and shows the process of- using Spectral public and custom rule-sets to identify vulnerabilities early in the design and development process in
- SwaggerHub
- CI
- Locally
- rectifying the security vulnerabilities.
- deploying our updated API to an operational AWS environment.
- exploring the deployed API to ensure the security vulnerabilities have been resolve
- using Spectral public and custom rule-sets to identify vulnerabilities early in the design and development process in
- This example follows on the
Content | Link | Branch Name | Overview |
---|---|---|---|
How to Streamline API Development on Amazon API Gateway Using SmartBear’s SwaggerHub | Blog | main |
simple e2e solution |
Defend Your APIs - Secure by Design | Watch / Slides | vuln-api |
Vulnerable API e2e deployed and explored |
Defend Your APIs - Secure by Design | Watch / Slides | vuln-api-hardened |
Vulnerable API issues identified and rectified |
- Import an existing OAS
Books API
definition into SwaggerHub SaaS design & documentation tool - Enable and test API auto-mocking on the newly imported API in SwaggerHub
- Build, test and deploy the following AWS resources/services:
- AWS Lambda Serverless Functions (a small backend microservice for the
Books API
) - AWS API Gateway (to proxy API traffic to the implemented function)
- S3 bucket to store the release artifacts
- IAM roles needed to deploy and run the resources
- AWS Lambda Serverless Functions (a small backend microservice for the
- Update the SwaggerHub API to include the published AWS API Gateway endpoint
- Call the AWS API Gateway endpoints using SwaggerHub
- Review the AWS API Gateway (the setup of which is driven off the OAS definition)
- Learn about additional steps and functionality supported by the environment that has been setup
The assets and solution overview is as follows:
You will need an AWS subscription and a SwaggerHub account in order to be able to work on the API design, AWS Lambda implementation, and automated deployment to AWS API Gateway.
- Sign up for an AWS account (if required)
- Sign up for a SwaggerHub trial account (if required)
- Sign up for a GitHub account (if required)
The sample Lambda function generated within this repo uses dotnet6
. If you would like to edit the code implementation, then the following are required:
- VS Code (or similar IDE)
- Install .NET 6.0
- Install Amazon Lambda Tools
dotnet tool install -g Amazon.Lambda.Tools
- Install SAM CLI (AWS Serverless Application Model Command Line Interface)
The instructions laid out below cover the following steps:
- Forking the repository for your local needs
- Importing the Book API into SwaggerHub
- Setup Auto Mock integration in SwaggerHub
- Test the Auto Mock integration
- IAM Role Setup for Deployment towards AWS
- Run GitHub Action to deploy AWS API Gateway and AWS Lambda Function
- Update the SwaggerHub Books API with the AWS API Gateway endpoint
- Calling your AWS API Gateway hosted Books API from SwaggerHub
OK - let's get started!
- Fork the repo to your local GitHub profile/organization
- Login into SwaggerHub
- From the Create New menu select Import and Document API
- Enter the following URL in the Path or URL input box
https://raw.githubusercontent.com/SmartBear-DevRel/SwaggerHub-AWSGateway-Lambda/main/API-Definition/openapi.yaml
- Press the Import button
- In the next pop-up window click the Import Definition button
- In the SwaggerHub Portal page, click on the
Books API
- In the left pane, click on the API name
books-api
- Click on the Integrations tab
- Click on Add New Integrations
- In the Integrations dropdown, select API Auto Mocking and click the ADD button
- In the Name text box, enter
Auto Mocking
- Click the CREATE AND EXECUTE button
- Click on the DONE button
- Click on the API name
book-api
again to return to the editor view
Note that a new description and url tags have been added in the servers section of the API
- In the right panel, in the Servers dropdown, select the SwaggerHub API Auto Mocking server URL
- Open any of the Method/Path end-points (e.g.
GET /books
) and click the Try it out button - Provide any
required
parameters - Click the Execute button
- Review the data returned in the Server response box
In the next steps, we'll deploy a working version of the
books-api
to AWS API Gateway and have a functioning Lambda function as the API implementation. Once deployed, we'll call the implemented API from SwaggerHub!
- Follow the IAM and Resource Setup Guide to ensure you can run the pipeline
- In your forked GitHub repository, navigate to the Actions tab
- Click on the
Pipeline
action on the left-hand pane - Run the
Pipeline
workflow by clicking on the Run workflow button - Once the pipeline has completed, navigate to the bottom of the pipeline summary and locate the output-endpoint summary section
- Copy the AWS_API_Gateway_Endpoint URL
We'll need the API endpoint above to call the API, so keep it to hand!
- In the SwaggerHub Portal page, click on
Books API
to open the API definition - Locate the
servers
tag, and replace theurl
for theAWS API Gateway Endpoint
server tag (currently holding a value ofhttps://example.com
) with the value of the URL value copied from the pipeline summary above - Click the Save button
- In the SwaggerHub Portal page, click on
Books API
to open the API definition - In the SwaggerUI pane (the right-hand pane), choose the AWS API Gateway Endpoint from the Servers dropdown
- Expand
GET /books
, click the Try it out button - Optionally, enter an
title
orauthor
query parameter - Click Execute
The Books API OpenAPI definition and the SAM Template combined drive much of the setup of the AWS API Gateway and linked Lambda Functions. The benefit of this is that the gateway will get automatically hydrated based on the design definitions.
Let's take a quick look
The API resources automatically configured in the gateway conform to the API definition (e.g. GET /books
and GET /books/{id}
).
The API models are automatically generated from the JSON Schema components contained in the API definition.
The API documentation is also generated from the API definition negating the need for documentation to be added after publishing to the gateway.
One of the major benefits of a gateway function is being able to observe how an API is being consumed. AWS API Gateway gives plenty of insights on API calls and performance out of the box.
- Using SwaggerHub Explore to interact with the surface area of an API
- Using the SwaggerHub CLI (Command Line Interface)
- Adding custom standardization rules and guidelines to SwaggerHub
- Using SwaggerHub domains (shared code libraries) to share common API components across multiple APIs
- Adding authentication to the sample API and AWS Gateway