Skip to content

Commit f82a97c

Browse files
docs: document Automation API resource import (#20859)
* docs: document Automation API resource import Add an "Importing resources" section to the Automation API concepts page covering Stack.import / import_resources / ImportResources / ImportAsync across TypeScript, Python, Go, and .NET, and note that Java's Automation API has no equivalent yet. Fixes #20858 * docs: fix stack-constructor calls in import snippets Python used a nonexistent LocalWorkspace.create_or_select_stack method; switch to the module-level auto.create_or_select_stack used by every other Python sample on the site. Go used a nonexistent auto.UpsertStack; switch to auto.UpsertStackLocalSource, matching the local-project narrative and the rest of the site. Also tightens one wordy sentence. --------- Co-authored-by: workprentice <257153108+workprentice@users.noreply.github.com>
1 parent 4b66133 commit f82a97c

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

content/docs/iac/concepts/automation-api.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,133 @@ A `Workspace` still exposes an explicit `installPlugin` method (`install_plugin`
9292

9393
For background on plugins and the [`pulumi plugin`](/docs/iac/cli/commands/pulumi_plugin/) CLI commands that manage them, see [Pulumi packages](/docs/iac/concepts/packages/).
9494

95+
## Importing resources
96+
97+
A `Stack` also exposes an import operation, the programmatic equivalent of the [`pulumi import`](/docs/iac/cli/commands/pulumi_import/) CLI command. It brings existing cloud resources under Pulumi management without creating or modifying anything in the target cloud, generates program code for the imported resources, and records them in the stack's state so later updates manage them going forward. This makes it the building block for programmatic brownfield adoption---platforms that migrate teams onto Pulumi Cloud in bulk, rather than one resource at a time from the CLI, drive that migration through this method.
98+
99+
The method takes a list of resources to import, each identified by its Pulumi type token, a logical name, and the cloud provider's own resource ID. If any imported resource specifies a parent or provider, you also need a name table mapping the language names used in the generated program to their corresponding parent and provider URNs. By default, imported resources are protected from deletion and the operation generates program code alongside the import; both behaviors can be turned off.
100+
101+
The method name differs slightly across languages, since `import` is a reserved word in some of them:
102+
103+
{{< chooser language "typescript,python,go,csharp,java" >}}
104+
105+
{{% choosable language "typescript" %}}
106+
107+
```typescript
108+
import { LocalWorkspace } from "@pulumi/pulumi/automation";
109+
110+
const stack = await LocalWorkspace.createOrSelectStack(args);
111+
112+
const result = await stack.import({
113+
resources: [
114+
{
115+
type: "aws:s3/bucketV2:BucketV2",
116+
name: "my-bucket",
117+
id: "my-existing-bucket-name",
118+
},
119+
],
120+
protect: false,
121+
});
122+
123+
console.log(result.generatedCode);
124+
```
125+
126+
{{% /choosable %}}
127+
128+
{{% choosable language "python" %}}
129+
130+
```python
131+
from pulumi import automation as auto
132+
from pulumi.automation import ImportResource
133+
134+
stack = auto.create_or_select_stack(stack_name=stack_name, work_dir=work_dir)
135+
136+
result = stack.import_resources(
137+
resources=[
138+
ImportResource(
139+
type="aws:s3/bucketV2:BucketV2",
140+
name="my-bucket",
141+
id="my-existing-bucket-name",
142+
),
143+
],
144+
protect=False,
145+
)
146+
147+
print(result.generated_code)
148+
```
149+
150+
{{% /choosable %}}
151+
152+
{{% choosable language "go" %}}
153+
154+
```go
155+
import (
156+
"fmt"
157+
158+
"github.com/pulumi/pulumi/sdk/v3/go/auto"
159+
"github.com/pulumi/pulumi/sdk/v3/go/auto/optimport"
160+
)
161+
162+
stack, err := auto.UpsertStackLocalSource(ctx, stackName, workDir)
163+
if err != nil {
164+
return err
165+
}
166+
167+
result, err := stack.ImportResources(ctx,
168+
optimport.Resources([]*optimport.ImportResource{
169+
{
170+
Type: "aws:s3/bucketV2:BucketV2",
171+
Name: "my-bucket",
172+
ID: "my-existing-bucket-name",
173+
},
174+
}),
175+
optimport.Protect(false),
176+
)
177+
if err != nil {
178+
return err
179+
}
180+
181+
fmt.Println(result.GeneratedCode)
182+
```
183+
184+
{{% /choosable %}}
185+
186+
{{% choosable language "csharp" %}}
187+
188+
```csharp
189+
using Pulumi.Automation;
190+
191+
var stack = await LocalWorkspace.CreateOrSelectStackAsync(args);
192+
193+
var result = await stack.ImportAsync(new ImportOptions
194+
{
195+
Resources = new List<ImportResource>
196+
{
197+
new ImportResource
198+
{
199+
Type = "aws:s3/bucketV2:BucketV2",
200+
Name = "my-bucket",
201+
Id = "my-existing-bucket-name",
202+
},
203+
},
204+
Protect = false,
205+
});
206+
207+
Console.WriteLine(result.GeneratedCode);
208+
```
209+
210+
{{% /choosable %}}
211+
212+
{{% choosable language "java" %}}
213+
214+
The Java Automation API doesn't yet expose a resource-import method; `WorkspaceStack` has no equivalent to the other languages' `import`/`import_resources`/`ImportResources`/`ImportAsync`. Drive `pulumi import` directly through the CLI in the meantime.
215+
216+
{{% /choosable %}}
217+
218+
{{% /chooser %}}
219+
220+
This capability has shipped since Pulumi CLI v3.127.0. If your program [installs the CLI programmatically](/docs/iac/guides/building-extending/automation-api/#install-the-cli-programmatically) rather than relying on a preinstalled copy, make sure it resolves to that version or later.
221+
95222
## Supported languages
96223

97224
Like the rest of Pulumi, Automation API is available in multiple languages, so you can build applications that use it in TypeScript/JavaScript, Python, Go, .NET, and Java. Automation API also supports cross-language use, where it runs in a program written in a different language than the Pulumi programs it manages.

0 commit comments

Comments
 (0)