簡體   English   中英

腳本到AWS Lambda?

[英]Script to AWS Lambda?

我有這個bash腳本,其中列出了跨區域的所有活動EC2實例:

for region in `aws ec2 describe-regions --output text | cut -f3` do
 echo -e "\nListing Instances in region:'$region'..."
 aws ec2 describe-instances --region $region
done

我想將此移植到AWS上的Lambda函數。 今天最好的方法是什么? 我必須使用包裝紙或類似包裝紙嗎? 節點? 我在Google上搜索后發現最像是解決方法..但它們已經存在了兩年。 將不勝感激的最新指示。

有兩種方法:

  1. 使用自定義運行時和圖層: https//github.com/gkrizek/bash-lambda-layer

  2. 從另一個運行時執行: https : //github.com/alestic/lambdash

您應該使用具有AWS開發工具包的語言(例如Python)來編寫它。

您還應該考慮Lambda函數應如何處理輸出,因為此刻Lambda函數僅檢索信息但不對其進行任何操作。

這是示例AWS Lambda函數:

import boto3

def lambda_handler(event, context):

    instance_ids = []

    # Get a list of regions    
    ec2_client = boto3.client('ec2')
    response = ec2_client.describe_regions()

    # For each region
    for region in response['Regions']:

        # Get a list of instances
        ec2_resource = boto3.resource('ec2', region_name=region['RegionName'])
        for instance in ec2_resource.instances.all():
            instance_ids.append(instance.id)

    # Return the list of instance_ids
    return instance_ids

請注意,順序調用所有區域需要花費大量時間。 以上過程可能需要15到20秒。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM