Terraform can understand configurations written in either HashiCorp Configuration Language (HCL) syntax or JSON. Because neither of these is a programming language, Terraform has developed ways to enable users to request and publish named values. These are:
You may need to occasionally use these elements in your CDK for Terraform (CDKTF) application instead of passing data through the conventions available in your preferred programming language.
You can define Terraform variables as input parameters to customize stacks and modules. For example, rather than hardcoding the number and type of AWS EC2 instances to provision, you can define a variable that lets users change these parameters based on their needs.
When to use Input VariablesVariables are useful when you plan to synthesize your CDKTF application into a JSON configuration file for Terraform. For example, when you are planning to store configurations and run Terraform inside HCP Terraform.
If you plan to use CDKTF to manage your infrastructure, we recommend using your language's APIs to consume the data you would normally pass through Terraform variables. You can read from disk (synchronously) or from the environment variables, just as you would in any normal program.
Important: The synthesized Terraform configuration will contain any values that you pass directly to CDKTF constructs. This includes credentials and any other sensitive data provided as input for the cdktf
application. If you plan to commit the generated cdk.tf.json
to version control, use input variables for secrets instead.
You must specify values in exactly the same way as you would in an HCL configuration file. Refer to the Terraform variables documentation for details. The CDKTF CLI currently also supports configuration via environment variables.
The following example uses TerraformVariable
to provide inputs to resources.
const imageId = new TerraformVariable(this, "imageId", {
type: "string",
default: "ami-abcde123",
description: "What AMI to use to create an instance",
});
new Instance(this, "hello", {
ami: imageId.value,
instanceType: "t2.micro",
});
TerraformVariable imageId = new TerraformVariable(this, "imageId", TerraformVariableConfig.builder()
.type("string")
.defaultValue("ami-abcde123")
.description("What AMI to use to create an instance")
.build()
);
new Instance(this, "hello", InstanceConfig.builder()
.ami(imageId.getStringValue())
.instanceType("t2.micro")
.build()
);
imageId = TerraformVariable(self, "imageId",
type = "string",
default = "ami-abcde123",
description = "What AMI to use to create an instance"
)
Instance(self, "hello",
ami = imageId.string_value,
instance_type = "t2.micro"
)
TerraformVariable imageId = new TerraformVariable(this, "imageId", new TerraformVariableConfig
{
Type = "string",
Default = "ami-abcde123",
Description = "What AMI to use to create an instance",
});
new Instance(this, "hello", new InstanceConfig
{
Ami = imageId.StringValue,
InstanceType = "t2.micro",
});
imageId := cdktf.NewTerraformVariable(stack, jsii.String("imageId"), &cdktf.TerraformVariableConfig{
Type: jsii.String("string"),
Default: jsii.String("ami-abcde123"),
Description: jsii.String("What AMI to use to create an instance"),
})
instance.NewInstance(stack, jsii.String("hello"), &instance.InstanceConfig{
Ami: imageId.StringValue(),
InstanceType: jsii.String("t2.micro"),
})
Define Complex Input Variables
const nodeGroupConfig = new TerraformVariable(this, "node-group-config", {
type: VariableType.object({
node_group_name: VariableType.STRING,
instance_types: VariableType.list(VariableType.STRING),
min_size: VariableType.NUMBER,
desired_size: VariableType.NUMBER,
max_size: VariableType.NUMBER,
}),
nullable: false,
description: "Node group configuration",
});
new TerraformResource(this, "resource", {
nodeGroupConfig: nodeGroupConfig.value,
});
nodeGroupConfig := cdktf.NewTerraformVariable(stack, jsii.String("node-group-config"), &cdktf.TerraformVariableConfig{
Type: cdktf.VariableType_Object(&map[string]*string{
"node_group_name": cdktf.VariableType_STRING(),
"instance_types": cdktf.VariableType_List(cdktf.VariableType_STRING()),
"min_size": cdktf.VariableType_NUMBER(),
"desired_size": cdktf.VariableType_NUMBER(),
"max_size": cdktf.VariableType_NUMBER(),
}),
Nullable: jsii.Bool(false),
Description: jsii.String("Node group configuration"),
})
eksnodegroup.NewEksNodeGroup(stack, jsii.String("node-group"), &eksnodegroup.EksNodeGroupConfig{
ClusterName: jsii.String("my-cluster"),
NodeRoleArn: jsii.String("arn:::::dummy"),
SubnetIds: jsii.Strings("id-1234"),
NodeGroupName: jsii.String(fmt.Sprintf("${%s.node_group_name}", *nodeGroupConfig.Fqn())),
InstanceTypes: cdktf.Token_AsList(fmt.Sprintf("${%s.instance_types}", *nodeGroupConfig.Fqn()), &cdktf.EncodingOptions{}),
ScalingConfig: &eksnodegroup.EksNodeGroupScalingConfig{
DesiredSize: cdktf.Token_AsNumber(fmt.Sprintf("${%s.desired_size}", *nodeGroupConfig.Fqn())),
MinSize: cdktf.Token_AsNumber(fmt.Sprintf("${%s.min_size}", *nodeGroupConfig.Fqn())),
MaxSize: cdktf.Token_AsNumber(fmt.Sprintf("${%s.max_size}", *nodeGroupConfig.Fqn())),
},
})
Passing Input Variables to CDKTF
You can pass input variables to cdktf diff
, cdktf deploy
, and cdktf destroy
in the following ways:
TF_VAR_imageId=ami-abcde123
--var
CLI option: cdktf deploy --var='imageId=ami-abcde123'
--var-file
CLI option: cdktf deploy --var-file=/path/to/variables.tfvars
*.auto.tfvars
and terraform.tfvars
files found in the current working directory. Refer to the Terraform documentation for details.A Terraform local assigns a name to an expression to allow repeated usage. They are similar to a local variables in a programming language.
When to Use Local ValuesUse local values when you need use Terraform functions to transform data that is only available when Terraform applies a configuration. For example, instance IDs that cloud providers assign upon creation.
When values are available before synthesizing your code, we recommend using native programming language features to modify values instead.
Define Local ValuesThe following example uses TerraformLocal
to create a local value.
const commonTags = new TerraformLocal(this, "common_tags", {
Service: "service_name",
Owner: "owner",
});
new Instance(this, "example", {
ami: "ami-abcde123",
instanceType: "t2.micro",
tags: commonTags.expression,
});
TerraformLocal commonTags = new TerraformLocal(this, "common_tags", new HashMap<String, String>(){{
put("service", "service_name");
put("owner", "owner");
}});
new Instance(this, "example", InstanceConfig.builder()
.tags((Map<String, String>) commonTags.getExpression())
.build()
);
commonTags = TerraformLocal(self, "common_tags", {
"service": "service_name",
"owner": "owner"
})
Instance(self, "example",
tags = commonTags.as_string_map
)
TerraformLocal commonTags = new TerraformLocal(this, "common_tags", new Dictionary<string, string> {
{ "Service", "service_name" },
{ "Owner", "owner" },
});
new Instance(this, "example", new InstanceConfig
{
Ami = "ami-abcde123",
InstanceType = "t2.micro",
Tags = Token.AsStringMap(commonTags.Expression),
});
commonTags := cdktf.NewTerraformLocal(stack, jsii.String("common_tags"), map[string]string{
"Service": "service_name",
"Owner": "owner",
})
instance.NewInstance(stack, jsii.String("example"), &instance.InstanceConfig{
Ami: jsii.String("ami-abcde123"),
InstanceType: jsii.String("t2.micro"),
Tags: commonTags.AsStringMap(),
})
When you run cdktf synth
the TerraformLocal
synthesizes to the following JSON.
"locals": {
"common_tags": {
"Service": "service_name",
"Owner": "owner"
}
}
...
"resource": {
"aws_instance": {
"example": {
"tags": "${local.common_tags}"
}
}
}
You can define Terraform outputs to export structured data about your resources. Terraform prints the output value for the user after it applies infrastructure changes, and you can use this information as a data source for other Terraform workspaces.
When to use Output ValuesUse outputs to make data from Terraform resources and data sources available for further consumption or to share data between stacks. Outputs are particularly useful when you need to access data that is only known after Terraform applies the configuration. For example, you may want to get the URL of a newly provisioned server.
When values are available before synthesizing your code, we recommend using the functionality in your preferred programming language to supply this data as direct inputs.
The following example uses a TerraformOutput
to create an output.
import { TerraformLocal, TerraformStack, TerraformVariable } from "cdktf";
import { Construct } from "constructs";
import { App, TerraformOutput } from "cdktf";
interface OutputVariableStackConfig {
readonly myDomain: string;
}
class OutputVariableStack extends TerraformStack {
constructor(
scope: Construct,
name: string,
config: OutputVariableStackConfig,
) {
super(scope, name);
const { myDomain } = config;
new TerraformOutput(this, "my-domain", {
value: myDomain,
});
}
}
const app = new App();
new OutputVariableStack(app, "cdktf-producer", {
myDomain: "example.com",
});
app.synth();
import software.constructs.Construct;
import com.hashicorp.cdktf.TerraformStack;
import com.hashicorp.cdktf.App;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformOutputConfig;
public class VariablesAndOutputsValues extends TerraformStack {
public VariablesAndOutputsValues(Construct scope, String id, VariablesAndOutputsValuesProps props){
super(scope, id);
new TerraformOutput(this, "my-domain", TerraformOutputConfig.builder()
.value(props.myDomain)
.build()
);
}
public static class VariablesAndOutputsValuesProps {
public String myDomain;
public VariablesAndOutputsValuesProps(String myDomain){
this.myDomain = myDomain;
}
}
public static void main(String[] args) {
final App app = new App();
new VariablesAndOutputsValues(app, "cdktf-producer", new VariablesAndOutputsValuesProps("example.com"));
app.synth();
}
}
from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput
class OutputValuesProps:
myDomain: str
def __init__(self, myDomain: str):
self.myDomain = myDomain
class OutputValuesStack(TerraformStack):
def __init__(self, scope: Construct, name: str, props: OutputValuesProps):
super().__init__(scope, name)
TerraformOutput(self, "my-domain",
value = props.myDomain
)
app = App()
OutputValuesStack(app, "cdktf-producer", OutputValuesProps(myDomain = "example.com"))
app.synth()
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Constructs;
using HashiCorp.Cdktf;
using aws.Provider;
using aws.Instance;
namespace Examples
{
class OutputVariableStackConfig
{
public OutputVariableStackConfig(string myDomain)
{
this.MyDomain = myDomain;
}
public string MyDomain { get; set; }
}
class OutputVariableStack : TerraformStack
{
public OutputVariableStack(Construct scope, string name, OutputVariableStackConfig config) : base(scope, name)
{
new TerraformOutput(this, "my-domain", new TerraformOutputConfig
{
Value = config.MyDomain,
});
}
public static void Main(string[] args)
{
App app = new App();
new OutputVariableStack(app, "cdktf-producer", new OutputVariableStackConfig("example.com"));
app.Synth();
}
}
}
import (
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/hashicorp/terraform-cdk-go/cdktf"
)
type OutputsStackConfig struct {
MyDomain string
}
func NewOutputsStack(scope constructs.Construct, name string, config OutputsStackConfig) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &name)
cdktf.NewTerraformOutput(stack, jsii.String("my-domain"), &cdktf.TerraformOutputConfig{
Value: &config.MyDomain,
})
return stack
}
func main() {
app := cdktf.NewApp(nil)
NewOutputsStack(app, "outputs", OutputsStackConfig{
MyDomain: "example.com",
})
app.Synth()
}
Define Output Values
To access outputs, use the _output
suffix for Python and the Output
suffix for other languages.
Outputs return an HCL expression representing the underlying Terraform resource, so the return type must always be string
. When TerraformOutput
is any other type than string, you must add a typecast to compile the application (e.g. mod.numberOutput as number
). If a module returns a list, you must use an escape hatch to access items or loop over it. Refer to the Resources page for more information about how to use escape hatches.
The following Typescript example uses TerraformOutput
to create an output for a Random provider resource.
import { TerraformLocal, TerraformStack, TerraformVariable } from "cdktf";
import { Construct } from "constructs";
import { App, TerraformOutput } from "cdktf";
import { RandomProvider } from "@cdktf/provider-random/lib/provider";
import { Pet } from "@cdktf/provider-random/lib/pet";
class OutputsUsageStack extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new RandomProvider(this, "random", {});
const pet = new Pet(this, "pet", {});
new TerraformOutput(this, "random-pet", {
value: pet.id,
});
}
}
const app = new App();
new OutputsUsageStack(app, "outputs-usage");
app.synth();
import software.constructs.Construct;
import com.hashicorp.cdktf.TerraformStack;
import com.hashicorp.cdktf.App;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformOutputConfig;
import imports.random.pet.Pet;
import imports.random.provider.RandomProvider;
public class VariablesAndOutputsDefineValues extends TerraformStack {
public VariablesAndOutputsDefineValues(Construct scope, String id) {
super(scope, id);
new RandomProvider(this, "random");
Pet pet = new Pet(this, "pet");
new TerraformOutput(this, "random-pet", TerraformOutputConfig.builder()
.value(pet.getId())
.build());
public static void main(String[] args) {
final App app = new App();
new VariablesAndOutputsDefineValues(app, "cdktf-demo");
app.synth();
}
}
from imports.random.provider import RandomProvider
from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput
class DefineOutputStack(TerraformStack):
def __init__(self, scope: Construct, name: str):
super().__init__(scope, name)
RandomProvider(self, "random")
pet = random.pet.Pet(self, "pet")
TerraformOutput(self, "random-pet",
value = pet.id
)
app = App()
DefineOutputStack(app, "cdktf-demo")
app.synth()
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Constructs;
using HashiCorp.Cdktf;
using random.Provider;
using random.Pet;
namespace Examples
{
class OutputsUsageStack : TerraformStack
{
public OutputsUsageStack(Construct scope, string name) : base(scope, name)
{
new RandomProvider(this, "random", new RandomProviderConfig());
var pet = new Pet(this, "pet", new PetConfig());
new TerraformOutput(this, "random-pet", new TerraformOutputConfig
{
Value = pet.Id,
});
}
public static void Main(string[] args)
{
App app = new App();
new OutputsUsageStack(app, "outputs-usage");
app.Synth();
}
}
}
import (
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/hashicorp/terraform-cdk-go/cdktf"
"github.com/hashicorp/terraform-cdk/examples/go/documentation/generated/hashicorp/random/pet"
random "github.com/hashicorp/terraform-cdk/examples/go/documentation/generated/hashicorp/random/provider"
)
func NewOutputsUsageStack(scope constructs.Construct, name string) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &name)
random.NewRandomProvider(stack, jsii.String("random"), &random.RandomProviderConfig{})
pet := pet.NewPet(stack, jsii.String("pet"), &pet.PetConfig{})
cdktf.NewTerraformOutput(stack, jsii.String("random-pet"), &cdktf.TerraformOutputConfig{
Value: pet.Id(),
})
return stack
}
func main() {
app := cdktf.NewApp(nil)
NewOutputsUsageStack(app, "outputs-usage")
app.Synth()
}
When you run cdktf synth
, CDKTF synthesizes the code to the following JSON configuration.
"output": {
"random-pet": {
"value": "${random_pet.pet.id}"
}
}
When you run cdktf deploy
, CDKTF displays the following output.
Deploying Stack: cdktf-demo
Resources
✔ RANDOM_PET pet random_pet.pet
Summary: 1 created, 0 updated, 0 destroyed.
Output: random-pet = choice-haddock
Define & Reference Outputs via Remote State
The following example uses outputs to share data between stacks, each of which has a remote backend to store the Terraform state files remotely.
import { TerraformLocal, TerraformStack, TerraformVariable } from "cdktf";
import { Construct } from "constructs";
import { App, TerraformOutput } from "cdktf";
import {
CloudBackend,
DataTerraformRemoteState,
NamedCloudWorkspace,
} from "cdktf";
class Producer extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new CloudBackend(this, {
organization: "hashicorp",
workspaces: new NamedCloudWorkspace("producer"),
});
new RandomProvider(this, "random", {});
const pet = new Pet(this, "pet", {});
new TerraformOutput(this, "random-pet", {
value: pet.id,
});
}
}
class Consumer extends TerraformStack {
constructor(scope: Construct, name: string) {
super(scope, name);
new CloudBackend(this, {
organization: "hashicorp",
workspaces: new NamedCloudWorkspace("consumer"),
});
const remoteState = new DataTerraformRemoteState(this, "remote-pet", {
organization: "hashicorp",
workspaces: {
name: "producer",
},
});
new TerraformOutput(this, "random-remote-pet", {
value: remoteState.getString("random-pet"),
});
}
}
const app = new App();
new Producer(app, "cdktf-producer");
new Consumer(app, "cdktf-consumer");
app.synth();
import imports.random.provider.RandomProvider;
import imports.random.pet.Pet;
import software.constructs.Construct;
import com.hashicorp.cdktf.App;
import com.hashicorp.cdktf.TerraformStack;
import com.hashicorp.cdktf.TerraformOutput;
import com.hashicorp.cdktf.TerraformOutputConfig;
import com.hashicorp.cdktf.CloudBackend;
import com.hashicorp.cdktf.CloudBackendConfig;
import com.hashicorp.cdktf.NamedCloudWorkspace;
import com.hashicorp.cdktf.NamedRemoteWorkspace;
import com.hashicorp.cdktf.DataTerraformRemoteState;
import com.hashicorp.cdktf.DataTerraformRemoteStateRemoteConfig;
public class VariablesAndOutputsRemoteState {
public static class Producer extends TerraformStack{
public Producer(Construct scope, String id){
super(scope, id);
new CloudBackend(this, CloudBackendConfig.builder()
.organization("hashicorp")
.workspaces(new NamedCloudWorkspace("producer"))
.build()
);
new RandomProvider(this, "random");
Pet pet = new Pet(this, "pet");
new TerraformOutput(this, "random-pet", TerraformOutputConfig.builder()
.value(pet.getId())
.build()
);
}
}
public static class Consumer extends TerraformStack{
public Consumer(Construct scope, String id){
super(scope, id);
new CloudBackend(this, CloudBackendConfig.builder()
.organization("hashicorp")
.workspaces(new NamedCloudWorkspace("consumer"))
.build()
);
DataTerraformRemoteState remoteState = new DataTerraformRemoteState(this, "remote-pet", DataTerraformRemoteStateRemoteConfig.builder()
.organization("hashicorp")
.workspaces(new NamedRemoteWorkspace("producer"))
.build()
);
}
}
public static void main(String[] args) {
final App app = new App();
new Producer(app, "cdktf-producer");
new Consumer(app, "cdktf-consumer");
app.synth();
}
}
import imports.random as random
from constructs import Construct
from cdktf import App, TerraformStack, TerraformOutput, CloudBackend, NamedRemoteWorkspace, DataTerraformRemoteState
class Producer(TerraformStack):
def __init__(self, scope: Construct, name: str):
super().__init__(scope, name)
CloudBackend(self,
organization = "hashicorp",
workspaces = NamedCloudWorkspace("producer")
)
random.provider.RandomProvider(self, "random")
pet = random.pet.Pet(self, "pet")
TerraformOutput(self, "random-pet",
value = pet.id
)
class Consumer(TerraformStack):
def __init__(self, scope: Construct, name: str):
super().__init__(scope, name)
CloudBackend(self,
organization = "hashicorp",
workspaces = NamedCloudWorkspace("consumer")
)
remoteState = DataTerraformRemoteState(self, "remote-pet",
organization = "hashicorp",
workspaces = NamedRemoteWorkspace(name = "producer")
)
TerraformOutput(self, "random-remote-pet",
value = remoteState.get_string("random-pet")
)
app = App()
Producer(app, "cdktf-producer")
Consumer(app, "cdktf-consumer")
app.synth()
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using Constructs;
using HashiCorp.Cdktf;
using random.Provider;
using random.Pet;
namespace Examples
{
class Producer : TerraformStack
{
public Producer(Construct scope, string name) : base(scope, name)
{
new CloudBackend(this, new CloudBackendConfig
{
Organization = "hashicorp",
Workspaces = new NamedCloudWorkspace("producer"),
});
new RandomProvider(this, "random", new RandomProviderConfig());
var pet = new Pet(this, "pet", new PetConfig());
new TerraformOutput(this, "random-pet", new TerraformOutputConfig
{
Value = pet.Id,
});
}
}
class Consumer : TerraformStack
{
public Consumer(Construct scope, string name) : base(scope, name)
{
new CloudBackend(this, new CloudBackendConfig
{
Organization = "hashicorp",
Workspaces = new NamedCloudWorkspace("consumer"),
});
DataTerraformRemoteState remoteState = new DataTerraformRemoteState(this, "remote-state", new DataTerraformRemoteStateRemoteConfig
{
Organization = "hashicorp",
Workspaces = new NamedRemoteWorkspace("producer"),
});
new TerraformOutput(this, "random-remote-pet", new TerraformOutputConfig
{
Value = remoteState.GetString("random-pet"),
});
}
}
class Main
{
public static void Main(string[] args)
{
App app = new App();
new Producer(app, "cdktf-producer");
new Consumer(app, "cdktf-consumer");
app.Synth();
}
}
}
import (
"github.com/aws/constructs-go/constructs/v10"
"github.com/aws/jsii-runtime-go"
"github.com/hashicorp/terraform-cdk-go/cdktf"
"github.com/hashicorp/terraform-cdk/examples/go/documentation/generated/hashicorp/random/pet"
random "github.com/hashicorp/terraform-cdk/examples/go/documentation/generated/hashicorp/random/provider"
)
func NewProducerStack(scope constructs.Construct, name string) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &name)
cdktf.NewCloudBackend(stack, &cdktf.CloudBackendConfig{
Organization: jsii.String("hashicorp"),
Workspaces: cdktf.NewNamedCloudWorkspace(jsii.String("producer"), nil),
})
random.NewRandomProvider(stack, jsii.String("random"), &random.RandomProviderConfig{})
pet := pet.NewPet(stack, jsii.String("pet"), &pet.PetConfig{})
cdktf.NewTerraformOutput(stack, jsii.String("random-pet"), &cdktf.TerraformOutputConfig{
Value: pet.Id(),
})
return stack
}
func NewConsumerStack(scope constructs.Construct, name string) cdktf.TerraformStack {
stack := cdktf.NewTerraformStack(scope, &name)
cdktf.NewCloudBackend(stack, &cdktf.CloudBackendConfig{
Organization: jsii.String("hashicorp"),
Workspaces: cdktf.NewNamedCloudWorkspace(jsii.String("consumer"), nil),
})
remoteState := cdktf.NewDataTerraformRemoteState(stack, jsii.String("remote-pet"), &cdktf.DataTerraformRemoteStateRemoteConfig{
Organization: jsii.String("hashicorp"),
Workspaces: cdktf.NewNamedCloudWorkspace(jsii.String("producer"), nil),
})
cdktf.NewTerraformOutput(stack, jsii.String("random-remote-pet"), &cdktf.TerraformOutputConfig{
Value: remoteState.GetString(jsii.String("random-pet")),
})
return stack
}
func main() {
app := cdktf.NewApp(nil)
NewProducerStack(app, "cdktf-producer")
NewConsumerStack(app, "cdktf-consumer")
app.Synth()
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4