AWS SAM to Terraform Converter
This project provides a tool to convert AWS Serverless Application Model (SAM) templates to Terraform configuration files. It parses SAM templates and generates equivalent Terraform resources, making it easier to migrate from SAM to Terraform or to use Terraform for deploying serverless applications originally defined in SAM.
- Converts AWS::Serverless::Function to aws_lambda_function
- Converts AWS::Serverless::HttpApi to aws_apigatewayv2_api
- Handles DynamoDB tables (AWS::DynamoDB::Table)
- Supports Step Functions (AWS::StepFunctions::StateMachine)
- Generates Terraform variables from SAM Parameters
- Supports environment-specific configurations
- Handles conditional resource creation based on SAM template conditions
-
Clone the repository:
git clone https://github.com/sauravpanda/aws-sam-to-terraform.git cd aws-sam-to-terraform
-
Install the required dependencies:
poetry install
-
Ensure your SAM template is in the same directory as the script, named
sam_template.yaml
. -
Run the converter:
poetry run python sam2terra/converter.py sam_template.yaml
-
Check the
terraform_output
directory for the generated Terraform files.
*_lambda.tf.json
: Lambda function configurations*_api.tf.json
: API Gateway configurations*_dynamodb.tf.json
: DynamoDB table configurations*_step_function.tf.json
: Step Functions configurationsvariables.tf.json
: Terraform variablesdata_sources.tf.json
: Data sources for existing resources
The converter generates an environment_name
variable. When applying the Terraform configuration, specify the environment:
terraform apply -var="environment_name=dev"
This will replace all instances of <EnvironmentName>
in the parameter default values with the specified environment name.
- Complex SAM features or nested intrinsic functions may not be fully supported
- Generated Terraform files may require manual adjustments for advanced scenarios
- Not all SAM resource types are currently supported
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.