Use Terraform modules in Pulumi
This guide will walk you through the process of using existing Terraform modules directly in your Pulumi programs, allowing you to leverage the vast Terraform module ecosystem.
INFO
Prerequisites:
- The Pulumi CLI
- One of Pulumi's supported language runtimes installed
- Access to a cloud provider account for deployment (e.g., AWS, Azure, Google Cloud)
Why use Terraform modules in Pulumi?
Terraform has a mature ecosystem with thousands of modules available in the Terraform Registry. These modules encapsulate well-tested infrastructure patterns that you might want to leverage in your Pulumi projects without having to rewrite them.
Also, many Terraform users have created their own custom modules and would like to avoid re-writing them in order to use Pulumi. Using Terraform modules directly within Pulumi allows you to use Terraform and Pulumi side-by-side, enabling the two technologies to coexist, taking a slower gentler migration path from Terraform to Pulumi. This is especially powerful in larger organizations, where some teams prefer Pulumi's workflows, and others prefer to continue using Terraform.
Key benefits
- Leverage Existing Modules: Use the rich ecosystem of Terraform modules directly in Pulumi.
- Migrate Gradually: Incrementally migrate from Terraform to Pulumi without rewriting everything at once.
- Consistency: Maintain consistency across teams that may be using a mix of Terraform and Pulumi.
How it works
The Any HCL Module package allows you to consume Terraform modules as if they were native Pulumi packages. It works by:
- Automatically installing and managing OpenTofu (an open-source Terraform-compatible implementation) to execute the module.
- Passing the inputs you provide to the module as Terraform variables.
- Managing state through your standard Pulumi state backend.
- Exposing module outputs as native Pulumi outputs.
Get started
Add a Terraform module to your Pulumi project
To use a Terraform module in Pulumi, first add it to your project using the pulumi package add command:
pulumi package add hcl module <module-source> [<version>]Where:
<module-source>is either a registry module identifier (e.g.terraform-aws-modules/rds/aws) or a local path<version>is an optional version constraint (e.g.3.5.0)
For example, to add the AWS S3 bucket module from the Terraform Registry:
pulumi package add hcl module terraform-aws-modules/s3-bucket/aws 4.1.2This will generate a local SDK in your programming language that you can import into your Pulumi program.
TIP
See Local SDKs for details on generating and using SDKs from local or parameterized providers.
Use a local Terraform module
You can also use local Terraform modules:
pulumi package add hcl module ./path/to/moduleAny directory containing .tf files and optionally variables.tf and outputs.tf is considered a valid module.
Use a module from Pulumi Cloud
If your organization publishes Terraform modules to the Pulumi Cloud registry, every published version is converted into a Pulumi package for you. Install it by package name, which is the module's name and system joined with a hyphen. The system is the last segment of the module's address, naming what the module provisions, such as aws or azurerm:
pulumi package add <name>-<system>[@<version>]A module published as acme-corp/vpc/aws installs as vpc-aws. This is the same as any other Pulumi package: you get a generated SDK in your language, an API reference on the package's page, and usage tracking showing which of your stacks depend on it and which are behind the latest version. Installing a converted package requires Pulumi CLI 3.248.0 or newer; see Download & Install Pulumi to upgrade.
The package's page in Pulumi Cloud shows whether a given version has converted.
See Terraform Modules in the Pulumi Cloud Registry for the publishing side and the broader module workflow.
Example: AWS S3 bucket module
Here's an example of how to use the AWS S3 bucket module to create a bucket in your Pulumi program.
First, add the module to your project:
$ pulumi package add hcl module terraform-aws-modules/s3-bucket/aws 4.1.2After adding the package, your Pulumi.yaml is updated with the module definition:
Example: Pulumi.yaml
name: s3-bucket-example
runtime:
name: nodejs
options:
packagemanager: npm
packages:
s3-bucket-aws:
source: hcl
version: 0.19.1
parameters:
- module
- terraform-aws-modules/s3-bucket/aws
- 4.1.2The module creates AWS resources, so the stack needs an AWS region. If AWS_REGION isn't set in your environment, set one for the stack:
$ pulumi config set aws:region us-west-2Since this was a TypeScript project, Pulumi generated a TypeScript SDK for the module, making it available as @pulumi/s3-bucket-aws. We can now use the Terraform module directly in our code:
Example: index.ts
import * as s3BucketAws from "@pulumi/s3-bucket-aws";
// Create an S3 bucket using the Terraform module
const bucket = new s3BucketAws.Module("my-bucket", {
bucketPrefix: "my-example-bucket",
tags: {
Environment: "dev",
},
});
// The module's outputs are strongly typed
export const bucketArn = bucket.s3BucketArn;
export const bucketId = bucket.s3BucketId;In the above code, the imported Terraform module works the same as any other Pulumi code. Outputs are returned, and resource state is stored in your Pulumi state storage, alongside all your other Pulumi-native resources. This also means that resource dependencies work as expected between Pulumi-native resources and resources created by Terraform modules.
Load a module at runtime
Generating an SDK gives you strongly typed inputs and outputs, IDE completion, and a package you can pin and share across your team. When you'd rather trade that type safety for flexibility — for example, to load a module whose address isn't known until runtime — you can use the Any HCL Module package directly.
Add the Any HCL Module package to your project:
$ pulumi package add hclThen use its Module resource to load the module you want. The constructor takes a module source, an optional version, and a map of inputs, and exposes the module's outputs as an untyped map:
import * as pulumi from "@pulumi/pulumi";
import * as hcl from "@pulumi/hcl";
const bucket = new hcl.Module("bucket", {
source: "terraform-aws-modules/s3-bucket/aws",
version: "4.1.2",
inputs: {
bucket_prefix: "my-example-bucket",
},
});
export const bucketArn = bucket.outputs.apply(o => o["s3_bucket_arn"]);Compare with Terraform
The same functionality in Terraform would look like:
# Terraform equivalent
module "s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
bucket_prefix = "my-example-bucket"
tags = {
Environment = "dev"
}
}
output "bucket_arn" {
value = module.s3_bucket.s3_bucket_arn
}
output "bucket_id" {
value = module.s3_bucket.s3_bucket_id
}Best practices
- Pin module versions: Always specify module versions in production
- Review module source: Understand what the module does before using it
- Test module outputs: Verify that module outputs work as expected
- Monitor module updates: Keep track of module updates and breaking changes
- Use reputable sources: Prefer well-maintained modules from trusted sources
- Document module usage: Document why you chose specific modules and their configuration
- Use
pulumi installfor setup: When cloning a project that uses Terraform modules, runpulumi installto install all dependencies, including local SDKs defined inPulumi.yaml
Configure Terraform providers
A Terraform module runs against your project's default provider configuration — the same configuration your Pulumi-native resources use — so it inherits whatever region, account, or credentials you've already set. To target a specific region, for example, set it the way you would for any Pulumi provider:
$ pulumi config set aws:region us-west-2You can also supply provider settings, including short-lived credentials, through a Pulumi ESC environment. See Providers for the full range of configuration options.
Troubleshooting
Fix invalid relative paths
When a module accepts a file path, pass an absolute path instead of a relative one. For example, the AWS Lambda module accepts a source_path that points to your function's code:
import * as hcl from "@pulumi/hcl";
const lambdaModule = new hcl.Module("my-lambda", {
source: "terraform-aws-modules/lambda/aws",
inputs: {
function_name: "my-function",
handler: "index.handler",
runtime: "nodejs20.x",
source_path: `${process.cwd()}/src`,
},
});This is necessary because Terraform modules run from a different working directory than your Pulumi program, so a relative path would resolve incorrectly. The example above uses the Node.js built-in process.cwd() to anchor the path to the function code in src to your project's working directory.