Skip to main content
 首页 » 编程设计

python之AWS Lambda python 描述异常

2024年11月24日7over140

工作代码:

import boto3 
def lambda_handler(event, context): 
    ec2 = boto3.client('ec2') 
 
    # Get list of regions 
    regions = ec2.describe_regions().get('Regions',[] ) 
 
    # Iterate over regions 
    for region in regions: 
        print("*************** Checking region  --   %s " % region['RegionName']) 
        reg = region['RegionName'] 
        print(reg) 

输出:

*************** Checking region  --   eu-north-1  
eu-north-1 
*************** Checking region  --   ap-south-1  
ap-south-1 
*************** Checking region  --   eu-west-3  
eu-west-3 
*************** Checking region  --   eu-west-2  
eu-west-2 
*************** Checking region  --   eu-west-1 

它迭代并显示所有区域,但在我尝试描述资源详细信息后,我的代码只是在第一次迭代时退出。

import boto3 
def lambda_handler(event, context): 
    ec2 = boto3.client('ec2') 
 
    # Get list of regions 
    regions = ec2.describe_regions().get('Regions',[] ) 
 
    # Iterate over regions 
    for region in regions: 
        print("*************** Checking region  --   %s " % region['RegionName']) 
        reg = region['RegionName'] 
        print(reg) 
        print ("+++++++++++++ Starting EC2 Instances now -----------------")  
        client = boto3.client('ec2', region_name=reg) 
        response = client.describe_instances() 

输出错误:

Response: 
{ 
  "errorMessage": "2019-03-14T18:08:00.104Z 5fb67a9a-3bf9-40e3-ad56 Task timed out after 3.00 seconds" 
} 
 
Request ID: 
"5fb67a9a-3bf9-40e3-ad56" 
 
Function Logs: 
START RequestId: 5fb67a9a-3bf9-40e3-ad56 Version: $LATEST 
*************** Checking region  --   eu-north-1  
eu-north-1 
+++++++++++++ Starting EC2 Instances now ----------------- 
*************** Checking region  --   ap-south-1  
ap-south-1 
+++++++++++++ Starting EC2 Instances now ----------------- 
END RequestId: 5fb67a9a-3bf9-40e3-ad56 
REPORT RequestId: 5fb67a9a-3bf9-40e3-ad56-Duration: 3003.21 ms  Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 79 MB   
2019-03-14T18:08:00.104Z 5fb67a9a-3bf9-40e3-ad56-Task timed out after 3.00 seconds 

我已授予 lambda 角色访问资源的所有权限。 任何人都可以帮助我我做错了什么以及如何知道错误是什么?

请您参考如下方法:

您的代码成功迭代了 eu-north-1 和 ap-south-1 但随后在默认的 Lambda 超时 3 秒后超时。您需要使代码运行得更快或延长 Lambda 超时。

  1. 转到您的 Lambda console
  2. 找到你的函数并打开它
  3. 向下滚动并在基本设置下查找超时
  4. 延长您的超时时间(15 分钟是当前的最大值)
  5. 点击顶部的保存