简体   繁体   中英

Trying to deploy ec2 instance with Apache libcloud outside of default VPC

My organization creates our own VPC's and does not leverage the default one provided by AWS. I am trying to use apache libcloud to deploy instances within non-default VPC's. However, libcloud complains that no VPC Id was specified. I can't seem to find where you are supposed to specify this parameter. I looked in the code, asked on IRC, and combed through the documentation. I tried providing a "location" parameter using an AZ but that doesn't seemto take either. Has anyone come across this or dealt with it?

Ok, looks like you have to specify the subnet id you want to deploy the instance to if you do not have a default VPC.

Here is my example for anyone else that needs assistance with this.

from libcloud.compute.providers import get_driver
from libcloud.compute.base import NodeImage
import os

ACCESS_ID = os.getenv('AWS_ACCESS_KEY_ID')
SECRET_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')
AMI_ID = os.getenv('AMI_ID')
SIZE_ID = os.getenv('SIZE_ID', 't2.micro')
REGION = os.getenv('REGION','us-east1')
KEYNAME = os.getenv('KEYNAME')
SUBNET = os.getenv('SUBNET')

cls = get_driver(Provider.EC2)

driver = cls(ACCESS_ID, SECRET_KEY, region=REGION)

subnet = [ s for s in driver.ex_list_subnets() if s.id == SUBNET][0]
size = [ s for s in driver.list_sizes() if s.id == SIZE_ID][0]
image = NodeImage(id=AMI_ID, name=None, driver=driver)

node = driver.create_node(name='group-test', image=image, size=size, ex_subnet=subnet, ex_keyname=keyname)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM