Skip to main content

CDK For Terraform Resolver

The CdkTfResolver is able to resolve any TerraformOutput defined by your CDKTF application. In this example, we create an S3 Bucket with the CDKTF, and pass its (deploy time generated) name as an environment variable to a Kubernetes CronJob resource.

import * as tf from "cdktf";
import * as aws from "@cdktf/provider-aws";
import * as k8s from 'cdk8s';
import * as kplus from 'cdk8s-plus-26';

import { CdkTfResolver } from '@cdk8s/cdktf-resolver';

const awsApp = new tf.App();
const stack = new tf.TerraformStack(awsApp, 'aws');

const k8sApp = new k8s.App({ resolvers: [new resolver.CdktfResolver({ app: awsApp })] });
const manifest = new k8s.Chart(k8sApp, 'Manifest', { resolver });

const bucket = new aws.s3Bucket.S3Bucket(stack, 'Bucket');
const bucketName = new tf.TerraformOutput(constrcut, 'BucketName', {
  value: bucket.bucket,
});

new kplus.CronJob(manifest, 'CronJob', {
  schedule: k8s.Cron.daily(),
  containers: [{
    image: 'job',
    envVariables: {
      // directly passing the value of the `TerraformOutput` containing
      // the deploy time bucket name
      BUCKET_NAME: kplus.EnvValue.fromValue(bucketName.value),
    }
 }]
});

awsApp.synth();
k8sApp.synth();

During cdk8s synthesis, the custom resolver will detect that bucketName.value is not a concrete value, but rather a value of a TerraformOutput. It will then perform cdktf CLI commands in order to fetch the actual value from the deployed infrastructure in your account. This means that in order for cdk8s synth to succeed, it must be executed after the CDKTF resources have been deployed. So your deployment workflow should (conceptually) be:

  1. cdktf deploy
  2. cdk8s synth

Note that the CdkTfResolver is only able to fetch tokens that have a TerraformOutput defined for them.

Permissions

Since running cdk8s synth will 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 down ReadOnly role dedicated for cdk8s resolvers.

Following are the set of commands the resolver will execute:

Cross Repository Workflow

As we've seen, your cdk8s application 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 simple import statement 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.ts in the CDKTF application, stored in a dedicated repository.

import * as tf from "cdktf";
import * as aws from "@cdktf/provider-aws";

import { CdkTfResolver } from '@cdk8s/cdktf-resolver';

const awsApp = new tf.App();
const stack = new tf.TerraformStack(awsApp, 'aws');

const bucket = new aws.s3Bucket.S3Bucket(stack, 'Bucket');
const bucketName = new tf.TerraformOutput(constrcut, 'BucketName', {
  value: bucket.bucket,
});

awsApp.synth();

In order for the cdk8s application 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.ts

import * as tf from "cdktf";
import * as aws from "@cdktf/provider-aws";

import { CdkTfResolver } from '@cdk8s/cdktf-resolver';

// export the app so we can pass it to the cdk8s resolver
export const awsApp = new tf.App();
const stack = new tf.TerraformStack(awsApp, 'aws');

const bucket = new aws.s3Bucket.S3Bucket(stack, 'Bucket');
// export the thing we want to have available for cdk8s applications
export const bucketName = new tf.TerraformOutput(constrcut, 'BucketName', {
  value: bucket.bucket,
});

// note that we don't call awsApp.synth here

main.ts

import { awsApp } from './app.ts'

awsApp.synth();

Now, publish the app.ts file to a package manager, so that your cdk8s application 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.

Assuming app.ts was published as the my-cdktf-app package, our cdk8s application will now look like so:

import * as k8s from 'cdk8s';
import * as kplus from 'cdk8s-plus-27';

// import the desired instance from the CDKTF app.
import { bucketName, awsApp } from 'my-cdktf-app';

import { CdkTfResolver } from '@cdk8s/cdktf-resolver';

const k8sApp = new k8s.App({ resolvers: [new resolver.CdktfResolver({ app: awsApp })] });
const manifest = new k8s.Chart(k8sApp, 'Manifest');

new kplus.CronJob(manifest, 'CronJob', {
  schedule: k8s.Cron.daily(),
  containers: [{
    image: 'job',
    envVariables: {
      // directly passing the value of the `TerraformOutput` containing
      // the deploy time bucket name
      BUCKET_NAME: kplus.EnvValue.fromValue(bucketName.value),
    }
 }]
});

k8sApp.synth();

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk8s_cdktf_resolver-0.0.300.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cdk8s_cdktf_resolver-0.0.300-py3-none-any.whl (33.5 kB view details)

