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:

Deployment:

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:

Real-Time Infrastructure Monitoring with Amazon Echo

Years ago, managing your infrastructure through voice was a science-fiction movie, but thanks to virtual assistants like Alexa it becomes a reality. In this post, I will show you how I was able to monitor my infrastructure on AWS using a simple Alexa Skill.

At a high level, the architecture of the skill is as follows:

I installed data collector agent (Telegraf) in each EC2 instance to collect metrics about system usage (disk, memory, cpu ..) and send them to a time-series database (InfluxDB)

Once my database is populated with metrics, Amazon echo will transform my voice commands to intents that will trigger a Lambda function, which will use the InfluxDB REST API to query the database.

Enough with talking, lets build this skill from scratch, clone the following GitHub repository:

Create a simple fleet of EC2 instances using Terraform. Install the AWS provider:

Set your own AWS credentials in variables.tfvars. Create an execution plan:

Provision the infrastructure:

You should see the IP address for each machine:

Login to AWS Management Console, you should see your nodes has been created successfully:

To install Telegraf on each machine, I used Ansible, update the ansible/inventory with your nodes IP addresses as follows:

Execute the playbook:

If you connect via SSH to one of the server, you should see the Telegraf agent is running as Docker container:

In few seconds the InfluxDB database will be populated with some metrics:

Sign in to the Amazon Developer Portal, create a new Alexa Skill:

Create an invocation name – aws – This is the word that will trigger the Skill.

In the Intent Schema box, paste the following JSON code:

Create a new slot types to store the type of metric and machine hostname:

Under Uterrances, enter all the phrases that you think you might say to interact with the skill

Click on “Next” and you will move onto a page that allows us to use an ARN (Amazon Resource Name) to link to AWS Lambda.

Before that, let’s create our lambda function, go to AWS Management Console and create a new lambda function from scratch:

Note: Select US East (N.Virginia), which is a supported region for Alexa Skill Kit.

Make sure the trigger is set to Alexa Skills Kit, then select Next.

The code provided uses the InfluxDB client to fetch metrics from database.

Specify the .zip file name as your deployment package at the time you create the Lambda function. Don’t forget to set the InfluxDB Hostname & Database name as an environment variables:

Then go to the Configuration step of your Alexa Skill in the Amazon Developer Console and enter the Lambda Function ARN:

Click on “Next“. Under the “Service Simulator” section, you’ll be able to enter a sample utterance to trigger your skill:

Memory usage:

Disk usage:

CPU usage:

Test your skill on your Amazon Echo, Echo Dot, or any Alexa device by saying, “Alexa, ask AWS for disk usage of machine in Paris

Amazon Alexa GitHub Followers Counter

This post is part of “Alexa” series. I will walk you through how to build an Amazon Alexa Skill with Node.JS and Lambda to get numbers of followers & repositories in GitHub in real-time.

Note: all the code is available in my GitHub.

Amazon Echo will captures voice commands and send them to the Alexa Skill to convert them into structured text commands. A recognized command is sent to an AWS Lambda function that will call GitHub API to get response.

To get started, sign up to Amazon Developer Console,  and create a new Alexa Skill:

The invocation name is what user will say to trigger the skill. In our case it will be “github“.

Click on “Next” to bring up the Interaction Model page, use the intent schema below:

Intents will map user’s voice command to services that our Alexa skill can address. For instance, here I defined an intent called GetGithubFollowerCount, which will line up with a portion of code in my Lambda funtion that I leverage in a bit.

The programming languages are defined as a Custom Slot Type, with the following possible values:

Now our intents are defined, we need to link them to a human request that will trigger this linkage. To do this multiple sentences (utterances) are listed to make the interaction as natural as possible.

Result:

Click on “Next” and you will move onto a page that allows us to use an ARN (Amazon Resource Name) to link to AWS Lambda.

Before that, let’s create our lambda function, login to AWS Management Console, then navigate to Lambda Dashboard and create a new function from scratch:

Select Alexa Skills Kit as trigger:

I wrote the Lambda functions in Node.JS, although that code isn’t actually that interesting so I won’t go into it in much detail.

This function is fired when there is an incoming request from Alexa. The function will:

  • Process the request
  • Call GitHub API
  • Send the response back to Alexa

Create a zip file consisting of the function above and any dependencies (node_modules). Then, specify the .zip file name as your deployment package at the time you create the Lambda function. Don’t forget to set your GitHub Username as an environment variable:

Back in the Alexa Skill we need to link our Lambda function as our endpoint for the Alexa Skill:

That’s it, let’s test it out using a Service Simulation by clicking on “Next“.

GetFollowerCount Intent : 

GetRepositoryCount Intent:

GetGithubRepositoryCountByLanguage Intent:

You can see that the Lambda responds as expected !

Test it now with Amazon Echo, by saying “Alexa, ask GitHub for …” :

 

Highly Available WordPress Blog

In this post you will learn about the easiest way to deploy a fault tolerant and scalable WordPress on AWS.

To get started, setup a Swarm cluster on AWS by following this tutorial Setup Docker Swarm on AWS using Ansible & Terraform:

Now your cluster is ready to use. You are ready to go !

WordPress stores some files on disk (plugins, themes, images …) which causes a problem if you want to use a fleet of EC2 instances to run your blog in case of high traffic:

That’s where AWS EFS (Elastic File System) comes into the play. The idea is to mount shared volumes using the NFS protocol in each host to synchronize files between all nodes in the cluster.

So create an Elastic File System, make sure to deploy it in the same VPC on which your Swarm cluster is created:

Once created, note the DNS name:

Now, mount Amazon EFS file systems via the NFSv4.1 protocol on each node:

We can verify the mount with a plain df -h command:

WordPress requires a relational database. Create an Amazon Aurora database:

Wait couple of minutes, then the database should be ready, copy the endpoint of database:

To deploy the stack, I’m using the following Docker Compose file:

In addition to wordpress container, Im using Traefik as reverse proxy to be able to scale out my blog easily with docker service scale command.

In your Manager node run the following command to deploy the stack:

At this point, you should have a clean install of WordPress running.

Fire up your browser and point it to manager public IP address, you will be greeted with the familiar WordPress setup page:

If you’re expecting a high traffic, you can easily scale the WP service using the command:

Verify Traefik Dashboard:

That’s how to build a scalable WordPress blog with no single points of failure.

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: