Hello,
I am trying to iterate over a custom construct to create multiple resources.
I have a construct to create a Terraform Cloud Workspace.
import { Workspace } from "@cdktf/provider-tfe/lib/workspace";
import { TerraformMetaArguments, TerraformOutput } from "cdktf";
import { Construct } from "constructs";
export interface WorkspaceParams extends TerraformMetaArguments{
name: string;
tags: string[];
organization: string;
autoApply: boolean;
description: string;
}
export class CustomWorkspace extends Construct{
_params: WorkspaceParams;
public workspace!: Workspace;
constructor(scope: Construct, name: string, params: WorkspaceParams) {
super(scope, name)
this._params = params;
this._setup();
this._outputs();
}
_setup = () => {
this.workspace = new Workspace(this, `metronet-workspace`, {
name: this._params.name,
organization: this._params.name,
description: this._params.description,
tagNames: this._params.tags,
autoApply: this._params.autoApply
});
}
_outputs = () => {
new TerraformOutput(this, `workspace-id`, {
value: this.workspace.name
});
}
}
And then in my stack I am trying to use an iterator so I can create multiple workspaces based on an Input Variable of Complex List.
this._workspaces = new TerraformVariable(this, "workspaces", {
type: VariableType.list(VariableType.object({
name: VariableType.STRING,
description: VariableType.STRING,
tags: VariableType.LIST_STRING,
})),
description: "List of Workspaces"
});
new CustomWorkspace(this, `${this._name}-workspace`, {
forEach: iterator,
name: iterator.getString("name"),
description: iterator.getString("description"),
autoApply: true,
organization: this._orgName.stringValue,
tags: iterator.getList("tags"),
});
But I get the error on plan
,
I was thinking I need to use iterator.dynamic
but thats also giving the same error.
Let me know if anyone has gone through the same problem.
I was following this link for the custom construct. Constructs - CDK for Terraform | Terraform | HashiCorp Developer