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!
How to invoke AWS Lambda function using Boto3. To invoke the Lambda function, you need to use the invoke() function of the Boto3 client. To send input to your Lambda function, you need to use the Payload argument, which should contain JSON string data.
Because the boto3 module is already available in the AWS Lambda Python runtimes, don't bother including boto3 and its dependency botocore in your Lambda deployment zip file.
Syntax. Simply put, a lambda function is just like any normal python function, except that it has no name when defining it, and it is contained in one line of code. A lambda function evaluates an expression for a given argument. You give the function a value (argument) and then provide the operation (expression).
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With