Reputation: 77072
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:
awscli
available to be called during lambda function execution?aws s3 sync
call directly from python awscli
library?Upvotes: 2
Views: 6869
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
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
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