Package: cdk8s-cdktf-resolver
CDK For Terraform ResolverTheCdkTfResolveris able to resolve anyTerraformOutputdefined by your CDKTF application. In this example, we create an S3Bucketwith the CDKTF, and pass its (deploy time generated)
name as an environment variable to a KubernetesCronJobresource.import*astffrom"cdktf";import*asawsfrom"@cdktf/provider-aws";import*ask8sfrom'cdk8s';import*askplusfrom'cdk8s-plus-26';import{CdkTfResolver}from'@cdk8s/cdktf-resolver';constawsApp=newtf.App();conststack=newtf.TerraformStack(awsApp,'aws');constk8sApp=newk8s.App({resolvers:[newresolver.CdktfResolver({app:awsApp})]});constmanifest=newk8s.Chart(k8sApp,'Manifest',{resolver});constbucket=newaws.s3Bucket.S3Bucket(stack,'Bucket');constbucketName=newtf.TerraformOutput(constrcut,'BucketName',{value:bucket.bucket,});newkplus.CronJob(manifest,'CronJob',{schedule:k8s.Cron.daily(),containers:[{image:'job',envVariables:{//directlypassingthevalueofthe`TerraformOutput`containing//thedeploytimebucketnameBUCKET_NAME:kplus.EnvValue.fromValue(bucketName.value),}}]});awsApp.synth();k8sApp.synth();During cdk8s synthesis, the custom resolver will detect thatbucketName.valueis not a concrete value,
but rather a value of aTerraformOutput. It will then performcdktfCLI commands in order to fetch the
actual value from the deployed infrastructure in your account. This means that in order
forcdk8s synthto succeed, it must be executedafterthe CDKTF resources
have been deployed. So your deployment workflow should (conceptually) be:cdktf deploycdk8s synthNote that theCdkTfResolverisonlyable to fetch tokens that have aTerraformOutputdefined for them.PermissionsSince runningcdk8s synthwill now require reading terraform outputs, it must have permissions to do so.
In case a remote state file is used, this means providing a set of credentials for the account that have access
to where the state is stored. This will vary depending on your cloud provider, but in most cases will involve giving
read permissions on a blob storage device (e.g S3 bucket).Note that the permissions cdk8s require are far more scoped down than those normally required for the
deployment of CDKTF applications. It is therefore recommended to not reuse the same set of credentials,
and instead create a scoped downReadOnlyrole dedicated for cdk8s resolvers.Following are the set of commands the resolver will execute:cdktf outputCross Repository WorkflowAs we've seen, yourcdk8sapplication needs access to the objects defined in your cloud application. If both applications
are defined within the same file, this is trivial to achieve. If they are in different files, a simpleimportstatement will suffice.
However, what if the applications are managed in two separate repositories? This makes it a little trickier, but still possible.In this scenario,cdktf.tsin the CDKTF application, stored in a dedicated repository.import*astffrom"cdktf";import*asawsfrom"@cdktf/provider-aws";import{CdkTfResolver}from'@cdk8s/cdktf-resolver';constawsApp=newtf.App();conststack=newtf.TerraformStack(awsApp,'aws');constbucket=newaws.s3Bucket.S3Bucket(stack,'Bucket');constbucketName=newtf.TerraformOutput(constrcut,'BucketName',{value:bucket.bucket,});awsApp.synth();In order for thecdk8sapplication to have cross repository access, the CDKTF object instances
that we want to expose need to be available via a package repository. To do this, break up the
CDKTF application into the following files:app.tsimport*astffrom"cdktf";import*asawsfrom"@cdktf/provider-aws";import{CdkTfResolver}from'@cdk8s/cdktf-resolver';//exporttheappsowecanpassittothecdk8sresolverexportconstawsApp=newtf.App();conststack=newtf.TerraformStack(awsApp,'aws');constbucket=newaws.s3Bucket.S3Bucket(stack,'Bucket');//exportthethingwewanttohaveavailableforcdk8sapplicationsexportconstbucketName=newtf.TerraformOutput(constrcut,'BucketName',{value:bucket.bucket,});//notethatwedon't call awsApp.synth heremain.tsimport{awsApp}from'./app.ts'awsApp.synth();Now, publish theapp.tsfile to a package manager, so that yourcdk8sapplication can install and import it.
This approach might be somewhat counter intuitive, because normally we only publish classes to the package manager,
not instances. Indeed, these types of applications introduce a new use-case that requires the sharing of instances.
Conceptually, this is no different than writing state*to an SSM parameter or an S3 bucket, and it allows us to remain
in the boundaries of our programming language, and the typing guarantees it provides.*Actually, we are only publishing instructions for fetching state, not the state itself.Assumingapp.tswas published as themy-cdktf-apppackage, ourcdk8sapplication will now look like so:import*ask8sfrom'cdk8s';import*askplusfrom'cdk8s-plus-27';//importthedesiredinstancefromtheCDKTFapp.import{bucketName,awsApp}from'my-cdktf-app';import{CdkTfResolver}from'@cdk8s/cdktf-resolver';constk8sApp=newk8s.App({resolvers:[newresolver.CdktfResolver({app:awsApp})]});constmanifest=newk8s.Chart(k8sApp,'Manifest');newkplus.CronJob(manifest,'CronJob',{schedule:k8s.Cron.daily(),containers:[{image:'job',envVariables:{//directlypassingthevalueofthe`TerraformOutput`containing//thedeploytimebucketnameBUCKET_NAME:kplus.EnvValue.fromValue(bucketName.value),}}]});k8sApp.synth();