Go to file
Emma May 7da72bd5dd
Merge pull request #169 from starhawking/back-at-it
No Longer Archived
2022-02-04 18:33:27 -06:00
docs docs/list_of_providers.rst is now a converted ../PROVIDERS.md 2021-09-26 03:29:07 +10:00
examples Should generate docs without warnings, and keep examples updated 2020-09-11 16:32:35 +02:00
terrascript Removed separate Installation and Quickstart sections from the documentation as they are already contained in the Introduction 2021-09-26 03:17:22 +10:00
tests Merge pull request #142 from mjuenema/feature/issue-120-allow-updating-terraform-block 2021-09-25 04:25:33 +10:00
tools Merged develop into feature/makecode-and-terraform-registry 2021-09-25 04:12:56 +10:00
.gitattributes Generate code from remote Github files 2017-07-26 10:47:53 +10:00
.gitignore makecode 25-Sep-2021 04:00 2021-09-25 04:02:35 +10:00
.gitmodules Generate code from remote Github files 2017-07-26 10:47:53 +10:00
.gitpod.Dockerfile Added black to the gitpod Dockerfile 2021-09-25 00:50:25 +10:00
.gitpod.yml Update .gitpod.yml 2020-01-03 08:53:47 +11:00
.travis.yml Should support building on Python 3.9 2020-11-24 08:43:25 +01:00
CHANGELOG.md Update CHANGELOG.md 2021-09-25 04:30:08 +10:00
CONTRIBUTORS.md add time provider 2021-09-16 13:20:09 -05:00
DEVELOPMENT.md Update DEVELOPMENT.md 2021-09-25 19:32:20 +10:00
LICENSE Initial commit 2017-07-19 15:31:54 +10:00
MANIFEST.in Added missing MANIFEST.in 2017-08-02 10:00:36 +10:00
Makefile Removed separate Installation and Quickstart sections from the documentation as they are already contained in the Introduction 2021-09-26 03:17:22 +10:00
PROVIDERS.md makecode 25-Sep-2021 04:00 2021-09-25 04:02:35 +10:00
README.rst No Longer Archived 2022-02-04 18:32:05 -06:00
RELEASING.md Update RELEASING.md 2021-08-24 21:16:53 +10:00
dev_requirements.txt docs/list_of_providers.rst is now a converted ../PROVIDERS.md 2021-09-26 03:29:07 +10:00
requirements.txt Added .travis.yml 2017-07-24 22:31:37 +00:00
setup.cfg Added .travis.yml 2017-07-24 22:31:37 +00:00
setup.py Should include python 3.9 2021-01-22 15:21:15 +01:00
test_requirements.txt Should include black in both dev and test requirements 2020-09-11 09:23:17 +02:00

README.rst

python-terrascript
------------------

.. image:: https://img.shields.io/github/license/mjuenema/python-terrascript
   :target: https://opensource.org/licenses/BSD-2-Clause

.. image:: https://img.shields.io/travis/mjuenema/python-terrascript
   :target: https://www.travis-ci.org/mjuenema/python-terrascript/builds
   
.. image:: https://img.shields.io/pypi/v/terrascript
   :target: https://pypi.org/project/terrascript/
   
.. image:: https://img.shields.io/pypi/pyversions/terrascript
   :target: https://pypi.org/project/terrascript/
   
.. image:: https://img.shields.io/pypi/dm/terrascript
   :target: https://pypi.org/project/terrascript/
   
.. image:: https://img.shields.io/github/issues/mjuenema/python-terrascript
   :target: https://github.com/mjuenema/python-terrascript/issues
   
.. image:: https://img.shields.io/github/stars/mjuenema/python-terrascript
   :target: https://github.com/mjuenema/python-terrascript/stargazers
 

About 
~~~~~

Python-Terrascript is a Python package for generating Terraform configurations in JSON format.

Creating Terraform through a Python script offers a degree of flexibility 
superior to writing Terraform configurations by hand.

* Control structures like ``if``/``else``, ``for``/``continue``/``break`` or ``try``/``except``/``finally``.
* More string methods.
* Python functions may be used as an alternative to Terraform_ Modules.
* Access to the Python Standard Library and third-party packages.

The file PROVIDERS.md_ lists all currently supported Terraform providers.

.. _PROVIDERS.md: PROVIDERS.md 

Installing Terrascript
~~~~~~~~~~~~~~~~~~~~~~

Terrascript is available from the Python Package Repository PyPi_ or
alternatively from its Github_ repository.

.. _PyPi: https://pypi.org/project/terrascript/#history
.. _Github: https://github.com/mjuenema/python-terrascript


Installing Terrascript from PyPi
..................

It is easiest to install Terrascript directly from the Python Package Index.

.. code-block:: console

   $ python3 -m pip install terrascript

Installing Terrascript from Github
...................................

Terrascript can also be installed from its Github_ repository.

.. code-block:: console

   $ git clone https://github.com/mjuenema/python-terrascript.git
   $ cd python-terrascript/
   $ git fetch
   $ git fetch --tags
   
The ``master`` branch should be identical to the version on PyPi.

.. code-block:: console

   $ git checkout master
   $ python3 setup.py install

The ``develop`` branch includes the latest changes but may not always
be in a stable state. Do not use the ``develop`` branch unless you want 
to submit a merge request on github.

