Вот несколько способов реализации планов облачного хостинга с примерами кода:
- Веб-службы Amazon (AWS) EC2:
import boto3
# Create an EC2 instance
ec2_client = boto3.client('ec2')
response = ec2_client.run_instances(
ImageId='ami-12345678',
InstanceType='t2.micro',
KeyName='my-key-pair',
MinCount=1,
MaxCount=1
)
print(response)
- Вычислительная система Google Cloud Platform (GCP):
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
# Create a Compute Engine instance
credentials = GoogleCredentials.get_application_default()
compute = discovery.build('compute', 'v1', credentials=credentials)
project = 'my-project'
zone = 'us-central1-a'
instance_name = 'my-instance'
config = {
'name': instance_name,
'machineType': f"zones/{zone}/machineTypes/n1-standard-1",
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': 'projects/debian-cloud/global/images/debian-9-stretch-v20200902',
}
}
],
'networkInterfaces': [
{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}
]
}
response = compute.instances().insert(project=project, zone=zone, body=config).execute()
print(response)
- Виртуальные машины Microsoft Azure:
from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.compute.models import DiskCreateOption
# Create a Virtual Machine
credential = DefaultAzureCredential()
subscription_id = 'your-subscription-id'
resource_group_name = 'your-resource-group'
location = 'eastus'
vm_name = 'your-vm'
network_name = 'your-virtual-network'
subnet_name = 'your-subnet'
image_id = '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Compute/images/{2}'.format(subscription_id, resource_group_name, 'your-image-name')
vm_size = 'Standard_DS1_v2'
compute_client = ComputeManagementClient(credential, subscription_id)
network_interface = compute_client.network_interfaces.create_or_update(
resource_group_name,
'your-network-interface',
{
'location': location,
'ip_configurations': [{
'name': 'your-ip-config',
'subnet': {'id': '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/virtualNetworks/{2}/subnets/{3}'.format(subscription_id, resource_group_name, network_name, subnet_name)},
'public_ip_address': {'id': '/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/publicIPAddresses/{2}'.format(subscription_id, resource_group_name, 'your-public-ip')}
}]
}
)
response = compute_client.virtual_machines.begin_create_or_update(
resource_group_name,
vm_name,
{
'location': location,
'hardware_profile': {
'vm_size': vm_size
},
'storage_profile': {
'image_reference': {
'id': image_id
}
},
'network_profile': {
'network_interfaces': [{
'id': network_interface.id
}]
},
'os_profile': {
'computer_name': vm_name,
'admin_username': 'your-username',
'admin_password': 'your-password'
}
}
)
print(response.result())
Эти примеры демонстрируют, как создавать экземпляры на популярных облачных платформах, таких как AWS, GCP и Azure, с помощью Python. Не забудьте заменить заполнители фактическими данными вашей конфигурации.