Why Migrate to AWS SAM?

As serverless architectures become increasingly popular, many teams find Terraform limiting for managing Lambda-centric applications. AWS SAM (Serverless Application Model) provides a framework optimized for serverless development with simplified syntax, local testing capabilities, and deeper integration with AWS services.

Terraform for Serverless

  • Verbose configuration
  • Limited local testing
  • Manual resource definition
  • Complex state management
  • Steep learning curve

AWS SAM Advantages

  • Simplified YAML syntax
  • Robust local testing
  • Auto-generated resources
  • Built-in best practices
  • Faster deployment cycles

Migrating to SAM can reduce configuration complexity by 40-60% for typical serverless applications while improving development velocity and deployment safety.

Migration Strategy

Assessment & Planning

Begin by auditing your existing Terraform configuration. Identify resources that map directly to SAM equivalents:

  • AWS Lambda functions → SAM Functions
  • API Gateway resources → SAM APIs
  • DynamoDB tables → SAM SimpleTable
  • IAM roles → SAM Policies

Document any Terraform resources without SAM equivalents that will require special handling during migration.

Terraform to SAM

Incremental Migration Approach

Adopt a phased migration strategy to minimize risk:

  1. Start with non-critical services
  2. Maintain Terraform for stateful resources
  3. Use CloudFormation nested stacks for hybrid management
  4. Gradually shift traffic to new SAM-deployed services

This approach allows you to validate SAM functionality while maintaining rollback capabilities to your Terraform-managed infrastructure.

Terraform SAM Hybrid

Step-by-Step Migration Process

Convert Terraform HCL to SAM Template

Begin by translating your Terraform resource definitions to SAM’s YAML format. SAM provides shortcuts for common serverless patterns:

// Terraform Lambda Definition
resource “aws_lambda_function” “example” {
function_name = “my-function”
handler = “index.handler”
runtime = “nodejs14.x”
role = aws_iam_role.lambda.arn
}

// Equivalent SAM Definition
MyFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: my-function
CodeUri: ./src/
Handler: index.handler
Runtime: nodejs14.x

Recreate Terraform Modules as SAM Applications

Convert Terraform modules into independent SAM applications. Use SAM’s nested application support for complex architectures:

# SAM template.yaml
Resources:
PaymentService:
Type: AWS::Serverless::Application
Properties:
Location: ./payments/sam-template.yaml

UserService:
Type: AWS::Serverless::Application
Properties:
Location: ./users/sam-template.yaml

Migrate Stateful Resources Carefully

For stateful resources (RDS, DynamoDB, S3), maintain Terraform management or migrate using CloudFormation Import:

  1. Export Terraform state to JSON
  2. Create matching SAM resource definitions
  3. Use CloudFormation resource import
  4. Verify resource integrity before destroying Terraform state

“The key to successful Terraform to SAM migration is maintaining idempotency throughout the process. Always validate your SAM templates against existing infrastructure before cutting over.”

AJ

Alex Johnson

AWS Certified Solutions Architect & Serverless Specialist

Advanced Migration Patterns

Handling Complex Dependencies

For advanced architectures with cross-stack dependencies:

  • Use CloudFormation Exports/Imports between SAM applications
  • Implement custom resources for Terraform-only dependencies
  • Leverage SSM Parameter Store for cross-stack configuration
  • Utilize AWS CDK for hybrid SAM/Terraform environments

This maintains loose coupling while enabling communication between SAM-managed and Terraform-managed resources during transition.

SAM App Terraform SSM

Security Considerations

When migrating IAM configurations:

  • Convert Terraform IAM policies to SAM Policy Templates
  • Leverage SAM’s built-in policy shortcuts (DynamoDBCrudPolicy, SQSPollerPolicy)
  • Audit permissions using IAM Access Analyzer
  • Implement least privilege with SAM policy boundaries

SAM’s policy templates reduce common misconfigurations by 85% compared to manual IAM policy authoring.

IAM Policies SAM