# CDK For Terraform Resolver
The `CdkTfResolver` is able to resolve any [`TerraformOutput`](https://developer.hashicorp.com/terraform/cdktf/concepts/variables-and-outputs#output-values)
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.
```python
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:
* [`cdktf output`](https://developer.hashicorp.com/terraform/cdktf/cli-reference/commands#output)
## 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.
```python
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`
```python
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`
```python
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<sup>*</sup> 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.
> <sup>*</sup> 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:
```python
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();
```
Raw data
{
"_id": null,
"home_page": "https://github.com/cdk8s-team/cdk8s-cdktf-resolver.git",
"name": "cdk8s-cdktf-resolver",
"maintainer": null,
"docs_url": null,
"requires_python": "~=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Amazon Web Services",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/4f/02/afbfe5cbd799e715c98030f91c2064a771e5abe104c91f51ca91e72109c3/cdk8s_cdktf_resolver-0.0.124.tar.gz",
"platform": null,
"description": "# CDK For Terraform Resolver\n\nThe `CdkTfResolver` is able to resolve any [`TerraformOutput`](https://developer.hashicorp.com/terraform/cdktf/concepts/variables-and-outputs#output-values)\ndefined by your CDKTF application. In this example, we create an S3 `Bucket` with the CDKTF, and pass its (deploy time generated)\nname as an environment variable to a Kubernetes `CronJob` resource.\n\n```python\nimport * as tf from \"cdktf\";\nimport * as aws from \"@cdktf/provider-aws\";\nimport * as k8s from 'cdk8s';\nimport * as kplus from 'cdk8s-plus-26';\n\nimport { CdkTfResolver } from '@cdk8s/cdktf-resolver';\n\nconst awsApp = new tf.App();\nconst stack = new tf.TerraformStack(awsApp, 'aws');\n\nconst k8sApp = new k8s.App({ resolvers: [new resolver.CdktfResolver({ app: awsApp })] });\nconst manifest = new k8s.Chart(k8sApp, 'Manifest', { resolver });\n\nconst bucket = new aws.s3Bucket.S3Bucket(stack, 'Bucket');\nconst bucketName = new tf.TerraformOutput(constrcut, 'BucketName', {\n value: bucket.bucket,\n});\n\nnew kplus.CronJob(manifest, 'CronJob', {\n schedule: k8s.Cron.daily(),\n containers: [{\n image: 'job',\n envVariables: {\n // directly passing the value of the `TerraformOutput` containing\n // the deploy time bucket name\n BUCKET_NAME: kplus.EnvValue.fromValue(bucketName.value),\n }\n }]\n});\n\nawsApp.synth();\nk8sApp.synth();\n```\n\nDuring cdk8s synthesis, the custom resolver will detect that `bucketName.value` is not a concrete value,\nbut rather a value of a `TerraformOutput`. It will then perform `cdktf` CLI commands in order to fetch the\nactual value from the deployed infrastructure in your account. This means that in order\nfor `cdk8s synth` to succeed, it must be executed *after* the CDKTF resources\nhave been deployed. So your deployment workflow should (conceptually) be:\n\n1. `cdktf deploy`\n2. `cdk8s synth`\n\n> Note that the `CdkTfResolver` is **only** able to fetch tokens that have a `TerraformOutput` defined for them.\n\n##### Permissions\n\nSince running `cdk8s synth` will now require reading terraform outputs, it must have permissions to do so.\nIn case a remote state file is used, this means providing a set of credentials for the account that have access\nto where the state is stored. This will vary depending on your cloud provider, but in most cases will involve giving\nread permissions on a blob storage device (e.g S3 bucket).\n\nNote that the permissions cdk8s require are far more scoped down than those normally required for the\ndeployment of CDKTF applications. It is therefore recommended to not reuse the same set of credentials,\nand instead create a scoped down `ReadOnly` role dedicated for cdk8s resolvers.\n\nFollowing are the set of commands the resolver will execute:\n\n* [`cdktf output`](https://developer.hashicorp.com/terraform/cdktf/cli-reference/commands#output)\n\n## Cross Repository Workflow\n\nAs we've seen, your `cdk8s` application needs access to the objects defined in your cloud application. If both applications\nare defined within the same file, this is trivial to achieve. If they are in different files, a simple `import` statement will suffice.\nHowever, what if the applications are managed in two separate repositories? This makes it a little trickier, but still possible.\n\nIn this scenario, `cdktf.ts` in the CDKTF application, stored in a dedicated repository.\n\n```python\nimport * as tf from \"cdktf\";\nimport * as aws from \"@cdktf/provider-aws\";\n\nimport { CdkTfResolver } from '@cdk8s/cdktf-resolver';\n\nconst awsApp = new tf.App();\nconst stack = new tf.TerraformStack(awsApp, 'aws');\n\nconst bucket = new aws.s3Bucket.S3Bucket(stack, 'Bucket');\nconst bucketName = new tf.TerraformOutput(constrcut, 'BucketName', {\n value: bucket.bucket,\n});\n\nawsApp.synth();\n```\n\nIn order for the `cdk8s` application to have cross repository access, the CDKTF object instances\nthat we want to expose need to be available via a package repository. To do this, break up the\nCDKTF application into the following files:\n\n`app.ts`\n\n```python\nimport * as tf from \"cdktf\";\nimport * as aws from \"@cdktf/provider-aws\";\n\nimport { CdkTfResolver } from '@cdk8s/cdktf-resolver';\n\n// export the app so we can pass it to the cdk8s resolver\nexport const awsApp = new tf.App();\nconst stack = new tf.TerraformStack(awsApp, 'aws');\n\nconst bucket = new aws.s3Bucket.S3Bucket(stack, 'Bucket');\n// export the thing we want to have available for cdk8s applications\nexport const bucketName = new tf.TerraformOutput(constrcut, 'BucketName', {\n value: bucket.bucket,\n});\n\n// note that we don't call awsApp.synth here\n```\n\n`main.ts`\n\n```python\nimport { awsApp } from './app.ts'\n\nawsApp.synth();\n```\n\nNow, publish the `app.ts` file to a package manager, so that your `cdk8s` application can install and import it.\nThis approach might be somewhat counter intuitive, because normally we only publish classes to the package manager,\nnot instances. Indeed, these types of applications introduce a new use-case that requires the sharing of instances.\nConceptually, this is no different than writing state<sup>*</sup> to an SSM parameter or an S3 bucket, and it allows us to remain\nin the boundaries of our programming language, and the typing guarantees it provides.\n\n> <sup>*</sup> Actually, we are only publishing instructions for fetching state, not the state itself.\n\nAssuming `app.ts` was published as the `my-cdktf-app` package, our `cdk8s` application will now look like so:\n\n```python\nimport * as k8s from 'cdk8s';\nimport * as kplus from 'cdk8s-plus-27';\n\n// import the desired instance from the CDKTF app.\nimport { bucketName, awsApp } from 'my-cdktf-app';\n\nimport { CdkTfResolver } from '@cdk8s/cdktf-resolver';\n\nconst k8sApp = new k8s.App({ resolvers: [new resolver.CdktfResolver({ app: awsApp })] });\nconst manifest = new k8s.Chart(k8sApp, 'Manifest');\n\nnew kplus.CronJob(manifest, 'CronJob', {\n schedule: k8s.Cron.daily(),\n containers: [{\n image: 'job',\n envVariables: {\n // directly passing the value of the `TerraformOutput` containing\n // the deploy time bucket name\n BUCKET_NAME: kplus.EnvValue.fromValue(bucketName.value),\n }\n }]\n});\n\nk8sApp.synth();\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "@cdk8s/cdktf-resolver",
"version": "0.0.124",
"project_urls": {
"Homepage": "https://github.com/cdk8s-team/cdk8s-cdktf-resolver.git",
"Source": "https://github.com/cdk8s-team/cdk8s-cdktf-resolver.git"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c154ee35c20f3ee351c704bf68e5d278a6cca5c8e82b0a37a6041b4d84fd3ef5",
"md5": "e2fdf813061dcc0d3771014169dee2d6",
"sha256": "fd33a7aba9e1732b39d071fac84eea4cd3372e966c21ab28a4378030e729bf7a"
},
"downloads": -1,
"filename": "cdk8s_cdktf_resolver-0.0.124-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e2fdf813061dcc0d3771014169dee2d6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "~=3.8",
"size": 33570,
"upload_time": "2024-11-23T12:19:17",
"upload_time_iso_8601": "2024-11-23T12:19:17.231468Z",
"url": "https://files.pythonhosted.org/packages/c1/54/ee35c20f3ee351c704bf68e5d278a6cca5c8e82b0a37a6041b4d84fd3ef5/cdk8s_cdktf_resolver-0.0.124-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "4f02afbfe5cbd799e715c98030f91c2064a771e5abe104c91f51ca91e72109c3",
"md5": "689881955bb6ba722c545a701111b9c4",
"sha256": "1c74b3197b3c410813946872e3c1284c87dabdd6fd8c827c5b8dcc5f61acfd61"
},
"downloads": -1,
"filename": "cdk8s_cdktf_resolver-0.0.124.tar.gz",
"has_sig": false,
"md5_digest": "689881955bb6ba722c545a701111b9c4",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "~=3.8",
"size": 35115,
"upload_time": "2024-11-23T12:19:19",
"upload_time_iso_8601": "2024-11-23T12:19:19.342924Z",
"url": "https://files.pythonhosted.org/packages/4f/02/afbfe5cbd799e715c98030f91c2064a771e5abe104c91f51ca91e72109c3/cdk8s_cdktf_resolver-0.0.124.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-23 12:19:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "cdk8s-team",
"github_project": "cdk8s-cdktf-resolver",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "cdk8s-cdktf-resolver"
}