当我运行我的代码时,我得到了这个,我创建了一个带有两个子网的VPC,并且在2个实例中但在同一个子网内。当我尝试将子网发送到实例时出现我的问题我收到此错误,并且您可以在第一个屏幕上看到,使子网的名称和每个显示的ID分配ID
================================================================
============== creating the VPC ====================================
================================================================
Creating VPC with name: SAS1
================================================================
============== creating the IG ======================================
================================================================
Creating Internet Gateway with name: SAS1
================================================================
============== creating the Route ===================================
================================================================
Creating Route Table with name: public
================================================================
============== creating the subnets =================================
================================================================
Creating Subnet with name: sas01 in: us-west-2a
Subnet created with the name : sub-sas1 and the ID is : Subnet:subnet-2e65af4a
Creating Subnet with name: sas02 in: us-west-2a
Subnet created with the name : sub-sas2 and the ID is : Subnet:subnet-2f65af4b
Subnet:subnet-2f65af4b
Subnet:subnet-2e65af4a
================================================================
============== creating the hosts ===================================
================================================================
Creating Security Group with name: sas-sg
Creating Instance with name: sas01
Traceback (most recent call last):
File "/Users/Guille/Documents/git/aws_vpc_py/venv/lib/python2.7/site-packages/fabric/main.py", line 732, in main
*args, **kwargs
File "/Users/Guille/Documents/git/aws_vpc_py/venv/lib/python2.7/site-packages/fabric/tasks.py", line 345, in execute
results['<local-only>'] = task.run(*args, **new_kwargs)
File "/Users/Guille/Documents/git/aws_vpc_py/venv/lib/python2.7/site-packages/fabric/tasks.py", line 121, in run
return self.wrapped(*args, **kwargs)
File "/Users/Guille/Documents/git/aws_vpc_py/fabfile.py", line 25, in make_vpc
bastion_hosts = aws.make_vpc(vpc_name)
File "/Users/Guille/Documents/git/aws_vpc_py/aws.py", line 71, in make_vpc
bastion = get_or_create_bastion_host(conn, vpc_config, bastion_host_name, vpc, Subnet[0])
File "/Users/Guille/Documents/git/aws_vpc_py/aws.py", line 127, in get_or_create_bastion_host
key_name=key_pair.name, subnet= Subnet[0])
NameError: global name 'Subnet' is not defined
这是我运行的代码的一部分:
bastion_hosts = []
for instancia in vpc_config.sections():
if (instancia != 'vpc' ) and (instancia != 'sub-sas1' ) and ( instancia != 'sub-sas2'):
cidr_block = vpc_config.get(instancia, 'cidr_block')
bastion_host_name = vpc_config.get(instancia, 'bastion_host')
instance_type = vpc_config.get(instancia,'default_instance_type')
availability_zone = vpc_config.get(instancia, 'availability_zone')
bastion = get_or_create_bastion_host(conn, vpc_config, bastion_host_name, vpc, Subnet[0])
bastion_hosts.append(bastion)
print bastion_hosts
return bastion_hosts
其中堡垒由:
定义def get_or_create_bastion_host(conn, vpc_config, bastion_host_name, vpc, subnet):
image_id = vpc_config.get('vpc', 'default_image_id')
image_login_user = vpc_config.get('vpc', 'default_image_login_user')
instance_type = vpc_config.get('vpc','default_instance_type')
key_pair = get_bastion_host_key(conn, vpc_config)
security_group = get_or_create_vpc_security_group(conn, vpc_config, vpc.id)
for reservation in fetch_running_reservations(conn, bastion_host_name, vpc.id):
for instance in reservation.instances:
public_ip = associate_elastic_ip(conn, instance)
return Node(bastion_host_name, public_ip, image_login_user, BASTION_KEY_FILE)
print 'Creating Instance with name:', bastion_host_name
reservation = conn.ec2.run_instances(image_id, instance_type=instance_type,
security_group_ids=[security_group.id],
key_name=key_pair.name, subnet= Subnet[0])
for instance in reservation.instances:
print 'Waiting for', bastion_host_name, instance.id, 'to start ...'
wait_until(instance, 'running')
tag_with_name(instance, bastion_host_name)
public_ip = associate_elastic_ip(conn, instance)
print bastion_host_name, 'is associated with Elastic IP', public_ip
return Node(bastion_host_name, public_ip, image_login_user, BASTION_KEY_FILE)
任何帮助??
答案 0 :(得分:0)
当范围中没有定义此类变量时,您正尝试使用Subnet[0]
。由于您将subnet
传递给get_or_create_bastion_host
,因此您应使用subnet
代替Subnet[0]
。
get_or_create_bastion_host(conn,vpc_config,bastion_host_name,vpc,子网)
要使其有效,请尝试将传递的subnet
分配给参数subnet
:
reservation = conn.ec2.run_instances(image_id, instance_type=instance_type,
security_group_ids=[security_group.id],
key_name=key_pair.name, subnet_id=subnet)