簡體   English   中英

使用 Boto3 Python 代碼創建 AWS Lambda Function?

[英]Create AWS Lambda Function using Boto3 Python Code?

我需要使用從頭開始選項創建 lambda function 選項。 我看到 AWS 應用程序中有 3 個選項。 我瀏覽了 AWS Boto3 文檔,但無法找到 select 3 種選擇方式的方法。

我試着調查 Boto3 Doc。 我的代碼無法獲取 S3 密鑰。 如何使用 Boto3 代碼創建一個簡單的 lambda function !

我的代碼:

  lambda_Client = boto3.client('lambda', aws_access_key_id=accessKey,
                       aws_secret_access_key=secretKey,region_name=region)
  response =lambda_Client.create_function(
            Code={
                'S3Bucket': 's3bucket',
                'S3Key': 'function.zip', #how can i create or fetch this S3Key
            },
            Description='Process image objects from Amazon S3.',
            FunctionName='function_name',
            Handler='index.handler',
            Publish=True,
            Role='arn:aws:iam::123456789012:role/lambda-role',
            Runtime='nodejs12.x',
        )

        print(response)

錯誤:GetObjet S3 密鑰無效。

如何創建 s3 密鑰,或者是否有一種簡單的方法可以創建沒有任何依賴關系的 AWS Lambda Function。 請指導我!

此密鑰來自將 object 上傳到 Amazon S3,您可以通過 Boto3 SDK 調用put_object以編程方式執行此操作。

如何使用的粗略示例如下

import zipfile
archive = zipfile.ZipFile('function.zip', 'w')
zip.write('index.js', 'path/on/disk/index.js')
.......

client.put_object(Body=archive, Bucket='bucket-name', Key='function.zip')

lambda_Client = boto3.client('lambda', aws_access_key_id=accessKey,
                       aws_secret_access_key=secretKey,region_name=region)
response = lambda_Client.create_function(
            Code={
                'S3Bucket': 'bucket-name',
                'S3Key': 'function.zip', #how can i create or fetch this S3Key
            },
            Description='Process image objects from Amazon S3.',
            FunctionName='function_name',
            Handler='index.handler',
            Publish=True,
            Role='arn:aws:iam::123456789012:role/lambda-role',
            Runtime='nodejs12.x',
        )

您在上傳時指定密鑰,確保在上傳時 zip 您的代碼。

或者,改用ZipFile屬性,從Boto3 文檔中它說明了以下內容。

部署 package 的 base64 編碼內容。 AWS SDK 和 AWS CLI 客戶端為您處理編碼。

我在嘗試使用 zip 文件創建 lambda function 時發現了很多問題,但最后我做到了並成功了。

此代碼將從 ZIP 文件創建 lambda function:

First we declare the path of the zip file Then on the aws_file function we convert it into bytes so amazon can read it Finally the lambda_creator will upload it and create the lambda function with the parameters given

ZIPNAME = "code\\my-deployment-package.zip"


def aws_file():
    with open(ZIPNAME, 'rb') as file_data:
        bytes_content = file_data.read()
    return bytes_content


def lambda_creator(name):
    lambda_client = boto3.client('lambda', aws_access_key_id=ACCESSKEY,
                                 aws_secret_access_key=SECRETKEY, region_name=REGION)
    response = lambda_client.create_function(
        Code={
            'ZipFile': aws_file()
        },
        Description='Hello World Test.',
        FunctionName='Test-lambda',
        Handler='lambda_function.lambda_handler',
        Publish=True,
        Role='arn:aws:iam:: 123456789012:role/lambda-rol',
        Runtime='python3.8',
    )
    return response

暫無
暫無

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

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