python-2.7 - 更新 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])
解决方案
导入 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)
推荐阅读
- javascript - 具有反应状态的object.js(无法读取未定义的属性'map')
- html - Vue v-for 指令不呈现正确的 html
- cassandra - 如果在 Apache Cassandra 中,数据中心和机架不直接与物理机架或数据中心相关联,那么如何处理容错?
- laravel - Laravel 类型提示,模型保存时 id 返回 null
- arrays - C:减去数组
- hibernate - 在 Hibernete 多租户环境中,persist() 后无法获取实体 ID
- python - Pandas:将一行字符串分成 4 列;问题:列由逗号、制表符和空格分隔
- ios - Flutter iOS:无法检测到连接的iOS模拟器
- java - 尝试使用 OncePerRequestFilter 时出现 java.lang.NoSuchMethodException
- verilog - 我的 verilog 代码出现此错误,“Illegal operation for constant expression”