简体   繁体   English

使用 Boto3 Python 代码创建 AWS Lambda Function?

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

I need to create a lambda function using from scratch option.我需要使用从头开始选项创建 lambda function 选项。 I see there are 3 options in AWS Application.我看到 AWS 应用程序中有 3 个选项。 I went through AWS Boto3 document but unable to find the way to select 3 ways of selecting.我浏览了 AWS Boto3 文档,但无法找到 select 3 种选择方式的方法。

I tried looking into Boto3 Doc.我试着调查 Boto3 Doc。 My code is failing for S3 key.我的代码无法获取 S3 密钥。 How can I create a simple lambda function using Boto3 code!如何使用 Boto3 代码创建一个简单的 lambda function !

My code:我的代码:

  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)

Error: GetObjet S3 key is invalid.错误:GetObjet S3 密钥无效。

How can I create an s3 key or is there a simple way to create an AWS Lambda Function without any dependency.如何创建 s3 密钥,或者是否有一种简单的方法可以创建没有任何依赖关系的 AWS Lambda Function。 Please guide me!请指导我!

This key would come from uploading an object to Amazon S3, you can do this programmatically by calling put_object via the Boto3 SDK.此密钥来自将 object 上传到 Amazon S3,您可以通过 Boto3 SDK 调用put_object以编程方式执行此操作。

A rough example of how to use would be the following如何使用的粗略示例如下

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',
        )

You specify the key when you upload this, make sure that you zip your code when you upload it.您在上传时指定密钥,确保在上传时 zip 您的代码。

Alternatively use the ZipFile attribute instead, from the Boto3 documentation it states the following.或者,改用ZipFile属性,从Boto3 文档中它说明了以下内容。

The base64-encoded contents of the deployment package.部署 package 的 base64 编码内容。 AWS SDK and AWS CLI clients handle the encoding for you. AWS SDK 和 AWS CLI 客户端为您处理编码。

I found a lot of problems trying to create a lambda function with the a zip file, but finally I did this and worked.我在尝试使用 zip 文件创建 lambda function 时发现了很多问题,但最后我做到了并成功了。

This code will create a lambda function from a ZIP file:此代码将从 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 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