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)

Go serverless

AWS announced its support for Go as the language for AWS Lambda in January 2018. There were already some open source frameworks and libraries with which to shim Go applications that used Node.js (Apex serverless Framework), but now Go is officially supported and added to list of programming languages that you can use to write your Lambda Functions:

  • Go
  • Node.js
  • Java
  • Python
  • .NET

But which language should we use to write efficient Lambda Functions? One of the reasons to go serverless is being a polygot. Regardless of the language you choose, there is a common pattern to writing code for a Lambda Function. Meanwhile, you need to pay extra attention to performance and cold starts. That's where Go comes into play. The following diagram highlights the main advantages of using Go for serverless applications in AWS Lambda:

  • Cloud-oriented: It was designed by Google primarily for the cloud with scalability in mind, and to reduce the amount of build time. Go is a solid language for distributed systems and infrastructure tools. Docker, Kubernetes, Terraform, etcd, Prometheus, and many orchestration, provisioning, and monitoring tools are built using Go.
  • Fast: Go complies into a single binary. Therefore, you provide a precompiled Go binary to AWS Lambda. AWS does not compile the Go source files for you and this has certain consequences, such as Fast cold-boot time. Lambda doesn't need to set up a runtime environment; Java, on the other hand, requires spinning up a JVM instance to make your function hot. Go has a clean syntax and clear language specifications. This delivers an easy language for developers to learn and shows good results quickly while producing maintainable code.
  • Scalable: Go has built-in concurrency with goroutines instead of threads. They consume almost 2 Kb memory from the heap and work faster than threads; hence, you can spin up millions of goroutine at any time. For software development, there is no need for a framework; the Golang community has built many tools that are natively supported by Go's language core:
    • Go's error-handling is elegant.
    • Lightweight framework for unit testing.
    • Solid standard library—HTTP protocol support out of the box.
    • Common data type and structure supported—maps, array, structs, and so on.
  • Efficient: It involves efficient execution and compilation. Go is a compiled language; it compiles into a single binary. It uses static linking to combine all dependencies and modules into one single binary file. Also, its faster compilation speed allows for rapid feedback. Speedy development saves time and money; thus, this is certainly the most significant advantage for someone with a tight budget. Moreover, it provides efficient memory utilization with garbage collector.
  • Growing community: The following screenshot shows the rising popularity and usage (as observed in the StackOverflow Survey 2017) for the most loved, dreaded, and wanted programming languages:

In addition, Go is backed by Google and has a large, growing ecosystem and numerous contributors to the language on GitHub, and great IDE support (IntelliJ, VSCode, Atom, GoGland) and debugging.