Amazon web services has a very comprehensive set of documentation when it comes to cloud providers. But the readability is, quite frankly, terrible. I spent some time today in setting up everything to get a simple CRUD running. Thought I’d document the whole process for my future self, or any one else who’s trying to do the same thing.
What are we building ?
This:
Why?
Just to demonstrate how we could build a serverless data store for headless apps (Mobile, IoT) and make it easily accessible via an HTTP API using AWS lambda.
Prerequisites
- AWS CLI.
- Knowledge of javascript
- Amazon AWS account
Instructions in this post are written for Ubuntu, so if you're on any other OS you'll have to improvise a little :)
Step 1 : Setting up the AWSCLI and keys
Install the AWS Cli, by a simple
sudo apt-get install awscli
Next, connect it to your account. For this part we’d need a key first:
Go to Amazon IAM console. Click on the Users link in the left hand side menu. Then go to the Security Credentials Tab. In here you’ll find an Access Keys section, go ahead and click on the Create Access Key button. Note down the Key ID and secret Access key. Provide both of these to the AWS CLI below.
Run :
aws configure
and provide your access keys.
Step 2 : Setting up our Lambda function.
PS: You can do this step easily using the serverless.com tool.
But, I wanted to do it for myself, so I went to AWS Lambda Console. Clicked the Create Function button. Select any node.js based blueprint, we won’t be using it anyway. I selected the simple-mobile-backend, quite frankly only because the name sounded familiar to what I was trying to achieve.
Next, in the triggers select API Gateway. In security, choose what ever you want. If you want to keep your API public, then select open. Else, select AWS IAM or open with access key. For development, you can choose open. But be sure to change it later on.
Next, give your function a name and a security role name.
Next, go to the triggers tab and just note the invocation URL for use in connecting our client to it.
Now, let’s download the lambda function we created and update it’s code via the CLI.
Why? Can’t we use the web based code editor?
Yes, you can if your javascript code won’t be including any other dependencies. Other wise, you’ll have to code it locally make a zip and upload to the Lambda console manually.
Create a folder for this project in your development directory. Open a terminal in this directory and run
aws lambda get-function --function-name YourFunctionNameHere
It’ll spit out some json, with a link to your code as a zip, download it and place it in the current directory. Next, let’s start editing this file out. Or you can go ahead and use my file below, I’ll add some code explanations below.
exports.handler is our main lambda function, this can also be changed in the AWS lambda console, by going to the configuration tab and changing Handler. But for now, that’s set to index.handler, which means this function.
Since, this is an API handler, we’re adding a little check for the OPTIONS preflight request and responding appropriately. This allows us to test our app on browsers, without them blocking our requests. You can remove this if you have a custom client code to connect to your Lambda function AND will not be using a browser at all.
Next we check the event.body which is basically the request body of the incoming request.
By the way, we will be communicating with this API by sending POST requests, in the following format (of the POST body):
The operation can be create, update, delete, list.
{
"operation": "update",
"data": {"id":"bf9d4066-2a97-4106-8b82-7de0d3c7676c", "name": "hello world!"}
}
The API then responds with either a 200 or a 500, based upon the results of the request. The rest of the code is pretty self explanatory.
IMPORTANT :
A little note, the database schema I’ve used above is extremely terrible and violates all good software design principles. I trust you not to follow it, it’s just here for demonstrating how AWS Lambda + DynamoDB could be used for serverless CRUDS.
The rest of my code is pretty self explanatory.
Now there’s only 1 thing left to do.
Step 3: Giving Lambda access to the DB
Open up the AWS IAM console. Go to the Roles section, and find the role you just created for the Lambda function above.
Next, click on the Create Policy button and search for DynamoDB in the policy type section . Check the box for AWSLambdaDynamoDBExecutionRole. Then attach this policy to your role.
Step 4: Deploying it all to the cloud
Next make a zip of the whole code directory:
zip -r blob.zip *
and push it to AWS:
aws lambda update-function-code --function-name "YourFunctionNameHere" --zip-file fileb://dist/build.zip
Now, simply access your brand new API using your favorite client (I use POSTMAN) to test it out.
Optional:
To add Authentication or block requests or simply design your API further. You can visit the Amazon API Gateway
Interested in my React Redux client source code too? Here you go: https://github.com/fifthsegment/boilerplates/tree/master/Redux-Serverless-DynamoDB
comments powered by Disqus