• Overview
    • Enforce Policy as Code
    • Infrastructure as Code
    • Inject Secrets into Terraform
    • Integrate with Existing Workflows
    • Manage Kubernetes
    • Manage Virtual Machine Images
    • Multi-Cloud Deployment
    • Network Infrastructure Automation
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
  • Registry
  • Tutorials
    • About the Docs
    • Intro to Terraform
    • Configuration Language
    • Terraform CLI
    • Terraform Cloud
    • Terraform Enterprise
    • Provider Use
    • Plugin Development
    • Registry Publishing
    • Integration Program
    • Terraform Tools
    • CDK for Terraform
    • Glossary
  • Community
GitHubTerraform Cloud
Download

    CDK for Terraform

  • Overview
  • Get Started
    • Architecture
    • HCL Interoperability
    • Constructs
    • Providers and Resources
    • Modules
    • Data Sources
    • Variables and Outputs
    • Functions
    • Remote Backends
    • Aspects
    • Assets
    • Tokens
    • Stacks
  • Examples and Guides
    • Project Setup
    • Configuration File
    • Best Practices
    • Environment Variables
    • Remote Templates
    • AWS Adapter [preview]
    • Unit Tests
    • Debugging
    • CLI Configuration
    • Commands
    • Overview
    • Upgrading to Version 0.6
    • Upgrading to Version 0.7
    • Upgrading to Version 0.9
    • Upgrading to Version 0.10
    • Upgrading to Version 0.11
  • Community
  • Telemetry
  • Other Docs

  • Intro to Terraform
  • Configuration Language
  • Terraform CLI
  • Terraform Cloud
  • Terraform Enterprise
  • Provider Use
  • Plugin Development
  • Registry Publishing
  • Integration Program
  • Terraform Tools
  • CDK for Terraform
  • Glossary
Type '/' to Search

»Tokens

Tokens represent values that are unknown until Terraform applies your configuration. For example, names of cloud resources are only assigned upon creation.

Some attributes specified using CDK for Terraform (CDKTF) may not directly map to the values required for Terraform configurations. You can use Tokens to cast these attributes to the correct Terraform language syntax.

»Use Tokens

You may need to use Tokens for:

  • Module outputs for boolean, string, lists, maps, and other complex types.
  • Resource attributes (such as id).
  • Terraform outputs based on resource attributes.

»Example

An EKS module requires a list of subnet ids in order to create a cluster. The VPC module outputs a list of subnets.

To pass the subnet id list to the EKS module, you can use publicSubnetsOutput to retrieve the list from the VPC. However, the subnets attribute requires a list of strings. Use Token.asList(vpc.publicSubnetsOutput) to cast the interpolated module output as a list of strings.

const logRetention = new TerraformVariable(this, "logRetentionInDays", {
  type: "number",
});

const vpc = new Vpc(this, vpcName, {
  name: vpcName,
  publicSubnets: ["10.0.1.0/24", "10.0.2.0/24"],
});

new Eks(this, "EksModule", {
  clusterName: "my-kubernetes-cluster",
  subnets: Token.asList(vpc.publicSubnetsOutput),
  clusterLogRetentionInDays: logRetention.numberValue,
});
const logRetention = new TerraformVariable(this, "logRetentionInDays", {
  type: "number",
});

const vpc = new Vpc(this, vpcName, {
  name: vpcName,
  publicSubnets: ["10.0.1.0/24", "10.0.2.0/24"],
});

new Eks(this, "EksModule", {
  clusterName: "my-kubernetes-cluster",
  subnets: Token.asList(vpc.publicSubnetsOutput),
  clusterLogRetentionInDays: logRetention.numberValue,
});

Initially, CDKTF will resolve Token.asList(vpc.publicSubnetsOutput) to ["#{TOKEN[TOKEN.9]}"] and logRetention.numberValue to a big negative number like -123828381238238. Later in synthesis, CDKTF will resolve the token to ${module.<module id>.public_subnets} and ${var.logRetentionInDays}.

{
  "module": {
    "helloterraEksModule5DDB67AE": {
      "cluster_name": "my-kubernetes-cluster",
      "subnets": "${module.helloterraMyVpc62D94C17.public_subnets}"
    }
  }
}
{
  "module": {
    "helloterraEksModule5DDB67AE": {
      "cluster_name": "my-kubernetes-cluster",
      "subnets": "${module.helloterraMyVpc62D94C17.public_subnets}"
    }
  }
}

Refer to the AWS CDK documentation for more detailed information about tokens.

github logoEdit this page
  • Overview
  • Docs
  • Extend
  • Privacy
  • Security
  • Press Kit
  • Consent Manager