crazyTech
crazyTech

Reputation: 1477

Terraform: - How to properly pass input variables for cdktf commands for a C# based solution?

Here is info about our technical development environment :

The AWS Lambda project contains the following:

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",
        });

        ...Blah Blah configuring ITerraformAssetConfig and

        IS3BucketObjectConfig and IIamRoleConfig

        Blah Blah...

        ...Blah Blah instantiating S3BucketObject , IamRole ,

        LambdaFunction Blah Blah Blah

    } 
}

In the aforementioned tutorial guide webpage, I’ve tried all the various ways of supplying argument parameters:

• Environment variable: TF_VAR_imageId=testing ( in Powershell, I execute $Env:TF_VAR_imageId = "testing" before running the cdktf commands )

imageId=testing

However, when I run the Debug launcher, and I added the imageId TerraformVariable instance to the Visual Studio 2022 Watch list.

I frantically reviewed the properties of the imageId ‘s properties in the Visual Studio 2022 Watch list but None of them contain the 'testing' value.

Could someone please explain how I can properly pass input variables for cdktf commands?

Upvotes: 0

Views: 158

Answers (0)

Related Questions