0

我一直在尝试使用 Fabric 和 Boto 来自动化创建 EC2 实例、创建 AMI 和安装依赖项、创建 s3 存储桶的任务。但是我遇到了一些错误。具体来说,我收到以下错误:

AttributeError: 'NoneType' object has no attribute 'region'

提前感谢您的所有帮助。如果有必要,我可以分享我的配置文件。

这是我拥有的代码:

@task
def create_ami(aws_regions = 'us-east-1e', ami_type = 't1.micro', ami_name = 'test',        ami_description = 'testing', 
           package_name = 'db', root_device='/dev/sda1', root_device_size=25):

"""Creates an EBS backed AMI in one or more AWS regions. 

:param aws_regions: Comma delimited list of AWS regions where the AMI is saved. 
:param ami_type: The type of AMI to create. This determines how the AMI is configured.
:param ami_name: The name of the AMI. AMI names are unique within a region. 
:param ami_description: Description of the AMI. 
:param root_device: The device mapping for the AMI's root volume. Defaults to /dev/sda1.
:param root_device_size: The size of the root EBS volume in GB. 
"""

if ',' in aws_regions:
    aws_region_list = aws_regions.split(',')
else:
    aws_region_list = [aws_regions]

for aws_region in aws_region_list:
    if aws_region not in config.AWS_REGIONS:
        print('Unknown region {0}'.format(aws_region))
        continue
    if ami_type not in config.AMI_TYPES:
        raise ValueError('Unknown AMI Type {0}'.format(ami_type))

    ec2_connection = _get_ec2_connection(aws_region)

    print('Connected to {0}'.format(ec2_connection))

    ami_id = config.AMI_ID_BY_REGION[ec2_connection.region.name]

    root_ebs_device_mapping = _get_block_device_mapping(root_device, root_device_size)

    reservation = ec2_connection.run_instances(ami_id, key_name=config.AWS_KEY_FILE,
        security_group_ids=[config.AMI_SECURITY_GROUP], block_device_map=root_ebs_device_mapping)

    template_instance = reservation.instances[0]

    print('spinning up the instance')
    sleep(10)

    template_instance.update()

    while template_instance.state != 'running':
        sleep(10)
        template_instance.update()

    print('configuring instance {0}'.format(template_instance.id))

    with settings(host_string=template_instance.public_dns_name, 
                  key_filename=config.LOCAL_AWS_KEY_FILE,
                  user=config.AWS_USER_NAME, 
                  connection_attempts=10):

        _configure_packages(package_name)

    # create the AMI based off of our instance
    print('Creating AMI {0}'.format(ami_name))
    new_ami_id = ec2_connection.create_image(template_instance.id, ami_name, ami_description)

    print('Creating new AMI for {0}. AMIID: {1}'.format(ami_name, new_ami_id))
    new_ami = ec2_connection.get_all_images([new_ami_id])[0]
    sleep(20)

    while (new_ami.state == 'pending'):
        new_ami.update()
        sleep(20)

    print('AMI Created')

    ec2_connection.create_tags([new_ami.id], {'Name': ami_name})

    # clean up
    print('Terminating instance')

    while template_instance.state != 'running':
        template_instance.update()
        sleep(20)

    ec2_connection.terminate_instances([template_instance.id])
4

0 回答 0