Python Pulumi: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(15 intermediate revisions by the same user not shown)
Line 7: Line 7:
The SDK is available as a [[pip]] package.
The SDK is available as a [[pip]] package.
=Python Pulumi and Virtual Environments=
=Python Pulumi and Virtual Environments=
A new Python project created with <code>[[Pulumi_Operations#New_Project|pulumi new]]</code> will have a [[Python_Language#Environment|virtual environment]] created in a <code>venv</code> directory with required dependencies from <code>[[pip#requirements.txt|requirements.txt]]</code> installed in it. This behavior is controlled by the <code> virtualenv</code> <code>runtime</code> option in [[Pulumi_Concepts#pulumi:runtime|Pulumi.yaml]]:
A new Python project created with <code>[[Pulumi_Operations#New_Project|pulumi new]]</code> will have a [[Python_Language#Environment|virtual environment]] created in a <code>venv</code> directory with required dependencies from <code>[[pip#requirements.txt|requirements.txt]]</code> installed in it. This behavior is controlled by the <code> virtualenv</code> <code>runtime</code> option in <code>[[Pulumi.yaml#Example|Pulumi.yaml]]</code>.
<syntaxhighlight lang='yaml'>
==Manually Initialize Pulumi Python Virtual Environment==
runtime:
To manually create a virtual environment and install dependencies, run the following commands in your project directory:
  name: python
<syntaxhighlight lang='bash'>
  options:
python3 -m venv venv
    virtualenv: venv
venv/bin/pip install -r requirements.txt
</syntaxhighlight>
</syntaxhighlight>
<code>virtualenv</code> is the path, relative to the project directory, to the virtual environment to use.
 
=Pulumi Python Packges=
=Pulumi Python Packges=
{{External|https://www.pulumi.com/docs/reference/pkg/}}
{{External|https://www.pulumi.com/docs/reference/pkg/}}
Line 23: Line 23:
</syntaxhighlight>
</syntaxhighlight>
=Project Layout=
=Project Layout=
The [[Pulumi_Concepts#Project|project directory]] must contain either a <code>[[#__main__.py|__main__.py]]</code> file or a <code>setup.py</code> file that defines the entry point.
<font size=-1>
<font size=-1>
   my-project
   my-project
   │
   │
   ├─ README.md
   ├─ README.md
   ├─ [[Pulumi_Concepts#Pulumi.yaml|Pulumi.yaml]]
   ├─ [[Pulumi.yaml#Overview|Pulumi.yaml]]
   ├─ [[Pulumi_Concepts#Stack_Settings_File|Pulumi.my-stack.yaml]]
   ├─ [[Pulumi_Concepts#Stack_Settings_File|Pulumi.my-stack.yaml]]
   ├─ [[#main_.py|__main__.py]]
   ├─ [[#main_.py|__main__.py]]
Line 39: Line 40:
       └─ pyvenv.cfg
       └─ pyvenv.cfg
</font>
</font>
Also see [[Pulumi_Concepts#Project_Layout|Pulumi Concepts &#124; Project Layout]].
Also see: {{Internal|Pulumi_Concepts#Project_Layout|Pulumi Concepts &#124; Project Layout}}
==<tt>__main__.py</tt>==
==<tt>__main__.py</tt>==
<code>__main__.py</code> is the Pulumi Python [[Pulumi_Concepts#Program|program]] that defines the stack resources.
<code>__main__.py</code> is the Pulumi Python [[Pulumi_Concepts#Program|program]] that defines the stack resources.
Line 52: Line 53:


=Python Pulumi Programming Model=
=Python Pulumi Programming Model=
<span id='Getting_Project_Programmatically'></span>Get the name of the currently deploying project:
<syntaxhighlight lang='python'>
project = pulumi.get_project()
</syntaxhighlight>
<Font color=darkkhaki>
Getting whether we're doing a dry run or an actual deployment from the program:
<syntaxhighlight lang='python'>
project = pulumi.get_project()
</syntaxhighlight>
Getting a stack programmatically:
<syntaxhighlight lang='python'>
stack = pulumi.get_stack()
</syntaxhighlight>
</Font>
==Environment Variables==
All shell environment variables are passed to the running program and can be accessed using the standard runtime APIs <code>[[Python_Module_os#Environment_Variables|os.environ]]</code>. This can also be used for dynamic behavior. Configuration is preferable, however, because it makes the behavior less sensitive to local environment configuration.


=AWS=
=Code Examples=
* [[Python Pulumi AWS SSM Parameter|SSM Parameter]]
{{Internal|Pulumi_Programming_Model#Other_Code_Examples|Pulumi Programming Model &#124; Code Examples}}

Latest revision as of 00:58, 15 April 2022

External

Internal

Pulumi Python SDK

https://www.pulumi.com/docs/reference/pkg/python/pulumi/

The SDK is available as a pip package.

Python Pulumi and Virtual Environments

A new Python project created with pulumi new will have a virtual environment created in a venv directory with required dependencies from requirements.txt installed in it. This behavior is controlled by the virtualenv runtime option in Pulumi.yaml.

Manually Initialize Pulumi Python Virtual Environment

To manually create a virtual environment and install dependencies, run the following commands in your project directory:

python3 -m venv venv
venv/bin/pip install -r requirements.txt

Pulumi Python Packges

https://www.pulumi.com/docs/reference/pkg/

Adding a New Dependency

To install a new dependency in the virtual environment, add the entry to requirements.txt and run in the project directory:

venv/bin/pip install -r requirements.txt

Project Layout

The project directory must contain either a __main__.py file or a setup.py file that defines the entry point.

 my-project
  │
  ├─ README.md
  ├─ Pulumi.yaml
  ├─ Pulumi.my-stack.yaml
  ├─ __main__.py
  ├─ requirements.txt
  └─ venv
      ├─ bin
      │   ├─ pip
      │   └─ python
      ├─ include
      ├─ lib
      └─ pyvenv.cfg

Also see:

Pulumi Concepts | Project Layout

__main__.py

__main__.py is the Pulumi Python program that defines the stack resources.

import pulumi
from pulumi_aws import ssm

parameter = ssm.Parameter("/test/a", type="String", value="1234")

pulumi.export("parameter_name", parameter.id)

Python Pulumi Programming Model

Get the name of the currently deploying project:

project = pulumi.get_project()

Getting whether we're doing a dry run or an actual deployment from the program:

project = pulumi.get_project()

Getting a stack programmatically:

stack = pulumi.get_stack()

Environment Variables

All shell environment variables are passed to the running program and can be accessed using the standard runtime APIs os.environ. This can also be used for dynamic behavior. Configuration is preferable, however, because it makes the behavior less sensitive to local environment configuration.

Code Examples

Pulumi Programming Model | Code Examples