• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

metadata

GCP Compute Startup Script

February 12, 2022

How to add startup and shutdown scripts on GCP Compute Engine.

Startup Script

gcloud compute instances add-metadata servername \
--project project-id \
--zone us-central1-c \
--metadata=startup-script='#! /bin/bash
sudo -i
echo "Time: $(date)" >> /tmp/date.txt'

gcloud compute instances add-metadata servername \ --project project-id \ --zone us-central1-c \ --metadata=startup-script='#! /bin/bash sudo -i echo "Time: $(date)" >> /tmp/date.txt'

Shutdown Script

gcloud compute instances add-metadata servername \
--project project-id \
--zone us-central1-c \
--metadata=shutdown-script='#! /bin/bash
# Shuts down Apache server
/etc/init.d/apache2 stop'

gcloud compute instances add-metadata servername \ --project project-id \ --zone us-central1-c \ --metadata=shutdown-script='#! /bin/bash # Shuts down Apache server /etc/init.d/apache2 stop'

Filed Under: Cloud Tagged With: compute, gcloud, metadata, script, startup

GCP Compute add startup-script to metadata

February 2, 2022

How to add a startup-script to metadata. The script executes every time VM is started.

gcloud compute instances add-metadata instance-name \
--project your-project-id \
--zone us-central1-c \
--metadata=startup-script='#! /bin/bash
sudo -i
echo "Time: $(date)" >> /tmp/date.txt'

gcloud compute instances add-metadata instance-name \ --project your-project-id \ --zone us-central1-c \ --metadata=startup-script='#! /bin/bash sudo -i echo "Time: $(date)" >> /tmp/date.txt'

Filed Under: Cloud Tagged With: add, boot, compute, gcp, metadata, startup-script

Metadata URL

January 15, 2022

Here’s the metadata URLs for both AWS and GCP.

curl http://169.254.169.254/computeMetadata/v1/ -H "Metadata-Flavor: Google"
curl http://169.254.169.254/latest/meta-data/

curl http://169.254.169.254/computeMetadata/v1/ -H "Metadata-Flavor: Google" curl http://169.254.169.254/latest/meta-data/

Filed Under: Cloud Tagged With: aws, gcp, metadata, url

GCP SSH Issues Return Code [1]

July 16, 2021

If you are getting this return code [1] error, check if enable-oslogin metadata is set to TRUE in your VM.

Here’s the error when logging in.

gcloud compute ssh your-server-name \
--zone us-central1-a \
--project your-project-id \
--internal-ip &
ERROR: (gcloud.compute.ssh) [C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\sdk\plink.exe] exited with return code [1].

gcloud compute ssh your-server-name \ --zone us-central1-a \ --project your-project-id \ --internal-ip & ERROR: (gcloud.compute.ssh) [C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\sdk\plink.exe] exited with return code [1].

Remove metadata.

gcloud compute instances remove-metadata your-server-name \
--keys=enable-oslogin \
--zone us-central1-a \
--project your-project-id
Updated [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-central1-a/instances/your-server-name].

gcloud compute instances remove-metadata your-server-name \ --keys=enable-oslogin \ --zone us-central1-a \ --project your-project-id Updated [https://www.googleapis.com/compute/v1/projects/your-project-id/zones/us-central1-a/instances/your-server-name].

Run gcloud ssh again.

gcloud compute ssh your-server-name \
--zone us-central1-a \
--project your-project-id \
--internal-ip &
[1] 1257

gcloud compute ssh your-server-name \ --zone us-central1-a \ --project your-project-id \ --internal-ip & [1] 1257

This time it opens up a Putty session and you’re able to login.

Filed Under: Cloud Tagged With: enable-oslogin, gcp, metadata, ssh

GCP Update Instance Metadata

June 13, 2021

How to update an instance metadata in GCP.

gcloud compute instances add-metadata instance-name \
--metadata key-name=value \
--project your-project-id \
--zone us-central1-a

gcloud compute instances add-metadata instance-name \ --metadata key-name=value \ --project your-project-id \ --zone us-central1-a

In this example, we are adding enable-oslogin=TRUE.

gcloud compute instances add-metadata instance-name \
--metadata enable-oslogin=TRUE \
--project your-project-id \
--zone us-central1-a

gcloud compute instances add-metadata instance-name \ --metadata enable-oslogin=TRUE \ --project your-project-id \ --zone us-central1-a

Filed Under: Cloud Tagged With: compute, create, gcp, instances, metadata

GCP Web Server

January 8, 2020

Here’s a quick script to stand up a web server based on Centos image.

gcloud compute instances create [VM-NAME] \
    --zone=[ZONE] \
    --image-family=debian-9 \
    --image-project=debian-cloud \
    --tags=allow-ssh,allow-health-check \
    --subnet=lb-subnet \
    --metadata=startup-script='#! /bin/bash
apt-get update
apt-get install apache2 -y
a2ensite default-ssl
a2enmod ssl
vm_hostname="$(curl -H "Metadata-Flavor:Google" \
http://169.254.169.254/computeMetadata/v1/instance/name)"
echo "Page served from: $vm_hostname" | \
tee /var/www/html/index.html
systemctl restart apache2'

gcloud compute instances create [VM-NAME] \ --zone=[ZONE] \ --image-family=debian-9 \ --image-project=debian-cloud \ --tags=allow-ssh,allow-health-check \ --subnet=lb-subnet \ --metadata=startup-script='#! /bin/bash apt-get update apt-get install apache2 -y a2ensite default-ssl a2enmod ssl vm_hostname="$(curl -H "Metadata-Flavor:Google" \ http://169.254.169.254/computeMetadata/v1/instance/name)" echo "Page served from: $vm_hostname" | \ tee /var/www/html/index.html systemctl restart apache2'

Filed Under: Cloud Tagged With: apache, debian, gcp, metadata, server, vm, web

Display Instance ID & IP Address

July 7, 2019

How to display IP address and Instance ID on web page behind a AWS load balancer.

Add these 2 commands in crontab. The job runs every 5 mins.

*/5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/instance-id > /var/www/html/id.txt
*/5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4 > /var/www/html/ip.txt

*/5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/instance-id > /var/www/html/id.txt */5 * * * * /usr/bin/curl http://169.254.169.254/latest/meta-data/public-ipv4 > /var/www/html/ip.txt

You can view on the site.

# your domain
http://yourdomain.com/ip.txt
http://yourdomain.com/id.txt
# your server ip address
http://1.1.1.1/ip.txt
http://1.1.1.1/id.txt

# your domain http://yourdomain.com/ip.txt http://yourdomain.com/id.txt # your server ip address http://1.1.1.1/ip.txt http://1.1.1.1/id.txt

Filed Under: Cloud Tagged With: aws, crontab, ec2, instance-id, ip address, metadata

  • Home
  • About
  • Archives

Copyright © 2023