Hi, is there a way to import existing gcp resource...
# general
g
Hi, is there a way to import existing gcp resources using pulumi automation api? Any example/ideas/advice/etc will be greatly appreciated.
l
No. You could script it. But given that importing is necessarily a once-only thing, this isn't a supported use case
g
Thx. Can the resources be imported into an existing stack that is created through automation api?
l
Yes, absolutely.
g
Ok, we create project and stacks through the automation api and there are no yaml files (Pulumi.yaml and Pulumi.dev.yaml). How do I link/refer the resource that is imported through Pulumi CLI (pulumi import) which creates the yaml files. And import command just displayed the code for the imported resource. I'm trying to figure out how to use this generated code with my pulumi automation solution. Here is the use-case: I imported the existing gcp network and subnets and now I want to enable other options that are not enabled by default. default_network = gcp.compute.get_network( name=NETWORK, project=GCP_project_ID, ) default_subnet = gcp.compute.get_subnetwork( name=SUBNETWORK, project=GCP_project_ID, region=varregion, # network=default_network.id, ) # this is the code generated by pulumi import for subnet existing_subnet = gcp.compute.Subnetwork( "my-subnet", # ip_cidr_range="10.128.0.0/20", ip_cidr_range=default_subnet.ip_cidr_range, name=SUBNETWORK, # network="https://www.googleapis.com/compute/v1/projects/plated-entry-378508/global/networks/default", network=default_network.self_link, private_ip_google_access=
True
, private_ipv6_google_access="DISABLE_GOOGLE_ACCESS", project=GCP_project_ID, purpose="PRIVATE", region=varregion, stack_type="IPV4_ONLY", opts=ResourceOptions( import_=default_subnet.id, protect=True, ), # opts=pulumi.ResourceOptions(protect=True) ) ============================= This is the modification I want to make i.e private_ip_google_access=
False
, # Create a new subnet resource using the imported resource as a base new_subnet = gcp.compute.Subnetwork( "new-subnet", region=existing_subnet.region, network=existing_subnet.network, ip_cidr_range="10.0.1.0/24", private_ip_google_access=
False
, ) But instead of modify the existing resource, it is creating another subnet
l
If you're using
pulumi import
, you can use the
--stack
option to specify which stack you need, and you don't need a Pulumi.stack.yaml file. I think you need a Pulumi.yaml file, but you don't need to commit it to source control, you can delete it as soon as you've imported. You do need to add the generated code to your project. If you're using the
import
opt, then you don' t need to do anything. Just run your program once with the import opts, and then remove them.
g
It worked and thank you. Is there a way to avoid two passes i.e. first import the resource with opts and then remove it?
l
If you're using the code-it-yourself method, no.
pulumi import
doesn't require 2 passes.