Note: As of April 2023, it appears that only TypeScript supports the conversion of existing Terraform projects.
The project directory structure in this example steps is as follows: cdktf
is the working directory and tf
is the existing Terraform Project.
$ tree
├── cdktf
└── tf
├── main.tf
└── vcl
└── main.vcl
Install the cdktf-cli
$ npm install --global cdktf-cli@latest
- Run the
cdktf init
command and specify the Terraform project directory when prompted:
$ cdktf init --template=typescript --local
Note: By supplying '--local' option you have chosen local storage mode for storing the state of your stack.
This means that your Terraform state file will be stored locally on disk in a file 'terraform.<STACK NAME>.tfstate' in the root of your project.
? Project Name ts
? Project Description A simple getting started project for cdktf.
? Do you want to start from an existing Terraform project? Yes
? Please enter the path to the Terraform project ../tf <--- (Existing Terraform Project)
? Do you want to send crash reports to the CDKTF team? No
- Add the following line to tsconfig.json:
"ignoreDeprecations": "5.0",
- Add
FastlyProvider
tomain.ts
and include the necessary import statement
import { FastlyProvider } from './.gen/providers/fastly/provider';
( snip )
new FastlyProvider(this, 'fastly', {
apiKey: 'XXXXX',
});
API tokens should be imported from outside main.ts using dotenv or similar.
- Add the VCL file as a TerraformAsset
import { AssetType, TerraformAsset } from 'cdktf';
( snip )
const VCLAsset = new TerraformAsset(this, 'values-asset', {
path: `${process.cwd()}/../tf/vcl/main.vcl`,
type: AssetType.FILE,
});
- Specify the TerraformAsset (VCL file) using the file function
import { Fn } from 'cdktf';
( snip )
new fastly.serviceVcl.ServiceVcl(this, 'service', {
backend: [
{
address: 'httpbin.org',
name: 'httpbin',
port: 443,
sslCertHostname: 'httpbin.org',
sslSniHostname: 'httpbin.org',
useSsl: true,
},
],
domain: [
{
name: domain,
},
],
forceDestroy: true,
name: domain,
vcl: [
{
content: Fn.file(VCLAsset.path), <--- (Replaced Part)
main: true,
name: 'main',
},
],
});
/*Provider bindings are generated by running cdktf get.
See https://cdk.tf/provider-generation for more details.*/
import * as fastly from './.gen/providers/fastly';
import { App, AssetType, Fn, TerraformAsset, TerraformStack } from 'cdktf';
import { Construct } from 'constructs';
import { FastlyProvider } from './.gen/providers/fastly/provider';
class MyStack extends TerraformStack {
constructor(scope: Construct, id: string) {
super(scope, id);
new FastlyProvider(this, 'fastly', {
apiKey: 'XXXXX',
});
const VCLAsset = new TerraformAsset(this, 'values-asset', {
path: `${process.cwd()}/../tf/vcl/main.vcl`,
type: AssetType.FILE,
});
const domain = 'cdktest.global.ssl.fastly.net';
new fastly.serviceVcl.ServiceVcl(this, 'service', {
backend: [
{
address: 'httpbin.org',
name: 'httpbin',
port: 443,
sslCertHostname: 'httpbin.org',
sslSniHostname: 'httpbin.org',
useSsl: true,
},
],
domain: [
{
name: domain,
},
],
forceDestroy: true,
name: domain,
vcl: [
{
content: Fn.file(VCLAsset.path),
main: true,
name: 'main',
},
],
});
}
}
const app = new App();
new MyStack(app, 'ts');
app.synth();