-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_VM_instance_Master_Node.py
71 lines (64 loc) · 1.99 KB
/
create_VM_instance_Master_Node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import googleapiclient.discovery
import google.auth
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file('/home/saksham/Downloads/industrial-problem-378404-685329678653.json')
# Set the name, project, and zone for the instance
name = "master-instance"
project = "industrial-problem-378404"
zone = "us-central1-a"
# Authenticate with Google Cloud
service = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
# Get the latest Ubuntu 22.04 LTS image
image_response = service.images().getFromFamily(
project='ubuntu-os-cloud', family='ubuntu-2204-lts').execute()
source_disk_image = image_response['selfLink']
# Set the machine type and startup script
machine_type = f"zones/{zone}/machineTypes/e2-medium"
startup_script = open(os.path.join(
os.path.dirname(__file__), 'startup_instance.sh'), 'r').read()
# Set the configuration for the instance
config = {
'name': name,
'machineType': machine_type,
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
}
}
],
'networkInterfaces': [
{
'network': 'global/networks/default',
'accessConfigs': [
{
'type': 'ONE_TO_ONE_NAT',
'name': 'External NAT'
}
]
}
],
'serviceAccounts': [
{
'email': 'default',
'scopes': [
'https://www.googleapis.com/auth/devstorage.read_write',
'https://www.googleapis.com/auth/logging.write'
]
}
],
'metadata': {
'items': [
{
'key': 'startup-script',
'value': startup_script
}
]
}
}
# Create the instance
request = service.instances().insert(project=project, zone=zone, body=config)
response = request.execute()