如何在所有AWS区域中调用Lambda函数?

时间:2018-11-28 09:09:59

标签: amazon-web-services aws-lambda

我有以下lambda函数,该函数使用AutoOff_uat标记停止所有Ec-2实例。如果我想在两个区域us-east-1和us-east-2上运行此lambda。我需要进行哪些修改

     <dxe:SpinEdit x:Name="dxSpinEdit" 
                   Height="23" MinWidth="200" Width="Auto"
                   HorizontalAlignment="Right"
                   Text="{Binding Value, Mode=TwoWay}"
                   MaskType="Numeric"
                   IsFloatValue="{Binding FloatValue}"
                   MinValue="{Binding MinValue}"
                   MaxValue="{Binding MaxValue}"
                   Mask="{Binding Mask, Mode=TwoWay}" 
                   MaxLength="{Binding Path=InputLength}"
                   MaskShowPlaceHolders="{Binding ShowPlaceHolder}"
                   InvalidValueBehavior="WaitForValidValue"
                   MaskUseAsDisplayFormat="True"
                   AllowRoundOutOfRangeValue="True"
                   />

1 个答案:

答案 0 :(得分:1)

您可以在创建区域时将区域传递给资源

ec2 = boto3.resource('ec2', region_name='us-east-2')

我建议将所有代码包装在一个以区域为参数的函数中,然后遍历要操作的区域列表。

import boto3
import logging

#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.INFO)


def shutdown_instances(region):
    #define the connection
    ec2 = boto3.resource('ec2', region=region)

    # Use the filter() method of the instances collection to retrieve
    # all running EC2 instances.
    filters = [{
            'Name': 'tag:AutoOff_uat',
            '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. 


def lambda_handler(event, context):
    for region in ['us-east-1', 'us-east-2']:
        shutdown_instances(region)