In recent years, Infrastructure as Code (IaC) has emerged as a fundamental practise in cloud computing, allowing developers and operations teams to manage and provision infrastructure using code rather than manual processes. This shift has revolutionised the way applications are deployed and managed, especially in serverless environments like AWS Lambda. In this article, we’ll explore the pros and cons of using IaC tools such as AWS SAM, Terraform, and the Serverless Framework for AWS Lambda deployments.
AWS SAM (Serverless Application Model)
Pros:
- Native AWS Integration: AWS SAM is designed specifically for AWS services, providing seamless integration with the AWS ecosystem. This allows for simplified configuration and deployment of serverless applications.
- Simplified YAML Syntax: AWS SAM uses a straightforward YAML-based syntax, making it easy to define serverless resources, such as functions, APIs, and event sources, in a concise and human-readable format.
- Built-in Local Testing: AWS SAM provides a local testing environment, enabling developers to test Lambda functions and APIs locally before deploying them to the AWS cloud. This speeds up development cycles and improves code quality.
- Automated Packaging and Deployment: AWS SAM CLI (Command Line Interface) simplifies the packaging and deployment process by handling the creation of deployment packages and the uploading of artifacts to AWS S3.
- Template Validation: AWS SAM CLI provides a built-in validation tool that checks the syntax and structure of SAM templates, helping to catch errors early in the development process.
Cons:
- Limited Multi-Cloud Support: AWS SAM is tightly integrated with AWS services, which means it’s primarily optimised for AWS deployments. If you require multi-cloud support, you may need to supplement it with additional tools.
- Lack of Advanced Features: AWS SAM may not offer as many advanced features or customizations as other IaC tools like Terraform. It’s designed to streamline the deployment of serverless applications within AWS’s ecosystem.
Deploying a Hello World Lambda Function using AWS SAM
# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
Runtime: nodejs14.x
CodeUri: ./
FunctionName: HelloWorldFunction
MemorySize: 128
Timeout: 3
In this example, we’re using AWS SAM to define a basic Lambda function. This template specifies the AWS resources needed for the function, including its name, runtime, and code location.
To deploy this using the AWS SAM CLI, you would run:
sam build
sam deploy --guided
Terraform
Pros:
- Multi-Cloud Support: Terraform is a cloud-agnostic IaC tool, allowing you to provision and manage resources across multiple cloud providers, including AWS, Azure, Google Cloud, and more. This makes it a powerful choice for organisations with diverse cloud environments.
- Extensive Provider Ecosystem: Terraform boasts a large and active provider ecosystem, which means it supports a wide range of cloud services and other infrastructure providers. This provides flexibility in resource provisioning.
- Declarative Configuration: Terraform uses a declarative configuration language, HashiCorp Configuration Language (HCL), which allows you to specify the desired state of your infrastructure. It automatically manages the dependencies and execution order.
-
State Management: Terraform keeps track of the state of your infrastructure, allowing for easier collaboration and change management across teams.
Cons:
Learning Curve: Terraform may have a steeper learning curve compared to AWS SAM, especially for those new to the tool or IaC in general. It requires a deeper understanding of infrastructure concepts.
-
Complexity for Simple Tasks: While powerful, Terraform may be overkill for simple serverless deployments, where a more specialised tool like AWS SAM may provide a more streamlined experience.
Deploying a Hello World Lambda Function using Terraform
# main.tf
provider "aws" {
region = "us-east-1"
}
data "archive_file" "lambda_zip" {
type = "zip"
source_dir = "./lambda-function"
output_path = "./lambda.zip"
}
resource "aws_lambda_function" "hello_world" {
function_name = "hello-world-lambda"
handler = "index.handler"
runtime = "nodejs14.x"
filename = "lambda.zip"
role = aws_iam_role.lambda.arn
source_code_hash = filebase64sha256("lambda.zip")
environment {
variables = {
ENV_VAR = "Value"
}
}
}
resource "aws_iam_role" "lambda" {
name = "lambda-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
role = aws_iam_role.lambda.name
}
In this Terraform example, we’re defining an AWS Lambda function along with the required IAM role and policy. We’re also setting an environment variable for the Lambda function.
To deploy this using Terraform, you would run:
terraform init
terraform apply
Serverless Framework
Pros:
- Platform Agnostic: Similar to Terraform, the Serverless Framework is not tied to any specific cloud provider. It supports AWS, Azure, Google Cloud, and other providers, making it suitable for organisations with diverse cloud environments.
- Rich Plugin Ecosystem: Serverless Framework has a robust plugin ecosystem that extends its capabilities, allowing for a wide range of integrations and customisations.
- Deployment Pipelines and Workflows: The Serverless Framework provides features for setting up deployment pipelines and workflows, which can be essential for complex applications with multiple environments.
- Community Support: Serverless Framework has a large and active community, which means there are plenty of resources, tutorials, and plugins available to support your deployments. ### Cons:
- Less Opinionated: While flexibility is an advantage, Serverless Framework may be less opinionated compared to AWS SAM, which can lead to more customization but may require additional effort in configuration.
- Steep Initial Setup: Setting up a project in the Serverless Framework may take more time compared to using a more specialised tool like AWS SAM.
Deploying a Hello World Lambda Function using Serverless Framework
# serverless.yml
service: hello-world-service
provider:
name: aws
runtime: nodejs14.x
functions:
helloWorld:
handler: handler.hello
In this Serverless Framework example, we’re defining a service with one function (helloWorld) that uses the Node.js 14.x runtime.
To deploy this using the Serverless Framework, you would run:
serverless deploy
AWS SAM is excellent for AWS-centric environments, offering seamless integration with AWS services. It’s especially advantageous if you’re primarily working within the AWS ecosystem.
Terraform shines in multi-cloud environments, allowing you to manage resources across various cloud providers. It provides extensive flexibility and control, making it ideal for complex infrastructures.
Serverless Framework offers a balance between flexibility and ease of use. It’s platform-agnostic, making it suitable for organisations with diverse cloud environments.
I personally prefer Terraform as my go-to IaC, but ultimately, the choice depends on your specific needs, team familiarity, and the complexity of your serverless applications. Each tool brings its strengths to the table, enabling you to optimise your serverless deployments for success.
Top comments (0)