Using Pulumi automation API to deploy GCP instances in an http framework does not work

0 votes

When I deploy a GCP compute instance from a script eg test_deploy.py using the Pulumi Automation API the instance is deployed without a hitch. When I run the exact same code from a Django POST endpoint one of two things happen. If I run the deploy against an existing stack it will delete the running instance. If I run the deploy on a new stack nothing happens. Is there some setting I am missing? What is weird this does not happen when I deploy Firewalls or VPC,s via the endpoint. Everything will work as expected. code:

########## instance_stack.py##################
class DeployVmInstance(BaseStack):
    def __init__(self,
                 project_id,
                 subnet_name,
                 stack_name,
                 stack_region,
                 stack_zone,
                 instance_tags,
                 image_name,
                 disk_size=DEFAULT_DISK_SIZE,
                 machine_type=DEFAULT_VM_HOST_MACHINE_TYPE,
                 timeouts=DEFAULT_TIMEOUTS,
                 desired_status=DEFAULT_VM_DESIRED_STATUS):
        self.subnet_name = subnet_name
        self.stack_name = stack_name
        self.project_id = project_id
        self.project_name = f'{self.stack_name}-vm'
        self.vm_name = self.project_name
        self.stack_region = stack_region
        self.stack_zone = stack_zone
        self.machine_type = machine_type
        self.instance_tags = instance_tags
        self.disk_size = disk_size
        self.timeouts = timeouts
        self.image_name = image_name
        self.desired_status = desired_status

    def set_instance_status(self, status):
        self.desired_status = status

    def create_stack(self):
        '''
        Create instance
 subnet = compute.get_subnetwork(name=self.subnet_name,
                                        region=self.stack_region)    
        vm_instance = compute.Instance(self.vm_name,
                                       desired_status=self.desired_status,
                                       machine_type=self.machine_type,
                                       advanced_machine_features=FEATURE_ARGS(enable_nested_virtualization=True), # noqa
                                       boot_disk=BOOT_DISK_ARGS(
                                           initialize_params=BOOT_INIT_ARGS(
                                               image=self.image_name,
                                               size=self.disk_size
                                            )
                                        ),
                                       network_interfaces=[NETWORK_ARGS(subnetwork=subnet.id)], # noqa
                                       tags=self.instance_tags,
                                       zone=self.stack_zone)

        network_ip = vm_instance.network_interfaces[0]['network_ip']
        pulumi.export('network_ip', network_ip)
        pulumi.export('boot_disk', vm_instance.boot_disk.source)
        pulumi.export('boot_disk_name', vm_instance.boot_disk.device_name)

    def deploy_stack(self):
        '''
        Deploy stack
        '''
        stack = auto.create_or_select_stack(stack_name=self.stack_name,
                                            project_name=self.project_name,
                                            program=self.create_stack)
        stack.set_config("gcp:project", auto.ConfigValue(self.project_id))
        up_res = stack.up(on_output=print)
        return up_res.outputs

############test_deploy.py(works fine)############################
from instance_stack.py import DeployVmInstance

PROJECT_ID = '***************'
DEFAULT_REGION = 'us-central1'
new_vm_tags = ['test-vm']
new_vm = DeployVmInstance(project_id=PROJECT_ID,
                          subnet_name='testvm-dimcorp-subnet',
                          stack_name='test-created-vm',
                          stack_region=DEFAULT_REGION,
                          stack_zone='us-central1-b',
                          instance_tags = new_vm_tags,
                          image_name="****/global/images/ubuntu")
                          new_vm.deploy_stack()

############# From Django launch view.py(does not work)############
# Bad behavior includes deleting existing stack when run
# or Running new stack with no instance being deployed.
# no errors displayed.

from instance_stack.py import DeployVmInstance

PROJECT_ID = '***************'
DEFAULT_REGION = 'us-central1'


class ImageLaunchView(APIView):
    def post(self, request, pk, format=None):
        new_vm_tags = ['test-vm']
        new_vm = DeployVmInstance(project_id=PROJECT_ID,
                          subnet_name='testvm-dimcorp-subnet',
                          stack_name='test-created-vm',
                          stack_region=DEFAULT_REGION,
                          stack_zone='us-central1-b',
                          instance_tags = new_vm_tags,
                          image_name="****/global/images/ubuntu")
       new_vm.deploy_stack()

Mar 17, 2022 in GCP by Rahul
• 3,380 points
662 views

1 answer to this question.

0 votes

This would allow for constructors to not have side effects on the environment. When I do

new S3.Bucket() 

I don't want to do anything with that bucket definition unless I pass it to 

pulumi.up()

or some other function that consumes it. If it goes out of scope then it should be garbage collected and not held in some registry in the background.

With that there should be no need for wrapping the construction of these objects into functions. 
i would prefer an API like this

const context = pulumi.createContext();
const bucket = new S3.Bucket("myBucket");

context.add(bucket);

pulumi.up(context, ...options);

answered Apr 5, 2022 by Korak
• 5,820 points

Related Questions In GCP

0 votes
1 answer
0 votes
1 answer

Connect to an instance as a root user in GCP

If you configured an instance to allow ...READ MORE

answered Sep 24, 2019 in GCP by Sirajul
• 59,230 points
5,401 views
0 votes
1 answer

How does the pricing model work in GCP cloud?

While working with Google Cloud Platform, the ...READ MORE

answered Oct 7, 2019 in GCP by Sirajul
• 59,230 points
2,852 views
+1 vote
1 answer

Error while using a HTTP Cloud function in GCP.

If CORS isn't set up properly, you're ...READ MORE

answered Oct 23, 2019 in GCP by Sirajul
• 59,230 points
1,129 views
0 votes
1 answer
0 votes
1 answer

GCP pricing API does not have data for compute instances like N2, M2, C1

You can check the list of available ...READ MORE

answered Apr 6, 2022 in GCP by Korak
• 5,820 points
759 views
0 votes
1 answer

Scoutsuite output is showing IAM api is not enabled when it is enabled in gcp console

The error is failed to retrieve KMS ...READ MORE

answered Apr 6, 2022 in GCP by Korak
• 5,820 points
645 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP