Skip to content

Commit 56576e2

Browse files
authored
add a python example for cdk for terraform (#214)
1 parent 06eefd9 commit 56576e2

File tree

9 files changed

+293
-0
lines changed

9 files changed

+293
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
imports/
3+
terraform.tfstate*

cdk-for-terraform/python/Pipfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[requires]
7+
python_version = "3.9"
8+
9+
[packages]
10+
constructs = "*"

cdk-for-terraform/python/Pipfile.lock

Lines changed: 116 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cdk-for-terraform/python/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# LocalStack Demo: Deploying Resources via CDK
2+
3+
Simple demo application illustrating deployment of AWS resources using [CDK for Terraform](https://developer.hashicorp.com/terraform/cdktf).
4+
5+
## Prerequisites
6+
7+
* LocalStack
8+
* Docker
9+
* [`cdktf`](https://developer.hashicorp.com/terraform/tutorials/cdktf/cdktf-install)
10+
* [Terraform](https://developer.hashicorp.com/terraform/downloads)
11+
* [`pipenv`](https://pipenv.pypa.io/en/latest/)
12+
13+
## Install dependencies
14+
15+
To install the dependencies, run the following command:
16+
17+
```bash
18+
pipenv install
19+
```
20+
21+
## Generate CDK for Terraform
22+
23+
To generate CDK for Terraform constructs for Terraform providers and modules used in the project, run the following command:
24+
25+
```bash
26+
cdktf get
27+
```
28+
29+
To compile and generate Terraform configuration, run the following command:
30+
31+
```bash
32+
cdktf synth
33+
```
34+
35+
The above command will create a folder called `cdktf.out` that contains all Terraform JSON configuration that was generated.
36+
37+
## Deploy the stack
38+
39+
To deploy the stack, run the following command:
40+
41+
```bash
42+
cdktf deploy
43+
```
44+
45+
## Configuration
46+
47+
LocalStack currently does not provide a wrapper (similar to `cdklocal` or `tflocal`) to run CDK for Terraform. Therefore, you need to configure the AWS `provider` to redirect requests to the LocalStack API (`http://localhost:4566` by default), using [Terraform Override mechanism](https://developer.hashicorp.com/terraform/language/files/override). Check the [`localstack_config.py`](./localstack_config.py) file for an example.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"language": "python",
3+
"app": "pipenv run python main.py",
4+
"terraformProviders": [
5+
"aws@~> 3.0"
6+
],
7+
"terraformModules": [
8+
"terraform-aws-modules/vpc/[email protected]"
9+
],
10+
"codeMakerOutput": "imports",
11+
"projectId": "220c9a67-c870-4548-88db-4880e869b1eb"
12+
}

cdk-for-terraform/python/help

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 Synthesize Terraform resources to cdktf.out/
12+
13+
Diff:
14+
cdktf diff Perform a diff (terraform plan) for the given stack
15+
16+
Deploy:
17+
cdktf deploy Deploy the given stack
18+
19+
Destroy:
20+
cdktf destroy Destroy the given stack
21+
22+
========================================================================================================
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
AWS_CONFIG = {
2+
"region": "us-east-1",
3+
"endpoints": [
4+
{
5+
"s3": "http://localhost:4566",
6+
"sts": "http://localhost:4566",
7+
"apigateway": "http://localhost:4566",
8+
"apigatewayv2": "http://localhost:4566",
9+
"cloudformation": "http://localhost:4566",
10+
"cloudwatch": "http://localhost:4566",
11+
"dynamodb": "http://localhost:4566",
12+
"ec2": "http://localhost:4566",
13+
"es": "http://localhost:4566",
14+
"elasticache": "http://localhost:4566",
15+
"firehose": "http://localhost:4566",
16+
"iam": "http://localhost:4566",
17+
"kinesis": "http://localhost:4566",
18+
"lambda": "http://localhost:4566",
19+
"rds": "http://localhost:4566",
20+
"redshift": "http://localhost:4566",
21+
"route53": "http://localhost:4566",
22+
"s3": "http://s3.localhost.localstack.cloud:4566",
23+
"secretsmanager": "http://localhost:4566",
24+
"ses": "http://localhost:4566",
25+
"sns": "http://localhost:4566",
26+
"sqs": "http://localhost:4566",
27+
"ssm": "http://localhost:4566",
28+
"stepfunctions": "http://localhost:4566",
29+
"sts": "http://localhost:4566",
30+
}
31+
],
32+
}

cdk-for-terraform/python/main.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from constructs import Construct
2+
from cdktf import App, TerraformStack
3+
from imports.aws.provider import AwsProvider
4+
from imports.aws.sns_topic import SnsTopic
5+
from imports.aws.dynamodb_table import DynamodbTable
6+
from imports.terraform_aws_modules.aws import Vpc
7+
from localstack_config import AWS_CONFIG
8+
9+
10+
class MyStack(TerraformStack):
11+
def __init__(self, scope: Construct, ns: str):
12+
super().__init__(scope, ns)
13+
14+
AwsProvider(self, "Aws", **AWS_CONFIG)
15+
16+
Vpc(
17+
self,
18+
"CustomVpc",
19+
name="custom-vpc",
20+
cidr="10.0.0.0/16",
21+
azs=["us-east-1a", "us-east-1b"],
22+
public_subnets=["10.0.1.0/24", "10.0.2.0/24"],
23+
)
24+
25+
SnsTopic(self, "Topic", display_name="my-first-sns-topic")
26+
27+
DynamodbTable(
28+
self,
29+
"DynamoDB",
30+
name="my-dynamodb-table",
31+
read_capacity=1,
32+
write_capacity=1,
33+
hash_key="Id",
34+
attribute=[
35+
{"name": "Id", "type": "S"},
36+
],
37+
)
38+
39+
40+
app = App()
41+
MyStack(app, "python-aws")
42+
43+
app.synth()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "localstack-cdk-for-terraform-python",
3+
"scripts": {
4+
"reinstall": "rm Pipfile.lock && PIPENV_IGNORE_VIRTUALENVS=1 pipenv install ./../../../dist/python/*.whl",
5+
"build": "cdktf get",
6+
"synth": "cdktf synth"
7+
}
8+
}

0 commit comments

Comments
 (0)