Example of a serverless REST API built with Amazon Web Services. This API provides create, read, update, and delete (CRUD) functionality.
- AWS account for provisioning cloud services.
- Postman for testing the API.
First create a new DynamoDB table. This will be the data source for the API.
- Login to the AWS console using an IAM user.
- Search for
DynamoDB. - Click
Create table. - Enter a table name such as
contact-list. - Enter a partition key such as
contactIdwith a data type ofString(leave sort key blank for this example.) - Leave
Table settingsasDefault settings. - Click
Create table.
- Search for
Lambdain AWS Console. - Click
Create function - Enter a function name such as
serverlessRestApi. - Select
Node.jsas a runtime. - Under
PermissionsselectChange default execution roleand thenCreate a new role from AWS policy templateswith a name such asserverlessApiRole. - Click
Create function. - Click
Permissionsand then click on the role name that was just created. - Click
Attach policiesand search forCloudWatchLogsFullAccess, select the policy, then clickAttach policy. - Click
Attach policiesand search forDynamoDBFullAccess, select the policy, then clickAttach policy.
- Search for
API Gatewayin AWS Console. - Click
Create API. - Go to
REST APIand clickBuild. - Select
RESTandNew API. - Enter a name such as
contactListRestAPIand leave endpoint type asRegional
- Click
Actionsand thenCreate resource. - Enter resource name
healthand selectEnable API Gateway CORS. - Select the
Healthresource and then clickActionsandCreate method. - Select
GETmethod withIntegration typeasLambda - Select
Use Lambda Proxy integration. - Select the deployment region where the Lambda function will be deployed as
us-east-1. - Search for the Lambda function by name such as
serverlessRestApi.
- Click
Actionsand thenCreate resource. - Enter resource name
contactsand selectEnable API Gateway CORS. - Select the
Contactsresource and then clickActionsandCreate method. - Select
GETmethod withIntegration typeasLambda - Select
Use Lambda Proxy integration. - Select the deployment region where the Lambda function will be deployed as
us-east-1. - Search for the Lambda function by name such as
serverlessRestApi.
- PUT
- GET
- PATCH
- DELETE
Test the health of the API by sending a GET request to the /health endpoint which should return a 200 OK response.
Request Body:
"None"Response:
"200 OK"Sending a GET request to the /contacts endpoint will return a list of all contacts in the database.
Request Body:
"None"Response:
{
"contacts": [
{
"occupation": "Designer",
"contactId": "10003",
"lastName": "Smith",
"email": "louis.smith@gmail.com",
"phone": "505-426-8570",
"firstName": "Louis"
},
{
"occupation": "Designer",
"contactId": "10002",
"lastName": "Henson",
"email": "stacy.henson@hotmail.com",
"phone": "443-455-0735",
"firstName": "Stacy"
},
{
"occupation": "Software Engineer",
"contactId": "10001",
"lastName": "Neely",
"email": "stephen.neely@yahoo.com",
"phone": "803-740-4478",
"firstName": "Stephen"
}
]
}Add contacts to the database by sending a POST request to the /contact endpoint.
Request Body:
{
"occupation": "Dentist",
"contactId": "10004",
"lastName": "Smith",
"email": "louis.smith@gmail.com",
"phone": "505-426-8570",
"firstName": "Louis"
}Response:
{
"Operation": "SAVE",
"Message": "SUCCESS",
"Item": {
"occupation": "Dentist",
"contactId": "10004",
"lastName": "Smith",
"email": "louis.smith@gmail.com",
"phone": "505-426-8570",
"firstName": "Louis"
}
}Retrieve a single contact using the /contact endpoint with contactId as q query parameter.
Request Body:
"None"Response:
{
"occupation": "Designer",
"contactId": "10003",
"lastName": "Smith",
"email": "louis.smith@gmail.com",
"phone": "505-426-8570",
"firstName": "Louis"
}Request Body:
{
"contactId": "10002",
"updateKey": "phone",
"updateValue": "612-999-9903"
}Response:
{
"Operation": "UPDATE",
"Message": "SUCCESS",
"UpdatedAttributes": {
"Attributes": {
"phone": "612-999-0000"
}
}
}Request Body:
{
"contactId": "10004"
}Response:
{
"Operation": "DELETE",
"Message": "SUCCESS",
"Item": {
"Attributes": {
"occupation": "Dentist",
"contactId": "10004",
"lastName": "Smith",
"email": "louis.smith@gmail.com",
"phone": "505-426-8570",
"firstName": "Louis"
}
}
}