I am a new user with limited knowledge in scripting and overall Google cloud but I gave a try to do the ECS and ECE challenge lab using command lines (gcloud shell). I used the following commands and after a lot of trail and error – this looks to be working. Now, my question is:-
a) Can you please check and confirm if there is any better/ simpler steps that I could have used.
b) Is there a way that I can create an executable bash file which I could run with all the steps in order to automate this.
c) If yes – do you think – we can call one file (worker-startup-script.sh) from another .sh file (the executable file that we can creates) and how?
My commands were as follows:-
List the project available
gcloud projects list
Create a new project
gcloud projects create rudra-challenge-lab-project
Set to/Go to new project
gcloud config set project rudra-challenge-lan-project
Find available billing accounts
gcloud alpha billing accounts list
(Note down the billing account ID)
Link the project – rudra-challenge-lab-project to the billing ID
gcloud alpha billing account projects link rudra-challenge-lab-project –billing-account=<ACCOUNT ID/PROJECT ID>
gcloud beta billing projects link rudra-challenge-lab-project –billing-account=< ACCOUNT ID/PROJECT ID >
create a new bucket in this project called “rudra-challenge-lab-bucket”
gsutil mb -l asia-south1 gs://rudra-challenge-lab-bucket
enable the google api services
glcloud services enable cloudapis.googleapis.com
Create VM in us-west1-a zone with machine type as f1-micro with scope of full access to all api’s for default service account and then add meta data for startup script worker-startup-script.sh and add metadata for metadata lab-logs-bucket to be the newly created bucket which is =gs://rudra-challenge-lab-bucket
gcloud compute instances create rudra-challenge-lab-vm –zone us-west1-a –machine-type=f1-micro –scopes "https://www.googleapis.com/auth/cloud-platform" –service-account=default –metadata-from-file startup-script=worker-startup-script.sh –metadata lab-logs-bucket=gs://rudra-challenge-lab-bucket
Your solution is pretty similar to what I came up with when I was doing this exercise myself. And you’re right, it’s a challenging thing to do, but hopefully, you’ve learned a lot in the process. Nicely done!
One minor thing you could consider is how to set up the Service Account to use the Least Privilege required, as opposed to the Full API access. One of the upcoming chapters covers this in more detail if you’re interested. You can also use variables to make it easier to track some things, like the Project ID, Bucket Name, etc. But both of those are more "Something extra" rather than "something needed"
In terms of the points about scripting, I have some advice. None of this is required for passing the GCP ACE Exam as far as I know, but it’s interesting all the same
You can add all of your commands effectively into a file, with a simple header and it becomes a script. When you run the script, those commands are then executed sequentially. Put your commands into a single file, and name it whatever you choose; like challenge.sh
#!/bin/bash gcloud projects list gcloud projects create rudra-challenge-lab-project gcloud config set project rudra-challenge-lab-project ...
Once you’ve done that, you just need to mark the script as executable, which will then be able to run. This is done with the chmod command
chmod +x challenge.sh
Then you can run the script, and it will execute all of your commands.
Thanks very much for posting this. I was stuck on the billing account part. I forgot about the alpha and beta version of the gcloud command.
i have a question regarding startup scripts. Why do we need it in order to create a vm instance. I mean what is the benefit of running scripts?
this is my gcloud commands in order. Assumptions are that
1. you have set up a billing account with your admin account like explained at the begining of the course
2. you are logged in with your user account
3. you do not have a project
4. a maximum of 3 projects can be linked to a trial billing account. If you have setup a billing project for the bigquery export, then make sure that your user account have a single project at most currently linked to your
trial_billing_account so that you can create a new project for this lab
5. copy the startup script in your home folder, with name
my solution (not perfect)
gcloud projects create --set-as-default --name=sandbox # project sandbox-273609 created
gcloud beta billing accounts list # trial billing account has ID 02BFE6-CC7730-C245EF (has been changed)
gcloud beta billing projects link sandbox-273609 --billing-account=02BFE6-CC7730-C245EF
gsutil mb -l europe-west6 gs://my-sandbox-32id gcloud compute instances list # this enables the compute api
gcloud compute instances create --machine-type=f1-micro --zone=europe-west6-b --metadata-from-file=startup-script=acg_startup_script.sh --metadata=lab-logs-bucket=gs://my-sandbox-32id/ --scopes=https://www.googleapis.com/auth/devstorage.read_write my-sandbox-instance
gcloud compute instances tail-serial-port-output my-sandbox-instance --zone=europe-west6-b # follow logs
gsutil ls gsutil ls gs://my-sandbox-32id/ gcloud projects delete sandbox-273609