ServerlessSavants
Expert Insights on Cloud Infrastructure
Migrating Terraform Projects to AWS SAM: The Complete Guide
Transition from Terraform to AWS SAM seamlessly with this comprehensive migration guide
Discover how to transform your Terraform-managed infrastructure into streamlined AWS SAM applications without disrupting your production environment.
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.
Incremental Migration Approach
Adopt a phased migration strategy to minimize risk:
- Start with non-critical services
- Maintain Terraform for stateful resources
- Use CloudFormation nested stacks for hybrid management
- 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.
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:
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:
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:
- Export Terraform state to JSON
- Create matching SAM resource definitions
- Use CloudFormation resource import
- 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.”
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.
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.