Building Serverless APIs with API Gateway

Tutorial 3 of 5

Building Serverless APIs with API Gateway

1. Introduction

Objective

In this tutorial, we aim to guide you on how to build a serverless API using Amazon's API Gateway. This will allow you to create, publish, maintain, monitor, and secure APIs without managing any servers.

Learning Outcomes

  • Understand the basics of Amazon's API Gateway
  • Create, deploy and test a serverless API
  • Secure your API

Prerequisites

  • Basic knowledge of RESTful APIs
  • AWS account

2. Step-by-Step Guide

Understanding Amazon API Gateway

Amazon API Gateway is a fully managed service that makes it easy to create, publish, maintain, monitor, and secure APIs. With a few clicks in the AWS Management Console, you can create an API that acts as a "front door" for applications to access data, business logic, or functionality from your back-end services.

Creating an API

  • Sign in to the API Gateway console at https://console.aws.amazon.com/apigateway.
  • Choose 'Create API'.
  • Under REST API, choose 'Build'.
  • Under 'Create new API', choose 'New API'.
  • Enter a name and description for the API. Choose 'Create API'.

Creating a Resource and Method

  • In the APIs navigation pane, choose your API.
  • Under your API, choose 'Resources'.
  • Choose 'Create Resource', and enter a resource name and path. Choose 'Create Resource'.
  • Choose 'Create Method', then choose 'POST' from the dropdown, and choose the tick.
  • For integration type, choose 'Lambda Function'.

Deploying the API

  • In the APIs navigation pane, choose your API.
  • Choose 'Actions', 'Deploy API'.
  • For Deployment stage, choose '[New Stage]'.
  • Enter a name for the stage. Choose 'Deploy'.

3. Code Examples

Example 1: Creating a Resource

# Choose 'Create Resource'
aws apigateway create-resource --rest-api-id abc123 --parent-id abc123xyz --path-part myresource

This command will create a new resource under the specified REST API (abc123) and parent (abc123xyz).

Example 2: Creating a Method

# Choose 'Create Method'
aws apigateway put-method --rest-api-id abc123 --resource-id xyz123 --http-method POST --authorization-type "NONE"

This will create a new POST method for the specified REST API (abc123) and resource (xyz123).

Example 3: Deploying the API

# Choose 'Deploy API'
aws apigateway create-deployment --rest-api-id abc123 --stage-name dev

This will deploy the API (abc123) to the 'dev' stage.

4. Summary

In this tutorial, we have gone through the basics of Amazon's API Gateway, creating a serverless API, and deploying it. You can now create your own serverless APIs, deploy, and test them.

5. Practice Exercises

Exercise 1: Create a new API with a GET method which integrates with a Lambda function.

Exercise 2: Deploy the API created in Exercise 1 to a new stage called 'test'.

Exercise 3: Create a new resource under your API and add a PUT method to it.

Solutions:
1. Follow the same steps as above, but choose 'GET' instead of 'POST' when creating the method.
2. Follow the deployment steps, but use 'test' as the stage name.
3. Follow the steps to create a resource and method, but use 'PUT' instead of 'POST'.

Further Practice

Try adding more methods to your resources, such as DELETE and UPDATE. Test your APIs using different tools and integrate them with different AWS services.