Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0e4c33b
commit 573b3cf
Showing
7 changed files
with
143 additions
and
1 deletion.
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 |
---|---|---|
@@ -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 | ||
``` |
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,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'); | ||
} |
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,11 @@ | ||
{ | ||
"language": "python", | ||
"app": "poetry run python main.py", | ||
"terraformProviders": [], | ||
"terraformModules": [], | ||
"codeMakerOutput": "imports", | ||
"context": { | ||
"excludeStackIdFromLogicalIds": "true", | ||
"allowSepCharsInLogicalIds": "true" | ||
} | ||
} |
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,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 | ||
|
||
======================================================================================================== |
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,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() |
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,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" |
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,7 @@ | ||
dist/ | ||
imports/* | ||
!imports/__init__.py | ||
.terraform | ||
cdktf.out | ||
cdktf.log | ||
*terraform.*.tfstate* |