Skip to content

Instantly share code, notes, and snippets.

@benbonnet
Last active December 3, 2017 11:42
Show Gist options
  • Save benbonnet/04ce65fafaad247997f1e8b2f3690be2 to your computer and use it in GitHub Desktop.
Save benbonnet/04ce65fafaad247997f1e8b2f3690be2 to your computer and use it in GitHub Desktop.
create a gcloud compute instance with an init script

if not done already, type :

$ gcloud init

Assuming you already have ssh keys on your local workstation, create a gcloud specific one by appending your_username: to the file. In the end it will have the following format (we'll name the file gcloud_id_rsa.pub) :

your_username : ssh-rsa kfjdlsfjsdjklfghjkghkflkdjetc...

Then send it to gcloud :

gcloud compute project-info add-metadata --metadata-from-file sshKeys=/your/.ssh/path/gcloud_id_rsa.pub

You can then verify it has been added :

gcloud compute project-info describe

Then create your instance with the following :

gcloud compute instances create you_choose_you_instance_name --metadata-from-file startup-script=/path/to/init.sh

You can also add a specific key to a specific instance, with the following command :

gcloud compute instances add-metadata [INSTANCE_NAME] --metadata-from-file ssh-keys=[KEY_FILE_NAME].pub

#! /bin/bash
apt-get update
apt-get upgrade
apt-get install -y nginx
cat <<EOF > /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
server {
listen 80;
server_name default_server;
root /var/www/html;
index index.html;
}
}
EOF
cat <<EOF > /var/www/html/index.html
<html><body><h1>Hello World</h1>
<p>This page was created from a simple startup script!</p>
</body></html>
EOF
/etc/inid.d/nginx reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment