Hướng dẫn terraform python cdk
Developers are accustomed to using their own tooling and writing in familiar languages. For several years, HashiCorp Configuration Language (HCL) has been one of the fastest growing languages at GitHub, and yet we have often heard the desire to provision infrastructure with familiar programming languages. Today, we are pleased to announce the community preview of the Cloud Development Kit for HashiCorp Terraform which allows users to define infrastructure using
TypeScript and Python while leveraging the hundreds of providers and thousands of module definitions provided by Terraform and the Terraform ecosystem. Nội dung chính Nội dung chính
We collaborated with AWS and their Cloud Development Kit (CDK) team to support this new project leveraging two key components of the AWS CDK: a set of language native frameworks for defining infrastructure and adaptors to an underlying provisioning tool. Using these libraries, the AWS CDK generates CloudFormation configuration. Similarly, the CDK for Terraform generates Terraform configuration to enable provisioning with Terraform. The adaptor works with any existing provider and modules hosted in the Terraform Registry. The core Terraform workflow remains the same, with the ability to plan changes before applying. You can use this project today with our initial release. We've created step-by-step examples with a Docker quick start tutorial, AWS, and other examples in Python and Typescript. »Terraform as a PlatformThe goal of Terraform is to provide a consistent workflow for provisioning infrastructure. Terraform follows an infrastructure as code approach and is extensible to support many providers of cloud infrastructure and software services. The advantages of codification are the ability to automate the creation of infrastructure, use a version control-driven workflow, collaboratively review changes, and use best practices of software development. With the introduction of CDK support for Terraform, there are more ways than ever to provision and manage infrastructure with Terraform. Historically we have supported HCL and JSON. While HCL was meant to be read and written by people, JSON was meant to be machine-generated and consumed. Earlier this year, we announced experimental support for Kubernetes Custom Resource Definitions (CRDs) using a Terraform Operator. This enables Kubernetes users to configure Terraform resources with a Kubernetes native workflow. The Custom Resource Definition gets translated into Terraform, and executed remotely by Terraform Cloud. Traditional businesses that use ITSM software like ServiceNow can integrate with Terraform Enterprise to support a more traditional ticketing based approach. Finally, with the CDK support, programming languages such as Python and TypeScript can be used today. Additional language support for Javascript, Java, and C# could be used in the future. This enables Terraform to act as a common platform for infrastructure provisioning and lifecycle management. Today there are hundreds of Terraform providers, spanning public cloud, private cloud, networking and storage hardware, cloud services, and more. Users of Terraform can compose their infrastructure and extend it to support their needs easily. Our goal is to make it as accessible as possible to use Terraform, and we do that by providing the interfaces users find most convenient. There is a broad range of interfaces available today, and we continue to learn from our community and customers about where we can improve. While Terraform provides the core platform for provisioning, we’ve built Terraform Cloud on top to provide the core platform for collaboration as well. By providing a central place to store state, manage permissions, enable webhooks, and enforce policy we make it easy for teams of tens, hundreds, or thousands to collaborate on infrastructure. By supporting APIs and webhooks, we make it easy to integrate Terraform Cloud with other systems, such as GitHub, GitLab, CircleCI, and more. This allows you to use Terraform Cloud as part of a more complex delivery pipeline, but still maintain visibility and control. With the CDK for Terraform project you can define infrastructure resources using the supported programming languages. The tool generates a Terraform configuration in JSON that can be applied with The CDK for Terraform project includes two packages:
»Getting StartedThe community preview for CDK for Terraform includes features that allow a user familiar with Terraform to write programming constructs and generate Terraform configuration in JSON files. Use the CDK for Terraform CLI to initialize a project in either Typescript or Python. The example for getting started uses TypeScript. Begin by installing Node.js, yarn. Next, install the CDK for Terraform globally.
»Initialize a New ProjectCreate a directory to store TypeScript files for creating an AWS VPC. Initialize a set of TypeScript templates using
Enter details regarding the project including Terraform Cloud for storing the project state. You can use the
Several steps occur when running
After running
In To create a subnet based on the VPC identifier, pass the
»Synthesize TypeScript to Terraform ConfigurationNext, synthesize TypeScript to Terraform configuration by running
Inspect the generated Terraform JSON file by examining
Users can also print Terraform JSON configuration in their terminal using After synthesis, users can use the Terraform workflow of initializing, planning, and applying changes within the Terraform workflow is as follows:
CDK for Terraform CLI is as follows:
To
destroy, from the root directory run
The example demonstrates the synthesis of two AWS resources. To include resources from other providers, such as the Chef Provider, add the provider and its version to
To generate the TypeScript objects for the Chef Provider, run
In
The same workflow applies to the synthesis of resources for other Terraform providers and modules within the Terraform Registry. If you would like to use Python to generate Terraform configurations, check out the Getting Started guide for Python. »Community CollaborationWe would like to thank Sebastian Korfmann, maintainer of the Terrastack project, who helped to deliver the CDK for Terraform project. Talking to Sebastian we came to realize that the Terrastack project had a similar vision for providing support for native programming language for Terraform resources. We are pleased to share that we have Sebastian as a maintainer of the CDK for Terraform project and will continue to contribute to the project going forward. Additionally, we would like to recognize the AWS CDK team for helping us kick off the project and providing recommendations on patterns and practices for CDK constructs and jsii libraries. »What’s NextCDK for Terraform is in alpha and is in the early stages of development. This project is another way to interface with Terraform using languages like TypeScript and Python. We are working to support additional languages such as JavaScript, Java, and C#. In addition to language support, we plan on expanding the scope of CDK for Terraform project to support other first-class commands to provide a user experience similar to the AWS CDK. Learn more about the CDK for Terraform at hashicorp/terraform-cdk. For more information about AWS CDK and its frameworks, check out its FAQ page. Additional ecosystem projects for CDK include the CDK for Kubernetes. Try it yourself with our step-by-step examples using a Docker quick start tutorial, AWS, and other examples in Python and Typescript. Since CDK for Terraform is an experimental project, we would like to ask the community for feedback. If you would like to see a feature for the CDK for Terraform, please review existing GitHub issues and upvote. If a feature does not exist in a GitHub issue, feel free to open a new issue. In addition to opening issues, you can contribute to the project by opening a pull request. Also, use the HashiCorp Discuss form to ask questions and share your feedback. |