首页 > 解决方案 > 更新 Lambda 脚本以更改实例大小

问题描述

我想更新此脚本以停止此实例、进行备份、更改大小然后重新启动实例。

基本上我正在尝试将三个脚本组合在一起。所有人都说它们是用 Python 2.7 编写的,但语法不匹配。

请参阅下面的脚本。我真的可以混合这些脚本吗?第一个脚本看起来我会做这样的事情:

     def lambda_handler(event, context):
     ec2 = boto3.client('ec2', region_name=region)
     ec2.change_instances(InstanceIds=instances)
     print 'started your instances: ' + str(instances)

但我什至不确定这是否是正确的语法。在第二个脚本中说:

    client.modify_instance_attribute(InstanceId=my_instance, 
    Attribute='instanceType', Value='m3.xlarge')

显然我会更改变量,但不确定是否需要 def lamba_handler 语法。

你能指出我正确的方向吗?

脚本 1
    import boto3
    region = 'us-east-1'
    instances = ['XXXXXXXXXXXXXXXXX']

    def lambda_handler(event, context):
        ec2 = boto3.client('ec2', region_name=region)
        ec2.stop_instances(InstanceIds=instances)
        print 'started your instances: ' + str(instances)

    def lambda_handler(event, context):
        ec2 = boto3.client('ec2', region_name=region)
        ec2.start_instances(InstanceIds=instances)
        print 'started your instances: ' + str(instances)
脚本 2
    client = boto3.client('ec2')

    # Insert your Instance ID here
    region = 'us-east-1'
    instances = ['XXXXXXXXXXXXXXXXX']

    # Stop the instance
    client.stop_instances(InstanceIds=[instances])
    waiter=client.get_waiter('instance_stopped')
    waiter.wait(InstanceIds=[my_instance])

    # Change the instance type
    client.modify_instance_attribute(InstanceId=my_instance, 
    Attribute='instanceType', Value='m3.xlarge')

    # Start the instance
    client.start_instances(InstanceIds=[my_instance])

标签: python-2.7aws-lambdaamazon-iam

解决方案


导入 boto3 区域 = 'us-east-1' 实例 = ['XXXXXXXXXXXXXXXXX']

def lambda_handler(event, context):
    ec2 = boto3.client('ec2', region_name=region)
    ec2.stop_instances(InstanceIds=instances)
    print 'started your instances: ' + str(instances)
    waiter=ec2.get_waiter('instance_stopped')
    waiter.wait(InstanceIds=[my_instance])

# Change the instance type
    ec2.modify_instance_attribute(InstanceId=my_instance, 
Attribute='instanceType', Value='m3.xlarge')

    ec2.start_instances(InstanceIds=instances)
    print 'started your instances: ' + str(instances)

推荐阅读