A compatibility layer for using the CDK for Terraform with AWS CDK constructs
npm install @nrfcloud/cdktf-aws-adaptor#### But isn't there already an adaptor?
Yes, but it's in technical preview and missing many essential features.
It also doesn't seem to be actively maintained.
#### Caveats
* References to AWS CDK constructs only work within the scope of adaptor classes
* AWS CDK aspects are not supported
* Custom resources are not supported
Essentially, just use the provided AwsTerraformAdaptorStack class as your base stack class instead of TerraformStack
``typescript
import {AwsTerraformAdaptorStack} from "./cdk-adaptor-stack";
import {Bucket} from "aws-cdk-lib";
import {S3Bucket} from "@cdktf/provider-aws/lib/s3-bucket/index.js";
import {S3BucketObject} from "@cdktf/provider-aws/lib/s3-bucket-object";
class MyStack extends AwsTerraformAdaptorStack {
constructor(scope: Construct, id: string, region: string) {
super(scope, id, region);
// You can instantiate AWS CDK constructs as normal
const bucket = new Bucket(this, 'MyBucket', {
bucket: 'my-bucket',
});
// You can also instantiate CDK for Terraform constructs
// Bidirectional references between the two are automatically created
const tfBucket = new S3BucketObject(this, 'MyTfBucket', {
bucket: bucket.bucketName,
key: 'my-tf-bucket',
source: 'my-tf-bucket',
});
}
}
`
If you encounter an error like No resource mapping found for , you can add a mapping for it like so:`typescript``
// Map between a L1 Cloudformation construct and a CDK for Terraform construct
registerMappingTyped(CfnBucketPolicy, S3BucketPolicy, {
resource(scope, id, props) {
return new S3BucketPolicy(
scope,
id,
deleteUndefinedKeys({
// ALL keys of the provided construct must be mapped
policy: Fn.jsonencode(props.PolicyDocument),
bucket: props.Bucket,
}),
);
},
attributes: {
// ALL attributes of the provided construct must be mapped
Ref: (resource) => resource.id,
},
});