.. code-block:: console

   $ git checkout develop
   $ python3 setup.py install

Compatibility
~~~~~~~~~~~~~

Terraform releases
..................

`Terraform 0.13`_ added the `required providers`_ block together with tighter
integration with the `Terraform Provider Registry`_. Terrascript 0.10.x
reflects this change and is *not backwards compatible* with earlier Terraform
releases.   

`Terraform 0.12`_ introduced some changes to how it deals with configuration 
files in JSON format. This is reflected in Terrascript by currently having
separate releases for Terraform 0.12 and Terraform 0.11. Earlier releases of 
Terraform are not supported. 

.. _`Terraform 0.12`: https://www.hashicorp.com/blog/announcing-terraform-0-12
.. _`Terraform 0.13`: https://www.hashicorp.com/blog/announcing-hashicorp-terraform-0-13
.. _`required_providers`: https://www.terraform.io/upgrade-guides/0-13.html
.. _`Terraform Provider Registry`: https://registry.terraform.io/browse/providers

========== ============ ============================================================================================
Terraform  Terrascript  Notes
========== ============ ============================================================================================
0.13.x     0.10.x       Introduced namespaces, many more providers, suppports Terraform 0.13 only 
0.13.x     0.9.x        Cleanup efforts and bug fixes, dropping support for Python <3.6, supporting Terraform 0.13.x
0.12.x     0.8.x        Terrascript 0.8 are a (almost) complete rewrite
0.12.x     0.7.x        Never released
0.11.x     0.6.x        Last releases to support Terraform 0.11 and earlier
========== ============ ============================================================================================

Terrascript supports Python 3.6 and later.

Module layout
.............

Python-Terrascript's top-level directory layout is structured into providers, 
resources and data sources.

::

    import terrascript
    import terrascript.provider     
    import terrascript.resource     
    import terrascript.data         
    
This is then further structured into *namespaces* to reflect the changes in 
_`Terraform 0.13`. Below are examples for importing the modules for the
 `Amazon Web Services Provider`_ which is maintained by Hashicorp. 

.. _`Amazon Web Services Provider`: https://registry.terraform.io/providers/hashicorp/aws/latest

::

    import terrascript.provider.hashicorp.aws     
    import terrascript.resource.hashicorp.aws     
    import terrascript.data.hashicorp.aws

A first example
~~~~~~~~~~~~~~~

The following example has been taken from the official Terraform documentation 
for the `AWS Provider`_ and then converted into a Python script that generates 
the equivalent configuration in JSON syntax.

.. _`AWS Provider`: https://www.terraform.io/docs/providers/aws/index.html 

First the original Terraform HCL format, which since Terraform 0.13 must include
a ``required_providers`` block inside the ``terraform`` block.

::

    terraform {
      required_providers {
        aws = {
          source = "hashicorp/aws"
          version = "3.36.0"
        }
      }
    }

    provider "aws" {
      region  = "us-east-1"
    }

    resource "aws_vpc" "example" {
      cidr_block = "10.0.0.0/16"
    }


The Terrascript code would look like this. The ``required_providers`` argument
is supplied as a nested dictionary. In this example only the 
 `Amazon Web Services Provider`_ is used, which is maintained by Hashicorp.

::

    import terrascript
    import terrascript.provider.hashicorp.aws
    import terrascript.resource.hashicorp.aws

    config = terrascript.Terrascript()
    
    # The ``required_providers`` argument is supplied as a nested dictionary.
    config += terrascript.Terraform(required_providers={'aws': {'source': 'hashicorp/aws',
                                                                'version': '3.36.0' }
                                                       })

    # The provider is a module and a class:  ***.***
    config += terrascript.provider.hashicorp.aws.aws(region='us-east-1')
    config += terrascript.resource.hashicorp.aws.aws_vpc('example', cidr_block='10.0.0.0/16')

    with open('config.tf.json', 'wt') as fp:
        fp.write(str(config))


The content of ``config.tf.json`` is shown below. It is equivalent to the
original HCL format.

::

    {
      "terraform": {
        "required_providers": {
          "aws": {
            "source": "hashicorp/aws",
            "version": "3.36.0"
          }
        }
      },
      "provider": {
        "aws": [
          {
            "region": "us-east-1"
          }
        ]
      },
      "resource": {
        "aws_vpc": {
          "example": {
            "cidr_block": "10.0.0.0/16"
          }
        }
      }
    }

**Terrascript does not verify that the generated JSON code is a valid Terraform configuration.**
**This is a deliberate design decision and is explained in the** `Frequently Asked Questions (FAQ) <https://python-terrascript.readthedocs.io/en/develop/faq.html>`_

.. _Frequently Asked Questions (FAQ): https://python-terrascript.readthedocs.io/en/develop/faq.html

Links
~~~~~

* Terraform_.
* `Terraform Provider Registry`_.
* Documentation_ for Python-Terrascript.
* Github_ page of Python-Terrascript.
* `Terraform JSON`_ syntax.

.. _Terraform: https://www.terraform.io 
.. _Documentation: https://python-terrascript.readthedocs.io/en/develop/
.. _Github: https://github.com/mjuenema/python-terrascript
.. _`Terraform JSON`: https://www.terraform.io/docs/configuration/syntax-json.html
.. _`Terraform Provider Registry`: https://registry.terraform.io/browse/providers