I would like to call the aws s3 sync
command from within an AWS Lambda function with a runtime version of Python 3.6. How can I do this?
Why don't you just use the included boto3 SDK?
sync
commandArchitecturally this doesn't make sense!
For my use case I think it makes sense architecturally and financially, but I'm open to alternatives. My Lambda function:
Right now, I'm able to do all of the above on a 1536 MB (the most powerful) Lambda function in around 1-2 seconds. This function is only triggered when I commit changes to my website, so it's inexpensive to run.
Maybe it is already installed in the Lambda environment?
As of the time of this writing, it is not.
The AWS CLI and AWS SDK for Python will require Python 2.7+ or 3.4+ as their Python runtime. On January 10, 2020, in order to continue supporting our customers with tools that are secure and maintainable, AWS will publish a minor version bump of the AWS CLI and AWS SDK for Python (Boto3 and Botocore).
To update the runtime, just go into the Lambda console -> Code and Scroll to Runtime Settings to change the runtime. Depending what code your Lambda has jumping from Python 2 to 3 will probably not run - so just changing the runtime might not be the onlything you need to do.
From Running aws-cli Commands Inside An AWS Lambda Function:
import subprocess
command = ["./aws", "s3", "sync", "--acl", "public-read", "--delete",
source_dir + "/", "s3://" + to_bucket + "/"]
print(subprocess.check_output(command, stderr=subprocess.STDOUT))
The AWS CLI isn't installed by default on Lambda, so you have to include it in your deployment. Despite running in a Python 3.6 Lambda environment, Python 2.7 is still available in the environment, so the approach outlined in the article will continue to work.
To experiment on Lambda systems, take a look at lambdash.
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