How do I run aws-cli in AWS Lambda Python 3.6 environment?
I would like to call a command aws s3 sync
from an AWS Lambda function with Python 3.6 runtime. How can i do this?
Why don't you just use the included boto3 SDK file?
- boto3 has no command equivalent
sync
- boto3 does not automatically find MIME types ("If you do not provide anything for the ContentType for ExtraArgs, the final content type will always be binary / octet-stream").
- aws cli automatically finds MIME types ("By default, the mime type of the file is guessed when it is loaded")
In my use case, I think it makes sense architecturally and financially, but I'm open to alternatives. My lambda function :
- downloads Git and Hugo
- downloads my repository
- launches Hugo to build my small (<100 pages) website.
- loads generated files into s3
Right now I can do all of the above on a 1536MB (most powerful) Lambda function in about 1-2 seconds. This feature only fires when I commit changes to my website, so it's inexpensive to run.
Perhaps it is already installed in the Lambda environment?
At the time of this writing, this is not the case.
source to share
From Executing 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 is not installed on Lambda by default, so you must include it in your deployment. Despite running in the Python 3.6 Lambda environment, Python 2.7 is still available in the environment, so the approach described in this article will continue to work.
To experiment with lambda systems take a look at lambdash .
source to share