Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Python template without pipenv #327

Merged
merged 2 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
skorfmann marked this conversation as resolved.
Show resolved Hide resolved

### 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*