Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfraney committed Aug 19, 2021
1 parent 0e4c33b commit 573b3cf
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 1 deletion.
20 changes: 19 additions & 1 deletion README.md
@@ -1,2 +1,20 @@
# cdktf-remote-template-python-poetry
A terraform-cdk CLI template for Python projects using Poetry for dependency management

A [terraform-cdk](https://github.com/hashicorp/terraform-cdk) CLI template for Python projects using [Poetry](https://python-poetry.org) for dependency management.

## Usage

Before starting a project with this template, make sure [Poetry](https://python-poetry.org/docs/#installation) is installed on your system.

Then, in the `--template` argument to `cdktf init`, include the URL to a `.zip` file of this project.
The `.zip` file can either be a branch:

```bash
cdktf init --template="https://github.com/johnfraney/cdktf-remote-template-python-poetry/archive/refs/heads/main.zip" --local
```

Or a tag:

```bash
cdktf init --template="https://github.com/johnfraney/cdktf-remote-template-python-poetry/archive/refs/tags/v1.1.0.zip --local
```
53 changes: 53 additions & 0 deletions python-poetry/.hooks.sscaff.js
@@ -0,0 +1,53 @@
const { execSync } = require('child_process');
const { chmodSync } = require('fs');
const { readFileSync, writeFileSync } = require('fs');
const os = require('os');

exports.pre = () => {
try {
if (os.platform() === 'win32') {
execSync('where poetry')
}
else {
execSync('which poetry')
}
} catch {
console.error(`Unable to find "poetry". Install from https://python-poetry.org/docs/#installation`)
process.exit(1);
}
};

exports.post = options => {
// Terraform Cloud configuration settings if the organization name and workspace is set.
if (options.OrganizationName != '') {
console.log(`\nGenerating Terraform Cloud configuration for '${options.OrganizationName}' organization and '${options.WorkspaceName}' workspace.....`)
terraformCloudConfig(options.$base, options.OrganizationName, options.WorkspaceName)
}

const pypi_cdktf = options.pypi_cdktf;
if (!pypi_cdktf) {
throw new Error(`missing context "pypi_cdktf"`);
}

execSync('poetry install', { stdio: 'inherit' });
execSync(`poetry add ${pypi_cdktf}`, { stdio: 'inherit' });
chmodSync('main.py', '700');

console.log(readFileSync('./help', 'utf-8'));
};

function terraformCloudConfig(baseName, organizationName, workspaceName) {
template = readFileSync('./main.py', 'utf-8');

const templateWithImports = template.replace(`from cdktf import App, TerraformStack`,
`from cdktf import App, TerraformStack, RemoteBackend, NamedRemoteWorkspace`)

const result = templateWithImports.replace(`MyStack(app, "${baseName}")`, `stack = MyStack(app, "${baseName}")
RemoteBackend(stack,
hostname='app.terraform.io',
organization='${organizationName}',
workspaces=NamedRemoteWorkspace('${workspaceName}')
)`);

writeFileSync('./main.py', result, 'utf-8');
}
11 changes: 11 additions & 0 deletions python-poetry/cdktf.json
@@ -0,0 +1,11 @@
{
"language": "python",
"app": "poetry run python main.py",
"terraformProviders": [],
"terraformModules": [],
"codeMakerOutput": "imports",
"context": {
"excludeStackIdFromLogicalIds": "true",
"allowSepCharsInLogicalIds": "true"
}
}
24 changes: 24 additions & 0 deletions python-poetry/help
@@ -0,0 +1,24 @@
========================================================================================================

Your cdktf Python project is ready!

cat help Prints this message

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

Synthesize:
cdktf synth [stack] Synthesize Terraform resources to cdktf.out/

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

Deploy:
cdktf deploy [stack] Deploy the given stack

Destroy:
cdktf destroy [stack] Destroy the given stack

Learn more about using modules and providers https://cdk.tf/modules-and-providers

========================================================================================================
16 changes: 16 additions & 0 deletions python-poetry/main.py
@@ -0,0 +1,16 @@
#!/usr/bin/env python
from constructs import Construct
from cdktf import App, TerraformStack


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

# define resources here


app = App()
MyStack(app, "cdktf-python-poetry")

app.synth()
13 changes: 13 additions & 0 deletions python-poetry/pyproject.toml
@@ -0,0 +1,13 @@
[tool.poetry]
name = "cdktf-python-poetry"
version = "1.0.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.6"
cdktf = "~=0.5.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
7 changes: 7 additions & 0 deletions python-poetry/{{}}.gitignore
@@ -0,0 +1,7 @@
dist/
imports/*
!imports/__init__.py
.terraform
cdktf.out
cdktf.log
*terraform.*.tfstate*

0 comments on commit 573b3cf

Please sign in to comment.