迭代在 Lambda 中不起作用,因为我想在代码中列出的两个区域中使用 运行 lambda
Iteration not working in Lambda as I want to run lambda in two regions listed in code
您好,我有一个简单的 lambda 函数,它可以停止所有标记有 Auto_off 的 EC-2 实例。我设置了一个 for 循环,以便它适用于两个区域 us-east-1 和 us-east-2。我是运行us-east-2地区的函数
问题是只有位于 us-east2 的实例正在停止,而另一个实例没有(位于 us-east-1)。我可以做哪些修改。
请提出建议,因为我是 python 和 boto 库的新手
import boto3
import logging
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#define the connection
ec2 = boto3.resource('ec2')
client = boto3.client('ec2', region_name='us-east-1')
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
conn = boto3.resource('ec2',region_name=region)
def lambda_handler(event, context):
# Use the filter() method of the instances collection to retrieve
# all running EC2 instances.
filters = [{
'Name': 'tag:AutoOff',
'Values': ['True']
},
{
'Name': 'instance-state-name',
'Values': ['running']
}
]
#filter the instances
instances = ec2.instances.filter(Filters=filters)
#locate all running instances
RunningInstances = [instance.id for instance in instances]
#print the instances for logging purposes
#print RunningInstances
#make sure there are actually instances to shut down.
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop()
print shuttingDown
else:
print "Nothing to see here"
你在这里做的循环
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
conn = boto3.resource('ec2',region_name=region)
首先将us-east-1
赋值给conn
变量,然后在第二步用us-east-2
覆盖它,然后进入你的函数。
所以你可以做的是将循环放在你的函数中,并在该循环中执行函数的当前定义。
您正在创建 2 个 ec2 资源实例和 1 个 ec2 客户端实例。您只使用了一个 ec2 资源实例,根本没有使用客户端。您还将循环中的区域设置在与您实际使用的资源对象不同的资源对象上。
改变这一切:
ec2 = boto3.resource('ec2')
client = boto3.client('ec2', region_name='us-east-1')
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
conn = boto3.resource('ec2',region_name=region)
为此:
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
ec2 = boto3.resource('ec2',region_name=region)
此外,您问题中的代码中的缩进都是错误的。我希望这只是一个 copy/paste 问题,而不是您的代码真正缩进的方式,因为缩进是 Python.
中的语法
您好,我有一个简单的 lambda 函数,它可以停止所有标记有 Auto_off 的 EC-2 实例。我设置了一个 for 循环,以便它适用于两个区域 us-east-1 和 us-east-2。我是运行us-east-2地区的函数
问题是只有位于 us-east2 的实例正在停止,而另一个实例没有(位于 us-east-1)。我可以做哪些修改。
请提出建议,因为我是 python 和 boto 库的新手
import boto3
import logging
#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#define the connection
ec2 = boto3.resource('ec2')
client = boto3.client('ec2', region_name='us-east-1')
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
conn = boto3.resource('ec2',region_name=region)
def lambda_handler(event, context):
# Use the filter() method of the instances collection to retrieve
# all running EC2 instances.
filters = [{
'Name': 'tag:AutoOff',
'Values': ['True']
},
{
'Name': 'instance-state-name',
'Values': ['running']
}
]
#filter the instances
instances = ec2.instances.filter(Filters=filters)
#locate all running instances
RunningInstances = [instance.id for instance in instances]
#print the instances for logging purposes
#print RunningInstances
#make sure there are actually instances to shut down.
if len(RunningInstances) > 0:
#perform the shutdown
shuttingDown = ec2.instances.filter(InstanceIds=RunningInstances).stop()
print shuttingDown
else:
print "Nothing to see here"
你在这里做的循环
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
conn = boto3.resource('ec2',region_name=region)
首先将us-east-1
赋值给conn
变量,然后在第二步用us-east-2
覆盖它,然后进入你的函数。
所以你可以做的是将循环放在你的函数中,并在该循环中执行函数的当前定义。
您正在创建 2 个 ec2 资源实例和 1 个 ec2 客户端实例。您只使用了一个 ec2 资源实例,根本没有使用客户端。您还将循环中的区域设置在与您实际使用的资源对象不同的资源对象上。
改变这一切:
ec2 = boto3.resource('ec2')
client = boto3.client('ec2', region_name='us-east-1')
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
conn = boto3.resource('ec2',region_name=region)
为此:
ec2_regions = ['us-east-1','us-east-2']
for region in ec2_regions:
ec2 = boto3.resource('ec2',region_name=region)
此外,您问题中的代码中的缩进都是错误的。我希望这只是一个 copy/paste 问题,而不是您的代码真正缩进的方式,因为缩进是 Python.
中的语法