Importing Infrastructure
Most infrastructure projects require working with existing cloud resources, either by building on top of existing resources or adopting existing resources under management with a new and more robust infrastructure provisioning solution.
No matter how you've provisioned these resources — manually in your cloud provider's console or CLI, using an infrastructure as code tool like Terraform or AWS CloudFormation — Pulumi enables you to adopt and manage your resources.
When working with existing resources, there are two primary scenarios:
- You need to reference existing resources to use as inputs to new resources in Pulumi
- You need to adopt existing resources under management so they can be managed by Pulumi
For the first situation, consult the user guide index. For the second, let's now see how to adopt existing resources.
Adopting Existing Resources
There are two ways to adopt existing resources so that Pulumi is able to manage subsequent updates to them. The first way is to use the import cli command. This command specifies that a resource defined in your Pulumi program should adopt an existing resource from a cloud provider rather than creating a new one after running pulumi up. Another way is to use the import resource option. This resource option is defined in your Pulumi program, and like the import command, the import resource option adopts an existing resource in the cloud provider rather creating a new one. Using this option lets you specify the import behavior inside the Pulumi code for your infrastructure deployment, instead of outside of it in a manual workflow.
警告
Your Pulumi stack must be configured correctly---e.g., using the same AWS region as the resource your importing---otherwise the resource will not be found.
Pulumi Import Command
This example imports an existing AWS S3 bucket named company-infra-logs and defines the resource name for your Pulumi program as infra-logs:
pulumi import aws:s3/bucket:Bucket infra-logs company-infra-logs Type Name Plan
+ pulumi:pulumi:Stack import-post-dev create
= └─ aws:s3:Bucket infra-logs import
Resources:
+ 1 to create
= 1 to import
2 changesPulumi will perform the import of the S3 bucket and generate the code required for you to add it to your application.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const demo = new aws.s3.Bucket("infra-logs", {
acl: "private",
bucket: "company-infra-logs",
forceDestroy: false,
}, {
protect: true,
});After successfully importing a resource and adding the generated code to your program, you can run pulumi up and all subsequent operations will behave as though Pulumi provisioned the resource from the outset. The resource is added to the Pulumi state, and marked as a protected resource (by default) to ensure that imported infrastructure is not accidentally deleted if the user forgets to include the code for the resource in their program before doing a deployment.
Pulumi Import Resource Operation
This example imports an existing AWS EC2 security group with ID sg-04aeda9a214730248:
import * as aws from "@pulumi/aws";
let group = new aws.ec2.SecurityGroup("my-sg", {
name: "my-sg-62a569b",
ingress: [{ protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }],
}, { import: "sg-04aeda9a214730248" });Note: Import IDs are resource specific. The ID to use is the same as the ID that gets assigned when Pulumi has provisioned a resource of that type from scratch.
When Pulumi first sees a resource with an import option set (in this case my-sg), it will adopt the existing resource by querying the target cloud provider for a resource of that type with the given ID, instead of creating a new resource as usual.
To perform the import, run pulumi up as usual, and you will see = instead of the usual +, indicating an import operation:
$ pulumi up
Previewing update (dev):
Type Name Plan
pulumi:pulumi:Stack import-dev
= └─ aws:ec2:SecurityGroup my-sg import
Resources:
= 1 to import
1 unchangedIf the resource is not found, an error will occur:
error: Preview failed: importing sg-04aeda9a214730248: security group not foundAfter successfully importing a resource, you can delete the import statement, rerun pulumi up, and all subsequent operations will behave as though Pulumi provisioned the resource from the outset. Be careful, as this applies to destroy operations also.
Mismatched State
An important part of importing resources using the import resource option is that the resulting Pulumi program, after the import is complete, will faithfully generate the same desired state as your existing infrastructure's actual state. After the import, you may edit your program to generate and apply new desired states to update your infrastructure.
Because of this, all properties need to be fully specified. If you forget to specify a property, or that property's value is incorrect, you'll first receive a warning during preview, and then an error during the actual import update.
For instance, keeping with the example above, let's say we specified the wrong ingress rule by choosing port 22 instead of port 80. We'll see a warning:
$ pulumi preview
Previewing update (dev):
Type Name Plan Info
pulumi:pulumi:Stack import-dev
= └─ aws:ec2:SecurityGroup my-sg import [diff: ~ingress]; 1 warning
Diagnostics:
aws:ec2:SecurityGroup (my-sg):
warning: imported resource sg-04aeda9a214730248's property 'ingress' does not match the existing value;
importing this resource will failIf we'd like to see details on what specifically did not match, select the details option:
+ pulumi:pulumi:Stack: (create)
[urn=urn:pulumi:dev::import::pulumi:pulumi:Stack::import-dev]
= aws:ec2/securityGroup:SecurityGroup: (import)
[id=sg-0d188488272df7df8]
[urn=urn:pulumi:dev::import::aws:ec2/securityGroup:SecurityGroup::my-sg]
[provider=urn:pulumi:dev::import::pulumi:providers:aws::default_1_22_0::04da6b54-80e4-46f7-96ec-b56ff0331ba9]
~ ingress: [
~ [0]: {
~ cidrBlocks : [
~ [0]: "0.0.0.0/0" => "0.0.0.0/0"
]
- description: ""
~ fromPort : 80 => 22
~ protocol : "tcp" => "tcp"
~ self : false => false
~ toPort : 80 => 22
}
]Attempting to proceed will fail completely with an error:
Diagnostics:
pulumi:pulumi:Stack (import-dev):
error: update failed
aws:ec2:SecurityGroup (my-sg):
error: imported resource sg-04aeda9a214730248's property 'ingress' does not match the existing valueNote: Because of auto-naming, it's common to accidentally get in a situation where names don't match. For example, if we left off the security group's name,
"my-sg-62a569b", in the earlier example, Pulumi would still auto-name the resource, leading to an errorimported resource sg-04aeda9a214730248's property 'name' does not match the existing value. To fix this problem, make sure to specify names for all resources explicitly.
Bulk Import Operations
If you need to import multiple resources, the CLI import command can be used with a JSON file that contains references to existing cloud resources. Using a JSON file with the import command can be helpful as part of scripting large bulk imports of cloud resources.
{
"resources": [{
"type": "aws:ec2/vpc:Vpc",
"name": "application-vpc",
"id": "vpc-0ad77710973388316"
},
{
"type": "aws:ec2/subnet:Subnet",
"name": "public-1",
"id": "subnet-0fb5fdff92b9e5a3b"
},
{
"type": "aws:ec2/subnet:Subnet",
"name": "private-1",
"id": "subnet-0a39d25dd9f7b7808"
}
]
}We can then run the command:
pulumi import -f resources.jsonWe can then see Pulumi will generate all of the resource code for us as follows:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const application_vpc = new aws.ec2.Vpc("application-vpc", {
assignGeneratedIpv6CidrBlock: false,
cidrBlock: "172.16.0.0/16",
enableDnsSupport: true,
instanceTenancy: "default",
tags: {
Name: "pulumi-vpc",
Owner: "pulumi",
Project: "pulumi-k8s-aws-cluster",
},
}, {
protect: true,
});
const public_1 = new aws.ec2.Subnet("public-1", {
assignIpv6AddressOnCreation: false,
cidrBlock: "172.16.32.0/19",
mapPublicIpOnLaunch: true,
tags: {
Name: "pulumi-vpc-public-1",
Owner: "pulumi",
Project: "pulumi-k8s-aws-cluster",
"kubernetes.io/role/elb": "1",
type: "public",
},
vpcId: "vpc-0ad77710973388316",
}, {
protect: true,
});
const private_1 = new aws.ec2.Subnet("private-1", {
assignIpv6AddressOnCreation: false,
cidrBlock: "172.16.160.0/19",
mapPublicIpOnLaunch: false,
tags: {
Name: "pulumi-vpc-private-1",
Owner: "pulumi",
Project: "pulumi-k8s-aws-cluster",
"kubernetes.io/role/internal-elb": "1",
type: "private",
},
vpcId: "vpc-0ad77710973388316",
}, {
protect: true,
});Check out the video clip below for a demo.