7

I need to create a lambda function using from scratch option. I see there are 3 options in AWS Application. I went through AWS Boto3 document but unable to find the way to select 3 ways of selecting.

I tried looking into Boto3 Doc. My code is failing for S3 key. How can I create a simple lambda function using Boto3 code!

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.

How can I create an s3 key or is there a simple way to create an AWS Lambda Function without any dependency. Please guide me!

2 Answers 2

7

I found a lot of problems trying to create a lambda function with the a zip file, but finally I did this and worked.

This code will create a lambda function from a ZIP file:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Please explain what your code does and how it does it.
3

This key would come from uploading an object to Amazon S3, you can do this programmatically by calling put_object via the Boto3 SDK.

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.

Alternatively use the ZipFile attribute instead, from the Boto3 documentation it states the following.

The base64-encoded contents of the deployment package. AWS SDK and AWS CLI clients handle the encoding for you.

5 Comments

I have added a rough example of this which should indicate how it should be implemented
Your Lambda role that you're specifying just need to have the permission for xray:PutTraceSegments added to its policy. Alternatively add the AWSXrayWriteOnlyAccess managed policy to your role :)
Ok Thanks, I am usning python 3.7.3 For the code import zipfile archive = zipfile.ZipFile('function.zip', 'w') zip.write('index.js', 'path/on/disk/index.js') I am getting error as AttributeError: type object 'zip' has no attribute 'write' write is not getting highlighted whereas import zip is going to proper library when clicked on it
I just need to upload a zip file and not write how can i do that?
Oops, that should have been archive.write

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.