What is Serverless?
A Serverless Lambda is essentially code, written by you, which a cloud provider like Amazon or Google will run for you. Your code gets run on a cloud providers servers on demand, as and when it is needed. The benefit of having someone else run your code for you is that there is no need to worry about paying for idle servers, managing servers, keeping servers secure, upgrading servers, ensuring servers have capacity and a whole plethora of other issues. Serverless code makes experimentation faster and cheaper because someone else takes over the responsibility of running your code for you. This lets you focus on the execution, what your code actually does, rather than getting bogged down in the intricacies of how code is executed.
Sounds nice doesn’t it, well it is a game changer. Serverless code allows start-ups to run production quality code very cheaply without having to invest in the cost of buying and maintaining servers. This evens the playing field when it comes to building software. It makes it easier for a new company with a good idea to test their idea out so they can see if its viable before sinking loads of cash into it. I have worked for two companies so far which were using Serverless technologies. Unsurprisingly, both were start-ups.
Working at start-ups also has the advantage that creating new things allows the team to use the latest technologies rather than inheriting choices made a long time ago. In my day job I write Go code. Go is a modern programming language created by Google in the tradition of C. Most people don’t realise that Go was first announced in November 2009, making it 10 years old very soon.
Building AWS Serverless Lambdas in Go
AWS Serverless Lambdas support Go as well as many other languages including Java, Node.js, C#, Python, PowerShell and Ruby code.
Writing Serverless Lambdas in Go is concise and straight forward. The AWS lambda package is imported, and then from main() the Lambda package is told to start the Lambda function, which is usually named handler. The code that will run on demand is placed into the handler function.
Below I have written the simplest example of a Lambda function in Go. This Lambda will print “Hello Lambda” when it is executed.
Go is a compiled language so the next step after writing the code is to build the code using the
go build command.
For Serverless Lambdas to run on Amazon servers they need to be built with the target operating system set to Linux. This is done by setting the GOOS environment variable. Once the binary is built, it needs to be zipped up. Once zipped up, it can be uploaded to Amazon via the console.
Building and zipping the above Hello Lambda code can be done in a terminal window using
GOOS=linux go build -v -o compiledAssets/main && zip -j compiledAssets/deployment.zip compiledAssets/main
Once deployment.zip is created, log into your AWS account, select Lambda from the list of AWS services then click the Create Function button. Give your Lambda a name and select the Go 1.x runtime.
In the Function Code section of the console, select upload a .zip file and upload the deployment.zip that was created, make sure Runtime is set to Go 1.x and then in the Handler box you will need to type the name of the executable binary that is inside the deployment.zip. If you used the terminal command above to generate the zip file the binary will be called “main”.
After providing these details, click the Save button at the top of the console. Congratulations, you have just created a Serverless Lambda function.
To see your new Lambda run, click on the Test button at the top of the console to create a test event, the default event will send three key value pairs. Our Lambda doesn’t care about the input it receives, it just prints “Hello Lambda” so this is fine, just click the Create button.
Your test event will now appear in the drop down box, make sure its selected and click the Test button again. Your Lambda will execute and you should see Execution result: succeeded
Click on the Monitoring tab, then click the View logs in CloudWatch button.
You should see your lambda has logged “Hello Lambda”
Lambda Event Triggers
Lambdas can do so much more than just print “Hello Lambda” they can be set up to trigger and run in response to a variety of events. When events trigger Serverless Lambda functions, they pass information about the event into the Lambda and the code inside the Lambda function can take actions upon that data.
At the time of writing this article these are some of the ways Serverless Lambdas can be triggered…
- Alexa skills e.g. when someone gives a voice command
- API Gateway e.g. when an API request is made
- Dynamo events e.g. when a database is written to
- IoT events e.g. when a device or thing sends data
- Lambda events e.g. Serverless lambdas can trigger other Serverless Lambdas
- S3 events e.g. when a file is uploaded
- SNS events e.g. when a message is received
- Time e.g. every n hours
The Serverless Lambda is essentially a building block that can be joined to other building blocks. Serverless lambdas are stateless, they don’t have a memory. This means that each time they run they can’t remember anything about the previous time they ran. It’s much easier to write unit tests around code that is stateless. By breaking code down into smaller block size pieces that can run independently, code also becomes easier to reason about and easier to maintain. This makes Serverless Lambda perfect for creating solutions for complex real world problems.
I love little Lambdas. At work I am currently using them to build a telematics platform for smart devices in cars.