CDKTF string manipulation of runtime created resource using AddOverride

469 Views Asked by At

I'm currently creating some resources in Azure and I am using CDKTF for that with C#.

I have a resource value from api management that is the host url of APIM. I need to remove the "https://" at the beginning so that the string start from the apex domain.

in standard terraform i would just do split("://", module.apim.apim_gateway_url)[1] and be on my way, but in CDKTF I can't do this because I can't manipulate the value until after C# runs, so I need to use AddOverride to the resource to update the value.

The issue is I can't figure out the proper syntax to do that. Here is what I currently have:

                origin.AddOverride(
                    "host_name", "${split(\"://\","+ hostname +")[1]}"
                );

I don't know what is wrong with this but the error i get is

Error: Invalid expression
              │ 
              │   on cdk.tf.json line 2648, in resource.azurerm_cdn_frontdoor_origin.fd-api-backend_origin_1783E930 (fd-api-backend/origin):
              │ 2648:         "host_name": "${split(\"://\",${azurerm_api_management.apim_api-management_639C6211 (apim/api-management).gateway_url})[1]}",
              │ 
              │ Expected the start of an expression, but found an invalid expression token.
1

There are 1 best solutions below

0
On

I wanted to answer my own question incase anyone else runs into this issue in the future. It's actually quite simple but I didn't realize it from the examples give in the documentation. You can do string manipulation like so:

Token.AsString(Fn.Element(Fn.Split("://", apim.gatewayUrl), 1))

So apim.gatewayUrl is actually a Token object that once processed, will have a value like "https://test.aim-stuff.net".

So by doing Fn.Element(Fn.Split("://", apim.gatewayUrl), 1) I am able to have a post C# build processing of the string result from the apimanagement object. This passes along the command to terraform's tf output file, and then the Token.AsString() is added because I required a string object to be passed to the actual hostName field of the object I was creating.