Convert Terraform HCL to Pulumi
Do you need to convert?
Converting is not the only way to run Terraform configuration with Pulumi. Pulumi's HCL runtime runs your existing .tf files directly: set runtime: hcl in Pulumi.yaml and pulumi up deploys the configuration you already have, unchanged.
Which path you take comes down to what you want out of the move:
- Run your HCL natively when you want to keep writing HCL and are after Pulumi's engine, state management, secrets, and cloud platform.
- Convert when you want the infrastructure code itself in a general-purpose language, for the testing, abstraction, and IDE support that comes with TypeScript, JavaScript, Python, Go, .NET, or Java.
If you haven't written much code in one of these languages, Language Essentials translates the HCL you already know into the small set of constructs a Pulumi program actually uses.
The rest of this page covers converting.
When to convert
Converting HCL to Pulumi code makes sense when:
- Complex logic: Operations that need rich runtime logic can be more natural in a general-purpose language
- Testing requirements: You want to test infrastructure with your language's own unit-testing framework and mocking libraries
- Integration needs: Infrastructure code needs to integrate with application code
- Team preferences: Your team prefers general-purpose programming languages
- Advanced features: You want to use Pulumi-specific features like Pulumi Policies or Automation API
Conversion approaches
You can convert HCL with a coding agent or with the deterministic pulumi convert command. For most configurations, use an agent. pulumi convert translates HCL construct by construct and emits a TODO wherever it meets something it can't translate, so its output mirrors the shape of the HCL rather than the idioms of the target language. An agent working through the Pulumi MCP server writes idiomatic code, handles the constructs the converter can't, and can keep iterating until pulumi preview reports no changes. pulumi convert is still a fast, repeatable first pass, and the rest of this section shows what it produces.
Automated conversion with pulumi convert
The pulumi convert command can automatically translate Terraform configurations to Pulumi programs. Two converters read HCL, selected with --from:
--from terraformis the long-standing Terraform converter. Reach for it for a one-off translation of Terraform configuration you have been running with the Terraform or OpenTofu CLI.--from hclis the converter that ships alongside Pulumi's HCL runtime, and reads your configuration the same way the runtime executes it. Reach for it when you have been running your.tffiles underruntime: hcland now want that same program in another language.
First, ensure you have a Terraform configuration:
# main.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
variable "aws_region" {
description = "AWS region"
type = string
default = "us-west-2"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
data "aws_ami" "amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-2023.*-x86_64"]
}
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "main-vpc"
}
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
map_public_ip_on_launch = true
tags = {
Name = "public-subnet"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "public-rt"
}
}
resource "aws_route_table_association" "public" {
subnet_id = aws_subnet.public.id
route_table_id = aws_route_table.public.id
}
resource "aws_security_group" "web" {
name = "web-sg"
description = "Security group for web servers"
vpc_id = aws_vpc.main.id
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "web-sg"
}
}
resource "aws_instance" "web" {
ami = data.aws_ami.amazon_linux.id
instance_type = var.instance_type
subnet_id = aws_subnet.public.id
vpc_security_group_ids = [aws_security_group.web.id]
user_data = <<-EOF
#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello from Pulumi converted infrastructure!</h1>" > /var/www/html/index.html
EOF
tags = {
Name = "web-server"
}
}
output "vpc_id" {
description = "ID of the VPC"
value = aws_vpc.main.id
}
output "public_ip" {
description = "Public IP address of the web server"
value = aws_instance.web.public_ip
}
output "website_url" {
description = "URL of the website"
value = "http://${aws_instance.web.public_ip}"
}Now convert it to Pulumi:
# Convert to TypeScript
$ pulumi convert --from terraform --language typescript --out ./pulumi-converted
# Convert to Python
$ pulumi convert --from terraform --language python --out ./pulumi-converted
# Convert to Go
$ pulumi convert --from terraform --language go --out ./pulumi-converted
# Convert to C#
$ pulumi convert --from terraform --language csharp --out ./pulumi-converted
# Convert to Java
$ pulumi convert --from terraform --language java --out ./pulumi-converted
# Convert to YAML
$ pulumi convert --from terraform --language yaml --out ./pulumi-converted
# Convert with the HCL runtime's converter instead of the Terraform converter
$ pulumi convert --from hcl --language typescript --out ./pulumi-convertedConverted TypeScript example
The pulumi convert command would generate something like this:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create configuration
const config = new pulumi.Config();
const awsRegion = config.get("awsRegion") || "us-west-2";
const instanceType = config.get("instanceType") || "t3.micro";
// Configure the AWS provider
const awsProvider = new aws.Provider("aws", {
region: awsRegion,
});
// Data source for Amazon Linux AMI
const amazonLinux = aws.ec2.getAmiOutput({
mostRecent: true,
owners: ["amazon"],
filters: [{
name: "name",
values: ["al2023-ami-2023.*-x86_64"],
}],
}, {
provider: awsProvider,
});
// Create VPC
const main = new aws.ec2.Vpc("main", {
cidrBlock: "10.0.0.0/16",
enableDnsHostnames: true,
enableDnsSupport: true,
tags: {
Name: "main-vpc",
},
}, {
provider: awsProvider,
});
// Create public subnet
const publicSubnet = new aws.ec2.Subnet("public", {
vpcId: main.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
mapPublicIpOnLaunch: true,
tags: {
Name: "public-subnet",
},
}, {
provider: awsProvider,
});
// Create internet gateway
const mainIgw = new aws.ec2.InternetGateway("main", {
vpcId: main.id,
tags: {
Name: "main-igw",
},
}, {
provider: awsProvider,
});
// Create route table
const publicRt = new aws.ec2.RouteTable("public", {
vpcId: main.id,
routes: [{
cidrBlock: "0.0.0.0/0",
gatewayId: mainIgw.id,
}],
tags: {
Name: "public-rt",
},
}, {
provider: awsProvider,
});
// Associate route table with subnet
const publicRtAssociation = new aws.ec2.RouteTableAssociation("public", {
subnetId: publicSubnet.id,
routeTableId: publicRt.id,
}, {
provider: awsProvider,
});
// Create security group
const webSg = new aws.ec2.SecurityGroup("web", {
name: "web-sg",
description: "Security group for web servers",
vpcId: main.id,
ingress: [
{
description: "HTTP",
fromPort: 80,
toPort: 80,
protocol: "tcp",
cidrBlocks: ["0.0.0.0/0"],
},
{
description: "SSH",
fromPort: 22,
toPort: 22,
protocol: "tcp",
cidrBlocks: ["0.0.0.0/0"],
},
],
egress: [{
fromPort: 0,
toPort: 0,
protocol: "-1",
cidrBlocks: ["0.0.0.0/0"],
}],
tags: {
Name: "web-sg",
},
}, {
provider: awsProvider,
});
// Create EC2 instance
const webInstance = new aws.ec2.Instance("web", {
ami: amazonLinux.id,
instanceType: instanceType,
subnetId: publicSubnet.id,
vpcSecurityGroupIds: [webSg.id],
userData: `#!/bin/bash
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello from Pulumi converted infrastructure!</h1>" > /var/www/html/index.html
`,
tags: {
Name: "web-server",
},
}, {
provider: awsProvider,
});
// Outputs
export const vpcId = main.id;
export const publicIp = webInstance.publicIp;
export const websiteUrl = pulumi.interpolate`http://${webInstance.publicIp}`;Testing the converted code
Deploy and verify that the converted code produces the same infrastructure:
# Initialize the new Pulumi project
$ cd pulumi-converted
$ pulumi stack init dev
$ pulumi up
# Test the deployment
$ curl $(pulumi stack output websiteUrl)
# Compare with original Terraform outputs
$ terraform output -json > tf-outputs.json
$ pulumi stack output --json > pulumi-outputs.json
# Clean up
$ pulumi destroyVerifying conversion accuracy
After converting existing infrastructure, verify that your Pulumi program produces identical results by importing the Terraform state and running a preview.
The resource IDs below (vpc-12345, subnet-67890, i-abcdef123) are placeholders. Replace them with the actual IDs of your existing resources, which you can find in your Terraform state (for example, with terraform state show <resource>) or in your cloud provider's console. Run these commands from within your converted Pulumi project directory.
# Import individual resources by ID
$ pulumi import aws:ec2/vpc:Vpc main vpc-12345
$ pulumi import aws:ec2/subnet:Subnet public subnet-67890
$ pulumi import aws:ec2/instance:Instance web i-abcdef123
# Run preview to ensure no changes
$ pulumi preview
# Expected result: "no changes required"For anything beyond a handful of resources, import in bulk from the Terraform state file instead:
$ pulumi import --from hcl terraform.tfstateThis reads a Terraform or OpenTofu state file and imports every managed resource in its root module in one pass. Resources nested inside modules are skipped with a warning, so import those individually with the per-resource form above. The state file itself is only read — Pulumi does not adopt or reuse it, and later updates use Pulumi's own state.
Don't skip this step when you're converting production infrastructure: a preview that reports no changes is your confirmation that the Pulumi program matches the resources Terraform is already managing.
AI-assisted conversion with the Pulumi MCP server
For most conversions, use a coding agent rather than pulumi convert. The converter is deterministic, so it handles what it recognizes and leaves a TODO for the rest, including heavy for_each and dynamic blocks and module indirection, and its output keeps the structure of the HCL rather than the idioms of the language you're moving to. An agent such as Pulumi Neo, Claude Code, Cursor, or Codex converts those constructs, writes idiomatic code, and can run pulumi preview and iterate until it reports no changes. Neo already knows this workflow end to end, including migrating your Terraform state; see Migrating from Terraform for that path.
Whichever agent you use, the Pulumi MCP (Model Context Protocol) server gives it access to the Pulumi Registry, your stacks, and a convert-terraform-to-typescript prompt.
Using the Pulumi MCP server
The Pulumi MCP server works with any agent that speaks MCP, including Claude Code, Cursor, Windsurf, and Kiro. It ships a convert-terraform-to-typescript prompt that converts Terraform HCL to Pulumi TypeScript, so the agent has a Pulumi-authored starting point rather than whatever it would improvise.
Beyond conversion, the server exposes tools for listing your stacks, searching your deployed resources, reading Registry schemas, checking policy findings, and handing longer jobs to Pulumi Neo. The MCP server docs list every tool and prompt.
To set it up and use it:
Add the server to your agent. In Claude Code, that's:
bash$ claude mcp add --transport http pulumi https://mcp.ai.pulumi.com/mcpOther agents configure it differently — see Configuration in the MCP server docs.
Gather your Terraform code. Collect the configuration files you want converted (
.tf,terraform.tfvars, and any module sources they reference).Invoke the conversion prompt. Once the server is connected, attach the prompt and paste your configuration:
text@convert-terraform-to-typescript Please convert this Terraform configuration to Pulumi TypeScript: [Paste your Terraform HCL code here]
Whatever the agent produces, review it the same way you'd review the converter's output — see Review the output.
If your agent doesn't support MCP prompts
Some agents connect to MCP servers for tools but can't attach a server's prompts. In that case, describe the job yourself and let the agent read the Pulumi docs and Registry through the server's tools:
Convert this Terraform configuration to a Pulumi TypeScript program. Look up each
resource in the Pulumi Registry to get the property names and types right, use
pulumi.Config for the variables, and export the Terraform outputs as stack outputs.
[Paste your Terraform HCL code here]Review the output
Any time you use an automated conversion tool, review and validate what it produced. Some things to check for:
- Configuration values read through
pulumi.Configwith the defaults the HCL declared - Resource properties that carried over completely, including tags and labels
- Idiomatic resource definitions for the language you converted to
- Error handling around anything the converter couldn't translate directly
Best practices for conversion
- Start small: Convert smaller configurations first to understand the process
- Verify outputs: Ensure converted code produces identical infrastructure
- Test thoroughly: Write tests for critical infrastructure components
- Preserve structure: Keep similar resource organization when possible
- Document changes: Note any differences between original and converted code
- Version control: Use Git to track conversion changes