Google Certified Associate Cloud Engineer 2020

Sign Up Free or Log In to participate!

Advanced Challenge Lab in CLI confusion

While I’m working through the lab in the console, I’m also trying to think about how I will do the lab with the command line interface. There’s a couple things I am confused on: 

How can I link on project to a billing account in the command line?

How can I create an instance with a startup script in the command line? I typed in [gcloud compute instances create –help] and I didn’t find anything that seemed specific to a startup script.

3 Answers

To your second question: You supply that through metadata

https://cloud.google.com/compute/docs/startupscript?_ga=2.118305089.-913515807.1597820611#providing_startup_script_contents_directly

(select gcloud tab)

Alternatively you could play with 

1. Loading the longer startup script to a bucket.

2. Referencing that location in your shorter script provided directly

FYI, below are the commands i used for the advanced revision of this lab:

gcloud projects create lab81a

gcloud config set project lab81a

gcloud alpha billing projects link (google the remaining bits pls)

gsutil mb -c standard -l us-east1 gs://lab81-adv-bucket

gcloud compute instances create lab81-ce1 --machine-type=f1-micro --preemptible

--zone us-east1-c

--metadata=startup-script-url=gs://lab81-adv-bucket/worker-startup-script.sh,lab-logs-bucket=gs://lab81-adv-bucket/

--scopes=default,storage-rw

Celeste Wilson

Thank you!

Simplifying it, you can do like below:

#: create the new project  
gcloud projects create new-project005 --enable-cloud-apis  

#: list billing accounts

gcloud alpha billing accounts list  

#: link the project and the billing account

gcloud alpha billing projects link new-project005 --billing-account XXXXXXXXXXX  

#: set the current session project's

gcloud config set project new-project005
#: set the default region and the default zone on the cloud shell  
gcloud config set compute/region us-west2  
gcloud config set compute/zone us-west2-b

To create an instance loading up a startup script, it a script at the home of your user on Cloud Shell instance and execute a command like the one below:

#: create the instance with the user-data script, the metadata for the bucket and the scopes for monitoring, storage and logging  
gcloud compute instances create myvm01 --metadata lab-logs-bucket=gs://new-project005/ --metadata-from-file startup-script=script.sh --scopes storage-rw,monitoring-write,logging-write --machine-type f1-micro --deletion-protection

I hope it helps.

-Bianchi

Celeste Wilson

Thanks this is perfect!

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?