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!
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.