-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a python example for cdk for terraform (#214)
- Loading branch information
1 parent
06eefd9
commit 56576e2
Showing
9 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
dist/ | ||
imports/ | ||
terraform.tfstate* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "*" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
======================================================================================================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |