https://pulumi.com logo
#general
Title
# general
p

proud-artist-4864

03/09/2019, 4:50 AM
Question about importing existing resources. Other than manually editing a state file, is there a way to do this? What happens if I do a “pulumi refresh” with an empty state file? Will it sort of “import” any of the resources that it finds that match?
👍 1
g

gentle-diamond-70147

03/09/2019, 3:49 PM
There’s not an official built-in way to do this today, but this is something we are actively working on.
You can do some manual steps to import resources similar to what you described - export your stack file, add the resource with its associated cloud id (you can omit all the properties about it), then import it back into your stack, and do
pulumi refresh
. This will fill in all its properties.
Be sure to get the urn and provider references correct for each resource you’re adding to the stack file. Then you’ll need to get the code to match the resource otherwise pulumi will try to change the resource on the next
pulumi up
.
Please do this first in a test environment so you don’t import and then unintentionally change or destroy any resources. 😬
p

proud-artist-4864

03/10/2019, 4:14 AM
heh, not planning on doing anything on prod until we’re sure 😄
That’s why I want to know how to import 😄
What I’d really like is a way for pulumi to be run in some sort of “import/refresh” mode where it runs the engine like in “update” but instead of creating resources, it imports them if they match the relevant information (ie, tags, attributes etc). That way, it avoids using the “data source” type functions because there’s no difference, perhaps the opts could have a “import_before_create” option or something.
👍 1
s

stocky-island-3676

03/11/2019, 4:06 PM
@proud-artist-4864 Where can I find the “data source” type functions? I didn’t find them yet.
@gentle-diamond-70147 The fields
"custom": true
&
type
must also be set for the manual import. I.e., you need at least these fields in the stack, right?: •
urn
, •
custom
, •
id
, •
type
, •
parent
, •
provider
g

gentle-diamond-70147

03/11/2019, 4:19 PM
What I’d really like is a way for pulumi to be run in some sort of “import/refresh” mode where it runs the engine like in “update” but instead of creating resources, it imports them if they match the relevant information (ie, tags, attributes etc).
I think that's very close to how we've designed it.
@stocky-island-3676 generally any of the functions listed in the API documentation are data sources - e.g. https://pulumi.io/reference/pkg/nodejs/@pulumi/aws/index.html#getAvailabilityZones
And yes, you need to set all of those. Sorry I omitted some of them in my answer.
s

stocky-island-3676

03/11/2019, 5:24 PM
@gentle-diamond-70147 Thanks for your answers.
get...
is so obvious. It’s a shame that I didn’t found them, yet 🙂
g

gentle-diamond-70147

03/11/2019, 6:10 PM
We need to do a better job of point them out. I didn't notice them at first either. 😁
p

proud-artist-4864

03/12/2019, 5:07 AM
That’s why I’d prefer to get rid of the distinction 🙂 Really, a data source is just another resource in the graph that doesn’t get created or deleted. So if I want to import something, I instantiate whatever it is, with an option to say that this needs to exist and to fail if it doesn’t. (or alternatively, create it if it doesn’t exist).