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?
van
  • 74,297
  • 13
  • 168
  • 171
  • Why would you want to invoke aws cli from lambda? Can't you get the same using the sdk. – Shibashis May 23 '16 at 05: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. – van May 23 '16 at 09:29
  • 1
    Possible duplicate of [Call aws-cli from AWS Lambda](http://stackoverflow.com/questions/33513604/call-aws-cli-from-aws-lambda) – stephan May 25 '16 at 06:24

3 Answers3

1

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

Lucio Veloso
  • 850
  • 9
  • 10
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.

four43
  • 1,675
  • 2
  • 20
  • 33
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

Djai
  • 188
  • 10