├── .gitignore ├── README.md ├── help └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Local .terraform directories 2 | **/.terraform/* 3 | 4 | # .tfstate files 5 | *.tfstate 6 | *.tfstate.* 7 | 8 | # Crash log files 9 | crash.log 10 | crash.*.log 11 | 12 | # Exclude all .tfvars files, which are likely to contain sensitive data, such as 13 | # password, private keys, and other secrets. These should not be part of version 14 | # control as they are data points which are potentially sensitive and subject 15 | # to change depending on the environment. 16 | *.tfvars 17 | *.tfvars.json 18 | 19 | # Ignore override files as they are usually used to override resources locally and so 20 | # are not checked in 21 | override.tf 22 | override.tf.json 23 | *_override.tf 24 | *_override.tf.json 25 | 26 | # Include override files you do wish to add to version control using negated pattern 27 | # !example_override.tf 28 | 29 | # Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan 30 | # example: *tfplan* 31 | 32 | # Ignore CLI configuration files 33 | .terraformrc 34 | terraform.rc 35 | 36 | 37 | # Byte-compiled / optimized / DLL files 38 | __pycache__/ 39 | *.py[cod] 40 | *$py.class 41 | 42 | # C extensions 43 | *.so 44 | 45 | # Distribution / packaging 46 | .Python 47 | build/ 48 | develop-eggs/ 49 | dist/ 50 | downloads/ 51 | eggs/ 52 | .eggs/ 53 | lib/ 54 | lib64/ 55 | parts/ 56 | sdist/ 57 | var/ 58 | wheels/ 59 | share/python-wheels/ 60 | *.egg-info/ 61 | .installed.cfg 62 | *.egg 63 | MANIFEST 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cdktf-python-aws-ec2 2 | 3 | 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. 4 | 5 | In this tutorial, you will provision an EC2 instance on AWS using your preferred programming language. 6 | 7 | ## Prerequisites 8 | 9 | * [Terraform](https://www.terraform.io/downloads) >= v1.0 10 | * [CDK for Terraform](https://learn.hashicorp.com/tutorials/terraform/cdktf-install) >= v0.8 11 | * A [Terraform Cloud](https://app.terraform.io/) account, with [CLI authentication](https://learn.hashicorp.com/tutorials/terraform/cloud-login) configured 12 | * [an AWS account](https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=default&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start) 13 | * AWS Credentials [configured for use with Terraform](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#authentication) 14 | 15 | 16 | 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. 17 | 18 | ```shell 19 | $ export AWS_ACCESS_KEY_ID="anaccesskey" 20 | $ export AWS_SECRET_ACCESS_KEY="asecretkey" 21 | $ export AWS_REGION="us-west-2" 22 | ``` 23 | 24 | ## Install project dependencies 25 | 26 | ```shell 27 | mkdir learn-cdktf 28 | cd learn-cdktf 29 | cdktf init --template="python" 30 | ``` 31 | 32 | ## Install AWS provider 33 | 34 | ```shell 35 | pipenv install cdktf-cdktf-provider-aws 36 | ``` 37 | 38 | ## Define your CDK for Terraform Application 39 | 40 | Replace the contents of main.py with the following code for a new Python application 41 | 42 | ```python 43 | #!/usr/bin/env python 44 | from constructs import Construct 45 | from cdktf import App, NamedRemoteWorkspace, TerraformStack, TerraformOutput, RemoteBackend 46 | from cdktf_cdktf_provider_aws import AwsProvider, ec2 47 | 48 | 49 | class MyStack(TerraformStack): 50 | def __init__(self, scope: Construct, ns: str): 51 | super().__init__(scope, ns) 52 | 53 | AwsProvider(self, "AWS", region="us-west-1") 54 | 55 | instance = ec2.Instance(self, "compute", 56 | ami="ami-01456a894f71116f2", 57 | instance_type="t2.micro", 58 | ) 59 | 60 | TerraformOutput(self, "public_ip", 61 | value=instance.public_ip, 62 | ) 63 | 64 | 65 | app = App() 66 | stack = MyStack(app, "aws_instance") 67 | 68 | RemoteBackend(stack, 69 | hostname='app.terraform.io', 70 | organization='', 71 | workspaces=NamedRemoteWorkspace('learn-cdktf') 72 | ) 73 | 74 | app.synth() 75 | ``` 76 | ## Provision infrastructure 77 | ```shell 78 | cdktf deploy 79 | ``` 80 | After the instance is created, visit the AWS EC2 Dashboard. 81 | 82 | ## Clean up your infrastructure 83 | ```shell 84 | cdktf destroy 85 | ``` 86 | -------------------------------------------------------------------------------- /help: -------------------------------------------------------------------------------- 1 | ======================================================================================================== 2 | 3 | Your cdktf Python project is ready! 4 | 5 | cat help Prints this message 6 | 7 | Compile: 8 | pipenv run ./main.py Compile and run the python code. 9 | 10 | Synthesize: 11 | cdktf synth [stack] Synthesize Terraform resources to cdktf.out/ 12 | 13 | Diff: 14 | cdktf diff [stack] Perform a diff (terraform plan) for the given stack 15 | 16 | Deploy: 17 | cdktf deploy [stack] Deploy the given stack 18 | 19 | Destroy: 20 | cdktf destroy [stack] Destroy the given stack 21 | 22 | Learn more about using modules and providers https://cdk.tf/modules-and-providers 23 | 24 | ======================================================================================================== 25 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | from constructs import Construct 3 | from cdktf import App, TerraformStack 4 | from imports.aws import SnsTopic, AwsProvider 5 | from imports.terraform_aws_modules.vpc.aws import Vpc 6 | 7 | class MyStack(TerraformStack): 8 | def __init__(self, scope: Construct, ns: str): 9 | super().__init__(scope, ns) 10 | 11 | AwsProvider(self, 'Aws', region='eu-central-1') 12 | 13 | Vpc(self, 'CustomVpc', 14 | name='custom-vpc', 15 | cidr='10.0.0.0/16', 16 | azs=["us-east-1a", "us-east-1b"], 17 | public_subnets=["10.0.1.0/24", "10.0.2.0/24"] 18 | ) 19 | SnsTopic(self, 'Topic', display_name='my-first-sns-topic') 20 | 21 | app = App() 22 | MyStack(app, "python-aws") 23 | 24 | app.synth() 25 | --------------------------------------------------------------------------------