Reputation: 1477
Here is info about our technical development environment :
The AWS Lambda project contains the following:
cdktf debug
language: csharp
cdktf-cli: 0.20.6
node: v18.20.1
cdktf: 0.20.6
constructs: 10.3.0
jsii: 1.96.0
terraform: 1.5.7
arch: x64
os: win32 10.0.22631
dotnet: 8.0.202
providers
HashiCorp.Cdktf.Providers.Aws (PREBUILT)
terraform provider version: 5.44.0 prebuilt provider version: 19.13.0 cdktf version: ^0.20.0 HashiCorp.Cdktf.Providers.Docker (PREBUILT) terraform provider version: 3.0.2 prebuilt provider version: 11.0.0 cdktf version: ^0.20.0 P
I am trying to use Terraform CDKTF tool to deploy a C#-based AWS Lambda to Amazon AWS Cloud.
In my Visual Studio 2022 Solution, I have the following projects:
I build the AWSSrvlessHelloWorldApp Lambda Project.
I wanted to supply argument parameters when I run cdktf deploy
Therefore, as I researched, I came across the following webpage link that describes how to use Terraform variables as input parameters:
https://developer.hashicorp.com/terraform/cdktf/concepts/variables-and-outputs
Here is an excerpt from the aforementioned webpage that shows sample declaration & instantiation of Terraform variables as input parameters in C#:
I build the AWSSrvlessHelloWorldApp Lambda Project.
I wanted to supply argument parameters when I run cdktf deploy
Therefore, as I researched, I came across the following webpage link that describes how to use Terraform variables as input parameters:
https://developer.hashicorp.com/terraform/cdktf/concepts/variables-and-outputs
Here is an excerpt from the aforementioned webpage that shows sample declaration & instantiation of Terraform variables as input parameters in C#:
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",
});
In the MyTerraformStack ( i.e. the TerraformStack Project) I have the following files:
In Program.cs , I have the following code( Please note that I use Debugger.Launch() so that I can bring up the MyTerraformStack in Visual Studio 2022 whenever I run cdktf from Powershell commandline ) :
class Program
{
public static void Main(string[] args)
{
Debugger.Launch();
App app = new App();
MainStack stack = new MainStack(app, "aws_instance");
app.Synth();
}
}
In MainStack.cs, the code content’s are:
class MainStack : TerraformStack
{
public MainStack(Construct scope, string id) : base(scope, id)
{
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",
});
Console.Error.WriteLine("imageId.ToString()");
Console.Error.WriteLine(imageId.ToString());
Console.Error.WriteLine("imageId.StringValue");
Console.Error.WriteLine(imageId.StringValue);
...Blah Blah configuring ITerraformAssetConfig and
IS3BucketObjectConfig and IIamRoleConfig
Blah Blah...
...Blah Blah instantiating S3BucketObject , IamRole ,
LambdaFunction Blah Blah Blah
}
}
In Windows Powershell, I've run the following:
$Env:TF_VAR_imageId="testing"
cdktf synth
However, when I Console Writeline code gets executes, I gives the following output:
imageId.ToString()
[2024-04-08T14:59:56.904] [ERROR] default - ${TfToken[TOKEN.1]}
imageId.StringValue
[2024-04-08T14:59:56.904] [ERROR] default - ${TfToken[TOKEN.2]}
The imageId's value fails to get applied during the cdktf synth execution. Why, and could someone please tell me how to resolve said problem?
Upvotes: 0
Views: 132
Reputation: 1477
LOL you stackoverflow folks are all slacking. :)
I got the following solution from someone on reddit
In my C# Terraform CDKTF application, I have the following snippet of code:
TerraformVariable imageId = new TerraformVariable(this, "imageId", new TerraformVariableConfig
{
Type = "string",
Default = "ami-abcde123",
Description = "What AMI to use to create an instance",
});
Console.Error.WriteLine("****************************************");
Console.Error.WriteLine("****************************************");
Console.Error.WriteLine("imageId.StringValue");
Console.Error.WriteLine(imageId.StringValue);
Console.Error.WriteLine("imageId.Value.ToString()");
Console.Error.WriteLine(imageId.Value.ToString());
Console.Error.WriteLine("****************************************");
Console.Error.WriteLine("****************************************");
TerraformOutput outputVar = new TerraformOutput(this, "outputVar", new TerraformOutputConfig
{
Value = imageId.StringValue,
});
Console.Error.WriteLine("****************************************");
Console.Error.WriteLine("****************************************");
Console.Error.WriteLine("outputVar.Value");
Console.Error.WriteLine(outputVar.Value);
I have a file called inputfile.tfvars that will be processed as input, and it contains:
imageId = "testing"
after running the following commands in order, it worked:
cdktf synth
cdktf deploy -var-file="inputfile.tfvars"
Upvotes: 0