CDK for Terraform Improves HCL Conversion and Terraform Cloud Interactions
source link: https://www.infoq.com/news/2023/04/cdk-terraform-convert/?itm_source=infoq&itm_medium=popular_widget&itm_campaign=popular_content_list&itm_content=
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
CDK for Terraform Improves HCL Conversion and Terraform Cloud Interactions
Apr 22, 2023 2 min read
HashiCorp has released a number of new improvements to the CDK for Terraform (CDKTF). These improvements include enhanced type coercion, iterators support, and function support. Other improvements target the experience of working with CDKTF within Terraform Cloud or Terraform Enterprise. This includes improvements to plan and apply and the automated creation of Terraform workspaces.
The CDK allows for writing Terraform configurations in a number of programming languages including C#, Python, TypeScript, Go, and Java. It includes support for all existing Terraform providers and modules. The CDKTF application code synthesizes into JSON output that can be deployed with Terraform directly.
The 0.16 release improves type coercion by updating the convert
command to match the type of value being assigned to an attribute. The convert
command is used to convert pre-existing Terraform HCL code into a CDKTF-compatible language. The command now compares the type being generated from HCL and matches it to the provider schema. In the event of a type mismatch, the incoming value will be coerced into the correct type. This change prevents errors that could occur in previous releases as Terraform automatically converts primitive types to match the resource schema.
The release also improved the conversion of meta-arguments such as count
and for_each
. Now the count meta-attribute can be represented as an iterator via an escape hatch. In addition, convert is able to use iterators for for_each
, count
, and dynamic blocks without an escape hatch.
The following TypeScript example uses the new TerraformCount
function to create the specified number of instances as defined within servers.numberValue
:
const servers = new TerraformVariable(this, "servers", {
type: "number",
});
const count = TerraformCount.of(servers.numberValue);
new Instance(this, "server", {
count: count,
ami: "ami-a1b2c3d4",
instanceType: "t2.micro",
tags: {
Name: "Server ${" + count.index + "}",
},
});
HashiCorp recommends using iterators when referencing dynamic data that will not be known until after Terraform applies the configuration. For static data, they recommend using loops within the selected programming language. Escape hatches are needed when referencing a specific index within the list. This is because CDKTF implicitly converts lists to sets when iterating over them.
Conversion can also now convert Terraform functions to the appropriate CDKTF function. This improves the readability of code and permits autocomplete to function properly. With this change, the following HCL expression:
replace("hello-${22+22}", "44", "world")
Would be converted to:
cdktf.Fn.replace("hello-" + cdktf.Token.asString(cdktf.Op.add(22, 22)), "44", "world")
The 0.15 release made a number of improvements to how CDKTF interacts with both the Terraform Cloud and Terraform Enterprise environments. In previous releases, during stack configuration with CDKTF, the interaction with Terraform inside Terraform Cloud was hidden. It could only be viewed through a URL.
This release replaced the Terraform Cloud API-based implementation with one that makes calls directly to the Terraform CLI. This allows for the full plan to be displayed with details. Additionally, CDKTF now supports additional features such as cost estimations and Sentinel policies when run within Terraform Cloud.
The release also introduced automatic workspace creation when running plan and diff or apply and deploy. This improves the previous behavior where workspaces would have to be manually created through the Terraform Cloud or Enterprise UI.
More information about the contents of the release can be found in the blog post and upgrade guide. HashiCorp has a discussion forum for questions. The CDK for Terraform tutorials are recommended for users new to CDKTF.
About the Author
Matt Campbell
Matt leads the DevOps editoral team for InfoQ. He is the VP of Cloud Platform at D2L, an education technology company, and is responsible for their Infrastructure and Cloud platform teams. His area of focus is DevOps and SRE and implementing these at enterprise scale.
Show moreRecommend
-
6
The Go Cloud Development Kit The Go Cloud Development Kit (Go CDK) is an open source project building libraries and tools to improve the experience of developing for the cloud with Go. G...
-
17
Alexander ZeitlerMinimal IAM permissions for AWS CDK deploymentsPublished on Wednesday, September 30, 2020AWS CDK is leveraging AWS CloudFormation to deploy Stacks in AWS....
-
5
Imperative Infrastructure as Code using AWS CDK📅 September 11, 2019 – Kyle GalbraithWhen we talk about infrastructure as code we often talk about declarative frameworks. Frameworks like Terraform or CloudFormation often come t...
-
7
Infrastructure as JS with AWS CDKWhen you looked into backend development, you probably found out quickly that it isn't just about "coding an API." The API has to be hosted somewhere; you need data-stores, compute, load balancer, API gateways...
-
2
Cross-Account Inception Pipeline with AWS CDK ...
-
7
👋 Hey! My name is Wojciech Gawroński, but others call me AWS Maniac.My goal is to safely guide you through the cloudy and foggy space of the AWS portfolio.
-
7
Dropbox Improves Mobile Camera Uploads, Adds In-App File Conversion
-
5
An Introduction To AWS Cloud Development Kit (CDK)Quick summary ↬ In this article, Vivek Maskara introduces Amazon Web Services’ (AWS) Cloud Development Kit (CDK) which is increasingly becoming a popular tool for managin...
-
10
AWS CDK (Cloud Development Kit) Write dynamic Python, Java, .NET, NodeJs, or T...
-
5
CDK for Terraform Improves Performance by Adding Namespaces Oct 23, 2022...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK