Skip to content

Commit

Permalink
add a python example for cdk for terraform (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshCasper authored Jun 20, 2023
1 parent 06eefd9 commit 56576e2
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cdk-for-terraform/python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
imports/
terraform.tfstate*
10 changes: 10 additions & 0 deletions cdk-for-terraform/python/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[requires]
python_version = "3.9"

[packages]
constructs = "*"
116 changes: 116 additions & 0 deletions cdk-for-terraform/python/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions cdk-for-terraform/python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# LocalStack Demo: Deploying Resources via CDK

Simple demo application illustrating deployment of AWS resources using [CDK for Terraform](https://developer.hashicorp.com/terraform/cdktf).

## Prerequisites

* LocalStack
* Docker
* [`cdktf`](https://developer.hashicorp.com/terraform/tutorials/cdktf/cdktf-install)
* [Terraform](https://developer.hashicorp.com/terraform/downloads)
* [`pipenv`](https://pipenv.pypa.io/en/latest/)

## Install dependencies

To install the dependencies, run the following command:

```bash
pipenv install
```

## Generate CDK for Terraform

To generate CDK for Terraform constructs for Terraform providers and modules used in the project, run the following command:

```bash
cdktf get
```

To compile and generate Terraform configuration, run the following command:

```bash
cdktf synth
```

The above command will create a folder called `cdktf.out` that contains all Terraform JSON configuration that was generated.

## Deploy the stack

To deploy the stack, run the following command:

```bash
cdktf deploy
```

## Configuration

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.
12 changes: 12 additions & 0 deletions cdk-for-terraform/python/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"language": "python",
"app": "pipenv run python main.py",
"terraformProviders": [
"aws@~> 3.0"
],
"terraformModules": [
"terraform-aws-modules/vpc/[email protected]"
],
"codeMakerOutput": "imports",
"projectId": "220c9a67-c870-4548-88db-4880e869b1eb"
}
22 changes: 22 additions & 0 deletions cdk-for-terraform/python/help
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
========================================================================================================

Your cdktf Python project is ready!

cat help Prints this message

Compile:
pipenv run ./main.py Compile and run the python code.

Synthesize:
cdktf synth Synthesize Terraform resources to cdktf.out/

Diff:
cdktf diff Perform a diff (terraform plan) for the given stack

Deploy:
cdktf deploy Deploy the given stack

Destroy:
cdktf destroy Destroy the given stack

========================================================================================================
32 changes: 32 additions & 0 deletions cdk-for-terraform/python/localstack_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
AWS_CONFIG = {
"region": "us-east-1",
"endpoints": [
{
"s3": "http://localhost:4566",
"sts": "http://localhost:4566",
"apigateway": "http://localhost:4566",
"apigatewayv2": "http://localhost:4566",
"cloudformation": "http://localhost:4566",
"cloudwatch": "http://localhost:4566",
"dynamodb": "http://localhost:4566",
"ec2": "http://localhost:4566",
"es": "http://localhost:4566",
"elasticache": "http://localhost:4566",
"firehose": "http://localhost:4566",
"iam": "http://localhost:4566",
"kinesis": "http://localhost:4566",
"lambda": "http://localhost:4566",
"rds": "http://localhost:4566",
"redshift": "http://localhost:4566",
"route53": "http://localhost:4566",
"s3": "http://s3.localhost.localstack.cloud:4566",
"secretsmanager": "http://localhost:4566",
"ses": "http://localhost:4566",
"sns": "http://localhost:4566",
"sqs": "http://localhost:4566",
"ssm": "http://localhost:4566",
"stepfunctions": "http://localhost:4566",
"sts": "http://localhost:4566",
}
],
}
43 changes: 43 additions & 0 deletions cdk-for-terraform/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from constructs import Construct
from cdktf import App, TerraformStack
from imports.aws.provider import AwsProvider
from imports.aws.sns_topic import SnsTopic
from imports.aws.dynamodb_table import DynamodbTable
from imports.terraform_aws_modules.aws import Vpc
from localstack_config import AWS_CONFIG


class MyStack(TerraformStack):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)

AwsProvider(self, "Aws", **AWS_CONFIG)

Vpc(
self,
"CustomVpc",
name="custom-vpc",
cidr="10.0.0.0/16",
azs=["us-east-1a", "us-east-1b"],
public_subnets=["10.0.1.0/24", "10.0.2.0/24"],
)

SnsTopic(self, "Topic", display_name="my-first-sns-topic")

DynamodbTable(
self,
"DynamoDB",
name="my-dynamodb-table",
read_capacity=1,
write_capacity=1,
hash_key="Id",
attribute=[
{"name": "Id", "type": "S"},
],
)


app = App()
MyStack(app, "python-aws")

app.synth()
8 changes: 8 additions & 0 deletions cdk-for-terraform/python/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "localstack-cdk-for-terraform-python",
"scripts": {
"reinstall": "rm Pipfile.lock && PIPENV_IGNORE_VIRTUALENVS=1 pipenv install ./../../../dist/python/*.whl",
"build": "cdktf get",
"synth": "cdktf synth"
}
}

0 comments on commit 56576e2

Please sign in to comment.