Pulumi Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(142 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://www.pulumi.com/docs/intro/concepts/
* https://www.pulumi.com/docs/intro/concepts/
* https://www.pulumi.com/docs/guides/automation-api/concepts-terminology/
=Internal=
=Internal=
* [[Pulumi#Subjects|Pulumi]]
* [[Pulumi#Subjects|Pulumi]]
* [[Pulumi Operations]]
* [[Pulumi Operations]]
* [[Infrastructure as Code Concepts]]


=Overview=
=Overview=
Line 10: Line 13:
::::[[File:Pulumi_Concepts.png|591px]]
::::[[File:Pulumi_Concepts.png|591px]]
=Architecture=
=Architecture=
<font color=darkkhaki>TO PROCESS: https://www.pulumi.com/docs/intro/concepts/how-pulumi-works/</font>
{{Internal|Pulumi Architecture#Overview|Pulumi Architecture}}
 
=<span id='Supported_Programming_Languages'></span>Supported Runtimes and Programming Languages=
=<span id='Supported_Programming_Languages'></span>Supported Runtimes and Programming Languages=
{{External|https://www.pulumi.com/docs/intro/languages/}}
{{External|https://www.pulumi.com/docs/intro/languages/}}
Line 26: Line 30:


=Organization=
=Organization=
{{External|https://www.pulumi.com/docs/intro/pulumi-service/organizations/}}
Other organization is also referred to as the "owner".  
Other organization is also referred to as the "owner".  


Line 36: Line 42:
=Project=
=Project=
{{External|https://www.pulumi.com/docs/intro/concepts/project/}}
{{External|https://www.pulumi.com/docs/intro/concepts/project/}}
Projects are at the same time a [[#Local_Project_Representation|filesystem layout to support declaring programs and stacks]], and a [[#Projects_on_the_Backend|namespace mechanisms for stacks on the backend]].
A project is a collection of code and a [[#Local_Project_Representation|filesystem layout that supports declaring programs and stack configurations]], and at the same time, a [[#Projects_on_the_Backend|namespace mechanisms for stacks on the backend]].
==Local Project Representation==
==Local Project Representation==
A project is a directory that contains one [[#Program|program]] and metadata on how to run the program, such as what [[#Supported_Runtimes_and_Programming_Languages|runtime]] to use, where to look for the [[#Program|program]], etc. The project definition metadata is maintained in <code>[[#Pulumi.yaml|Pulumi.yaml]]</code>. The presence of <code>Pulumi.yaml</code> is the indication that directory is a Pulumi project.  
A project is a directory that contains one [[#Program|program]] and metadata on how to run the program, such as what [[#Supported_Runtimes_and_Programming_Languages|runtime]] to use, where to look for the [[#Program|program]], etc. The project definition metadata is maintained in <code>[[#Pulumi.yaml|Pulumi.yaml]]</code>. The presence of <code>Pulumi.yaml</code> is the indication that directory is a Pulumi project.  
Line 43: Line 49:


The project can be obtained programmatically. In Python, use <code>[[Python_Pulumi#Getting_Project_Programmatically|get_project()]]</code> function.
The project can be obtained programmatically. In Python, use <code>[[Python_Pulumi#Getting_Project_Programmatically|get_project()]]</code> function.
A [[#Project|project]], a [[#Program|program]] and multiple [[#Stack|stacks]] are tied together in a single execution context named [[#Workspace|workspace]].
===Creating a Project===
===Creating a Project===
Projects can be created with <code>[[Pulumi_Operations#New|pulumi new]]</code> command. The command creates the project metadata on the local filesystem and the first (and possibly the only one) [[#Stack|stack]], based on a template, and also modifies the backend, registering the newly created stack. At this point, no infrastructure resources are actually created in the infrastructure platform. The program and metadata can be read by the Pulumi CLI and applied to the infrastructure platform with the <code>[[Pulumi_Operations#Apply_Infrastructure|pulumi up]]</code> command, updating the stack. <font color=darkkhaki>Can valid projects be created by hand, without <code>pulumi new</code>, by manually crafting metadata and code?</font>
Projects can be created with <code>[[Pulumi_Operations#New|pulumi new]]</code> command. The command creates the project metadata on the local filesystem and the first (and possibly the only one) [[#Stack|stack]], based on a template, and also modifies the backend, registering the newly created stack. At this point, no infrastructure resources are actually created in the infrastructure platform. The program and metadata can be read by the Pulumi CLI and applied to the infrastructure platform with the <code>[[Pulumi_Operations#Apply_Infrastructure|pulumi up]]</code> command, updating the stack. <font color=darkkhaki>Can valid projects be created by hand, without <code>pulumi new</code>, by manually crafting metadata and code?</font>
Line 51: Line 60:
The project name should represent the intent of the code within the project. For example, if the project is intended to create infrastructure for an application named "blue", a good project name would be "blue" or "blue-infra". Don't rely on the [[#Stack_Name|stack name]] (or names) to fully convey that semantics. The stack names should be mostly driven by the particular instantiation of the infrastructure resource set, such as "dev", or "staging" ("blue-dev" is a good name).
The project name should represent the intent of the code within the project. For example, if the project is intended to create infrastructure for an application named "blue", a good project name would be "blue" or "blue-infra". Don't rely on the [[#Stack_Name|stack name]] (or names) to fully convey that semantics. The stack names should be mostly driven by the particular instantiation of the infrastructure resource set, such as "dev", or "staging" ("blue-dev" is a good name).


The project name is specified using the <code>[[#name|name]]</code> attribute in <code>[[#Pulumi.yaml|Pulumi.yaml]]</code>. It shows up in the Pulumi dashboard. It is used to aggregate the associated stacks and their resources underneath the project, under a simple hierarchy. Project names may only contain alphanumeric characters, hyphens, underscores and periods.
The project name is specified using the <code>[[Pulumi.yaml#name|name]]</code> attribute in <code>[[Pulumi.yaml#Overview|Pulumi.yaml]]</code>. It shows up in the Pulumi dashboard. It is used to aggregate the associated stacks and their resources underneath the project, under a simple hierarchy. Project names may only contain alphanumeric characters, hyphens, underscores and periods.
===Workspace===
A workspace is an execution context that contains a single [[#Project|project]], a single  [[#Program|program]] and multiple [[#Stack|stacks]]. Workspaces manage the execution environment, providing utilities such as plugin installation, environment configuration (<code>$PULUMI_HOME</code>) and creation, deletion and listing of stacks. <font color=darkkhaki>The workspace relies on <code>[[Pulumi.yaml]]</code> and [[#Stack_Settings_File|stack settings files]] as intermediate format for project and stack settings.</font> Pulumi maintains workspace local state in:
<font size=-1>
~
└─ .pulumi
      └─ workspaces
          └─ <project-name>-8698263d7571....4d46edf4e5bfeff-workspace.json
</font>
The content of the workspace JSON file is similar to:
<syntaxhighlight lang='json'>
{
  "stack": "<org-name>/<project-name>/<stack-name>"
}
</syntaxhighlight>
The value of the "stack" key indicates the [[#Active_Stack|active stack]]. The local state is initialized and updated by commands like <code>[[Pulumi_Operations#stack_init|pulumi stack init]]</code>, <font color=darkkhaki>etc.</font>
 
<font color=darkkhaki>What happens if the same program is checked out and executed from two different local directories?</font>


===Template===
===Template===
A list of available templates is presented executing <code>[[Pulumi_Operations#New|pulumi new]]</code> without any argument. An example is available here:
A template is a set of files, either built-in or exposed as a Git URL, which contain Python code and Pulumi metadata to bootstrap a project. An essential component of a template is the <code>[[Pulumi.yaml#template|template]]</code> section of <code>[[Pulumi.yaml#Overview|Pulumi.yaml]]</code> file. A list of available templates is presented executing <code>[[Pulumi_Operations#New|pulumi new]]</code> without any argument. An example is available here:
{{Internal|Pulumi Available Templates|Available Templates}}
{{Internal|Pulumi Available Templates|Available Templates}}
Intuitively, the right template can be invoked by using the name of the cloud and the language, example: <code>aws-python</code>.
Intuitively, the right template can be invoked by using the name of the cloud and the language, example: <code>aws-python</code>.
====GitHub Template Repository====
If intending to access templates directly from a GitHub repository, the repository should be set with "Enable anonymous Git read access" (Settings → Danger Zone → Enable anonymous Git read access)
===Project Layout===
===Project Layout===
<font size=-1>
<font size=-1>
   my-project
   my-project
   ├─ README.md
   ├─ README.md
   ├─ [[#Pulumi.yaml|Pulumi.yaml]]          <font color=teal># Project definition file</font>
   ├─ [[Pulumi.yaml#Overview|Pulumi.yaml]]          <font color=teal># Project definition file</font>
   ├─ [[#Stack_Settings_File|Pulumi.red-stack.yaml]] <font color=teal># [[#Stack_Settings_File|Stack settings file]]</font>
   ├─ [[#Stack_Settings_File|Pulumi.red-stack.yaml]] <font color=teal># [[#Stack_Settings_File|Stack settings file]]</font>
   ├─ Pulumi.green-stack.yaml
   ├─ Pulumi.green-stack.yaml
Line 69: Line 98:
Language-specific layouts:
Language-specific layouts:
* [[Python_Pulumi#Project_Layout|Python Project Layout]]
* [[Python_Pulumi#Project_Layout|Python Project Layout]]
===<tt>Pulumi.yaml</tt>===
===<tt>Pulumi.yaml</tt>===
{{External|https://www.pulumi.com/docs/reference/pulumi-yaml/}}
Contains the project definition.
Contains the project definition. The name must begin with a capital "P", and both "yaml" and "yaml" extensions are valid.
{{Internal|Pulumi.yaml|Pulumi.yaml}}
 
Example:
<font size=-1>
[[#name|name]]: simple-aws
runtime:
  name: python
  options:
    virtualenv: venv
description: An experimental AWS Pulumi project.
</font>
====Attributes====
=====<tt>name</tt>=====
Required attribute that specifies the [[#Project_Name|project name]].


===Deploying a Project===
===Deploying a Project===
Line 97: Line 114:


=Program=
=Program=
A program contains code that describes how cloud infrastructure should be composed. It can be written in [[Python]], [[TypeScript]] or [[Go]]. Infrastructure is declared by defining [[#Resource|resource]] objects whose properties correspond  to the desire state of the infrastructure. The properties are also used to express dependencies between resources, and can be exported outside the stack. It is recommended to group resource with common lifecycles together. Programs reside in [[#Project|projects]].
A program contains code that describes how cloud infrastructure should be composed. It can be written in [[Python]], [[TypeScript]] or [[Go]]. Infrastructure is declared by defining [[#Resource|resource]] objects whose properties correspond  to the desire state of the infrastructure. The properties are also used to express dependencies between resources, and can be exported outside the stack. It is recommended to group resource with common lifecycles together. Programs reside in [[#Project|projects]]. <font color=darkkhaki>[[#Automation_API|Automation API]] allows defining a Pulumi program as a function within an arbitrary codebase rather than a separated project, and use methods to get and set configuration parameters programmatically.</font>
==Local Program==
A local program is a traditional Pulumi CLI-driven program with its own directory, a <code>[[Pulumi.yaml]]</code> file, along with a file that defines the Pulumi program. [[#Automation API|Automation API]] can be used to drive these programs, as well as the CLI.
==Inline Program==
Unlike traditional Pulumi programs, inline programs don’t require a separate package on disk, with its own file and a <code>[[Pulumi.yaml]]</code>. Inline programs are simply functions that can be authored in the same file as your [[#Automation_API|Automation API]] program or be imported from anther package.
==Paths in Program==
==Paths in Program==
When the program references resources in the local file system, their paths must be relative to the <font color=darkkhaki>working directory. Not the [[#Project|project directory]], in which the program is in?</font>
When the program references resources in the local file system, their paths must be relative to the <font color=darkkhaki>working directory. Not the [[#Project|project directory]], in which the program is in?</font>
==Programming Model==
==Programming Model==
The Pulumi programming model defines the core concepts in use when creating infrastructure as code [[#Program|programs]]. These concepts are made available in the Pulumi SDKs, that support [[Python_Pulumi#Python_Pulumi_Programming_Model|Python]], [[TypeScript Pulumi#Overview|TypeScript]] and [[Go Pulumi#Overview|Go]].
{{Internal|Pulumi Programming Model|Pulumi Programming Model}}
 
=Resource=
{{External|https://www.pulumi.com/docs/intro/concepts/resources/}}
<font color=darkkhaki>
TO PROCESS:
* https://www.pulumi.com/docs/intro/concepts/resources/
* https://www.pulumi.com/docs/reference/pkg/python/pulumi/#the-pulumi-python-resource-model-1
</FONT>
 
Pulumi understands dependencies between resources and uses the relationship between resources to maximize execution parallelism and ensure correct ordering when a stack is instantiated.
 
<font color=darkkhaki>
Include [[Pulumi_Concepts_Inputs_and_Outputs#Output|Output]] in conversation.
 
Other attributes like id, etc.</font>
 
==Property==
<font color=darkkhaki>Difference between resource and stack inputs/outputs.</font>
===Input Property===
===Output Property===


=Stack=
=Stack=
{{External|https://www.pulumi.com/docs/intro/concepts/stack/}}
{{External|https://www.pulumi.com/docs/intro/concepts/stack/}}
A stack is an isolated, independently configurable instance of a Pulumi [[#Program|program]], materialized as a set of infrastructure resources created by executing the program against the [[Infrastructure_Concepts#Infrastructure_Platform|infrastructure platform]]. Every program is applied to the infrastructure as one, possibly more stacks. Stacks are commonly used to denote different phases of development, such as "development", "staging" and "production", or feature branches. A [[#Project|project]] can have an [[#Multiple_Stacks_per_Project|arbitrary number of stacks]]. By default, Pulumi creates a new stack per project when <code>[[Pulumi_Operations#New_Project|pulumi new]]</code> is used. Stack creation means creation of the associated [[#Stack_Settings_File|stack settings file]] and a stack state representation in the Pulumi backend.
A stack is a unit of deployment, an isolated, independently configurable instance of a Pulumi [[#Program|program]], materialized as a set of infrastructure resources created by executing the program against the [[Infrastructure_Concepts#Infrastructure_Platform|infrastructure platform]]. A stack is at the same time a state storage unit. The stack stores the “actual state” of the infrastructure resource set. Should the desired state, as expressed by the program, change, Pulumi compares the desired state with the actual state maintained by the stack, and if detects differences, it updates the infrastructure resources and the stack state. More details on the relationship between stacks and the Pulumi programming model are available in [[Pulumi Programming Model#Overview|Pulumi Programming Model]].
 
Every program is applied to the infrastructure as one, possibly more stacks. Stacks are commonly used to denote different phases of development, such as "development", "staging" and "production", or feature branches. A [[#Project|project]] can have an [[#Multiple_Stacks_per_Project|arbitrary number of stacks]]. By default, Pulumi creates a new [[#Empty_Stack|empty stack]] per project when <code>[[Pulumi_Operations#New_Project|pulumi new]]</code> is used, and a manually bootstrapped project starts with no stacks. Stacks can be created separately with <code>[[Pulumi_Operations#stack_init|pulumi stack init]]</code>. Stack creation means creation of the associated [[#Stack_Settings_File|stack settings file]] and the instantiation of the stack state representation in the Pulumi backend. Initially, a stack starts as "[[#Empty_Stack|empty]]", meaning that is has no resource representations stored in the backend, but it can become a target of a deployment initiated with the <code>[[Pulumi_Operations#update|pulumi update]]</code> command. Upon the first, and subsequent deployment, the stack backend representation is populated with resources. The state built in memory, after executing the program, represents the "desired state" of the infrastructure, and Pulumi aims to bring the actual state in line with the desired state. Each stack has its own separate configuration, secretes, role-based controls (RBAC) and policies. Deep insight into its structure and components can be achieved by [[#Stack_Import.2FExport|exporting]] the stack and examining the corresponding JSON file.
==Stack Name==
==Stack Name==
The stack name must be unique within a [[#Project|project]] and should express the intent behind a specific instantiation of the project infrastructure resource set, like "dev" or "testing". A pattern that seems reasonable is <code><project-name>-<function></code>, especially that in some circumstances, the project name is omitted from the stack name (like in <code><org-name>/<stack-name></code>. For more considerations on stack and project name, see [[#Project_Name|project name]]. The stack name may only contain alphanumeric characters, hyphens, underscores and periods.  
The stack name must be unique within a [[#Project|project]] and should express the intent behind a specific instantiation of the project infrastructure resource set, like "dev" or "testing". A pattern that seems reasonable is <code><project-name>-<function></code>, especially that in some circumstances, the project name is omitted from the stack name (like in <code><org-name>/<stack-name></code>). For more considerations on stack and project name, see [[#Project_Name|project name]]. The stack name may only contain alphanumeric characters, hyphens, underscores and periods.  
 
The stack name does now allow slashes, if attempting to use a stack name with slashes in it, the CLI complains: "Sorry, 'my-org/my-project/test1/test2' is not a valid stack name. could not parse stack name 'my-org/my-project/test1/test2'."
===Fully Qualified Stack Name===
===Fully Qualified Stack Name===
A fully qualified stack name includes the organization and the project name: <code><org-name>/<project-name>/<stack-name></code>. If you are using Pulumi in your organization, and when new stacks are created, they will be created by default in your user account. To create the stack in the organization instead, name the stack using <code><org-name>/<stack-name></code>.
A fully qualified stack name includes the organization and the project name: <code><org-name>/<project-name>/<stack-name></code>. If you are using Pulumi in your organization, and when new stacks are created, they will be created by default in your user account. To create the stack in the organization instead, name the stack using <code><org-name>/<stack-name></code>.
Line 133: Line 138:
==Stack URI==
==Stack URI==
<font color=darkkhaki>What is the semantics of a stack URI?</font>
<font color=darkkhaki>What is the semantics of a stack URI?</font>
==Empty Stack==
An empty stack is a stack for which no infrastructure resources have been created yet in the infrastructure platform. Empty stacks are created when projects are initialized with <code>[[Pulumi_Operations#New_Project|pulumi new]]</code>, or when <code>[[Pulumi_Operations#pulumi_stack_init|pulumi stack init]]</code> command is used. The opposite of an empty stack is a fully initialized stack. The stack is initialized as result of the <code>[[Pulumi_Operations#Project_Update|pulumi up]]</code> command.


==Multiple Stacks per Project==
==Multiple Stacks per Project==
When a project is created with <code>[[Pulumi_Operations#New_Project|pulumi new]]</code>, the [[#Stack_Settings_File|configuration]] of a stack is also automatically created. Additional stack configurations can be created for an existing project with the <code>[[Pulumi_Operations#stack_init|pulumi stack init]]</code> command. Note that multiple stacks per project means that all the stacks share the same [[#Program|program]] (behavior) but have different configurations, corresponding to different [[#Stack_Settings_File|stack settings files]]. Note that <code>pulumi stack init</code> does not fully manage the corresponding stack setting file, it does create it, but it leaves it into an incomplete state.
When a project is created with <code>[[Pulumi_Operations#New_Project|pulumi new]]</code>, the [[#Stack_Settings_File|configuration]] of a stack is also automatically created. Additional stack configurations and stack backend state instances can be created for an existing project with the <code>[[Pulumi_Operations#stack_init|pulumi stack init]]</code> command. Note that multiple stacks per project means that all the stacks share the same [[#Program|program]] (behavior) but have different configurations, corresponding to different [[#Stack_Settings_File|stack settings files]]. Note that <code>pulumi stack init</code> does not fully manage the corresponding stack setting file, it does create it, but it leaves it into an incomplete state.
 
==<span id='Current_Stack'></span><span id='Active_Stack'></span><span id='Current'></span><span id='Active'></span><span id='Current_.28Active.29_Stack'></span>Current (Active, Default) Stack==
When a project contains multiple stacks, one of them is active, or current, at any time. The current stack for a project can be displayed by running <code>[[Pulumi_Operations#about|pulumi about]]</code> from the project directory, or running <code>[[Pulumi_Operations#stack_ls|pulumi stack ls]]</code>, in which case the active stack will be marked with an asterisk. A stack can be set as active by running <code>[[Pulumi_Operations#select|pulumi stack select]]</code>. The active stack is maintained in the [[#Workspace|local workspace state]], as the value of the "stack" key.
 
==<span id='Configuration'></span>Stack Configuration==
{{External|https://www.pulumi.com/docs/intro/concepts/config}}
While each stack of a project has a similar resource set, it will differ from other stacks in its configuration. Pulumi offers a configuration system to manage such differences. The system provides different methods to introspect and set configuration, including CLI and the programming model. The key-value pairs for any given stack of the project are stored in the stack's [[#Settings_File|settings file]].
 
<font color=darkkhaki>TO PROCESS: https://www.pulumi.com/docs/intro/concepts/config/
 
Understand the difference between stack configuration and stack output, and how they are related.
</font>
===<span id='Settings_File'></span>Stack Settings File===
{{External|https://www.pulumi.com/docs/intro/concepts/project/#stack-settings-file}}
Each stack within a project has an associated file named <code>Pulumi.<stack-name>.yaml</code> that contains [[#Configuration|configuration]], in form of key/value pairs, for the stack. The file resides in the [[#Project|project root directory]]. Secret values are encrypted, which, according to the Pulumi documentation, makes them safe to be checked in into a repository. This is debatable. The stack settings for [[#Ephemeral_Stack|ephemeral stacks]] are typically not checked into source control. A stack setting file can be created and managed with the <code>[[Pulumi_Operations#config|pulumi config]]</code> command.
 
Stack settings files are an implementation of the Infrastructure as Code [[Infrastructure_as_Code_Concepts#Stack_Configuration_File|Stack Configuration File]] pattern.
 
===Configuration Management via CLI===
Stack configuration can be read and updated with the <code>[[Pulumi_Operations#Config|pulumi config]]</code> command.
===Programmatic Configuration Management===
<code>Config</code>.


==<span id='Current_Stack'></span><span id='Active_Stack'></span><span id='Current'></span><span id='Active'></span>Current (Active) Stack==
<font color=darkkhaki>TO DO</font>
When a project contains multiple stacks, one of them is active, or current, at any time. The current stack for a project can be displayed by running <code>[[Pulumi_Operations#about|pulumi about]]</code> in the project, or running <code>[[Pulumi_Operations#stack_ls|pulumi stack ls]]</code>, in which case the active stack will be marked with an asterisk. A stack can be set as active by running <code>[[Pulumi_Operations#select|pulumi stack select]]</code>.
==Stack Settings File==
Each stack of a project will have a file named <code>Pulumi.<stackname>.yaml</code> that contains [[#Configuration|configuration]] (key/value pairs) specific to the stack it is associated with. The file typically resides in the [[#Project|project root directory]]. Secret values are encrypted, which, according to the Pulumi documentation, makes them safe to be checked in into a repository. This is debatable. The stack settings for [[#Ephemeral_Stack|ephemeral stacks]] are typically not checked into source control.


A stack setting file can be created and managed with <code>[[Pulumi_Operations#config|pulumi config]]</code>.
===Configuration Keys and Namespace===
===<span id='Configuration'></span>Stack Configuration===
{{External|https://www.pulumi.com/docs/intro/concepts/config/#configuration}}
<font color=darkkhaki>TO PROCESS when I have access to a working environment so I can try code: https://www.pulumi.com/docs/intro/concepts/config/</font>
===Namespace===
Key space.
Key space.
==<span id='Stack_Output'></span>Stack Output (Exports)==
<font color=darkkhaki>TO DO</font>
{{External|https://www.pulumi.com/docs/intro/concepts/stack/#outputs}}
===Restoring Configuration from Backend===
A stack can be turned into a component that integrates with other components. The way Pulumi programs communicate information for external consumption and integration with other stacks is by using [[Pulumi_Concepts_Inputs_and_Outputs#Overview|outputs]], or exports. Outputs are values produced during an update and can be thought as programmatic results of a stack update, additional to the infrastructure resources created in the infrastructure platform. They are also known as "properties". They are maintained as part of the stack's [[#State|state]], by the backend service. If the stack was not deployed yet, no outputs are available, and the output values are resolved after <code>pulumi up</code> command completes. They can be retrieved via CLI with <code>[[Pulumi_Operations#stack_output|pulumi stack output <property-name>]]</code>, displayed on the console or shared with other stacks, via [[#Stack_References|stack references]]. Exposing an output is called "exporting". This is how it's done in Python:
<font color=darkkhaki>
<syntaxhighlight lang='python'>
TO DO: pulumi config refresh
import pulumi
</font>
...
pulumi.export("something", resource.someAttribute)
pulumi.export("color", "blue")
pulumi.export("details", {"shape": "square", "size": 10})
pulumi.export("whole_resource", resource)
</syntaxhighlight>
 
<span id='Secret_Values'></span>Some resource properties are handled as secrets, and not displayed as part of the <code>pulumi stack output</code> unless the <code>--show-secrets</code> flag is used.


The value of a stack export can be a regular value, <font color=darkkhaki>an [[Pulumi_Concepts_Inputs_and_Outputs#Output|Output]] or a [[#Promise|Promise]], effectively the same as an [[Pulumi_Concepts_Inputs_and_Outputs#Input|Input]]</font>. They are JSON, <font color=darkkhaki>though quotes are removed when exporting strings</font>. An entire resource can be exported, and if that happens, it will be serialized as JSON. For example, if a SSM parameter is exported, it will be available as:
==<span id='Stack_Output'></span><span id='Output'></span><span id='Stack_Output_.28Exports.29'></span><span id='Inputs_and_Outputs'></span><span id='Input'></span><span id='Promise'></span>Stack Inputs and Outputs==
<syntaxhighlight lang='text'>
{{Internal|Pulumi Concepts Inputs and Outputs#Overview|Stack Inputs and Outputs}}
whole_resource  {"allowed_pattern":"","arn":"arn:aws:ssm:us-west-2:999999999999:parameter/experimental/ovidiu/test2-382b9f1","data_type":"text","description":"","id":null,"key_id":"","name":"/experimental/ovidiu/test2-382b9f1","overwrite":null,"tags":{},"tags_all":{},"tier":"Standard","type":"String","urn":"urn:pulumi:ssm::blue::aws:ssm/parameter:Parameter::/experimental/ovidiu/test2","value":null,"version":1}
</syntaxhighlight>


==Stack References==
==Stack References==
{{External|https://www.pulumi.com/docs/intro/concepts/stack/#stackreferences}}
{{External|https://www.pulumi.com/docs/intro/concepts/stack/#stackreferences}}
{{External|https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#StackReference}}
{{External|https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#StackReference}}
{{External|https://www.pulumi.com/learn/building-with-pulumi/stack-references/}}


Stack references are used by [[Infrastructure_as_Code_Concepts#Consumer|consumer]] stacks to get their dependencies, as an implementation of the [[Infrastructure_as_Code_Concepts#Stack_Data_Lookup|Stack Data Lookup]] pattern: a '''stack reference''' allows accessing the output of a [[Infrastructure_as_Code_Concepts#Provider|provider]] stack from a [[Infrastructure_as_Code_Concepts#Consumer|consumer]] stack. To reference values produced by a producer stack, create an instance of <code>StackReference</code> representing the producer stack, in the consumer stack, and asynchronously access the values exported by producer via <code>Output</code>s:
Stack references are used by [[Infrastructure_as_Code_Concepts#Consumer|consumer]] stacks to get their dependencies. This is the Pulumi implementation of the [[Infrastructure_as_Code_Concepts#Stack_Data_Lookup|Stack Data Lookup]] pattern: the stack reference allows access to the outputs of a [[Infrastructure_as_Code_Concepts#Provider|provider]] stack from a [[Infrastructure_as_Code_Concepts#Consumer|consumer]] stack. To get values exposed by a producer stack, create an instance of <code>StackReference</code> representing the producer stack, from the consumer stack, and asynchronously access the values exported by producer via <code>Output</code>s. The provider stack name must be [[Pulumi_Concepts#Fully_Qualified_Stack_Name|fully qualified]], including organization, project and stack name. Then, get the <code>[[#Output|Output]]</code> instance corresponding to the piece of data you want to access by invoking <code>get_output()</code> on the <code>StackReference</code> instance. The data must be previously exported by the provider stack with <code>[[#Stack_Output_.28Exports.29|export()]]</code>. Note that <code>get_output()</code> returns an <code>Output</code> instance. The actual data exported by the provider stack can be accessed asynchronously via <code>[[Pulumi_Concepts_Inputs_and_Outputs#apply.28.29|apply()]]</code> or other [[Pulumi_Concepts_Inputs_and_Outputs#Output_Access_Patterns|Output access patterns]]. Once a <code>StackReference</code> has been created, and exported data is accessed via <code>Output</code> instances, Pulumi understands the inter-stack dependency for scenarios like cascading updates.
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python'>
# consumer stack code
# consumer stack code
Line 178: Line 191:
provider = pulumi.StackReference("some-org/some-project/some-provider-stack")
provider = pulumi.StackReference("some-org/some-project/some-provider-stack")


value = provider.get_output("some_parameter"); # this is an Output instance
output = provider.get_output("some_parameter"); # this is an Output instance
 
output.apply(lambda v: ... # do something with the value)
</syntaxhighlight>
</syntaxhighlight>
Note that <code>get_output()</code> returns an <code>[[Pulumi_Concepts_Inputs_and_Outputs#Output|Output]]</code> instance. The actual data exported by the provider stack can be accessed asynchronously via <code>[[Pulumi_Concepts_Inputs_and_Outputs#apply.28.29|apply()]]</code> or other [[Pulumi_Concepts_Inputs_and_Outputs#Output_Access_Patterns|Output access patterns]].


==Stack Tags==
==Stack Tags==
{{External|https://www.pulumi.com/docs/intro/concepts/stack/#stack-tags}}
{{External|https://www.pulumi.com/docs/intro/concepts/stack/#stack-tags}}
Stacks have associated metadata as tags. Each tags has a name and a value. A set of [[Pulumi_Concepts#Built-in_Tags|built-in tags]] are automatically assigned and updated each time a stack is updated. Tags are only supported with the [[#Backend|Pulumi Service backend]]. To view a stack's tags, run <code>[[Pulumi_Operations#stack_tag_ls|pulumi stack tag ls]]</code>. Tags are useful to group stacks in the console.
Metadata can be attached to stacks, in form of tags. Each tags has a name and a value. A set of [[Pulumi_Concepts#Built-in_Tags|built-in tags]] are automatically assigned and updated each time a stack is updated. Tags are only supported with the [[#Backend|Pulumi Service backend]]. To view a stack's tags, run <code>[[Pulumi_Operations#stack_tag_ls|pulumi stack tag ls]]</code>. Tags are useful to group stacks in the console.


Custom tags can be applied with <code>[[Pulumi_Operations#stack_tag_set|pulumi stack tag set <name> <value>]]</code>. Custom tags should not be prefixed with <code>pulumi:</code>, <code>gitHub:</code>, or <code>vcs:</code> to avoid conflicting with built-in tags that are assigned and updated with fresh values each time a stack is updated.
Custom tags can be applied with <code>[[Pulumi_Operations#stack_tag_set|pulumi stack tag set <name> <value>]]</code>. Custom tags should not be prefixed with <code>pulumi:</code>, <code>gitHub:</code>, or <code>vcs:</code> to avoid conflicting with built-in tags that are assigned and updated with fresh values each time a stack is updated.
===Built-in Tags===
===<span id='Built-in_Tags'></span>Built-in Stack Tags===
====<tt>pulumi:project</tt>====
{| class="wikitable" style="text-align: left;"
====<tt>pulumi:runtime</tt>====
! Tag Name
Example of a [[Python_Pulumi#Python_Pulumi_and_Virtual_Environments|Python runtime section]].
! Notes
====<tt>pulumi:description</tt>====
|-
====<tt>gitHub:owner</tt>====
| <span id='pulumi:project'></span><tt>pulumi:project</tt> ||
====<tt>gitHub:repo</tt>====
|-
====<tt>vcs:owner</tt>====
| <span id='pulumi:runtime'></span><tt>pulumi:runtime</tt> || Example of a [[Python_Pulumi#Python_Pulumi_and_Virtual_Environments|Python runtime section]]
====<tt>vcs:repo</tt>====
|-
====<tt>vcs:kind</tt>====
| <span id='pulumi:description'></span><tt>pulumi:description</tt> ||
|-
| <span id='gitHub:owner'></span><tt>gitHub:owner</tt> ||
|-
| <span id='gitHub:repo'></span><tt>gitHub:repo</tt> ||
|-
| <span id='vcs:owner'></span><tt>vcs:owner</tt> ||
|-
| <span id='vcs:repo'></span><tt>vcs:repo</tt> ||
|-
| <span id='vcs:kind'></span><tt>vcs:kind</tt> ||
|-
|}
===Tag Operations===
* [[Pulumi_Operations#Display_Stack_Tags|Display stack tags]]
* [[Pulumi_Operations#Set_Custom_Tags|Set custom tags]]
* [[Pulumi_Operations#Get_Specific_Tags|Get specific tags]]
* [[Pulumi_Operations#Delete_Tag|Delete tags]]


==Deploying a Stack==
==Stack Lifecycle==
===Create an Empty Stack===
An empty stacks with the given name, ready for updates, can be created with <code>[[Pulumi_Operations#Create_Stack|pulumi stack init]]</code>. The stack has no resources stored in the backend, but it can become a target of a deployment initiated with the <code>pulumi update</code> command. <code>pulumi new</code> command creates a default empty stack
===Deploying a Stack===
See [[#Deploying_a_Project|Deploying a Project]] above.
See [[#Deploying_a_Project|Deploying a Project]] above.
===Previewing a Stack===
===Refreshing a Stack===
===Destroying and Deleting a Stack===
'''Destroying''' a stack means releasing <font color=darkkhaki>and deleting</font> infrastructure resources associated with the stack in the infrastructure platform. A stack is destroyed with <code>[[Pulumi_Operations#destroy|pulumi destroy]]</code>. The command uses the latest configuration values, rather than the ones that were last used when the program was deployed. Pulumi waits until all resources are shut down and deleted before it considers the destroy operation to be complete. The destroy operation deletes resources, but it does not delete the stack history or configuration.


==Destroying and Deleting a Stack==
'''Deleting''' a stack with <code>[[Pulumi_Operations#Remove_.28Delete.29_a_Stack|pulumi stack rm]]</code> means removing all stack history from the backend and the stack configuration file <code>Pulumi.<stack-name>.yaml</code>. A stack must be first destroyed, then deleted. {{Note|⚠️ Forcefully deleting a stack before destroying it may leave orphaned resources behind}}
Destroying a stack means releasing <font color=darkkhaki>and deleting</font> resources associated with the stack with <code>[[Pulumi_Operations#destroy|pulumi destroy]]</code>. The command uses the latest configuration values, rather than the ones that were last used when the program was deployed. Pulumi waits until all resources are shut down and deleted before it considers the destroy operation to be complete.


Deleting a stack means removing all stack history from the backend and the stack configuration file <code>Pulumi.<stack-name>.yaml</code>. Use <code>[[Pulumi_Operations#delete|pulumi rm]]</code>. A stack must be first destroyed, then deleted.  
==Stack Export==
A stack can be [[Pulumi_Operations#Export|exported]] to see the raw data associated with the stack. The exported data can be manually modified, then [[Pulumi_Operations#Import|imported]] to set the current state of the stack.  


⚠️ Forcefully deleting a stack before destroying it may leave orphaned resources behind.
⚠️ Importing/exporting stacks in not a normal workflow, but an alternative workflow that subverts the usual way Pulumi manages resources and ensures immutable and repeatable infrastructure deployments. Importing an incorrect stack specification could lead to orphaning of cloud resources or the inability to make future updates to the stack.
==Importing Infrastructure Resources into an Existing Stack==
{{External|https://www.pulumi.com/docs/guides/adopting/import/}}
The import of external resources can be done from CLI with <code>[[Pulumi_Operations#Import_External_Resources_into_a_Stack|pulumi import]]</code>.


==Ephemeral Stack==
==Ephemeral Stack==
Line 219: Line 261:
* [[Pulumi_Operations#Switch_the_Workspace_to_a_Given_Stack|Select a stack]]
* [[Pulumi_Operations#Switch_the_Workspace_to_a_Given_Stack|Select a stack]]
* [[Pulumi_Operations#Remove_.28Delete.29_a_Stack|Remove (delete) a stack]]
* [[Pulumi_Operations#Remove_.28Delete.29_a_Stack|Remove (delete) a stack]]
* [[Pulumi_Operations#Export|Export a stack]]
* [[Pulumi_Operations#Import|Import a stack]]
=<span id='Property'></span><span id='Input_Property'></span><span id='Output_Property'>Resource=
{{Internal|Pulumi Resource#Overview|Resource}}


=<span id='Input'></span><span id='Output'></span><span id='Promise'></span>Inputs and Outputs=
{{Internal|Pulumi Concepts Inputs and Outputs#Overview|Inputs and Outputs}}
=<span id='State'></span>State and Backends=
=<span id='State'></span>State and Backends=
{{External|https://www.pulumi.com/docs/intro/concepts/state/}}
{{Internal|Pulumi_Architecture#State_Management|Pulumi Architecture &#124; State Management}}
<font color=darkkhaki>TO PROCESS: https://www.pulumi.com/docs/intro/concepts/state/</font>
==Backend==
 
The main job of a backend is to reliably store and manage the state of stacks, because Pulumi needs to understand the state of the infrastructure. Pulumi supports different backends: blob storage, locally, or in the [[#Pulumi_Service|Pulumi Service]].
:::[[File:Pulumi_State.png|879px]]
 
State can be interacted with via both [[#CLICLI]] and [[#Programming_Model|programming model]].
 
==What Happens when Code Is Applied to Platform?==
The <code>[[Pulumi_Operations#up|pulumi up]]</code> command evaluates the program and determines resource updates to make. Part of the update process, pulumi will run the preview step of the update, which computes the minimally disruptive change to achieve the desired state described by the program.
 
<font color=darkkhaki>
* Where is the code executed?
* Where does stdout/stderr go?
* Reconcile with [[#Deploying_a_Project|Deploying a Project]]
</font>


==Backend==
Among many other things, a backend provides dependencies to consumer stacks via the [[Infrastructure_as_Code_Concepts#Stack_Data_Lookup|Stack Data Lookup]] pattern.
The main job of a service backend is to reliably manage the state of stacks. It provides dependencies to consumer stacks via the [[Infrastructure_as_Code_Concepts#Stack_Data_Lookup|Stack Data Lookup]] pattern.
===Name===
===Name===
===URL===
===URL===
Line 246: Line 278:
The backend decides what a user can and cannot see, for example [[#Organization|organizations]], based on user's OD group configuration. A user's OD group list, as known by the Pulumi backend, is given by <code>[[Pulumi_Operations#Info|pulumi whoami --verbose]]</code>.
The backend decides what a user can and cannot see, for example [[#Organization|organizations]], based on user's OD group configuration. A user's OD group list, as known by the Pulumi backend, is given by <code>[[Pulumi_Operations#Info|pulumi whoami --verbose]]</code>.


==Service Backend==
==<span id='Service_Backend'></span><span id='Pulumi_Service'></span>Pulumi Service Backend==
Pulumi SaaS:
{{External|https://app.pulumi.com/}}
{{External|https://app.pulumi.com/}}


=Secrets=
=Secrets=
{{External|https://www.pulumi.com/docs/intro/concepts/secrets/}}
{{External|https://www.pulumi.com/docs/intro/concepts/secrets/}}
<font color=darkkhaki>TO PROCESS: https://www.pulumi.com/docs/intro/concepts/secrets/</font>
<font color=darkkhaki>
TO PROCESS: https://www.pulumi.com/docs/intro/concepts/secrets/
 
PULUMI_CONFIG_PASSPHRASE
 
PULUMI_CONFIG_PASSPHRASE_FILE
 
You can swap a passphrase out for a cloud provider key e.g. AWS KMS: https://www.pulumi.com/docs/reference/cli/pulumi_stack_change-secrets-provider/
 
Same shared access to the key for members of your org, and other stacks that use StackReferences,  still applies as with the default passphrase approach.
 
TODO https://www.pulumi.com/learn/building-with-pulumi/secrets/
 
</font>


=Logging=
=Logging=
Line 264: Line 310:
=Function Serialization=
=Function Serialization=
<font color=darkkhaki>TO PROCESS: https://www.pulumi.com/docs/intro/concepts/function-serialization/</font>
<font color=darkkhaki>TO PROCESS: https://www.pulumi.com/docs/intro/concepts/function-serialization/</font>
=Workspace=
=Modularization=
=Modularization=


Line 270: Line 315:


==<span id='Package'>Packages==
==<span id='Package'>Packages==
Packages for various infrastructure providers are published in [[#Pulumi_Registry|Pulumi registry]].
==Pulumi Registry==
==Pulumi Registry==
{{External|https://www.pulumi.com/registry/}}
{{External|https://www.pulumi.com/registry/}}


=CLI=
=CLI=
=Code Examples=
=Auto-Naming=
* [[Python Pulumi Code Examples|Python Code Examples]]
 
Automatically generates a random identifier and places it at the end of the given name, to avoid namespace collisions.
=Automation API=
{{Internal|Pulumi Automation API|Automation API}}


=Pulumi vs Terraform=
=Pulumi vs Terraform=
{{External|https://www.pulumi.com/docs/intro/vs/terraform/}}
{{External|https://www.pulumi.com/docs/intro/vs/terraform/}}
Also see: {{Internal|Terraform|Terraform}}
Also see: {{Internal|Terraform|Terraform}}

Latest revision as of 01:03, 15 April 2022

External

Internal

Overview

Pulumi is an Infrastructure as Code platform that allows using common programming languages, tools, and frameworks, to provision, update, and manage cloud infrastructure resources. Pulumi is one of the tools that can be used to manage generic Infrastructure as Code stacks. In Pulumi, Infrastructure resources are declared in programs. Programs reside in a project. The programs are instantiated as stacks in the infrastructure platform. A stack is an isolated and configurable instance of the program.

Pulumi Concepts.png

Architecture

Pulumi Architecture

Supported Runtimes and Programming Languages

https://www.pulumi.com/docs/intro/languages/

Python

The runtime name is python.

Python Pulumi

TypeScript

The runtime name is nodejs.

TypeScript Pulumi

Go

The runtime name is go.

Go Pulumi

.NET

The runtime name is dotnet.

Organization

https://www.pulumi.com/docs/intro/pulumi-service/organizations/

Other organization is also referred to as the "owner".

The list of organizations available to the user in the backend is available with pulumi org ls.

  • Relationship between a stack and an organization ("owner"). "New stack owner, some-org, does not match existing owner, o_feodorov."

Project

https://www.pulumi.com/docs/intro/concepts/project/

A project is a collection of code and a filesystem layout that supports declaring programs and stack configurations, and at the same time, a namespace mechanisms for stacks on the backend.

Local Project Representation

A project is a directory that contains one program and metadata on how to run the program, such as what runtime to use, where to look for the program, etc. The project definition metadata is maintained in Pulumi.yaml. The presence of Pulumi.yaml is the indication that directory is a Pulumi project.

The project's program may be applied to the infrastructure platform creating one or more independent resource sets, as defined in the program. An instantiation of a program is called a stack. The project may contain one or more stacks. Each stack is defined in the project by its stack settings file.

The project can be obtained programmatically. In Python, use get_project() function.

A project, a program and multiple stacks are tied together in a single execution context named workspace.

Creating a Project

Projects can be created with pulumi new command. The command creates the project metadata on the local filesystem and the first (and possibly the only one) stack, based on a template, and also modifies the backend, registering the newly created stack. At this point, no infrastructure resources are actually created in the infrastructure platform. The program and metadata can be read by the Pulumi CLI and applied to the infrastructure platform with the pulumi up command, updating the stack. Can valid projects be created by hand, without pulumi new, by manually crafting metadata and code?

Current Project

The current project is the project given by the nearest Pulumi.yaml file.

Project Name

The project name should represent the intent of the code within the project. For example, if the project is intended to create infrastructure for an application named "blue", a good project name would be "blue" or "blue-infra". Don't rely on the stack name (or names) to fully convey that semantics. The stack names should be mostly driven by the particular instantiation of the infrastructure resource set, such as "dev", or "staging" ("blue-dev" is a good name).

The project name is specified using the name attribute in Pulumi.yaml. It shows up in the Pulumi dashboard. It is used to aggregate the associated stacks and their resources underneath the project, under a simple hierarchy. Project names may only contain alphanumeric characters, hyphens, underscores and periods.

Workspace

A workspace is an execution context that contains a single project, a single program and multiple stacks. Workspaces manage the execution environment, providing utilities such as plugin installation, environment configuration ($PULUMI_HOME) and creation, deletion and listing of stacks. The workspace relies on Pulumi.yaml and stack settings files as intermediate format for project and stack settings. Pulumi maintains workspace local state in:

~
└─ .pulumi
     └─ workspaces
         └─ <project-name>-8698263d7571....4d46edf4e5bfeff-workspace.json

The content of the workspace JSON file is similar to:

{
  "stack": "<org-name>/<project-name>/<stack-name>"
}

The value of the "stack" key indicates the active stack. The local state is initialized and updated by commands like pulumi stack init, etc.

What happens if the same program is checked out and executed from two different local directories?

Template

A template is a set of files, either built-in or exposed as a Git URL, which contain Python code and Pulumi metadata to bootstrap a project. An essential component of a template is the template section of Pulumi.yaml file. A list of available templates is presented executing pulumi new without any argument. An example is available here:

Available Templates

Intuitively, the right template can be invoked by using the name of the cloud and the language, example: aws-python.

GitHub Template Repository

If intending to access templates directly from a GitHub repository, the repository should be set with "Enable anonymous Git read access" (Settings → Danger Zone → Enable anonymous Git read access)

Project Layout

 my-project
  ├─ README.md
  ├─ Pulumi.yaml          # Project definition file
  ├─ Pulumi.red-stack.yaml # Stack settings file
  ├─ Pulumi.green-stack.yaml
  ├─ ...
  └─ ... # language-specific elements

Language-specific layouts:

Pulumi.yaml

Contains the project definition.

Pulumi.yaml

Deploying a Project

Deploying a project means applying the project's code with the active stack configuration to the infrastructure platform, thus creating or updating infrastructure resources. Deployment is initiated via CLI with the pulumi up command. Reconcile with What Happens when Code Is Applied to Platform?.

Project Operations

Project Operations

Projects on the Backend

Projects are not a first class concept on the backend, they only exist as a way to namespace stacks, and they are reflected in the stack full names. As such, there no command (yet?) that displays the project for a given organization, or for the current organization. To get a list of project for an organization, you can list all stacks, for all organizations, and then filter the results for the specific organization.

A project can be "deleted" by deleting all stacks that belong to it.

Program

A program contains code that describes how cloud infrastructure should be composed. It can be written in Python, TypeScript or Go. Infrastructure is declared by defining resource objects whose properties correspond to the desire state of the infrastructure. The properties are also used to express dependencies between resources, and can be exported outside the stack. It is recommended to group resource with common lifecycles together. Programs reside in projects. Automation API allows defining a Pulumi program as a function within an arbitrary codebase rather than a separated project, and use methods to get and set configuration parameters programmatically.

Local Program

A local program is a traditional Pulumi CLI-driven program with its own directory, a Pulumi.yaml file, along with a file that defines the Pulumi program. Automation API can be used to drive these programs, as well as the CLI.

Inline Program

Unlike traditional Pulumi programs, inline programs don’t require a separate package on disk, with its own file and a Pulumi.yaml. Inline programs are simply functions that can be authored in the same file as your Automation API program or be imported from anther package.

Paths in Program

When the program references resources in the local file system, their paths must be relative to the working directory. Not the project directory, in which the program is in?

Programming Model

Pulumi Programming Model

Stack

https://www.pulumi.com/docs/intro/concepts/stack/

A stack is a unit of deployment, an isolated, independently configurable instance of a Pulumi program, materialized as a set of infrastructure resources created by executing the program against the infrastructure platform. A stack is at the same time a state storage unit. The stack stores the “actual state” of the infrastructure resource set. Should the desired state, as expressed by the program, change, Pulumi compares the desired state with the actual state maintained by the stack, and if detects differences, it updates the infrastructure resources and the stack state. More details on the relationship between stacks and the Pulumi programming model are available in Pulumi Programming Model.

Every program is applied to the infrastructure as one, possibly more stacks. Stacks are commonly used to denote different phases of development, such as "development", "staging" and "production", or feature branches. A project can have an arbitrary number of stacks. By default, Pulumi creates a new empty stack per project when pulumi new is used, and a manually bootstrapped project starts with no stacks. Stacks can be created separately with pulumi stack init. Stack creation means creation of the associated stack settings file and the instantiation of the stack state representation in the Pulumi backend. Initially, a stack starts as "empty", meaning that is has no resource representations stored in the backend, but it can become a target of a deployment initiated with the pulumi update command. Upon the first, and subsequent deployment, the stack backend representation is populated with resources. The state built in memory, after executing the program, represents the "desired state" of the infrastructure, and Pulumi aims to bring the actual state in line with the desired state. Each stack has its own separate configuration, secretes, role-based controls (RBAC) and policies. Deep insight into its structure and components can be achieved by exporting the stack and examining the corresponding JSON file.

Stack Name

The stack name must be unique within a project and should express the intent behind a specific instantiation of the project infrastructure resource set, like "dev" or "testing". A pattern that seems reasonable is <project-name>-<function>, especially that in some circumstances, the project name is omitted from the stack name (like in <org-name>/<stack-name>). For more considerations on stack and project name, see project name. The stack name may only contain alphanumeric characters, hyphens, underscores and periods.

The stack name does now allow slashes, if attempting to use a stack name with slashes in it, the CLI complains: "Sorry, 'my-org/my-project/test1/test2' is not a valid stack name. could not parse stack name 'my-org/my-project/test1/test2'."

Fully Qualified Stack Name

A fully qualified stack name includes the organization and the project name: <org-name>/<project-name>/<stack-name>. If you are using Pulumi in your organization, and when new stacks are created, they will be created by default in your user account. To create the stack in the organization instead, name the stack using <org-name>/<stack-name>.

Stack URI

What is the semantics of a stack URI?

Empty Stack

An empty stack is a stack for which no infrastructure resources have been created yet in the infrastructure platform. Empty stacks are created when projects are initialized with pulumi new, or when pulumi stack init command is used. The opposite of an empty stack is a fully initialized stack. The stack is initialized as result of the pulumi up command.

Multiple Stacks per Project

When a project is created with pulumi new, the configuration of a stack is also automatically created. Additional stack configurations and stack backend state instances can be created for an existing project with the pulumi stack init command. Note that multiple stacks per project means that all the stacks share the same program (behavior) but have different configurations, corresponding to different stack settings files. Note that pulumi stack init does not fully manage the corresponding stack setting file, it does create it, but it leaves it into an incomplete state.

Current (Active, Default) Stack

When a project contains multiple stacks, one of them is active, or current, at any time. The current stack for a project can be displayed by running pulumi about from the project directory, or running pulumi stack ls, in which case the active stack will be marked with an asterisk. A stack can be set as active by running pulumi stack select. The active stack is maintained in the local workspace state, as the value of the "stack" key.

Stack Configuration

https://www.pulumi.com/docs/intro/concepts/config

While each stack of a project has a similar resource set, it will differ from other stacks in its configuration. Pulumi offers a configuration system to manage such differences. The system provides different methods to introspect and set configuration, including CLI and the programming model. The key-value pairs for any given stack of the project are stored in the stack's settings file.

TO PROCESS: https://www.pulumi.com/docs/intro/concepts/config/

Understand the difference between stack configuration and stack output, and how they are related.

Stack Settings File

https://www.pulumi.com/docs/intro/concepts/project/#stack-settings-file

Each stack within a project has an associated file named Pulumi.<stack-name>.yaml that contains configuration, in form of key/value pairs, for the stack. The file resides in the project root directory. Secret values are encrypted, which, according to the Pulumi documentation, makes them safe to be checked in into a repository. This is debatable. The stack settings for ephemeral stacks are typically not checked into source control. A stack setting file can be created and managed with the pulumi config command.

Stack settings files are an implementation of the Infrastructure as Code Stack Configuration File pattern.

Configuration Management via CLI

Stack configuration can be read and updated with the pulumi config command.

Programmatic Configuration Management

Config.

TO DO

Configuration Keys and Namespace

Key space. TO DO

Restoring Configuration from Backend

TO DO: pulumi config refresh

Stack Inputs and Outputs

Stack Inputs and Outputs

Stack References

https://www.pulumi.com/docs/intro/concepts/stack/#stackreferences
https://www.pulumi.com/docs/reference/pkg/nodejs/pulumi/pulumi/#StackReference
https://www.pulumi.com/learn/building-with-pulumi/stack-references/

Stack references are used by consumer stacks to get their dependencies. This is the Pulumi implementation of the Stack Data Lookup pattern: the stack reference allows access to the outputs of a provider stack from a consumer stack. To get values exposed by a producer stack, create an instance of StackReference representing the producer stack, from the consumer stack, and asynchronously access the values exported by producer via Outputs. The provider stack name must be fully qualified, including organization, project and stack name. Then, get the Output instance corresponding to the piece of data you want to access by invoking get_output() on the StackReference instance. The data must be previously exported by the provider stack with export(). Note that get_output() returns an Output instance. The actual data exported by the provider stack can be accessed asynchronously via apply() or other Output access patterns. Once a StackReference has been created, and exported data is accessed via Output instances, Pulumi understands the inter-stack dependency for scenarios like cascading updates.

# consumer stack code
import pulumi

provider = pulumi.StackReference("some-org/some-project/some-provider-stack")

output = provider.get_output("some_parameter"); # this is an Output instance

output.apply(lambda v: ... # do something with the value)

Stack Tags

https://www.pulumi.com/docs/intro/concepts/stack/#stack-tags

Metadata can be attached to stacks, in form of tags. Each tags has a name and a value. A set of built-in tags are automatically assigned and updated each time a stack is updated. Tags are only supported with the Pulumi Service backend. To view a stack's tags, run pulumi stack tag ls. Tags are useful to group stacks in the console.

Custom tags can be applied with pulumi stack tag set <name> <value>. Custom tags should not be prefixed with pulumi:, gitHub:, or vcs: to avoid conflicting with built-in tags that are assigned and updated with fresh values each time a stack is updated.

Built-in Stack Tags

Tag Name Notes
pulumi:project
pulumi:runtime Example of a Python runtime section
pulumi:description
gitHub:owner
gitHub:repo
vcs:owner
vcs:repo
vcs:kind

Tag Operations

Stack Lifecycle

Create an Empty Stack

An empty stacks with the given name, ready for updates, can be created with pulumi stack init. The stack has no resources stored in the backend, but it can become a target of a deployment initiated with the pulumi update command. pulumi new command creates a default empty stack

Deploying a Stack

See Deploying a Project above.

Previewing a Stack

Refreshing a Stack

Destroying and Deleting a Stack

Destroying a stack means releasing and deleting infrastructure resources associated with the stack in the infrastructure platform. A stack is destroyed with pulumi destroy. The command uses the latest configuration values, rather than the ones that were last used when the program was deployed. Pulumi waits until all resources are shut down and deleted before it considers the destroy operation to be complete. The destroy operation deletes resources, but it does not delete the stack history or configuration.

Deleting a stack with pulumi stack rm means removing all stack history from the backend and the stack configuration file Pulumi.<stack-name>.yaml. A stack must be first destroyed, then deleted.


⚠️ Forcefully deleting a stack before destroying it may leave orphaned resources behind

Stack Export

A stack can be exported to see the raw data associated with the stack. The exported data can be manually modified, then imported to set the current state of the stack.

⚠️ Importing/exporting stacks in not a normal workflow, but an alternative workflow that subverts the usual way Pulumi manages resources and ensures immutable and repeatable infrastructure deployments. Importing an incorrect stack specification could lead to orphaning of cloud resources or the inability to make future updates to the stack.

Importing Infrastructure Resources into an Existing Stack

https://www.pulumi.com/docs/guides/adopting/import/

The import of external resources can be done from CLI with pulumi import.

Ephemeral Stack

Ephemeral stacks may have stack settings files.

Stack Operations

Resource

Resource

State and Backends

Pulumi Architecture | State Management

Backend

The main job of a backend is to reliably store and manage the state of stacks, because Pulumi needs to understand the state of the infrastructure. Pulumi supports different backends: blob storage, locally, or in the Pulumi Service.

Among many other things, a backend provides dependencies to consumer stacks via the Stack Data Lookup pattern.

Name

URL

Authentication and Identity

The backend decides what a user can and cannot see, for example organizations, based on user's OD group configuration. A user's OD group list, as known by the Pulumi backend, is given by pulumi whoami --verbose.

Pulumi Service Backend

Pulumi SaaS:

https://app.pulumi.com/

Secrets

https://www.pulumi.com/docs/intro/concepts/secrets/

TO PROCESS: https://www.pulumi.com/docs/intro/concepts/secrets/

PULUMI_CONFIG_PASSPHRASE

PULUMI_CONFIG_PASSPHRASE_FILE

You can swap a passphrase out for a cloud provider key e.g. AWS KMS: https://www.pulumi.com/docs/reference/cli/pulumi_stack_change-secrets-provider/

Same shared access to the key for members of your org, and other stacks that use StackReferences, still applies as with the default passphrase approach.

TODO https://www.pulumi.com/learn/building-with-pulumi/secrets/

Logging

TO PROCESS: https://www.pulumi.com/docs/intro/concepts/logging/

Assets and Archives

TO PROCESS: https://www.pulumi.com/docs/intro/concepts/assets-archives/

Plugin

Plugin Operations

Function Serialization

TO PROCESS: https://www.pulumi.com/docs/intro/concepts/function-serialization/

Modularization

TO PROCESS: https://www.pulumi.com/blog/creating-and-reusing-cloud-components-using-package-managers/

Packages

Packages for various infrastructure providers are published in Pulumi registry.

Pulumi Registry

https://www.pulumi.com/registry/

CLI

Auto-Naming

Automatically generates a random identifier and places it at the end of the given name, to avoid namespace collisions.

Automation API

Automation API

Pulumi vs Terraform

https://www.pulumi.com/docs/intro/vs/terraform/

Also see:

Terraform