hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.85k stars 450 forks source link

Fn.Strcontains() produces error in C# #3408

Open doctor-house opened 9 months ago

doctor-house commented 9 months ago

Expected Behavior

There should be no error when using Fn.Strcontains()

Actual Behavior

cdktf synth produces the following error:

[2024-01-10T17:52:38.960] [ERROR] default - Unhandled exception.
[2024-01-10T17:52:38.974] [ERROR] default - System.ArgumentException: Could not infer JSII type for .NET type '_Proxy' (Parameter 'type')
   at Amazon.JSII.Runtime.Services.Converters.FrameworkToJsiiConverter.InferType(IReferenceMap referenceMap, Type type)
   at Amazon.JSII.Runtime.Services.Converters.FrameworkToJsiiConverter.InferType(IReferenceMap referenceMap, Object value)
   at Amazon.JSII.Runtime.Services.Converters.ValueConverter.ConvertAny(Type type, IReferenceMap referenceMap, Object value)
   at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvertPrimitive(Type type, IReferenceMap referenceMap, Object value, Boolean isOptional, PrimitiveType primitiveType, Object& result)
   at Amazon.JSII.Runtime.Services.Converters.ValueConverter.TryConvert(IOptionalValue optionalValue, Type type, IReferenceMap referenceMap, Object value, Object& result)
   at Amazon.JSII.Runtime.Services.Converters.FrameworkToJsiiConverter.TryConvert(IOptionalValue optionalValue, IReferenceMap referenceMap, Object value, Object& result)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.<>c__DisplayClass20_0.<ConvertArguments>b__0(Parameter parameter, Object frameworkArgument)
   at System.Linq.Enumerable.ZipIterator[TFirst,TSecond,TResult](IEnumerable`1 first, IEnumerable`1 second, Func`3 resultSelector)+MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at Amazon.JSII.Runtime.Deputy.DeputyBase.ConvertArguments(Parameter[] parameters, Object[] arguments)
   at Amazon.JSII.Runtime.Deputy.DeputyBase..ctor(DeputyProps props)
   at Constructs.Construct..ctor(DeputyProps props)
   at HashiCorp.Cdktf.TerraformElement..ctor(DeputyProps props)
   at HashiCorp.Cdktf.TerraformLocal..ctor(Construct scope, String id, Object expression)
   at MyCompany.MyApp.MainStack..ctor(Construct scope, String id) in C:\work\cdktf-test\MainStack.cs:line 12

Steps to Reproduce

  1. Run cdktf init --template=csharp to make a new project
  2. Add the following to MainStack.cs
    var fooBar = new TerraformLocal(this, "foo_bar", Fn.Strcontains("foo", "bar"));
  3. Run cdktf synth to see the error.

Versions

language: csharp cdktf-cli: 0.20.0 node: v18.12.0 cdktf: 0.20.0 constructs: 10.0.25 jsii: 1.93.0 terraform: 1.2.6 arch: x64 os: win32 10.0.22621 dotnet: 6.0.320

Providers

┌┐ ││ └┘

Gist

No response

Possible Solutions

No response

Workarounds

No response

Anything Else?

No response

References

No response

Help Wanted

Community Note

ansgarm commented 8 months ago

Hi @doctor-house 👋

Please excuse the late reply! I was able to confirm the bug you're running into and it seems like JSII (the underlying library translating between the CDK supported languages) has problems with Terraform functions that return a boolean value (which is modeled as an IResolvable as there are no boolean tokens in the CDKs). This makes this problem hard to quickly fix.

However, in the meantime, it is possible to work around this by using strings containing raw expressions (e.g. "${some_function()}"). Below is a C# example for the function you encountered the bug for.

var str = new TerraformVariable(this, "str", new TerraformVariableConfig
{
    Type = "string",
    Default = "foo"
});
var fooBar = new TerraformLocal(this, "foo_bar", $"${{strcontains({str.Fqn}, \"bar\")}}");

This synthesizes the following Terraform JSON for the local:

// ...
"locals": {
  "foo_bar": "${strcontains(var.str, \"bar\")}"
},
// ...