2

I would like to run aws cli command (actually aws s3 sync) from within the aws lambda function. How do I do that? Ideally in python, but javascript (or java) would work too.

Using python I tried achieving this by Creating a Deployment Package where i would have awscli as a python package, so that I can use it later. However, the aws command is not available during lambda function execution, and only the awscli package is.

How can I:

  • either: make sure that I have awscli available to be called during lambda function execution?
  • or: construct a aws s3 sync call directly from python awscli library?
3
  • Why would you want to invoke aws cli from lambda? Can't you get the same using the sdk. Commented May 23, 2016 at 5:05
  • I do not think so (or at least, i did not find a way to do that). I could, of course, use SDK to create my own sync routine, but I really would like to use the one which is already offered by aws cli (which internally uses SDK (botocore)), but extends it quite a bit. Commented May 23, 2016 at 9:29
  • 1
    Possible duplicate of Call aws-cli from AWS Lambda Commented May 25, 2016 at 6:24

3 Answers 3

1

Look the project https://github.com/lucioveloso/cli2cloudformation and you will figure out how to wrapper the cli inside a lambda function.

Sign up to request clarification or add additional context in comments.

2 Comments

Put the information in your answer as links can go dead.
This is actually one of the cleanest solutions I've seen
1

Piggybacking on @lucio-veloso's answer, that's a pretty clever way of invoking the CLI from Python.

Include the awscli in your bundle but shipping the awscli as a dependency using whatever your build process is.

Then you can run something like:

aws_cli_driver = awscli.clidriver.create_clidriver()
aws_cli_driver.main(["s3", "sync", "--delete", "s3://test-my-bucket", "/tmp/my-path"])
if exit_code > 0:
    raise RuntimeError(f"awscli exited: {exit_code}")

Which seems to work for my aws s3 sync use case for using the AWS CLI in Lambda.

Comments

0

-Install AWS CLI in a local virtual environment

-Package AWS CLI and all its dependencies to a zip file

-Create a Lambda Layer

-Use that layer in your lambda function

Step by step guide is at : https://bezdelev.com/hacking/aws-cli-inside-lambda-layer-aws-s3-sync/

Or

use bash layers as suggested in other stack overflow ans Call aws-cli from AWS Lambda

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.