Reputation: 21
I would like to use Boto3 to upload a script to S3,create a Lambda function specifying the S3 key to run the script. To speed up the process, I would also have some Python Dependencies pre-stored in S3,how could I specify where the lambda function should look for those dependencies? Also, how can I delete the script from S3 once the function is executed, using Boto3?
My code will be similar as follows:
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',
)
Upvotes: 1
Views: 221
Reputation: 78663
I'm assuming that by "dependencies", you mean third-party Python packages.
Lambda will not fetch dependencies for your Python Lambda function. You need to package the dependencies when you initially create the Lambda function or when you subsequently update the Lambda function.
To delete a Lambda function, use delete_function. To delete a script, or other, from S3, use delete_object.
If by "dependencies", you simply mean assets that you have stored in S3, such as JSON files or other, then use the S3 features of boto3 such as get_object to retrieve them.
Upvotes: 1