How To Create A scalable Architecture With In AWS Using API-Gateway, Lambda & DynamoDB
AWS provides many managed services using which you can create a massively scalable architecture with very minimal effort. Here in this posts we will use three AWS managed services to create a simple application which can scale very easily. Let's get going...
Create DynamoDB Table
- The very first step is to create a DynamoDB table.
- Create a table, give it a name - "myinfo"
- Leave the "type" to string
- Leave other settings to default
- Click on "create" button to create the table
- After creating table, you will see the following table details
Create Lambda Function
- Open Lambda console, click on "create function" button
- Do not select any blueprint, click on "Author From Scratch" button
- Give the function name "myinfo"
- In Role section select "choose from existing role"
- Then select "dynamo_access" role (This role is being already created by us. You need to create this explicitly by attaching dynamodb full access policy to this role)
- Now click on "create function" button
- Now in the function code, paste the code from the below gist
- Select runtime as "Node.js 6.10" and leave handler as default
- Now click on "save and test"
- This will open a modal to create a test-event, give the event name myinfo.
- Put the below data in the text-pane - {"name": "appychip"}
- Now click on "create"
- Click on "Test" button and you will see the lambda is getting executed and prints out "SUCCESS" if executed successfully.
- You can also check DynamoDB table "myinfo", the entry for {"name": "appychip"} got created when you executed this function.
Create API-Gateway
- The last step is to create an API-Gateway which will give us an api to trigger lambda function which then on successful execution make an entry in DynamoDB.
- Open API-Gateway console.
- Click on "create" button to create an API. Give API name as "myinfo" and put some description and click on "create API" button
- After creating the API, click on "Action" dropdown and select on "create method", then in method dropdown select "POST" and finally click on small right icon to create the method.
- Now after creating method, It will ask for integration type, choose "Lambda function" and select the region in which "myinfo" lambda function was created (ap-south-1) in our case.
- Select the lambda function "myinfo"
- click on "save" button
- A pop-up will appear, click on "Ok"
- Now you will see a screen something like shown below
- Click on "Test" (below the small yellow circle)
- This will open a text pane for "Request Body" to provide the json content for the POST request.
- put the below json data - {"name": "test-name"}
- Now click on "Test" button
- This will trigger the Lambda function and will create an entry in DynamoDB table for {"name": "test-name"}
- The logs should show "SUCCESS" message as shown below:
- We have created our API successfully.
- Now, let's deploy our API for testing environment
- Click on "Action" dropdown
- Select "Deploy API"
- In Deployment stage dropdown, select "[New Stage]"
- Give "Stage name" as "testing". Put some description if you want.
- Click on "Deploy" button
- This will generate a URL on which we can hit a POST request.
- Click on "Save Changes" button
- Now you can make a curl request from your terminal to check if your API is working or not as shown below:
- curl -H "Content-Type: application/json" -X POST -d "{\"name\":\"curl-name\"}" https://de5w3pouo9.execute-api.ap-south-1.amazonaws.com/testing
- This should return "SUCCESS" and you will see the "curl-name" in DynamoDB table "myinfo"
Hurray!!! You have successfully created the Demo App which involve AWS API-Gateway, Lambda and DynamoDB.
0 comments:
Post a Comment