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 learn-cdktf
cd learn-cdktf
cdktf init --template="python"
pipenv install cdktf-cdktf-provider-aws
Replace the contents of main.py with the following code for a new Python application
#!/usr/bin/env python
from constructs import Construct
from cdktf import App, NamedRemoteWorkspace, TerraformStack, TerraformOutput, RemoteBackend
from cdktf_cdktf_provider_aws import AwsProvider, ec2
class MyStack(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)
AwsProvider(self, "AWS", region="us-west-1")
instance = ec2.Instance(self, "compute",
ami="ami-01456a894f71116f2",
instance_type="t2.micro",
)
TerraformOutput(self, "public_ip",
value=instance.public_ip,
)
app = App()
stack = MyStack(app, "aws_instance")
RemoteBackend(stack,
hostname='app.terraform.io',
organization='<YOUR_ORG>',
workspaces=NamedRemoteWorkspace('learn-cdktf')
)
app.synth()
cdktf deploy
After the instance is created, visit the AWS EC2 Dashboard.
cdktf destroy