4 Answers
That’s awesome! I love it! Nicely done. 👏 And I love how you skipped so many script hassles by just pointing to the github-hosted one. 😀👍
What you’ve done gets full marks, but if you want a few bonus points, you could 1) further reduce the scopes to a minimal set and 2) change the github URL to a commit-hash one so that the script can’t be changed to do something different without you having the opportunity to review and approve it. Both of these are about putting least privilege into practice.
Thanks for posting about your progress! I hope you’re enjoying the course, so far.
Mattias
#! /bin/bash</span>
# Create a new project
echo "Please enter your project name"
read project
echo "#####Start create your project#####"
gcloud projects create $project
echo "#####Gratz, you have create a new project#####"
# Set to new project
gcloud config set project $project
sleep 2
# Enable Billing API services
echo "#####Enabling Billing API services#####"
gcloud services enable cloudbilling.googleapis.com
echo "#####Billing API enabled#####"
sleep 1
# Enable VM API services
echo "#####Enabling VM API services#####"
gcloud services enable compute.googleapis.com
echo "#####VM API enabled#####"
sleep 1
# Link to Billing accounts
echo "#####start link to billing account#####"
gcloud beta billing projects link $project
# --billing-account 0X0X0X-0X0X0X-0X0X0X
echo "#####Billing account linked#####"
# Create a new bucket
echo "#####Please enter your bucket name#####"
read bucket
gsutil mb -c multi_regional -l us gs://$bucket
echo "#####Bucket created#####"
sleep 1
# Create VM using existing startup script
echo "#####Start initialing VM ..."
gcloud compute instances create myhappyvm --zone=us-west2-b
--labels=challenge=lab
--machine-type=f1-micro
--scopes=default,storage-rw
--metadata=lab-logs-bucket=gs://$bucket/,startup-script-url=https://raw.githubusercontent.com/ACloudGuru/gcp-cloud-engineer/9c92b7f7158953f33aaaff295c62a91923db635f/compute-labs/worker-startup-script.sh
This is the script I write, change the billing account ID to your own one 🙂
Very nice! 😁👍
!/bin/bash
PROJECT=acloudg-lab4-xxxx
INSTANCE=gk-inst-6
BUCKET=challenge-bucket-5
gcloud projects create $PROJECT
gcloud alpha billing accounts projects link $PROJECT –account-id xxxxx-xxxxx-xxxxx
gcloud config set project $PROJECT
gsutil mb gs://$BUCKET
gcloud services enable compute.googleapis.com
gcloud –project $PROJECT compute instances create $INSTANCE –zone us-central1-a –metadata=startup-script-url=$SCRIPT,lab-logs-bucket=gs://$BUCKET/ –scopes https://www.googleapis.com/auth/cloud-platform
Thanks for sharing, Krishna! 👍 More comments on the other post you made: https://acloud.guru/forums/gcp-certified-associate-cloud-engineer/discussion/-LXBS7iAh9Mc_oOZtpzW/gcs-and-gce-challenge-lab?answer=-LXKpp4xcLW_viHzDSNw
project_name="MySecondChallenge"
project_id="acloudguru-challenge"
billing_account="01F454-74724A-063A51"
log_bucket_name="acloudguru-003"
vm_name="myvmchallenge"
gcloud projects create $project_id –name $project_name
gcloud config set project $project_id
gcloud config set compute/zone asia-east1-b
gcloud alpha billing projects link $project_id –billing-account=$billing_account
gcloud services enable compute.googleapis.com
## create bucket
gsutil mb gs://$log_bucket_name/
gsutil cp worker-startup-script.sh gs://$log_bucket_name/
## create VM
gcloud compute instances create $vm_name –metadata lab-logs-bucket=gs://$log_bucket_name/,startup-script-url=gs://$log_bucket_name/worker-startup-script.sh –scopes=default,storage-rw,compute-rw –service-account=default
## clean up resources
gcloud compute instances delete $vm_name –quiet
gsutil rm -r -f "gs://$log_bucket_name/*"
gsutil rb -f "gs://$log_bucket_name/"
gcloud projects delete $project_id
Glad to see you’ve considered the clean-up side of things, too! 😁👍 I presume you have a delay in your script or are manually running these line by line? Also, did you find this to be a valuable exercise? Did you get a good feel for the different commands and for how gcloud tends to work, in general?
Thank you Mattias for the encouragement. And I appreciate the comments bonus points. I will try it out.
Np! 🙂 And I look forward to hearing how you found that, too. 👍