First steps i made in Google cloud automation
Google offers 1 year free trail (Up to 300$) for using Google Cloud Platform (GCP)
In this script i created a new virtual machine via python script
I used Ubuntu linux to run the script,
_
1. Enable Google compute API
go to – https://console.developers.google.com/apis/library/compute.googleapis.com
_
2. Create a new project
Click the small arrow at the top (near Google APIs) and choose ‘New Project’ and give the new project a name (Project1 in my case)
_
3. Create service account
this account will use to connect to the cloud via the API script, go to https://console.developers.google.com/iam-admin/serviceaccounts
and click ‘Create service account’
_
Set the new account name (in my case – ServiceAcc1)
_
Select the role of the new account (in my case – Owner)
Click ‘Continue’ , Click ‘Finish’
_
4. Create Key
Choose Action -> Create key and Choose ‘JSON’ key type and click ‘Create’
A new file will be download automatically , the file name will be somthing like – “project1-12345-2sfdgs9e35522.json”
_
5. Install Python Cloud library
SSH to the linux machine that will run the Python script and install apache-libcloud python library
1 2 |
sudo apt install python-pip sudo pip install apache-libcloud |
_
6. Copy the JSON file the the Linux machine
Via FTP/SCP or copy the file content and paste to a new file via VI,
For example (Using Copy/Paste):
vim project1-12345-2sfdgs9e35522.json
Paste the data from the file you downloaded
1 2 3 4 5 6 7 8 9 10 11 12 13 |
{ "type": "service_account", "project_id": "project1-XXXX", "private_key_id": "XXXXXXXXXXXXXXXXX", "private_key": "-----BEGIN PRIVATE KEY-----\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n-----END PRIVATE KEY-----\n", "client_email": "serviceacc1@project1-XXXX.iam.gserviceaccount.com", "client_id": "XXXXXXXXXXXXXXXXXX", "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://oauth2.googleapis.com/token", "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/serviceacc1%40project1-XXXX.iam.gserviceaccount.com" } |
Save the file and exit ( [ESC] & :wq )
_
7. Create the Python script
vim test.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver ComputeEngine = get_driver(Provider.GCE) driver = ComputeEngine('serviceacc1@project1-XXXX.iam.gserviceaccount.com', '/home/user1/project1-12345-2sfdgs9e35522.json', project='project1-XXXX') size = 'n1-standard-1' instance = 'debian-7' zone = 'us-central1-a' sa_scopes = [ { 'email': 'serviceacc1@project1-XXXX.iam.gserviceaccount.com', 'scopes': ['storage-ro'] } ] driver.create_node("node-web1", size, instance, zone, ex_service_accounts=sa_scopes) |
Update this parameters:
1 your service account email (You can found this inside the JSON file)
2 path to JSON file (The JSON file you downloaded)
3 your project id (You can found this inside the JSON file)
_
8. Run the script
Attention– This script will create a new VM, This can cost money $$$ in case you not eligible for free use
1 |
python test.py |
And after few seconds you have the new VM ‘node-web1’ running at Google Cloud, Don’t forget to delete it later 🙂
_
Summary
Google cloud automation is friendly and easy to use for python users
_
_