diff --git a/docs/getting-started/python.md b/docs/getting-started/python.md index bcb0d3ff2b..3b1bf593b4 100644 --- a/docs/getting-started/python.md +++ b/docs/getting-started/python.md @@ -5,7 +5,6 @@ - [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/) ### Install CDK for Terraform CLI @@ -20,9 +19,27 @@ 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 + +Note: Make sure [Pipenv](https://pipenv.pypa.io/en/latest/install/#installing-pipenv/) is installed. + +```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 diff --git a/packages/cdktf-cli/templates/python-pip/.hooks.sscaff.js b/packages/cdktf-cli/templates/python-pip/.hooks.sscaff.js new file mode 100644 index 0000000000..7799158bfd --- /dev/null +++ b/packages/cdktf-cli/templates/python-pip/.hooks.sscaff.js @@ -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'); +} diff --git a/packages/cdktf-cli/templates/python-pip/cdktf.json b/packages/cdktf-cli/templates/python-pip/cdktf.json new file mode 100644 index 0000000000..d5d565165c --- /dev/null +++ b/packages/cdktf-cli/templates/python-pip/cdktf.json @@ -0,0 +1,6 @@ +{ + "language": "python", + "app": "python3 ./main.py", + "terraformProviders": ["aws@~> 2.0"], + "codeMakerOutput": "imports" +} diff --git a/packages/cdktf-cli/templates/python-pip/help b/packages/cdktf-cli/templates/python-pip/help new file mode 100644 index 0000000000..dad363957e --- /dev/null +++ b/packages/cdktf-cli/templates/python-pip/help @@ -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 + +======================================================================================================== \ No newline at end of file diff --git a/packages/cdktf-cli/templates/python-pip/main.py b/packages/cdktf-cli/templates/python-pip/main.py new file mode 100644 index 0000000000..ede9cad8d9 --- /dev/null +++ b/packages/cdktf-cli/templates/python-pip/main.py @@ -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() diff --git a/packages/cdktf-cli/templates/python-pip/{{}}.gitignore b/packages/cdktf-cli/templates/python-pip/{{}}.gitignore new file mode 100644 index 0000000000..4d9e64e767 --- /dev/null +++ b/packages/cdktf-cli/templates/python-pip/{{}}.gitignore @@ -0,0 +1,6 @@ +dist/ +imports/* +!imports/__init__.py +.terraform +cdktf.out +terraform.tfstate* \ No newline at end of file