Do yall know a good tutorial for using Pulumi + GC...
# getting-started
p
Do yall know a good tutorial for using Pulumi + GCP to run a container? I can’t really use Cloud Run, [it doesn’t fit my model very well](https://stackoverflow.com/questions/72354350/how-to-run-container-every-5-seconds-with-gcp). I know on the UI you can configure a container right there when making your instance, I just can’t see how to do that via Pulumi.
o
Running every 5 seconds is a pretty tight budget for container execution, running a VM or "always on" Cloud Run with Cloud Schedule does seem like an appropriate solution.
What UI page in the Cloud Console are you looking at when you say this:
I know on the UI you can configure a container right there when making your instance
Are you talking about a Compute Engine VM?
p
Yeah Compute Engine VM
I managed to accomplish a similar thing with this
Copy code
// Build the run command.
let runCommand = pulumi.all([cfg.requireSecret("account_private_key"), cfg.requireSecret("spotify_client_id"), cfg.requireSecret("spotify_client_secret")]).apply(([account_private_key, spotify_client_id, spotify_client_secret]) => [
    "docker",
    "run",
    "--pull", "always",
    "-v", "/tmp:/hostcache",
    "<http://ghcr.io/banool/aptos-infinite-jukebox-driver:main|ghcr.io/banool/aptos-infinite-jukebox-driver:main>",
    "--account-private-key", account_private_key,
    "--spotify-client-id", spotify_client_id,
    "--spotify-client-secret", spotify_client_secret,
    "--cache-path", "/hostcache/cache.json",
].join(" "));

// Create a Compute Engine instance.
const driverInstance = new gcp.compute.Instance("driver-instance", {
    machineType: "f1-micro",
    zone: ${region}-c,
    bootDisk: {
        initializeParams: {
            // I used <https://console.cloud.google.com/compute/images> to find the
            // family and name to use here. This is the Container Optimized OS.
            image: "projects/cos-cloud/global/images/cos-stable-97-16919-29-21",
        },
    },
    allowStoppingForUpdate: true,
    deletionProtection: false,
    networkInterfaces: [{
        network: "default",
        accessConfigs: [{}],
    }],
    metadataStartupScript: runCommand
});

export const driverInstanceId = driverInstance.id;
q
Hello, I'm searching for an example like this thread, but using the metadata keys the COS image supports to launch the container automatically instead of a startup script. Any pointers?
By setting up an example project using the terraform-google-container-vm module, outputting it, and looking at how it uses the values in examples, I was able to reconstruct metadata values that work instead of using a startup script that bypasses konlet:
Copy code
dockerImage := fmt.Sprintf("%s-docker.pkg.dev/%s/example/image:latest", region, project)

spec := map[interface{}]interface{}{
  "spec": map[interface{}]interface{}{
    "containers": []interface{}{
      map[interface{}]interface{}{
        "env": []interface{}{
          map[interface{}]interface{}{
            "name":  "ENV_VAR",
            "value": value_str,
          },
        },
        "image": dockerImage,
      },
    },
    "volumes":       []string{},
    "restartPolicy": "OnFailure",
  },
}

specYAML, err := yaml.Marshal(spec)
if err != nil {
  return
}

metadata := map[string]string{
  "gce-container-declaration": string(specYAML),
  "google-logging-enabled":    "true",
  "google-monitoring-enabled": "true",
}

return compute.NewInstance(ctx, "example", &compute.InstanceArgs{
  ...
  BootDisk: &compute.InstanceBootDiskArgs{
	InitializeParams: &compute.InstanceBootDiskInitializeParamsArgs{
		Image: pulumi.String("projects/cos-cloud/global/images/family/cos-stable"),
	},
  },
  Metadata: pulumi.ToStringMap(metadata),
  ...
})
To follow up to my earlier question, I posted an example of what I found to run containers on GCE using metadata values in this thread.