0
votes

I'm trying to programmatically start up GCE instances from the C#/REST API (ie not using gcloud or the console). These instances must have values (identifying who started them, so will be different for each instance) passed to them during start-up, which will then get passed to the various applications running within. The obvious way of adding environment variables to the GCE request doesn't appear to be possible, so how does one provide dynamic configuration?

My current code for creating the instances which works fine:

    public void CreateInstance(string name)
    {
        var credentials = GoogleCredential.GetApplicationDefault().CreateScoped(ComputeService.Scope.Compute);
        var service = new ComputeService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credentials,
            ApplicationName = "Spin-up"
        });

        var spec = new Google.Apis.Compute.v1.Data.Instance
        {
            Name = name
        };

        var instanceTemplate = service.InstanceTemplates.Get(GCloudConfig.ProjectName, GCloudConfig.TemplateName).Execute();
        var insertRequest = service.Instances.Insert(spec, GCloudConfig.ProjectName, GCloudConfig.Region);

        insertRequest.SourceInstanceTemplate = instanceTemplate.SelfLink;
        insertRequest.Execute();
    }   
1
Create a startup script and include when launching or restarting the instance. cloud.google.com/compute/docs/startupscript - John Hanley

1 Answers

0
votes

You might use the API for Compute Engine Method: instances.start or instances.stop to trigger the start or stop of VM Instances with C# code requests.

Additional information about these API methods are described in documents: instances.start. and instances.stop.