Skip to content

Commit c21feb8

Browse files
committed
New Python template without pipenv
Closes #327
1 parent d62599d commit c21feb8

File tree

6 files changed

+119
-1
lines changed

6 files changed

+119
-1
lines changed

docs/getting-started/python.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [Terraform](https://www.terraform.io/downloads.html) >= v0.12
66
- [Node.js](https://nodejs.org) >= v12.16
77
- [Python](https://www.python.org/downloads/) >= v3.7
8-
- [Pipenv](https://pipenv.pypa.io/en/latest/install/#installing-pipenv/)
8+
- Optional: [Pipenv](https://pipenv.pypa.io/en/latest/install/#installing-pipenv/) (see below)
99

1010
### Install CDK for Terraform CLI
1111

@@ -20,9 +20,24 @@ Learn more how to use the cdktf command-line interface [here](../cli-commands.md
2020
```bash
2121
mkdir hello-terraform
2222
cd hello-terraform
23+
```
24+
25+
There are two Python templates available that you can choose from.
26+
The `python` template uses `Pipenv` for package management wheras the
27+
`python-pip` template just uses `pip` with a simple `requirements.txt` file.
28+
29+
Here's how to choose between the two
30+
31+
### pipenv
32+
```bash
2333
cdktf init --template="python" --local
2434
```
2535

36+
### pip
37+
```bash
38+
cdktf init --template="python-pip" --local
39+
```
40+
2641
This will initialize a brand new CDK for Terraform project in Python using an interactive command.
2742

2843
```bash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const { execSync } = require('child_process');
2+
const { chmodSync } = require('fs');
3+
const { readFileSync, writeFileSync } = require('fs');
4+
5+
const cli = require.resolve('../../bin/cdktf');
6+
7+
exports.pre = () => {
8+
try {
9+
execSync('which pip')
10+
} catch {
11+
console.error(`Unable to find "pip".`)
12+
process.exit(1);
13+
}
14+
};
15+
16+
exports.post = options => {
17+
// Terraform Cloud configuration settings if the organization name and workspace is set.
18+
if (options.OrganizationName != '') {
19+
console.log(`\nGenerating Terraform Cloud configuration for '${options.OrganizationName}' organization and '${options.WorkspaceName}' workspace.....`)
20+
terraformCloudConfig(options.$base, options.OrganizationName, options.WorkspaceName)
21+
}
22+
23+
const pypi_cdktf = options.pypi_cdktf;
24+
if (!pypi_cdktf) {
25+
throw new Error(`missing context "pypi_cdktf"`);
26+
}
27+
28+
writeFileSync('requirements.txt', pypi_cdktf, 'utf-8');
29+
execSync('pip install -r requirements.txt', { stdio: 'inherit' });
30+
chmodSync('main.py', '700');
31+
32+
execSync(`${cli} get`, { stdio: 'inherit' });
33+
execSync(`python3 ./main.py`);
34+
35+
console.log(readFileSync('./help', 'utf-8'));
36+
};
37+
38+
function terraformCloudConfig(baseName, organizationName, workspaceName) {
39+
template = readFileSync('./main.py', 'utf-8');
40+
41+
const result = template.replace(`MyStack(app, "${baseName}")`, `stack = MyStack(app, "${baseName}")
42+
stack.add_override('terraform.backend', {
43+
'remote': {
44+
'hostname': 'app.terraform.io',
45+
'organization': '${organizationName}',
46+
'workspaces': {
47+
'name': '${workspaceName}'
48+
}
49+
}
50+
})`);
51+
52+
writeFileSync('./main.py', result, 'utf-8');
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"language": "python",
3+
"app": "python3 ./main.py",
4+
"terraformProviders": ["aws@~> 2.0"],
5+
"codeMakerOutput": "imports"
6+
}
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+
python3 ./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+
========================================================================================================
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
from constructs import Construct
3+
from cdktf import App, TerraformStack
4+
5+
6+
class MyStack(TerraformStack):
7+
def __init__(self, scope: Construct, ns: str):
8+
super().__init__(scope, ns)
9+
10+
# define resources here
11+
12+
13+
app = App()
14+
MyStack(app, "{{ $base }}")
15+
16+
app.synth()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dist/
2+
imports/*
3+
!imports/__init__.py
4+
.terraform
5+
cdktf.out
6+
terraform.tfstate*

0 commit comments

Comments
 (0)