How Do I Convert Terraform Into Pulumi?
Introduction
If you’re looking to migrate from Terraform to Pulumi, you’re in luck! Pulumi provides robust tools to automate the conversion of Terraform code into Pulumi programs. This allows you to maintain your existing infrastructure while transitioning to Pulumi’s more flexible programming model. In this guide, we’ll explore how to convert Terraform code to Pulumi using the pulumi convert
command.
Explanation
Basic Conversion Process
Converting Terraform to Pulumi is straightforward with the pulumi convert
command. Here’s how to use it:
pulumi convert --from terraform --language typescript
You can specify any of Pulumi’s supported languages:
pulumi convert --from terraform --language typescript
pulumi convert --from terraform --language python
pulumi convert --from terraform --language go
pulumi convert --from terraform --language csharp
pulumi convert --from terraform --language java
pulumi convert --from terraform --language yaml
By default, the conversion outputs to the current directory. You can specify an output directory:
pulumi convert --from terraform --language typescript --out pulumi-ts-program
Support for Any Terraform Provider
A major advantage of Pulumi’s conversion tool is its ability to handle ANY Terraform provider, even those without native Pulumi equivalents. The converter:
- Automatically bridges Terraform providers
- Generates necessary SDK code for providers not in the Pulumi registry
- Creates a complete, ready-to-deploy Pulumi project
Project Structure After Conversion
After running the conversion, your new Pulumi project will include:
Pulumi.yaml
: The Pulumi project configuration file- Main program file (e.g.,
index.ts
for TypeScript,__main__.py
for Python) - An
sdks
directory containing generated code for providers not in the Pulumi registry - Language-specific project files (e.g.,
package.json
for TypeScript,requirements.txt
for Python)
Configuration After Conversion
While the code is converted automatically, you’ll need to set up your configuration values. The converter creates placeholders in the Pulumi.yaml
file for variables from your Terraform code:
name: terraform-convert-example
runtime: nodejs
config:
aws:region:
value: 'TODO: var.aws_region' # fill in here
You’ll need to replace these placeholders with actual values before deploying.
Importing Existing Resources
After converting your code, you may want to import your existing infrastructure. Pulumi supports importing resources from any provider, including those bridged from Terraform:
Find the Pulumi type for the resource:
pulumi package get-schema terraform-provider hashicorp/aws
Import the resource using its provider ID:
pulumi import "aws:s3/bucket:Bucket" my-bucket "my-bucket-id"
Pulumi will generate code you can add to your project to manage the imported resource.
Real-World Example
Let’s look at a simple example converting a Terraform file with multiple providers:
Original Terraform (main.tf):
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
planetscale = {
source = "planetscale/planetscale"
version = "~> 0.1.0"
}
}
}
provider "aws" {
region = var.aws_region
}
provider "planetscale" {
service_token = var.planetscale_token
}
resource "aws_s3_bucket" "data_bucket" {
bucket = "my-data-bucket"
}
resource "planetscale_database" "db" {
name = "my-app-db"
organization = var.planetscale_org
}
Converted TypeScript (index.ts):
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as planetscale from "./sdks/planetscale";
// Create an AWS S3 bucket
const dataBucket = new aws.s3.Bucket("dataBucket", {
bucket: "my-data-bucket",
});
// Create a PlanetScale database
const db = new planetscale.database.Database("db", {
name: "my-app-db",
organization: "my-org",
});
// Export the bucket name and database URL
export const bucketName = dataBucket.bucket;
export const databaseUrl = db.url;
Considerations and Limitations
While Pulumi’s conversion tool is powerful, there are some considerations to keep in mind:
Terraform Modules: If your Terraform modules are defined in parent directories of your deployment code, you may need to restructure before conversion.
Dynamic Types: Terraform is dynamically typed, so when converting to statically typed languages like TypeScript or Go, the converter may use catch-all types like
any
.Configuration: The
.tfvars
files aren’t automatically converted to Pulumi stack configurations and will need manual adjustment.Terraform Functions: While Pulumi supports most Terraform functions, some dynamic functions (like
try
) may require manual intervention.
Summary
Converting Terraform to Pulumi is simpler than ever with the pulumi convert
command. This tool handles any Terraform provider, generates appropriate code for your chosen language, and sets up a complete Pulumi project structure.
After conversion, you’ll need to:
- Set up your configuration values in
Pulumi.yaml
- Import existing resources if needed
- Make any necessary adjustments to handle dynamic Terraform features
The resulting Pulumi code gives you all the benefits of your preferred programming language, including better abstractions, error checking, and reusability, while maintaining compatibility with your existing infrastructure.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.