Skip to content

Commit

Permalink
New Python template without pipenv
Browse files Browse the repository at this point in the history
Closes #327
  • Loading branch information
cmclaughlin committed Aug 25, 2020
1 parent 2495f45 commit b8dd9c1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 1 deletion.
17 changes: 16 additions & 1 deletion docs/getting-started/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [Terraform](https://www.terraform.io/downloads.html) >= v0.12
- [Node.js](https://nodejs.org) >= v12.16
- [Python](https://www.python.org/downloads/) >= v3.7
- [Pipenv](https://pipenv.pypa.io/en/latest/install/#installing-pipenv/)
- Optional: [Pipenv](https://pipenv.pypa.io/en/latest/install/#installing-pipenv/) (see below)

### Install CDK for Terraform CLI

Expand All @@ -20,9 +20,24 @@ Learn more how to use the cdktf command-line interface [here](../cli-commands.md
```bash
mkdir hello-terraform
cd hello-terraform
```

There are two Python templates available that you can choose from.
The `python` template uses `Pipenv` for package management wheras the
`python-pip` template just uses `pip` with a simple `requirements.txt` file.

Here's how to choose between the two

### pipenv
```bash
cdktf init --template="python" --local
```

### pip
```bash
cdktf init --template="python-pip" --local
```

This will initialize a brand new CDK for Terraform project in Python using an interactive command.

```bash
Expand Down
53 changes: 53 additions & 0 deletions packages/cdktf-cli/templates/python-pip/.hooks.sscaff.js
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 cli = require.resolve('../../bin/cdktf');

exports.pre = () => {
try {
execSync('which pip')
} catch {
console.error(`Unable to find "pip".`)
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"`);
}

writeFileSync('requirements.txt', pypi_cdktf, 'utf-8');
execSync('pip install -r requirements.txt', { stdio: 'inherit' });
chmodSync('main.py', '700');

execSync(`${cli} get`, { stdio: 'inherit' });
execSync(`python3 ./main.py`);

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

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

const result = template.replace(`MyStack(app, "${baseName}")`, `stack = MyStack(app, "${baseName}")
stack.add_override('terraform.backend', {
'remote': {
'hostname': 'app.terraform.io',
'organization': '${organizationName}',
'workspaces': {
'name': '${workspaceName}'
}
}
})`);

writeFileSync('./main.py', result, 'utf-8');
}
6 changes: 6 additions & 0 deletions packages/cdktf-cli/templates/python-pip/cdktf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"language": "python",
"app": "python3 ./main.py",
"terraformProviders": ["aws@~> 2.0"],
"codeMakerOutput": "imports"
}
22 changes: 22 additions & 0 deletions packages/cdktf-cli/templates/python-pip/help
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:
python3 ./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

========================================================================================================
16 changes: 16 additions & 0 deletions packages/cdktf-cli/templates/python-pip/main.py
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, "{{ $base }}")

app.synth()
6 changes: 6 additions & 0 deletions packages/cdktf-cli/templates/python-pip/{{}}.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dist/
imports/*
!imports/__init__.py
.terraform
cdktf.out
terraform.tfstate*

0 comments on commit b8dd9c1

Please sign in to comment.