This is a simple Restful API on AWS using the following tech stack:
- Serverless Framework
- Go language
- AWS API Gateway
- AWS Lambda
- AWS DynamoDB
The API accepts the following JSON requests and produces the corresponding HTTP responses:
URL: https://<api-gateway-url>/api/devices
{
"id": "/devices/id1",
"deviceModel": "/devicemodels/id1",
"name": "Sensor",
"note": "Testing a sensor.",
"serial": "A020000102"
}HTTP 201 Created
HTTP 400 Bad Request (If any of the payload fields are missing)
The response body has a descriptive error message for the client to be able to detect the problem.
HTTP 500 Internal Server Error (If any exceptional situation occurs on the server side)
URL: https://<api-gateway-url>/api/devices/{id}
Example: https://api123.amazonaws.com/api/devices/id1
HTTP 200 OK
{
"id": "/devices/id1",
"deviceModel": "/devicemodels/id1",
"name": "Sensor",
"note": "Testing a sensor.",
"serial": "A020000102"
}HTTP 404 Not Found (If the request-id does not exist)
HTTP 500 Internal Server Error (If any exceptional situation occurs on the server side)
- The Go Programming Language
- Get Docker
- AWS CLI
- AWS SAM CLI
- use
go mod tidyin the root directory of project to download dependencies
- Build the project using
sam buildcommand - To run the project locally, use
sam local start-apicommand - It can be tested using PostMan
Use this link to test project on AWS like above example.
Possible errors are listed below.
var (
ErrorInvalidDeviceData = "Invalid user data"
ErrorFailedToFetchRecord = "Failed to fetch record"
ErrorFailedToUnmarshalRecord = "Failed to unmarshal record"
ErrorDeviceAlreadyExists = "Device already exists"
ErrorCouldNotMarshalItem = "Could not marshal item"
ErrorCouldNotDynamoPutItem = "Could not put item in dynamo"
ErrorItemWasNotInTheDataBase = "Item was not in the DataBase"
)Obtain all devices in the database.
func FetchDevices(tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*[]Device, error)type Device struct {
Id string `json:"id"`
DeviceModel string `json:"deviceModel"`
Name string `json:"name"`
Note string `json:"note"`
Serial string `json:"serial"`
}Generates a new device with the aforementioned features.
func CreateDevice(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (
*Device,
error,
)Get a particular device from the database.
func FetchDevice(id, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*Device, error)When a user requests a method that does not exist, the error "method not allowed" appears.
var ErrorMethodNotAllowed = "method not allowed"Handle the situation once the user requests the creation of a new device.
func CreateDevice(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*events.APIGatewayProxyResponse, error)Whenever the user requests a specific device, this method invokes the "FetchDevice" operation.
func GetDevice(req events.APIGatewayProxyRequest, tableName string, dynaClient dynamodbiface.DynamoDBAPI) (*events.APIGatewayProxyResponse, error)If a user requests a method that does not exist, an error will be sent to the API response.
func UnhandledMethod() (*events.APIGatewayProxyResponse, error)type ErrorBody struct {
ErrorMsg *string `json:"error,omitempty"`
}