The AWS Adapter is a technical preview and not ready for production usage. Its API is not stable and things might break unexpectedly.
AwsTerraformAdapter is included in the @cdktf/aws-cdk package and allows you to use Amazon Web Services Cloud Development Kit (AWS CDK) constructs in your CDK for Terraform (CDKTF) projects.
The AwsTerraformAdapter uses the aws_cloudcontrolapi_resource Terraform resource to communicate with the AWS Cloud Control API. Reference this list of supported resources for the Cloud Control API.
You need to manually map resources that the AWS Cloud Control API does not yet support to specific Terraform resources because attribute names and resource types differ between CloudFormation and Terraform. Some manual mappings are included in the adapter, and we are happy to accept PRs that add manual mappings for currently unsupported resources!
Requirements
TheAwsTerraformAdapter currently only supports TypeScript projects:
aws-cdk-lib>= 2.0.0 (requiresconstructsversion 10)cdktf>= 0.7.0@cdktf/aws-cdk>= 0.1 (contains the adapter)
Install the Adapter
To install theAwsTerraformAdapter:
- Set up a CDKTF TypeScript project. Refer to Project Setup for details.
-
Install the required packages via yarn or npm.
Use the Adapter
Import theAwsTerraformAdapter from @cdktf/aws-cdk and initialize it the same way you would initialize any other construct.
awsAdapter as the scope argument instead of this (referring to MyStack).
AwsTerraformAdapter adds an Aspect to MyStack that is invoked when the stack is synthesized. That Aspect then iterates over all AWS CDK constructs that have been added to the adapter, converts them to CDK Terrain constructs, and adds them to the stack. It does not add the AWS CDK constructs themselves.
The full example code is available in the cdktf-aws-cdk repository.
Write Manual Mappings
While some mappings are already included, you need to manually map most resources that the AWS Cloud Control API does not yet support (supported resources). The following example shows how to write and register a mapping for anAWS::DynamoDB::Table CloudFormation resource.
Consider the following code:
MyStack.
Register the Mapping
The example code imports theregisterMapping function and invokes it with arguments for registering an AWS::DynamoDB::Table resource. The second argument to the function is an object with two parts: resource and attributes. The resource is a function that is called for each instance of the registered CloudFormation type. The example logs the props and returns null, so AWS doesn’t create any resources.
props.
AWS::DynamoDB::Table resource.
Create Mappings
Write code that maps all attributes to a format that matches the Terraform resource for an AWS DynamoDBTable. For the target schema, you can either look at the docs on the Terraform Registry or at the code of theDynamoDB.DynamodbTableConfig you will supply to the resource upon creation.
The logged props show that you need to support at least setting these attributes and that you need to make sure they appear in the resulting CDKTF resources.
props object (that are not undefined) after the mapping function returned. This is a safeguard of the mapping implementation to block mappings that do not support all properties at the time they were written.
AttributeDefinitions array to make sure it fits the schema of the Terraform resource. It also looks up the hashKey in the KeySchema array. Finally, it deletes the properties that have been handled and returns a new DynamodbTable resource.
ProvisionedThroughput property has not yet been mapped or deleted. The following example shows a complete mapping for the DynamoDB table.
aws_dynamodb_table resource in the cdk.tf.json output file (available in ./cdktf.out/stacks/<stack>).
Map the Attributes Argument
You should also map theattributes argument. When AWS CDK constructs reference each other’s properties, they do so via the CloudFormation property name of the resource.
The follwing example maps the Arn CloudFormation property to the .arn of the Terraform resource. While this example might look like something that could be handled automatically, there are cases where this cannot map directly. For example, there are some resources that do not have a direct representation in Terraform but do in CloudFormation (and vice versa).
Arn property to the empty attributes object in the mapping.
Examples
For additional examples, reference the adapters repository.Known limitations
The adapter is an early preview of interoperability with AWS CDK constructs, and it has the following limitations:- Limited interoperability between CDKTF and AWS CDK tokens. For example, passing Terraform functions as arguments into AWS CDK constructs might unexpectedly fail.
- AWS CDK App, Stack, and nested Stack constructs are not supported.
- These CloudFormation features are not yet supported: Transforms, Parameters, Mappings, and Includes.
- These AWS CDK features are not yet supported: Assets, Aspects, and Annotations.