van
van

Reputation: 77072

How to run `AWS CLI` command within `aws lambda` function?

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:

Upvotes: 2

Views: 6869

Answers (3)

four43
four43

Reputation: 1750

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.

Upvotes: 1

Djai
Djai

Reputation: 198

-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

Upvotes: 0

Lucio Veloso
Lucio Veloso

Reputation: 1000

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

Upvotes: 1

Related Questions