Build API for a pizza store on AWS
Table of contents
DynamoDB configuration
-
Database, go to
DynamoDB
, ARN [arn:aws:dynamodb:eu-west-2:9****8:table/Pizza]- Navigate » Create table » Give table a name » Use
primary key
(this is the unique identifier) » Click create - Items tab > Create Item > Append …
- Navigate » Create table » Give table a name » Use
IAM configuration
-
Database role to access database, go to
IAM
- Navigate to role » Create new role » Click Lambda then Next » Search for: AWSLambdaBasicExecution Role » Tag (empty) » Review
- User Inline policy, role » Add inline policy » Service: DynamoDB » Read: GetItem & Write: PutItem » Resources add DB table ARN » Review policy: give a name » create policy
Lambda GET function
-
Lambda (program) to GET pizza from database -
Lambda
function.- Navigate to Lambda » Create function » Author from scratch » Give a name (PizzaGetFunction) » Runtime: Node.js 12.x » Permission: use an existing role » Create function.
- Paste below code for
PizzaGetFunction
» index.js
var AWS = require('aws-sdk'); //create DynamoDB document client exports.handler = async (event, context) => { var awsddbdc = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); //path parameters const {id} = event.pathParameters; //for testing, comment this line out. //Query Parameters var params = { TableName : 'Pizza', Key : { id: id //id: '1', comment out the above line and use this line } }; const data = await awsddbdc.get(params).promise(); const response = { statusCode: '200', body: JSON.stringify(data.Item) } return response; };
Create GET method
-
Create GET method api -
api gateway
- Endpoint type: [EdgeOptimized] » Cached version
- Create resource » Give path a name:
pizza
- Click pizza path » Create resource: id, path {id}
- Click {id} path » Create method » GET + ✅ » lambda + proxy integration + getFunction » save
- You are about to give API Gateway permission to invoke your Lambda function: arn:aws:lambda:eu-west-2:9****8:function:PizzaPostFunction
Lambda POST function
-
Lambda (program) to POST pizza in database -
Lambda
- Navigate to Lambda » Create function » Author from scratch » Give a name (PizzaPostFunction) » Runtime: Node.js 12.x » Permission: use an existing role » Create function.
- Paste below code for
PizzaPostFunction
» index.js
var AWS = require('aws-sdk'); //Create DynamoDB document client exports.handler = async (event, context) => { var awsddbdc = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); //Parse the incoming JSON values const {id,size,topping} = JSON.parse(event.body); //Query parameters var params = { TableName : 'Pizza', Item : { id: id, size: size, topping: topping //id: '5', size: 'medium', topping: 'green peppers' } }; const data = await awsddbdc.put(params).promise(); const response = { statusCode: '201', body: JSON.stringify(data) } return response; };
Create POST method
- POST api - navigate to
api gateway
again-
Create Method » POST » Add http header (content-type) & Add Model
-
Create Model
{ "type" : "object", "properties" : { "id" : {"type" : "string"}, "size" : {"type" : "string"}, "topping" : {"type" : "string"} } }
- Test
POST
, add below to body box when testing
{ "id" : "11", "size" : "large", "topping" : "pineapple" }
-
Create data in DynamoDB
- RDS » Managed relational database service » create database
- Create Table / Items - pizza: Table > Item > Append (string)
- Primary key - id
- Keep default, click
create
- ARN - Amazon Resource Name [arn:aws:dynamodb:eu-west-2:9****8:table/Pizza]