diff --git a/knowledge base/pulumi.md b/knowledge base/pulumi.md index 61cf564..b9d0233 100644 --- a/knowledge base/pulumi.md +++ b/knowledge base/pulumi.md @@ -16,6 +16,8 @@ 1. [Enforce specific backends for projects](#enforce-specific-backends-for-projects) 1. [Migrate to different backends](#migrate-to-different-backends) 1. [Compose resources](#compose-resources) +1. [Import resources](#import-resources) + 1. [Import components and their children](#import-components-and-their-children) 1. [Troubleshooting](#troubleshooting) 1. [A project with the same name already exists](#a-project-with-the-same-name-already-exists) 1. [Stack init fails because the stack supposedly already exists](#stack-init-fails-because-the-stack-supposedly-already-exists) @@ -907,6 +909,112 @@ const currentVpc = new StandardAwsVpc( +## Import resources + +FIXME: should this be under [Program] or [Stack]? + +Refer [Importing resources] and the [`pulumi import`][pulumi import] command. + +```sh +pulumi import --file 'import.json' +pulumi import 'aws:ec2/instance:Instance' 'logstash' 'i-abcdef0123456789a' --suppress-outputs +pulumi import 'aws:cloudwatch/logGroup:LogGroup' 'vulcan' '/ecs/vulcan' --generate-code='false' --protect='false' +pulumi import 'aws:ec2/subnet:Subnet' 'public_subnet' 'subnet-9d4a7b6c' --parent 'current=urn:pulumi:someStack::someProject::aws:ec2/vpc:Vpc::current' +``` + +### Import components and their children + +Create an import file for the resources that would be created, then import them using `pulumi import --file +'import.json'`. + +Simplify the process by leveraging the [`preview`][pulumi preview] command. + +
+ +1. Write some code that would create the components: + + ```ts + import * as awsx from "@pulumi/awsx"; + + const vpc = new awsx.ec2.Vpc("current"); + + export const vpcId = vpc.vpcId; + export const privateSubnetIds = vpc.privateSubnetIds; + export const publicSubnetIds = vpc.publicSubnetIds; + ``` + +1. Generate a placeholder import file for the resources that would be created: + + ```sh + pulumi preview --import-file 'import.json' + ``` + + ```json + { + "resources": [ + { + "type": "awsx:ec2:Vpc", + "name": "current", + "component": true + }, + { + "type": "aws:ec2/vpc:Vpc", + "name": "currentVpc", + "id": "", + "parent": "current", + "version": "6.66.3", + "logicalName": "current" + }, + { + "type": "aws:ec2/subnet:Subnet", + "name": "current-public-1", + "id": "", + "parent": "currentVpc", + "version": "6.66.3" + }, + … + } + ``` + +1. Change the IDs in the import file accordingly: + + ```diff + { + "resources": [ + { + "type": "awsx:ec2:Vpc", + "name": "current", + "component": true + }, + { + "type": "aws:ec2/vpc:Vpc", + "name": "currentVpc", + - "id": "", + + "id": "vpc-abcdef26", + "parent": "current", + "version": "6.66.3", + "logicalName": "current" + }, + { + "type": "aws:ec2/subnet:Subnet", + "name": "current-public-1", + - "id": "", + + "id": "subnet-0123456789abcdef0", + "parent": "currentVpc", + "version": "6.66.3" + }, + … + } + ``` + +1. Import using the import file: + + ```sh + pulumi import --file 'import.json' + ``` + +
+ ## Troubleshooting ### A project with the same name already exists @@ -1032,6 +1140,7 @@ Solution: follow the suggestion in the warning message: - [Pulumi troubleshooting] - [`pulumi new`][pulumi new] - [`pulumi config set-all`][pulumi config set-all] +- [Importing resources]