Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run the aws-cli in an AWS Lambda Python 3.6 environment?

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?

  • boto3 does not have an equivalent to the sync command
  • boto3 does not automatically find MIME types ("If you do not provide anything for ContentType to ExtraArgs, the end content type will always be binary/octet-stream.")
  • aws cli does automatically find MIME types ("By default the mime type of a file is guessed when it is uploaded")

Architecturally 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:

  • downloads Git and Hugo
  • downloads my repository
  • runs Hugo to generate my small (<100 pages) website
  • uploads the generated files to s3

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.

like image 929
pjgranahan Avatar asked Apr 25 '17 03:04

pjgranahan


People also ask

Can I use AWS CLI command in Python?

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

How do I change the version of AWS Lambda in Python?

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.


1 Answers

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.

like image 140
John Rotenstein Avatar answered Oct 23 '22 04:10

John Rotenstein