Uploaded Python 3

File details

Details for the file cdk8s_cdktf_resolver-0.0.300.tar.gz.

File metadata

  • Download URL: cdk8s_cdktf_resolver-0.0.300.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.14.7

File hashes

Hashes for cdk8s_cdktf_resolver-0.0.300.tar.gz
Algorithm Hash digest
SHA256 3ad7b72e3c3b56cc6717d437c68e91dff45582c0283d1e4a2e573e1de7fea352
MD5 028ba63bf1b400f7834be34743da429e
BLAKE2b-256 ae4606bf56a49eec5cf478f0d74915c02135e7330fa9e7ea5cb6db82732233c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdk8s_cdktf_resolver-0.0.300.tar.gz:

Publisher: release.yml on cdk8s-team/cdk8s-cdktf-resolver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cdk8s_cdktf_resolver-0.0.300-py3-none-any.whl.

File metadata

File hashes

Hashes for cdk8s_cdktf_resolver-0.0.300-py3-none-any.whl
Algorithm Hash digest
SHA256 7cd288041e18bb894cf16d390f6b5af63c1bbf85b4e3e5e02a9cf46ba43b6b95
MD5 9c88d248aa7034aafb141413f737f8b2
BLAKE2b-256 f9bc708ffe4f698db76b1e79c63a17a8a1b8e933dfe596382c0748a07b17d69f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cdk8s_cdktf_resolver-0.0.300-py3-none-any.whl:

Publisher: release.yml on cdk8s-team/cdk8s-cdktf-resolver

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.0.303

2 files

0.0.302

2 files

0.0.301

2 files

This release

0.0.300 This release

2 files

0.0.299

2 files

0.0.298

2 files

0.0.297

2 files

0.0.296

2 files

0.0.295

2 files

0.0.294

2 files

0.0.293

2 files

0.0.292

2 files

0.0.291

2 files

0.0.290

2 files

0.0.289

2 files

0.0.288

2 files

0.0.287

2 files

0.0.286

2 files

0.0.285

2 files

0.0.284

2 files

0.0.283

2 files

0.0.282

2 files

0.0.281

2 files

0.0.280

2 files

0.0.279

2 files

0.0.278

2 files

0.0.277

2 files

0.0.276

2 files

0.0.275

2 files

0.0.274

2 files

0.0.273

2 files

0.0.272

2 files

0.0.271

2 files

0.0.270

2 files

0.0.269

2 files

0.0.268

2 files

0.0.267

2 files

0.0.266

2 files

0.0.265

2 files

0.0.264

2 files

0.0.263

2 files

0.0.262

2 files

0.0.261

2 files

0.0.260

2 files

0.0.259

2 files

0.0.258

2 files

0.0.257

2 files

0.0.256

2 files

0.0.255

2 files

0.0.254

2 files

0.0.253

2 files

0.0.252

2 files

0.0.251

2 files

0.0.250

2 files

0.0.249

2 files

0.0.248

2 files

0.0.247

2 files

0.0.246

2 files

0.0.245

2 files

0.0.244

2 files

0.0.243

2 files

0.0.242

2 files

0.0.241

2 files

0.0.240

2 files

0.0.239

2 files

0.0.238

2 files

0.0.237

2 files

0.0.236

2 files

0.0.235

2 files

0.0.234

2 files

0.0.233

2 files

0.0.232

2 files

0.0.231

2 files

0.0.230

2 files

0.0.229

2 files

0.0.228

2 files

0.0.227

2 files

0.0.226

2 files

0.0.225

2 files

0.0.224

2 files

0.0.223

2 files

0.0.222

2 files

0.0.221

2 files

0.0.220

2 files

0.0.219

2 files

0.0.218

2 files

0.0.217

2 files

0.0.216

2 files

0.0.215

2 files

0.0.214

2 files

0.0.213

2 files

0.0.212

2 files

0.0.211

2 files

0.0.210

2 files

0.0.209

2 files

0.0.208

2 files

0.0.207

2 files

0.0.206

2 files

0.0.205

2 files

0.0.204

2 files

0.0.203

2 files

0.0.202

2 files

0.0.201

2 files

0.0.200

2 files

0.0.199

2 files

0.0.198

2 files

0.0.197

2 files

0.0.196

2 files

0.0.195

2 files

0.0.194

2 files

0.0.193

2 files

0.0.192

2 files

0.0.191

2 files

0.0.190

2 files

0.0.189

2 files

0.0.188

2 files

0.0.187

2 files

0.0.186

2 files

0.0.185

2 files

0.0.184

2 files

0.0.183

2 files

