Skip to content

Commit 7faebbe

Browse files
authored
Merge pull request #327 from cmclaughlin/rm-pipenv
New Python template without pipenv
2 parents 658b195 + 4c65afd commit 7faebbe

File tree

6 files changed

+121
-1
lines changed

6 files changed

+121
-1
lines changed

docs/getting-started/python.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
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/)
98

109
### Install CDK for Terraform CLI
1110

@@ -27,9 +26,27 @@ Learn more how to use the cdktf command-line interface [here](../cli-commands.md
2726
```bash
2827
mkdir hello-terraform
2928
cd hello-terraform
29+
```
30+
31+
There are two Python templates available that you can choose from.
32+
The `python` template uses `Pipenv` for package management wheras the
33+
`python-pip` template just uses `pip` with a simple `requirements.txt` file.
34+
35+
Here's how to choose between the two
36+
37+
### pipenv
38+
39+
Note: Make sure [Pipenv](https://pipenv.pypa.io/en/latest/install/#installing-pipenv/) is installed.
40+
41+
```bash
3042
cdktf init --template="python" --local
3143
```
3244

45+
### pip
46+
```bash
47+
cdktf init --template="python-pip" --local
48+
```
49+
3350
This will initialize a brand new CDK for Terraform project in Python using an interactive command.
3451

3552
```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)