Hello and welcome back to Going Serverless with AWS, In part 1 we learned what Serverless means and what we are going to build. In part 2 We will learn how to create an RDS instance with the AWS cli tools, and build our first lambda function, package it, deploy it, and invoke it using the AWS cli tools.

In the spirit of keeping this as simple as possible you will be able to copy and paste all commands and get a working setup however this will be super insecure as passwords and things will be set using the AWS cli. DO NOT RUN THIS AS A PRODUCTION APPLICATION… SERIOUSLY DON’T

So lets get started!

Creating your Database

What is RDS?

Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups. It frees you to focus on your applications so you can give them the fast performance, high availability, security and compatibility they need.

Creating our databse

To create our database as promised we will use the AWS Command Line Tools. Please make sure you have run ‘aws configure’ and selected a region close to you and setup your access keys.

To create the database we use the rds command from the aws command line tools. For the sake of simplicity we will make this publicly accessible.

The command we have just run will return JSON with details about the RDS Instance creation. Now we need to think about creating our tables for our users. In order to do this we need to get our Endpoint to connect to. This can be acheived by going to the AWS RDS Console or calling aws rds describe-db-instances –db-instance-identifier=mysqlforlambdatut and checking the Endpoint Address in the JSON.

Now we have our endpoint we can connect to it with an SQL Tool of our choice so we can create our schema.

We now have our users table setup and ready. Now we can start building our Lambda function.

User Registration with AWS Lambda

What is Lambda?

AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume – there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.

Writing our first Function

What we are going to create is called a Deployment Package. This allows us to write complex lambda functions locally using any library available for our language of choice. I am focusing on Python for this series however Lambda supports Java, C#, NodeJS and Python. So lets get started.

We are going to start with out Registration function createUser. We need users to authenticate against so we need to register them first. Instead of manually adding adding a new user its a good starting point for our Lambda function adventure.

Create an empty directory called createUser for this part of the project.

Third Party Deps

Part of our microservice we need to use the PyMySql library not provided as part of AWS Lambda. To do this we should create a requirements.txt file and install the deps.

Our requirements.txt file is

To install these into our project directory we call

This should have created the pymysql showing us the library can be used in our project.

Python Code

Now we have our requirements we need to write the code. Simply use the example code provided below. Save this as app.py

This isn’t a Python tutorial so I won’t be explaining this line by line. However what it does is uses the event dictionary passed from lambda to the handler in our application.

The handler contains an email and password, We take the password and Hash it with sha256 together with a randomly generated SALT.

Lambda Roles

In order for our Lambda function to access the RDS instance we need to give it a role allowing it to communicate with the internet and have basic execution functionality. I have prepared a JSON IAM role policy to help you here.

to import this role run

Take note of the ARN for this role as you need it to deploy your function.

The Deployment Package

What you upload to Lambda is called a Deployment Package. To create this zip up all the files inside your project directory. DO NOT ZIP THE ACTUAL DIRECTORY, MAKE SURE ITS ONLY THE FILES.

Now we upload it to Lambda, In order to do this we need to use the default VPC and Have a security group that allows external access.

To associate to a VPC you associate to subnets, You can find a list of your subnets using aws ec2 describe-subnets

Creating a security group goes beyond the scope of this guide. However its easy to make one with the AWS Console, Make sure it has global outbound connections allowed (Use your RDS Security group if you want).

Deploying the package

To deploy your package make sure you know where your zip file is. Using the cli you can deploy with ease. Make sure you have your Subnet IDs and security group ID, and role ARN

This will output some JSON describing your newly created Lambda function.

Testing the Deployment

There are a couple of ways you can test the deployment. The easiest being the AWS Console. However I will be testing it using the CLI.

If all has gone well you will be shown some JSON with statusCode 202 letting you know that it worked as planned.

You can verify this by Launching mysql and selecting all from users.

Congratulations you can Register a user in RDS using Lambda!

Stay tuned for part 3 Where we will build our API Gateway for User Registration, Test it using cURL and build our registration web interface hosting it all in S3 so you don’t have to worry about servers.