https://github.com/agn-7/python-aws-lambda
A simple example to run a python code snippet serverless using AWS Lambda service via Localstack
https://github.com/agn-7/python-aws-lambda
aws aws-cli aws-lambda awscli awslocal localstack python
Last synced: 10 months ago
JSON representation
A simple example to run a python code snippet serverless using AWS Lambda service via Localstack
- Host: GitHub
- URL: https://github.com/agn-7/python-aws-lambda
- Owner: agn-7
- License: mit
- Created: 2023-11-04T07:32:39.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-04T08:17:53.000Z (about 2 years ago)
- Last Synced: 2025-01-16T13:58:41.335Z (12 months ago)
- Topics: aws, aws-cli, aws-lambda, awscli, awslocal, localstack, python
- Language: Python
- Homepage:
- Size: 3.91 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# python-aws-lambda
A simple example of running a python code snippet serverless using AWS Lambda service via Localstack
## How to run
**Setup localstack to simulate AWS by docker-compose:**
- `docker compose up -d`
**Create a Lambda function:**
- Already existing on `lambda_function.py`
**Compress the python code snippet to a zip file:**
- `zip lambda.zip lambda_function.py`
**Install `awscli-local` and `awscli`:**
```
pip install awscli-local
pip install awscli
```
**Create a new Lambda function:**
```
awslocal lambda create-function \
--function-name lambda_function \
--runtime python3.8 \
--zip-file fileb://lambda.zip \
--handler lambda_function.lambda_handler \
--role arn:aws:iam::000000000000:role/lambda-role
```
**Invoke the Function:**
```
awslocal lambda invoke --function-name lambda_function \
--payload '{"num1": 5, "num2": 15}' output.txt
```
**Check the result:**
- `cat output.txt`
```
{"statusCode": 200, "body": 20}
```
**Create a Function URL:**
```
awslocal lambda create-function-url-config \
--function-name lambda_function \
--auth-type NONE
```
This will generate a HTTP URL that can be used to invoke the Lambda function. The URL will be in the format `http://.lambda-url.us-east-1.localhost.localstack.cloud:4566`
**Trigger the Lambda function URL:**
```
curl -X POST \
'http://iu4s187onr1oabg50dbvm77bk6r5sunk.lambda-url.us-east-1.localhost.localstack.cloud:4566/' \
-H 'Content-Type: application/json' \
-d '{"num1": "10", "num2": "10"}'
```