The Cloud Development Kit for Terraform (CDKTF) allows you to define your infrastructure in a familiar programming language such as TypeScript, Python, Go, C#, or Java.
In this tutorial, you will provision an EC2 instance on AWS using your preferred programming language.
- Terraform >= v1.0
- CDK for Terraform >= v0.8
- A Terraform Cloud account, with CLI authentication configured
- an AWS account
- AWS Credentials configured for use with Terraform
Credentials can be provided by using the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and optionally AWS_SESSION_TOKEN environment variables. The region can be set using the AWS_REGION or AWS_DEFAULT_REGION environment variables.
$ export AWS_ACCESS_KEY_ID="anaccesskey"
$ export AWS_SECRET_ACCESS_KEY="asecretkey"
$ export AWS_REGION="us-west-2"
mkdir cdktf-go-aws-vpc
cd cdktf-go-aws-vpc
cdktf init --template="go"
Open cdktf.json
in your text editor, and add aws
as one of the Terraform providers that you will use in the application.
{
"language": "go",
"app": "go run *.go",
"codeMakerOutput": "generated",
"projectId": "02f2d864-a2f2-49e8-ab52-b472e233755e",
"sendCrashReports": "false",
"terraformProviders": [
"hashicorp/aws@~> 3.67.0"
],
"terraformModules": [],
"context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
}
}
Run cdktf get
to install the AWS provider you added to cdktf.json
.
cdktf get
CDKTF uses a library called jsii
to allow Go code to interact with CDK,
which is written in TypeScript.
Ensure that the jsii runtime is installed by running go mod tidy
.
go mod tidy
cdktf deploy
After the instance is created, visit the AWS EC2 Dashboard.
cdktf destroy