Going Serverless with AWS — Serverless User Authentication — Part 3

Hello and welcome back to Going Serverless with AWS. In Part 2 we learned how to use Python with AWS Lambda to Register a user to our RDS instance. Today we will learn how to build an API Gateway an Register our users using a JQuery.

So lets get started!

Building our API Gateway

What is API Gateway

Amazon API Gateway is an AWS service that enables developers to create, publish, maintain, monitor, and secure APIs at any scale. You can create APIs that access AWS or other web services, as well as data stored in the AWS Cloud.

Using the AWS Console to build our API Gateway

Login to the aws console at https://aws.amazon.com once logged in select API Gateway from the dropdown. If you have not created an API before click Get Started. You will be greeted with the new API window.

API Gateway New API Screen
API Gateway New API Screen

Click “Create API”

You should now see the resources configuration screen. This is where we can add routes and resources to our API Gateway. Since we are registering users we will need to have a POST method. We could use get but thats insecure for user registration as we will be sending passwords from a form to our gateway.

Create a POST Method

Creating our POST method.

From the actions dropdown select “Create Method”. You will then have the option to select the Method Type. Select post and click the Check Icon.

Enable CORS

Now we have our POST endpoint we need to Enable the Cross Origin Request Header (CORS) this will allow us to call the API from other domains using JavaScript instead of only the API Gateway Domain. Select the POST method, Click Actions > Enable CORS. Leave everything as default and click Enable CORS and Replace Existing Headers.

Enable CORS

Linking our Lambda Function

Now we can link our Lambda function to our POST method. Select the method and you will be greeted by a configuration screen to build the post Method.

Select the Lambda Function Radio Button.

Now select the region you deployed the Lambda Function. I deployed mine to eu-west-2.

Finally enter the name of the Lambda Function

POST Setup

Once you click Save you will be prompted that you are giving your API Gateway permissions to invoke your lambda function click OK as we need this.

AWS Gateway Lambda Permissions

Testing our API Endpoint.

Under your POST method click test. You can test the API Endpoint with the following JSON.

{
"email": "test@example.test",
"password": "2345"
}

You should be returned the following

Body :

"{statusCode: 200, message: 'User created succesfully'}"

Headers :

{"Access-Control-Allow-Origin":"*","X-Amzn-Trace-Id":"sampled=0;root=1-5a1af614-6f93b0bd1cffe517d0387f4e","Content-Type":"application/json"}

Deploy your API

From the actions menu click Deploy API. Select a stage or create a new stage and click deploy and take note of the invoke URI.

Deploy API

Registration Form

Unfortunately building the registration form goes beyond the scope of this tutorial However its easy to call it with JQuery.

$(document).ready(function() {
$("#register").click(function(){
var email = $("#email").val();
var password = $("#password").val();
$.post(
"https://API-GATEWAY-INVOKE-URI",
"{\"email\": \"" + email + "\", \"password\": \"" + password + "\"}",
success,
'json'
)
});
});

An Example form is included on GitHub

Congratulations

Congratulations on completing this tutorial on Building Serverless Authentication with AWS and Lambda. I hope you have found it informative. I have not went as far as showing logging in as its all duplication of what we have already covered. I hope you found this mini series informative and has helped you on your way to building a serverless web application with AWS. As you should be aware you can host your form in Amazon S3 meaning you don’t need to have a server to build a fully functional dynamic web application backed with a Database.

A challenge to you!

Now that you have learned how to build your Lambda Functions, Access your RDS Database and build an API Gateway go on a build the following.

  1. Login AWS Lambda Function
  2. Login API Gateway Method
  3. Login html and JS
  4. Open a Pull Request against the git repo

All code used in this tutorial is available on github https://github.com/Affix/serverless-auth-tut