Network Infrastructure Weathermap

The main goal of collecting metrics is to store them for long term usage and to create graphs to debug problems or identify trends. However, storing metrics about your system isn’t enough to identity the problem’s & anomalies root cause. It’s necessary to have a high-level overview of your network backbone. Weathermap is perfect for a Network Operations Center (NOC). In this post, I will show you how to build one using Open Source tools only.

Icinga 2 will collect metrics about your backbone, write checks results metrics and performance data to InfluxDB (supported since Icinga 2.5). Visualize these metrics in Grafana in map form.

To get started, add your desired host configuration inside the hosts.conf file:

Note: the city & country attributes will be used to create the weathermap.

To enable the InfluxDBWriter on your Icinga 2 installation, type the following command:

Configure your InfluxDB host and database in /etc/icinga2/features-enabled/influxdb.conf (Learn more about the InfluxDB configuration)

Icinga 2 will forward all your metrics to a icinga2_metrics database. The included host and service templates define a storage, the measurement represents a table by which metrics are grouped with tags certain measurements of certain hosts or services are defined (notice the city & country tags usage).

Don’t forget to restart Icinga 2 after saving your changes:

Once Icinga 2 is up and running it’ll start collecting data and writing them to InfluxDB:

Once our data arrived, it’s time for visualization. Grafana is widely used to generate graphs and dashboards. To create a Weathermap we can use a Grafana plugin called Worldmap Panel. Make sure to install it using grafana-cli tool:

The plugin will be installed into your grafana plugins directory (/var/lib/grafana/plugins):

Restart Grafana, navigate to Grafana web interface and create a new datasource:

Create a new Dashboard:

The Group By clause should be the country code and an alias is needed too. The alias should be in the form $tag_field_name. See the image below for an example of a query:

Under the Worldmap tab, choose the countries option:

Finally, you should see a tile map of the world with circles representing the state of each host.

The field state possible values (0 – OK, 1 – Warning, 2 – Critical, 3 – Unknown/Unreachable)

Note: For lazy people I created a ready to use Dashboard you can import from GitHub.

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.