Serverless Primer Part 1 - AWS Lambda and CloudFormation

Serverless Primer Part 1: AWS Lambda and CloudFormation

Welcome back to my blog, clearly I did not do a good enough job scaring you off last time, so on to round 2. In this post I will be diving into deeper detail (and now with Code!) on what a basic serverless app will look like. I’ll provide some high level details of serverless on AWS and go into a little more detail on how to get into it on your own. In this post I will be dealing with AWS Lambda as my FaaS platform of choice and will be deploying Lambda using AWS CloudFormation.

Lambda and CloudFormation Overview

AWS Lambda is a serverless compute platform that allows you to execute code without needing to worry about any of the hardware or software required to run it (mostly). Pick a Runtime, upload some code, and let her rip! Ok, so it’s not quite that easy, but it’s not far off. Lambda can have a very inexpensive cost model when architected correctly. You pay for the number of executions performed X the amount of memory per execution X the amount of memory allocated per execution.

As of (10/30/19) I still have not had a charge for any of my executions during testing and development. Obviously with a larger deployment, in an enterprise setting, there will be costs associated with Lambda, but a lot of those shift towards Development and not Infrastructure. Serverless is not the solution for every application but it can be a very effective one for many.

AWS CloudFormation is an Infrastructure as Code (IaC) tool that allows you to model and deploy virtually all of your AWS infrastructure with a few lines of code in YAML or JSON templates. It allows you to create Stacks that are self defined applications built across the many offerings within AWS. CloudFormation is completely free to use and you only pay for the AWS resources that are consumed as part of the stack. Another great feature of CloudFormation is the ability to detect configuration drift when resources have changed from the initial deployment.

What do I need?

Know your way around AWS. A lot of what I will be posting about will assume some level of AWS familiarity. If you need more clarity feel free to reach out to me. I also recommend you to check out some of the great courses, that I personally have gone through, on A Cloud Guru.

To follow along and build out your own serverless gems you’ll need to have an AWS account (preferably a free dev account so as to keep any possible costs to a minimum and little risk). I would also recommend a simple linux EC2 spot instance to act as a disposable bastion host. I have a permissions policy, here, that you can attach to a user account setup in your spot instance. Please be sure to look over it, one should never apply a security policy without looking through what it may be opening up.

Diving In

AWS Lambda has a nice web interface, like most other AWS products, that one can use to build a serverless app. The real power behind AWS, is its commitment to IaC. Aside from the powerful API behind all of its products, CloudFormation is an excellent tool for deploying predictable stacks very easily.

This CloudFormation template will build and deploy a Lambda function along with new IAM role specific to this stack. The following snippet of code, from that template, shows the minimal steps needed in order to deploy a Lambda function using CloudFormation.

Resources:
  LambdaFunction:  
    Type: 'AWS::Lambda::Function'
    Properties:
      Code:
        S3Bucket: !Sub '${AWS::StackName}-lambda'
        S3Key: !Sub '${AWS::StackName}.zip'
      FunctionName: !Ref 'AWS::StackName'
      Role: !GetAtt
          - LambdaRole
          - Arn
      Handler: index.main
      Runtime: python3.7

The only Required part of a template is the Resources portion that defines the AWS resources that are to be deployed by CloudFormation.

Breaking down the Resource declaration, you have the logical name, LambdaFunction, which can be Referenced in other parts of the template. Next is the resource Type ‘AWS::Lambda::Function’ being defined. The only part left is to set the Properties for the resource.

A Lambda function requires the following:

Optional:

The function itself is a simple Python script:

import sys
def main(event, context):
    concat = event['k1'] + event['k2']
    return(concat)

if __name__ == '__main__':
    main(event, context)

It takes two values (k1 and k2) from the event parameter and returns the concatenated value. The context parameter providers runtime information to your handler.

This Python script is a self contained deployment and cleanup script to demonstrate using code throughout the entire process of deploying a serverless app with CloudFormation. It is paired up with the CloudFormation template and Python Script from above and this yaml configuration file to de-couple some configuration settings out of the script.

When you execute the script, it will prompt you to either, Create:

[root@ServerlessPrimer]# python3 lambda_primer.py
(C)reate or (D)elete LambdaStack?:
C
Enter name of stack:
vmadbroteststack
Creating bucket vmadbroteststack-cf...
Creating bucket vmadbroteststack-lambda...
Copying app_code.py to index.py...
Zipping index.py into vmadbroteststack.zip...
Uploading cf_app_lambda.yaml to vmadbroteststack-cf...
Uploading vmadbroteststack.zip to vmadbroteststack-lambda...
Creating stack vmadbroteststack...
Removing index.py...
Removing vmadbroteststack.zip...
[root@ServerlessPrimer]#

or Delete :

[root@ServerlessPrimer]# python3 lambda_primer.py
(C)reate or (D)elete LambdaStack?:
D
List of Stacks:
vmadbroteststack
Hybrid1-Test
Which stack to delete? (case sensitive)
vmadbroteststack
Deleting stack: vmadbroteststack...
Deleting file: vmadbroteststack.yaml...
Deleting bucket: vmadbroteststack-cf...
Deleting file: vmadbroteststack.zip...
Deleting bucket: vmadbroteststack-lambda...
[root@ServerlessPrimer]#

If everything is configured correctly, when you execute the Create function, within seconds you will have a CloudFormation stack that has deployed a working Lambda Function. You can run a quick test against the function on the AWS console to see if it works. In future posts I will dive deeper into Lambda and will integrate with API Gateway to allow us to get really fancy with our application.

To help with cleanup, I added the Delete function to make sure nothing is left behind. Because some parts of this were configured outside of CloudFormation deleting the stack will not recycle everything that we deploy. For clarity, this script deletes the S3 buckets that were created, the code that was uploaded, and the stack that was created.

Wrapping Up

Why are you still reading? Clearly you have almost as much free time as I had while writing this.. But I thank you for making it through. I know there was a good bit covered and a whole lot more that was not. There is a lot I plan to learn, build, and share with Lambda and CloudFormation going forward. I plan to branch out in more fun and creative ways to use code to accomplish a lot with a few lines of text.

In part two of this I will be diving into Terraform by HashiCorp as another way to deploy Lambda functions and build on the application (yes concatenating two strings is an application…) to show more serverless examples.

Thank you and if you have any questions or feedback please reach out to me on LinkedIn or Twitter @GregMadro.