0.0.182

2 files

0.0.181

2 files

0.0.180

2 files

0.0.179

2 files

0.0.178

2 files

0.0.177

2 files

0.0.176

2 files

0.0.175

2 files

0.0.174

2 files

0.0.173

2 files

0.0.172

2 files

0.0.171

2 files

0.0.170

2 files

0.0.169

2 files

0.0.168

2 files

0.0.167

2 files

0.0.166

2 files

0.0.165

2 files

0.0.164

2 files

0.0.163

2 files

0.0.162

2 files

0.0.161

2 files

0.0.160

2 files

0.0.159

2 files

0.0.158

2 files

0.0.157

2 files

0.0.156

2 files

0.0.155

2 files

0.0.154

2 files

0.0.153

2 files

0.0.152

2 files

0.0.151

2 files

0.0.150

2 files

0.0.149

2 files

0.0.148

2 files

0.0.147

2 files

0.0.146

2 files

0.0.145

2 files

0.0.144

2 files

0.0.143

2 files

0.0.142

2 files

0.0.141

2 files

0.0.140

2 files

0.0.139

2 files

0.0.138

2 files

0.0.137

2 files

0.0.136

2 files

0.0.135

2 files

0.0.134

2 files

0.0.133

2 files

0.0.132

2 files

0.0.131

2 files

0.0.130

2 files

0.0.129

2 files

0.0.128

2 files

0.0.127

2 files

0.0.126

2 files

0.0.125

2 files

0.0.124

2 files

0.0.123

2 files

0.0.122

2 files

0.0.121

2 files

0.0.120

2 files

0.0.119

2 files

0.0.118

2 files

0.0.117

2 files

0.0.116

2 files

0.0.115

2 files

0.0.114

2 files

0.0.113

2 files

0.0.112

2 files

0.0.111

2 files

0.0.110

2 files

0.0.109

2 files

0.0.108

2 files

0.0.107

2 files

0.0.106

2 files

0.0.105

2 files

0.0.104

2 files

0.0.103

2 files

0.0.102

2 files

0.0.101

2 files

0.0.100

2 files

0.0.99

2 files

0.0.98

2 files

0.0.97

2 files

0.0.96

2 files

0.0.95

2 files

0.0.94

2 files

0.0.93

2 files

0.0.92

2 files

0.0.91

2 files

0.0.90

2 files

0.0.89

2 files

0.0.88

2 files

0.0.87

2 files

0.0.86

2 files

0.0.85

2 files

0.0.84

2 files

0.0.83

2 files

0.0.82

2 files

0.0.81

2 files

0.0.80

2 files

0.0.79

2 files

0.0.78

2 files

0.0.77

2 files

0.0.76

2 files

0.0.75

2 files

0.0.74

2 files

0.0.73

2 files

0.0.72

2 files

0.0.71

2 files

0.0.70

2 files

0.0.69

2 files

0.0.68

2 files

0.0.67

2 files

0.0.66

2 files

0.0.65

2 files

0.0.64

2 files

0.0.63

2 files

0.0.62

2 files

0.0.61

2 files

0.0.60

2 files

0.0.59

2 files

0.0.58

2 files

0.0.57

2 files

0.0.56

2 files

0.0.55

2 files

0.0.54

2 files

0.0.53

2 files

0.0.52

2 files

0.0.51

2 files

0.0.50

2 files

0.0.49

2 files

0.0.48

2 files

0.0.47

2 files

0.0.46

2 files

0.0.45

2 files

0.0.44

2 files

0.0.43

2 files

0.0.42

2 files

0.0.41

2 files

0.0.40

2 files

0.0.39

2 files

0.0.38

2 files

0.0.37

2 files

0.0.36

2 files

0.0.35

2 files

0.0.34

2 files

0.0.33

2 files

0.0.32

2 files

0.0.31

2 files

0.0.30

2 files

0.0.29

2 files

0.0.28

2 files

0.0.27

2 files

0.0.26

2 files

0.0.25

2 files

0.0.24

2 files

0.0.23

2 files

0.0.22

2 files

0.0.21

2 files

0.0.20

2 files

0.0.19

2 files

0.0.18

2 files

0.0.17

2 files

0.0.16

2 files

0.0.15

2 files

0.0.14

2 files

0.0.13

2 files

0.0.12

2 files

0.0.11

2 files

0.0.10

2 files

0.0.9

2 files

0.0.8

2 files

0.0.7

2 files

0.0.6

2 files

0.0.5

2 files

0.0.4

2 files

0.0.3

2 files

0.0.2

2 files

0.0.1

2 files

0.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page