This repository is the template for authoring a Pulumi package from an existing Terraform provider as part of the guide for authoring and publishing Pulumi packages.
This repository is initially set up as a fictitious provider named "xyz" to demonstrate a resource, a data source and configuration derived from the github.com/pulumi/terraform-provider-xyz provider.
Ensure the following tools are installed and present in your $PATH
:
pulumictl
- Go or 1.latest
golangci-lint
- NodeJS Active or maintenance version (Node.js Releases). We recommend using nvm to manage NodeJS installations.
- Yarn
- TypeScript
- Python (called as
python3
). For recent versions of MacOS, the system-installed version is fine. - .NET
Pulumi offers this repository as a GitHub template repository for convenience. From this repository:
- Click "Use this template".
- Follow the new repository options:
- Owner: select your GitHub organization or username
- Repository name: A name prefixed with
pulumi-
as is the standard convention for Pulumi providers - Description: E.g. "Pulumi provider for ..."
- Repository type: This must be Public to be published to the Pulumi registry
- Clone your new repository ready for development.
From the templated repository run the init program and follow the prompts to replace the name "xyz" with the name of your provider:
./setup.sh
Tip
This program can also be run non-interactively, passing the options via arguments. Run ./setup.sh -h
for the usage instructions.
Pulumi provider repositories have the following general structure:
examples/
sample code which may optionally be included as integration tests to be run as part of a CI/CD pipelineprovider/
Go code used to create the provider as well as generate the SDKs in the various languages that Pulumi supportsprovider/cmd/pulumi-tfgen-xyz
executable program to generate the Pulumi resource schemaprovider/cmd/pulumi-resource-xyz
provider plugin executableprovider/resources.go
Terraform-to-Pulumi mappings and configuration
sdk/
generated SDKs for each language in a separate subfolder
-
Review the updated
README.md
to include the following:- Short introductory paragraph describing the type of resources the provider manages.
- In the "Installing" section, correct package names for the various SDK libraries in the languages Pulumi supports.
- In the "Configuration" section, any configurable options for the provider. These may include, but are not limited to, environment variables or options that can be set via
pulumi config set
. - In the "Reference" section, provide a link to the to-be-published documentation.
-
Update the
DisplayName
,Publisher
, andHomepage
values inprovider/resources.go
. -
Verify the upstream Terraform provider module import in the
provider/go.mod
andprovider/resources.go
are correct.- If the name of the desired Pulumi provider differs from the name of the Terraform provider, you will need to carefully distinguish between the references - see https://github.com/pulumi/pulumi-azure for an example.
-
Verify and download dependencies in the
provider/go.mod
:(cd provider && go mod tidy)
-
Generate the provider's schema:
make tfgen
Tip
Take note of warnings & errors in the output when generating the schema.
Note
Most providers can use automatic token mapping which generates the bridge-metadata.json
file. If you have warnings about unmapped resources in the step above, refer to the section on Manual Mappings.
If you need to customise the behaviour of the provider configuration, refer to the section Customise Provider Configuration.
-
Build the provider binary and ensure there are no warnings about unmapped resources and no warnings about unmapped data sources:
make provider
You may see warnings about documentation and examples, including "unexpected code snippets". These can be safely ignored for now. Pulumi will add additional documentation on mapping docs in a future revision of this guide.
-
Build the SDKs in the various languages Pulumi supports:
make build_sdks
-
Ensure the Golang SDK is a proper go module:
cd sdk && go mod tidy && cd -
This will pull in the correct dependencies in
sdk/go.mod
as well as setting the dependency tree insdk/go.sum
. -
Finally, ensure the provider code conforms to Go standards:
make lint_provider
Some issues found by the linter can be fixed automatically by running
make lint_provider.fix
.
Note
If you make revisions to code in resources.go
, you must re-run the make tfgen
target to regenerate the schema.
In this section, we will create a Pulumi program in TypeScript that utilizes the provider we created to ensure everything is working properly.
-
Create an account with the provider's service and generate any necessary credentials, e.g. API keys.
- Email: [email protected]
- Password: (Create a random password in 1Password with the maximum length and complexity allowed by the provider.)
- Ensure all secrets (passwords, generated API keys) are stored in Pulumi's 1Password vault.
-
Copy the
pulumi-resource-xyz
binary generated bymake provider
and place it in your$PATH
($GOPATH/bin
is a convenient choice), e.g.:cp bin/pulumi-resource-xyz $GOPATH/bin
-
Tell Yarn to use your local copy of the SDK:
make install_nodejs_sdk
-
Create a new Pulumi program in the
examples/
directory, e.g.:mkdir examples/my-example/ts # Change "my-example" to something more meaningful. cd examples/my-example/ts pulumi new typescript # (Go through the prompts with the default values) npm install yarn link @pulumi/xyz
-
Create a minimal program for the provider, i.e. one that creates the smallest-footprint resource. Place this code in
index.ts
. -
Configure any necessary environment variables for authentication, e.g
$XYZ_USERNAME
,$XYZ_TOKEN
, in your local environment. -
Ensure the program runs successfully via
pulumi up
. -
Once the program completes successfully, verify the resource was created in the provider's UI.
-
Destroy any resources created by the program via
pulumi destroy
.
Integration tests are Go tests residing in examples/*_test.go
. Each test executes a program from a subdirectory within examples/
. All tests are initially marked to be skipped as the example xyz
provider is not fully implemented and will fail with "not implemented".
-
Update an example to update to use a resource generated from your provider SDK (e.g. update the code in
index.ts
ofexamples/basic-ts
) -
Locate the Go test for the example (e.g.
examples/basic-ts
is tested byTestBasicTs
inexamples/examples_nodejs_test.go
) -
Remove the "skip" directive on the first line:
t.Skip(...)
-
Run the tests:
make test
You can also run each test file separately via test tags:
(cd examples && go test -v -tags=nodejs)
-
Repeat the process for each language and add new examples and tests as needed.
This template utilizes Pulumi's ci-mgmt templates to automatically generate CI workflows and other build tooling.
This is configured via the .ci-mgmt.yaml
file and can be re-generated by running make ci-mgmt
which also updates to the latest version of the tool.
The default configuration is designed for third-party use and will need to be adjusted for Pulumi internal providers.
-
Ensure all required configurations (API keys, etc.) are documented in README-PROVIDER.md.
-
Delete the boilerplate's
SETUP.md
andsetup.sh
files. -
Register your provider in the Pulumi Registry. See Publishing a Community Package on the Registry.
Now you are ready to use the provider, cut releases, and have some well-deserved 🍨!
In this section we will add the mappings that allow the interoperation between the Pulumi provider and the Terraform provider. Terraform resources map to an identically named concept in Pulumi. Terraform data sources map to plain old functions in your supported programming language of choice. Pulumi also allows provider functions and resources to be grouped into namespaces to improve the cohesion of a provider's code, thereby making it easier for developers to use. If your provider has a large number of resources, consider using namespaces to improve usability.
The following instructions all pertain to provider/resources.go
, in the section of the code where we construct a tfbridge.ProviderInfo
object:
-
Add resource mappings: For each resource in the provider, add an entry in the
Resources
property of thetfbridge.ProviderInfo
, e.g.:// Most providers will have all resources (and data sources) in the main module. // Note the mapping from snake_case HCL naming conventions to UpperCamelCase Pulumi SDK naming conventions. // The name of the provider is omitted from the mapped name due to the presence of namespaces in all supported Pulumi languages. "xyz_something": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "Something")}, "xyz_something_else": {Tok: tfbridge.MakeResource(mainPkg, mainMod, "SomethingElse")},
-
Add CSharpName (if necessary): Dotnet does not allow for fields named the same as the enclosing type, which sometimes results in errors during the dotnet SDK build. If you see something like
error CS0542: 'ApiKey': member names cannot be the same as their enclosing type [/Users/guin/go/src/github.com/pulumi/pulumi-artifactory/sdk/dotnet/Pulumi.Artifactory.csproj]
you'll want to give your Resource a CSharpName, which can have any value that makes sense:
"xyz_something": { Tok: tfbridge.MakeResource(mainPkg, mainMod, "Something"), Fields: map[string]*tfbridge.SchemaInfo{ "something": { CSharpName: "SomethingField", }, }, },
-
Add data source mappings: For each data source in the provider, add an entry in the
DataSources
property of thetfbridge.ProviderInfo
, e.g.:// Note the 'get' prefix for data sources "xyz_something": {Tok: tfbridge.MakeDataSource(mainPkg, mainMod, "getSomething")}, "xyz_something_else": {Tok: tfbridge.MakeDataSource(mainPkg, mainMod, "getSomethingElse")},
-
Add documentation mapping (sometimes needed): If the upstream provider's repo is not a part of the
terraform-providers
GitHub organization, specify theGitHubOrg
property oftfbridge.ProviderInfo
to ensure that documentation is picked up by the codegen process, and that attribution for the upstream provider is correct, e.g.:GitHubOrg: "my-gh-org",
Pulumi's Terraform bridge automatically detects configuration options for the upstream provider. However, in rare cases these settings may need to be overridden, e.g. if we want to change an environment variable default from API_KEY
to XYZ_API_KEY
. Examples of common uses cases:
"additional_required_parameter": {},
"additional_optional_string_parameter": {
Default: &tfbridge.DefaultInfo{
Value: "default_value",
},
"additional_optional_boolean_parameter": {
Default: &tfbridge.DefaultInfo{
Value: true,
},
// Renamed environment variables can be accounted for like so:
"apikey": {
Default: &tfbridge.DefaultInfo{
EnvVars: []string{"XYZ_API_KEY"},
},
If you receive the following error: use of internal package github.com/example/terraform-provider-example/internal/provider not allowed
, you need to use
the shim model below, and replace the example item:
mkdir -p provider/shim
cat <<-EOF> provider/shim/go.mod
module github.com/example/terraform-provider-example/shim
go 1.18
require github.com/hashicorp/terraform-plugin-sdk/v2 v2.22.0
require github.com/example/terraform-provider-example v1.0.0
EOF
cat <<-EOF> provider/shim/shim.go
package shim
import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/example/terraform-provider-example/internal/provider"
)
// fix provider.Provider here to match whats in internal/provider
func Provider() *schema.Provider {
return provider.Provider()
}
EOF
cd provider/shim/ && go mod tidy && cd ../../
cat <<EOF>> provider/go.mod
replace github.com/example/terraform-provider-example/shim => ./shim
require github.com/example/terraform-provider-example/shim v0.0.0
EOF
cd provider && go mod tidy