CI/CD Lambda functions with AWS Serverless Application Model (SAM)

February 21, 2020 in #aws #serverless #lamdba | | | Share on Google+

Deploy Lambda functions to API Gateway using SAM, CloudFormation

"A server-less datacenter..."

Your takeaways from this post

  • Understanding what is a serverless function
  • Why do we need SAM
  • How to quickly deploy some Python functions and connect them to an API
  • CI/CD with SAM: a developer push in Github will automatically deploy the new version of the app

Serverless Architecture

We had several trends in infrastructure the last decades:
1. Breaking down the monolithic architecture: one server alone should not manage all applications
2. Virtualization: we should factorize physical server unused resources by creating multiple Virtual Machines (VMs)
3. Containerization: we should remove the OS overheard of these VMs, and isolate each app in container (Docker)
4. Serverless: do we really need to manage the life cycle of the container? Let's concentrate on the app code only...

In parallel, Cloud vendors expand their offers to make our (developers) job easier:
1. Bare metal as a Service: we manage the whole server, hardware, OS, drivers, network
2. IaaS (Infrastructure): we take care of the VM, OS patches, framework, application
3. PaaS (Platform): for example, Kubernetes / ECS / EKS, we only manage the containers, images and ports
4. FaaS (Function) or Serverless: we manage only the code, in an elastic environment, the vendor takes care of all the rest

Serverless is a dream for developers:

  • No servers nor containers to manage: "server-less"
  • Elasticity: from zero to "infinity"
  • Pay only what you use

As we speak, many applications are getting migrated to become serverless to reduce cost and become elastic. But there are few downsides:

  • Debugging and security concerns as we cannot totally see all the components running the serverless function.
  • Serverless architectures are not built for long-running processes (Lambda timeout is 15 minutes). A container will be less costly for long run-times.
  • There will be a boot time of few seconds to start the function if not recently used.

Serverless in AWS Cloud

Lambda: Serverless functions

Lambda is a Serverless function running code from many popular languages (JS, Python, Go). With Lambda, your code will run in AWS Cloud, and you will pay only for what you use. AWS will automatically scale more Lambda functions of your app if the demand grows.

When deployed, other AWS services like CloudWatch, CloudFormation, SNS can call this Lambda to execute the code. From the outside, API Gateway integrate Lambda to provide a unified and secured way to query Lambda functions.

API Gateway

Api Gateway is a fully managed service to create, publish, maintain, monitor, and secure APIs at any scale. It acts as the "front door" for applications to access data, business logic, or functionality from your backend services.

Below is an example of API Gateway interface in AWS. We can see that 3 resources are declared (/capital, /hello, /sum). Resource /hello method GET will connect to the lambda helloLambda.

Serverless Application Model (SAM)

To deploy a serverless function accessible from an API, we need to create and configure our Lambda and API Gateway. To make the whole process easier, AWS introduced the Serverless Application Model SAM, an open-source framework for building serverless applications to express complex architecture in simple syntax.

SAM features include:

  • Single Deployment Configuration: a simple file to deploy complex architecture
  • Local Testing and Debugging: test the function locally on your laptop
  • Built-In Best Practices
  • Built on AWS CloudFormation: at the end, SAM will create CloudFormation templates to deploy the serverless components
  • Integration with Development Tools

Alternative to SAM

Serverless.com Framework is another popular alternative to deploy Serverless functions in AWS cloud. The advantages of this contender:

  • Compatible with all major clouds (AWS, Azure, Google)
  • The enterprise version will unlock more testing, monitoring and alerting of your serverless functions and deployments

At this point, both products offer quite similar functionalities. As Terraform compares to CloudFormation, I would say that Serverless.com should be used when integrations with other Cloud providers might come in. If you are certain you will stay within AWS premises only, SAM and CloudFormation are better choices as these services are well integrated and part of the AWS support.

Deploy Lambda with SAM

I setup a small demo using SAM to deploy Lambda and API Gateway on AWS Cloud. The deployment is automated with CloudFormation, and CI/CD is configured via CodeBuild.

Infra

Infra

  • Cloud: AWS
  • API: API Gateway from AWS, to unify and control user requests
  • App: 3 simple Lambda functions in Python
  • Code source: Github
  • CI: CodeBuild
  • Deployment: CloudFormation templates
  • SAM serverless: AWS framework making easier the deployment (CD) of Lambda and API Gateway
  • S3: to store CloudFormation template during deployment

CI/CD flow diagram

CI/CD

A simple git push from a developer in Github will launch the whole CI/CD process. SAM framework will build and deploy the CloudFormation template to create/update API Gateway and Lambda functions.

Get the code

Check out the Github repo to deploy the infra.

Conclusion

  • Lambda Serverless functions offer the possibility for developers to only think about the code and forget about the painful job of deploying, configuring, scaling up and down, patching/rebooting the OS.
  • Serverless architecture has huge advantage, and should be considered for new Cloud projects, as well as legacy applications which could be migrated to reduce cost and increase scalability.
  • SAM is a powerful framework to deploy complex serverless architectures in AWS Cloud. Without SAM, it is really hard to configure and automate all moving pieces, environments, API version, security settings.
  • CI/CD could be achieve with minimum effort, thanks to SAM and its integration with CloudFormation.

Thank you for reading :-) See you in the next post!

February 21, 2020 in #aws #serverless #lamdba | | | Share on Google+