As I mentioned in my previous article, serverless architecture has become more and more popular. AWS pay a lot of effort to promote this stuff. Today we will also have some hands-on experience on serverless architecture. What we will do is to make a health checker for a web site. When the web site is down, then we will receive an email notification. I assume that you all had AWS account already. If you don’t have, please visit here to register a 1 year free-tier account.
The flow will look like below:
- A time-based event will trigger lambda function 1 time per minute.
- The lambda function will check whether the EC2, our web site, is healthy or not.
- If lambda function detects the web site is not healthy, it will report fail to CloudWatch alarm.
- The alarm message will automatically send to the subscriptions via SNS email.
In this tutorial, it is up to you to choose any region. For your reference, I will choose London. You could choose the region which is close to you.
Host a Website Using EC2
In console, we search for EC2 and choose it.
Click Launch instance.
Select Amazon Linux 2 AMI.
Keep it default and click Next.
#!/bin/bash yum update -y yum install httpd -y echo "<html> <body> <center> <h1>Welcome to Cloud Tutorial!</h1> </center> </body> </html>" > /var/www/html/index.html systemctl start httpd systemctl enable httpd
Scroll down and copy the above script and paste into the user data. Then click Next.
Click Next and Next, nothing change.
In this page, we apply a new rule to allow port 80 to access. Then click Review and launch.
If you do not have existing key pair, just create a new one and download it. If you have existing key pair, just use it and click launch instances.
We wait until the instance is in running state. Then copy the public IP for later use.
When we browse the public IP, we will see our web site.
Create a Lambda Function
Search Lambda on AWS Console.
Click Create function.
In this page, we will use blueprint function. Blueprint is nothing but just a preset template provided by others, then we do not need to create our own. Then type “lambda-canary” in search bar and press enter to search. We choose the “lambda-canary” and click configure.
In this page, we have to give a name to the function. If no preference, just call it Health-Check. Then by default we create a new role for basic permissions.
In EventBridge session, we have to set the time-based event for checking, in our case is 1 time per minute. And check “Enable trigger”.
In Environment variables, we set the site as our website IP address. The key expected means what we expect to have in our web site, please type “Welcome to Cloud Tutorial!”. If the web site does not show this string, then it will be treated as failure. Then click Create function.
When we see this page, it means we have created our lambda function successfully.
Create SNS Topic
Select Topic on left hand side menu, and click Create topic.
Give a name to the topic and then click “Create topic”.
Then we click “Create subscription”. This decides who will receive the email.
In protocol we select Email, in endpoint we just type the email of receivers.
Check your email box, and you will receive a confirmation email, just confirm it to subscript.
Create CloudWatch Alarm
Click Alarm on left hand side and click Create Alarm.
Click Select metric.
Click Lambda=>Across All Functions=>check Error=>Select Metric.
In this page, we set the condition if the sum of error during 1 minute is equal or greater than 1, then it will send email to the users. The notification will be set on next page.
We just make sure to choose a right SNS topic and email. Click Next.
We give a name to the alarm, and click Next.
After checking all are fine, scroll down and click Create alarm.
Now all the things done, let’s test.
It is very easy to check it works or not. We just go back to EC2 and stop our instance by clicking Actions=>Instance State=>Stop.
Within few minutes, you should receive an email about the failure of the website.
Terminate the instance by clicking Actions=>Instance State=>Terminate.
In Security Groups, choose the security group we just created, and click Actions=>Delete security group.
In Lambda function, Click Actions=>Delete.
In Cloudwatch, choose Alarms and click Actions=>Delete.
In SNS, click Delete.
Enjoy serverless architecture!