Serverless Golang API with AWS Lambda

AWS has announced few days ago, Go as supported language for AWS Lambda. So, I got my hand dirty and I made a Serverless Golang Lambda Function to discover new Movies by genres, I went even further and created a Frontend in top of my API with Angular 5.

Note: The full source code for this application can be found on GitHub 

To get started, install the dependencies below:

Create a main.go file with the following code:

The handler function takes as a parameter the movie genre ID then query the TMDb API  – Awesome free API for Movies and TV Shows – to get list of movies. I registred the handler using the lambda.Start() method.

To test our handler before deploying it, we can create a basic Unit Test:

Issue the following command to run the test:

Next, build an executable binary for Linux:

Zip it up into a deployment package:

Use the AWS CLI to create a new Lambda Function:

Note: substitute role flag with your own IAM role.

Sign in to the AWS Management Console, and navigate to Lambda Dashboard, you should see your lambda function has been created:

Set TMDb API KEY (Sign up for an account) as environment variable:

Create a new test event:

 Upon successful execution, view results in the console:

To provide the HTTPS frontend for our API, let’s add API Gateway as a trigger to the function:


Now, if you point your favorite browser to the Invoke URL:

Congratulations   you have created your first Lambda function in Go.

Let’s build a quick UI in top of the API with Angular 5. Create an Angular project from scratch using Angular CLI. Then, generate a new Service to calls the API Gateway URL:

In the main component iterate over the API response:

Note: the full code is in GitHub.

Generate production grade artifacts:

The build artifacts will be stored in the dist/ directory

Next, create an S3 bucket with AWS CLI:

Upload the build artifacts to the bucket:

Finally, turns website hosting on for your bucket:

If you point your browser to the S3 Bucket URL, you should be happy:

Chatbot with Angular 5 & DialogFlow

I have seen many posts on how to build a chatbot for a wide variety of collaboration platforms such as Slack, Facebook Messenger, HipChat … So I decided to build a chatbot from scratch to production using Angular latest release v5.0.0, DialogFlow, and AWS.

Here is how our chatbot will look like at the end of this post:

Note: This project is open source and can be found on my Github.

To get started, create a brand new Angular project using the Angular CLI:

1 – Chatbot Architecture

We will split out chat app in different components and each component will be able to communicate with others using attribute directives:

2 – Message Entity

Create an empty class by issuing the following command:

The message entity has 3 fields:

3 – Message List Component

Generate a new component:

Now we can display the messages by iterating over them:

The code of this component should look like this:

Note the usage of @app/models instead of the relative path, its called alias. To be able to use aliases we have to add the paths properties to our tsconfig.json file like this:

Note: I also added @env alias to be able to access environment variables from anywhere in our application.

4 – Message Item Component

Let’s build a component that will simply display a message in our message list:

In message-item.component.html, add the following content:

The code of the component should look like this:

5 – Message Form Component

Let’s build the form that will be responsible for sending the messages:

In the message-form.component.html, add the following content:

And it’s corresponding typescript code in message-form.component.ts:

The sendMessage() method will be called each time a user click on send button.

That’s it! Try it by yourself and you will see that it’s working.

At this moment, you wont get any response, that’s where NLP comes to play.

6 – NLP Backend

I choose to go with DialogFlow.  Sign up to DialogFlow and create a new agent:

Then, enable the Small Talk feature to have a simple chitchat:

Note: You can easily change the responses to the questions if you don’t like them. To go further you can create your own Intents & Entities as described in my previous tutorial.

Copy the DialogFlow Client Access Token. It will be used for making queries.

Past the token into your environments/environment.ts file:

7 – DialogFlow Service

Generate a DialogFlow Service which will make calls the DialogFlow API to retreive the corresponding response:

It uses the DialogFlow API to process natural language in the form of text. Each API request, include the Authorization field in the HTTP header.

Update the sendMessage() method in MessageFormComponent as follows:

Finally, in app.component.html, copy and past the following code to include the message-list and the message-form directives:

8 – Deployment to AWS

Generate production grade artifacts:

The build artifacts will be stored in the dist/ directory

Next, create an S3 bucket with AWS CLI:

Upload the build artifacts to the bucket:

Finally, turns website hosting on for your bucket:

If you point your browser to the S3 Bucket URL, you should see the chatbox:

Create Front-End for Serverless RESTful API

In this post, we will build an UI for our Serverless REST API we built in the previous tutorial, so make sure to read it before following this part.

Note: make sure to enable CORS for the endpoint. In the API Gateway Console under Actions and Enable CORS:

The first step is to clone the project:

Head into the ui folder, and modify the js/app.js with your own API Gateway Invoke URL:

Once done, you are ready to create a new S3 bucket:

Copy all the files in the ui directory into the bucket:

Finally, turns website hosting on for your bucket:

After running this command all of our static files should appear in our S3 bucket:

Your bucket is configured for static website hosting, and you now have an S3 website url like this http://<bucket_name>

Install MEAN Stack Using CloudFormation

AWS CloudFormation is a service that helps you model, setup and replicate your AWS resources. It uses a template file to bring up a collection of resources together as single stack.

To create templates we use a JSON file or AWS CloudFormation Designer. For this tutorial I opted the first solution.

Note: The template is available on my Github 😎.

We start with a basic template that defines a single EC2 instance with a security group that allows SSH traffic on port 22, MongoDB traffic on port 27017, and the NodeJS app on port 3000 from anywhere, as shown below:

In addition to that, we create two input parameters that specify the instance type and a Key Pair for SSH access. Then, we use UserData property to provide a set of shell commands to install MongoDB, NodeJS and bootstrap a simple MEAN application. Finally the output section print the public URL of the MEAN application.

Now we defined our template. Go to AWS Management Console then navigate to CloudFormation Dashboard and click on “Create Stack“:

Upload the JSON file and click on “Next“:

Assign a name to the stack, and choose your instance type and key pair you will use to ssh to the instance. Then, click on “Next“:

Left all fields unchanged and click on “Next“, then “Create

Once launched, you will get the following screen with launching process events:

After a while, you will get the CREATE_COMPLETE message in the status tab.

If you point your browser to the URL shown in the Outputs tab, you should see: a simple HTML message:

If we change the endpoint we should see a JSON response:

Congratulation ! ✨🎉 you have deployed your MEAN Stack application.

Go to EC2 Dashboard, you should see your instance there:

Verify the security group is setup as configured in the template:

To verify all packages and dependencies has been installed correctly, we can connect to the server via SSH:

To terminate the instance we can delete the stack from the CloudFormation Wizard, Deleting the stack will terminate all the instances launched by the stack: