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