Book Image

Hands-On Serverless Applications with Go

By : Mohamed Labouardy
Book Image

Hands-On Serverless Applications with Go

By: Mohamed Labouardy

Overview of this book

Serverless architecture is popular in the tech community due to AWS Lambda. Go is simple to learn, straightforward to work with, and easy to read for other developers; and now it's been heralded as a supported language for AWS Lambda. This book is your optimal guide to designing a Go serverless application and deploying it to Lambda. This book starts with a quick introduction to the world of serverless architecture and its benefits, and then delves into AWS Lambda using practical examples. You'll then learn how to design and build a production-ready application in Go using AWS serverless services with zero upfront infrastructure investment. The book will help you learn how to scale up serverless applications and handle distributed serverless systems in production. You will also learn how to log and test your application. Along the way, you'll also discover how to set up a CI/CD pipeline to automate the deployment process of your Lambda functions. Moreover, you'll learn how to troubleshoot and monitor your apps in near real-time with services such as AWS CloudWatch and X-ray. This book will also teach you how to secure the access with AWS Cognito. By the end of this book, you will have mastered designing, building, and deploying a Go serverless application.
Table of Contents (17 chapters)

AWS Lambda

AWS Lambda is the center of the AWS serverless platform:

AWS Lambda was launched at re:Invent 2014. It was the first implementation of serverless computing where users could upload their code to Lambda. It performs operational and administrative activities on their behalf, including provisioning capacity, monitoring fleet health, applying security patches, deploying their code, and publishing realtime logs and metrics to Amazon CloudWatch.

Lambda follows the event-driven architecture. Your code is triggered in response to events and runs in parallel. Every trigger is processed individually. Moreover, you are charged only per execution, while with EC2 you are billed by the hour. Therefore, you benefit from autoscaling and fault-tolerance for your application with low cost and zero upfront infrastructure investment.

Source events

AWS Lambda runs your code in response to events. Your function will be invoked when these event sources detect events:

Amazon is now supporting SQS as a source event for Lambda

Use cases

AWS Lambda can be used for endless application scenarios:

  • Web applications: Instead of a maintaining a dedicated instance with a web server to host your static website, you can combine S3 and Lambda to benefit from scalability at a cheaper cost. An example of a serverless website is described in the following diagram:

An alias record in Route 53 points to a CloudFront distribution. The CloudFront distribution is built on top of an S3 Bucket where a static website is hosted. CloudFront reduces the response time to static assets (JavaScripts, CSS, fonts, and images), improves webpage load times, and mitigates distributed denial of service (DDoS) attacks. HTTP requests coming from the website then go through API Gateway HTTP endpoints that trigger the right Lambda Function to handle the application logic and persist data to a fully managed database service, such as DynamoDB.

  • Mobile and IoT: A schematic for building a sensor application, which measures the temperature from a realtime sensor-connected device and sends an SMS alert if the temperature is out of range, can be given as follows:

The Connected Device will ingest data to AWS IoT. AWS IoT rules will invoke a Lambda Function in order to analyze the data and publish a message to an SNS Topic in case of emergency. Once the message is published, Amazon SNS will attempt to deliver that message to every endpoint that is subscribed to the topic. In this case it will be an SMS.

  • Data ingestion: Monitoring your logs and keeping an audit trail is mandatory, and you should be aware of any security breaches in your cloud infrastructure. The following diagram illustrates a realtime log-processing pipeline with Lambda:

The VPC Flow Logs feature captures information about the IP traffic going to and from network interfaces in your VPC and ships the logs to Amazon CloudWatch Logs. AWS CloudTrail maintains records of all AWS API calls on your account. All logs are aggregated and streamed to AWS Kinesis Data Streams.

Kinesis triggers Lambda Functions, which analyze logs for events or patterns and send a notification to Slack or PagerDuty in the event of abnormal activity. Finally, Lambda posts the dataset to Amazon Elasticsearch with a pre-installed Kibana to visualize and analyze network traffic and logs with dynamic and interactive dashboards. This is done for long-term retention and to archive the logs, especially for organizations with compliance programs. Kinesis will store logs in S3 bucket for backup. The bucket can be configured with a life cycle policy to archive unused logs to Glacier.

  • Scheduling tasks: Scheduled tasks and events are a perfect fit for Lambda. Instead of keeping an instance up and running 24/7, you can use Lambda to create backups, generate reports, and execute cron-jobs. The following schematic diagram describes how to use AWS Lambda to perform a post-processing job:

When a video arrives at an S3 bucket, an event will trigger a Lambda Function, which will pass the video filename and path to an Elastic Transcoder pipeline to perform video transcoding, generate multiple video formats (.avi, .h264, .webm, .mp3, and so on), and store the results in an S3 bucket.

  • Chatbots and voice assistants: You can use a Natural Language Understanding (NLU) or Automatic Speech Recognition (ASR) service, such as Amazon Lex, to build application bots that can trigger Lambda Functions for intent fulfillment in response to voice commands or text. The following diagram describes a use case for building a personal assistant with Lambda:

A user can ask Amazon Echo about its to-do list. Echo will intercept the user's voice command and pass it to a custom Alexa Skill, which will carry out speech recognition and transform the user's voice commands into intents, which will trigger a Lambda Function that in turn will query Trello API to fetch a list of tasks for today.

Due to Lambda's limitation in terms of memory, CPU, and timeout execution, it's not suited for long-running workflows and other massive workloads.