Run the Teleport Terraform Provider with Long-Lived Credentials
This guide explains how to create a Terraform user and have the Teleport Auth Service sign long-lived credentials for it. The Teleport Terraform Provider can then use those credentials to interact with Teleport.
How it works
A Teleport administrator defines a role for the Teleport Terraform provider, as
well as a role that can
impersonate
the Terraform provider role. A Teleport user assumes the impersonator role and
executes a tctl command to instruct the Teleport Auth Service to sign a user
certificate for the Terraform provider. The provider then loads the certificate
in order to authenticate to your Teleport cluster and manage resources on the
Teleport Auth Service backend.
Long-lived credentials are less secure than other Teleport credentials and their usage is discouraged.
You must protect and rotate the credentials as they hold full Teleport
administrative access. You should prefer using tbot,
native Machine & Workload Identity joining in CI or Cloud
environments, or create temporary bots for local use when
possible.
See the list of possible Terraform provider setups to find which one fits your use-case.
Long-lived credentials are not compatible with MFA for administrative actions (MFA4A) which is an additional security layer that protects Teleport in case of Identity Provider (IdP) compromise.
Prerequisites
-
A running Teleport cluster accessible at a hostname with a valid TLS certificate. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.
-
The
tctlandtshclients.Installing
tctlandtshclients-
Determine the version of your Teleport cluster. The
tctlandtshclients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at/v1/webapi/findand use a JSON query tool to obtain your cluster version. Replace teleport.example.com:443 with the web address of your Teleport Proxy Service:- Mac/Linux
- Windows - Powershell
TELEPORT_DOMAIN=teleport.example.com:443TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')"$TELEPORT_DOMAIN = "teleport.example.com:443"$TELEPORT_VERSION = (Invoke-RestMethod -Uri "https://${TELEPORT_DOMAIN}/v1/webapi/find").server_version -
Follow the instructions for your platform to install
tctlandtshclients:- Mac
- Windows - Powershell
- Linux
Download the signed macOS .pkg installer for Teleport, which includes the
tctlandtshclients:curl -O https://cdn.teleport.dev/teleport-${TELEPORT_VERSION?}.pkgIn Finder double-click the
pkgfile to begin installation.dangerUsing Homebrew to install Teleport is not supported. The Teleport package in Homebrew is not maintained by Teleport and we can't guarantee its reliability or security.
curl.exe -O https://cdn.teleport.dev/teleport-v$TELEPORT_VERSION-windows-amd64-bin.zipUnzip the archive and move the `tctl` and `tsh` clients to your %PATH%
NOTE: Do not place the `tctl` and `tsh` clients in the System32 directory, as this can cause issues when using WinSCP.
Use %SystemRoot% (C:\Windows) or %USERPROFILE% (C:\Users\<username>) instead.
All of the Teleport binaries in Linux installations include the
tctlandtshclients. For more options (including RPM/DEB packages and downloads for i386/ARM/ARM64) see our installation page.curl -O https://cdn.teleport.dev/teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gztar -xzf teleport-v${TELEPORT_VERSION?}-linux-amd64-bin.tar.gzcd teleportsudo ./installTeleport binaries have been copied to /usr/local/bin
Connecting with TLS routing disabled
This guide's commands assume your Teleport cluster uses TLS routing (
proxy_listener_mode: multiplex), where thetctlandtshclients reach every Teleport service through the Proxy Service's web address on port443. If you're not sure whether this applies to your cluster, check with whoever manages it.If your cluster uses separate listener ports instead, adjust ports as follows:
-
tshcommands (e.g.,tsh login --proxy=...): continue using the Proxy Service web address on port3080(or443if behind a load balancer). Do not change these to port3025. -
Direct
tctlor Auth Service API commands: use port3025for the Auth Service gRPC listener:tctl status --auth-server=teleport.example.com:3025
-
-
terraform version
Terraform v1.0.0
-
Check that you can connect to your Teleport cluster and verify that you can run
tctlandtshcommands using your current credentials.-
Assign teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and email@example.com to your Teleport username.
-
Authenticate to your Teleport cluster. This depends on whether your shell is interactive or not.
In an interactive shell: Run the following command. By default, this triggers a multi-factor authentication prompt:
tsh login --proxy=teleport.example.com --user=email@example.comtctl statusCluster teleport.example.com
Version 18.10.0
CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678
On non-interactive environments: If you are running
tshandtctlas an AI agent, in a CI/CD environment, or similar, make sure theTELEPORT_IDENTITY_FILEenvironment variable is assigned to a valid file path with credentials for your cluster.tshandtctlread the file path from the environment variable and do not require a separate authentication step. If there is no identity file available, we recommend that you set up Machine ID to provision one automatically.When executing
tctlcommands with an identity file, you must pass the--auth-serverflag to provide the Teleport Auth Service address, which is not included in the identity file. If you provide the Proxy Service address,tctlconnects to the Proxy Service, which forwards traffic to and from the Teleport Auth Service. Update 443 to3025if you are contacting the Auth Service directly withtctl:tctl status --auth-server=teleport.example.com:443For
tshcommands that read an identity file, you must pass the--proxyflag, which pointstshto the address of the Teleport Proxy Service:tsh status --proxy=teleport.example.comEnsure client commands can access your identity file. Replace path/to/identity/file with the path to your identity file:
export TELEPORT_IDENTITY_FILE="${TELEPORT_IDENTITY_FILE:-path/to/identity/file}"Add the
--auth-serveror--proxyflags to all subsequenttctlandtshcommands.
If you can connect to the cluster and run the
tctl statuscommand, you can use your current credentials to run subsequenttctlcommands from your workstation. If you host your own Teleport cluster, you can also runtctlcommands on the computer that hosts the Teleport Auth Service for full permissions. -
Step 1/3. Create Teleport credentials for Terraform
Terraform needs a signed identity file from the Teleport cluster certificate authority to manage resources in the cluster. You will create a local Teleport user for this purpose.
-
Create a folder called
teleport-terraformto hold temporary files:mkdir -p teleport-terraformcd teleport-terraform -
Create a new file called
terraform.yamland open it in an editor. -
Configure settings for a local Teleport user and role by pasting the following content into the
terraform.yamlfile:kind: user metadata: name: terraform spec: roles: ['terraform-provider'] version: v2Starting with 16.2, Teleport comes with a built-in role for the Terraform provider:
terraform-provider.RBAC for versions before v16.2
On older versions you will need to create the Terraform role yourself. Write the following
role.yamlmanifest:kind: role version: v7 metadata: name: terraform-provider spec: allow: db_labels: '*': '*' app_labels: '*': '*' node_labels: '*': '*' rules: - resources: - app - cluster_auth_preference - cluster_networking_config - db - device - github - login_rule - oidc - okta_import_rule - role - saml - session_recording_config - token - trusted_cluster - user - access_list - node verbs: ['list','create','read','update','delete']Use
tctl create -f ./role.yamlto create the role.These settings configure a user and role named
terraformwith the permissions required to manage resources in your Teleport cluster. -
Create the
terraformuser and role by running the following command:tctl create terraform.yamlThe
terraformuser can't sign in to get credentials, so you must have another user impersonate theterraformaccount to request a certificate. -
Create a new file called
terraform-impersonator.yamland open it in an editor. -
Configure a role that enables your user to impersonate the Terraform user by pasting the following content into the
terraform-impersonator.yamlfile:kind: role version: v7 metadata: name: terraform-impersonator spec: allow: # This impersonate role allows any user assigned to this role to impersonate # and generate certificates for the user named "terraform" with a role also # named "terraform". impersonate: users: ['terraform'] roles: ['terraform-provider'] -
Create the
terraform-impersonatorrole by running the following command:tctl create terraform-impersonator.yaml -
Assign the
terraform-impersonatorrole to your Teleport user. The commands to run depend on how you authenticate to Teleport, that is, whether you have a local Teleport user, a single sign-on authentication connector, or a non-interactive user:- Local User
- GitHub
- SAML
- OIDC
- Non-Interactive
-
Retrieve your local user's roles as a comma-separated list:
ROLES=$(tsh status -f json | jq -r '.active.roles | join(",")') -
Edit your local user to add the new role:
tctl users update $(tsh status -f json | jq -r '.active.username') \ --set-roles "${ROLES?},terraform-impersonator" -
Sign out of the Teleport cluster and sign in again to assume the new role.
-
Open your
githubauthentication connector in a text editor:tctl edit github/github -
Edit the
githubconnector, addingterraform-impersonatorto theteams_to_rolessection.The team you should map to this role depends on how you have designed your organization's role-based access controls (RBAC). However, the team must include your user account and should be the smallest team possible within your organization.
Here is an example:
teams_to_roles: - organization: octocats team: admins roles: - access + - terraform-impersonator -
Apply your changes by saving and closing the file in your editor.
-
Sign out of the Teleport cluster and sign in again to assume the new role.
-
Retrieve your
samlconfiguration resource:tctl get --with-secrets saml/mysaml > saml.yamlNote that the
--with-secretsflag adds the value ofspec.signing_key_pair.private_keyto thesaml.yamlfile. Because this key contains a sensitive value, you should remove the saml.yaml file immediately after updating the resource. -
Edit
saml.yaml, addingterraform-impersonatorto theattributes_to_rolessection.The attribute you should map to this role depends on how you have designed your organization's role-based access controls (RBAC). However, the group must include your user account and should be the smallest group possible within your organization.
Here is an example:
attributes_to_roles: - name: "groups" value: "my-group" roles: - access + - terraform-impersonator -
Apply your changes:
tctl create -f saml.yaml -
Sign out of the Teleport cluster and sign in again to assume the new role.
-
Retrieve your
oidcconfiguration resource:tctl get oidc/myoidc --with-secrets > oidc.yamlNote that the
--with-secretsflag adds the value ofspec.signing_key_pair.private_keyto theoidc.yamlfile. Because this key contains a sensitive value, you should remove the oidc.yaml file immediately after updating the resource. -
Edit
oidc.yaml, addingterraform-impersonatorto theclaims_to_rolessection.The claim you should map to this role depends on how you have designed your organization's role-based access controls (RBAC). However, the group must include your user account and should be the smallest group possible within your organization.
Here is an example:
claims_to_roles: - name: "groups" value: "my-group" roles: - access + - terraform-impersonator -
Apply your changes:
tctl create -f oidc.yaml -
Sign out of the Teleport cluster and sign in again to assume the new role.
For non-interactive users such as AI agents and CI/CD runners, follow these steps.
If you are using Machine ID to issue identity files to your user:
-
Add the new role to your bot. Assign mybot to the name of the Machine ID bot that issues your certificate:
tctl bots update mybot --add-roles terraform-impersonator -
Force
tbotto regenerate the certificate:pkill -HUP tbotThe bot will also generate a certificate with the new role on its next renewal.
Otherwise, update your user and regenerate your identity file:
-
Retrieve your local user's roles as a comma-separated list. Make sure username is the Teleport user you want to assign the new role to.
ROLES=$(tctl get user/username --format=json | jq -r '.[].spec.roles | join(",")') -
Edit your local user to add the new role:
tctl users update username \ --set-roles "${ROLES?},terraform-impersonator" -
Regenerate the identity file. Make sure path/to/identity/file corresponds to the correct output path:
tctl auth sign \ --user=username \ --out=path/to/identity/file \ --overwrite
-
Request a signed identity file for the Terraform user by running the following command:
tctl auth sign --user=terraform --out=terraform-identityAfter running this command, you have a
terraform-identityfile with credentials for the Terraform user.
Step 2/3. Prepare a Terraform configuration file
To prepare a Terraform configuration file:
-
Create a new file called
main.tfand open it in an editor. -
Define an example user and role using Terraform by pasting the following content into the
main.tffile, replacing teleport.example.com:443 with the host and port of the Teleport Proxy Service:terraform { required_providers { teleport = { source = "terraform.releases.teleport.dev/gravitational/teleport" version = "~> 18.0" } } } provider "teleport" { addr = 'teleport.example.com:443' identity_file_path = "terraform-identity" } # We must create a test role, if we don't declare resources, Terraform won't try to # connect to Teleport and we won't be able to validate the setup. resource "teleport_role" "test" { version = "v7" metadata = { name = "test" description = "Dummy role to validate Terraform Provider setup" labels = { test = "yes" } } spec = {} }
Step 3/3. Apply the configuration
To apply the configuration:
-
Check the contents of the
teleport-terraformfolder:lsmain.tf terraform-identity terraform-impersonator.yaml terraform.yaml
-
Initialize the working directory that contains Terraform configuration files by running the following command:
terraform initInitializing the backend...
Initializing provider plugins...- Finding terraform.releases.teleport.dev/gravitational/teleport versions matching ... -
Execute the Terraform plan defined in the configuration file by running the following command:
terraform applyTerraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:+ create
Terraform will perform the following actions:teleport_role.test will be created
+ resource "teleport_role" "test" { + id = (known after apply) + kind = (known after apply) + metadata = { + name = "test" + namespace = (known after apply) } + spec = {} + version = "v7" }
Plan: 1 to add, 0 to change, 0 to destroy.
Next steps
- Explore the full list of supported Terraform provider resources.
- Learn how to manage users and roles with IaC
- Read more about impersonation.