You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The launching works in web console. However, when executing the codes generated by "View Open API" in "Preview" step, it gives error:
Fail. Business error. Code: OperationDenied.InvalidNetworkType, Message: The specified network type is not available in the specified region.
I guess it is the VPC / default network type / default vSwitch causes the problem. Could you give a hint on how to fill in the NetworkType fields?
The executed code is attached as follows,
#!/usr/bin/env python# coding=utf-8importjsonimporttimeimporttracebackfromaliyunsdkcore.clientimportAcsClientfromaliyunsdkcore.acs_exception.exceptionsimportClientException, ServerExceptionfromaliyunsdkecs.request.v20140526.RunInstancesRequestimportRunInstancesRequestfromaliyunsdkecs.request.v20140526.DescribeInstancesRequestimportDescribeInstancesRequestRUNNING_STATUS='Running'CHECK_INTERVAL=3CHECK_TIMEOUT=180classAliyunRunInstancesExample(object):
def__init__(self):
self.access_id='<AccessKey>'self.access_secret='<AccessSecret>'# Whether to send a pre-check only request. If this is set to true, a pre-check only request will be sent, no instance will be created, and no fees will be generated. If this is set to false, a normal request will be sent. For normal requests, after the pre-check is passed, an instance will be created and fees will be generated.self.dry_run=False# The region IDself.region_id='cn-hangzhou'# The instance typeself.instance_type='ecs.t5-lc2m1.nano'# The billing method of the instanceself.instance_charge_type='PostPaid'# The image IDself.image_id='ubuntu_22_04_x64_20G_alibase_20221228.vhd'# The security group to which the instances belongsself.security_group_id='sg-bp1gsay3u7********'# The period of the subscriptionself.period=1# The unit of the subscription periodself.period_unit='Hourly'# The zone IDself.zone_id='random'# The billing method of the network bandwidthself.internet_charge_type='PayByTraffic'# The instance nameself.instance_name='worker[0,2]'# The password of the instanceself.password='Sensitive information. The actual value has been masked.'# The number of instances you want to createself.amount=1# The maximum outbound bandwidth to the Internetself.internet_max_bandwidth_out=5# The hostname of the instanceself.host_name='worker[0,2]'# Whether to add a sequential suffix to the instance name and hostnameself.unique_suffix=True# Whether the instance is I/O-optimizedself.io_optimized='optimized'# pay-as-you-goself.spot_strategy='SpotAsPriceGo'# Whether to enable security hardeningself.security_enhancement_strategy='Active'# The size of the system diskself.system_disk_size='20'# The type of the system diskself.system_disk_category='cloud_efficiency'self.client=AcsClient(self.access_id, self.access_secret, self.region_id)
defrun(self):
try:
ids=self.run_instances()
self._check_instances_status(ids)
exceptClientExceptionase:
print('Fail. Something with your connection with Aliyun go incorrect.'' Code: {code}, Message: {msg}'
.format(code=e.error_code, msg=e.message))
exceptServerExceptionase:
print('Fail. Business error.'' Code: {code}, Message: {msg}'
.format(code=e.error_code, msg=e.message))
exceptException:
print('Unhandled error')
print(traceback.format_exc())
defrun_instances(self):
""" Calling the instance creation API, and querying the instance status after the instance ID is retrieved. :return:instance_ids The ID of the instance that needs to be checked """request=RunInstancesRequest()
request.set_DryRun(self.dry_run)
request.set_InstanceType(self.instance_type)
request.set_InstanceChargeType(self.instance_charge_type)
request.set_ImageId(self.image_id)
request.set_SecurityGroupId(self.security_group_id)
request.set_Period(self.period)
request.set_PeriodUnit(self.period_unit)
request.set_ZoneId(self.zone_id)
request.set_InternetChargeType(self.internet_charge_type)
request.set_InstanceName(self.instance_name)
request.set_Password(self.password)
request.set_Amount(self.amount)
request.set_InternetMaxBandwidthOut(self.internet_max_bandwidth_out)
request.set_HostName(self.host_name)
request.set_UniqueSuffix(self.unique_suffix)
request.set_IoOptimized(self.io_optimized)
request.set_SpotStrategy(self.spot_strategy)
request.set_SecurityEnhancementStrategy(self.security_enhancement_strategy)
request.set_SystemDiskSize(self.system_disk_size)
request.set_SystemDiskCategory(self.system_disk_category)
body=self.client.do_action_with_exception(request)
data=json.loads(body)
instance_ids=data['InstanceIdSets']['InstanceIdSet']
print('Success. Instance creation succeed. InstanceIds: {}'.format(', '.join(instance_ids)))
returninstance_idsdef_check_instances_status(self, instance_ids):
""" Checking the instance status every 3 seconds within 3 minutes :param instance_ids The ID of the instance that needs to be checked :return: """start=time.time()
whileTrue:
request=DescribeInstancesRequest()
request.set_InstanceIds(json.dumps(instance_ids))
body=self.client.do_action_with_exception(request)
data=json.loads(body)
forinstanceindata['Instances']['Instance']:
ifRUNNING_STATUSininstance['Status']:
instance_ids.remove(instance['InstanceId'])
print('Instance boot successfully: {}'.format(instance['InstanceId']))
ifnotinstance_ids:
print('Instances all boot successfully')
breakiftime.time() -start>CHECK_TIMEOUT:
print('Instances boot failed within {timeout}s: {ids}'
.format(timeout=CHECK_TIMEOUT, ids=', '.join(instance_ids)))
breaktime.sleep(CHECK_INTERVAL)
if__name__=='__main__':
AliyunRunInstancesExample().run()
The text was updated successfully, but these errors were encountered:
qzweng
changed the title
InvalidNetworkType when executing codes generated from "Preview -> View Open API"
InvalidNetworkType when executing codes generated from "Preview -> View Open API"
Feb 27, 2023
RunInstancesRequest()
The launching works in web console. However, when executing the codes generated by "View Open API" in "Preview" step, it gives error:
Fail. Business error. Code: OperationDenied.InvalidNetworkType, Message: The specified network type is not available in the specified region.
I guess it is the VPC / default network type / default vSwitch causes the problem. Could you give a hint on how to fill in the
NetworkType
fields?The executed code is attached as follows,
The text was updated successfully, but these errors were encountered: