Python Pulumi: Difference between revisions
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 [[ | 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.yaml#Example|Pulumi.yaml]]: | ||
==Manually Initialize Pulumi Python Virtual Environment== | ==Manually Initialize Pulumi Python Virtual Environment== | ||
Revision as of 23:36, 19 January 2022
External
Internal
Pulumi Python SDK
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
Pulumi Python Packges
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:
__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.