Skip to content

Commit

Permalink
adding lambda handler
Browse files Browse the repository at this point in the history
  • Loading branch information
socraticDevBlog committed Aug 8, 2023
1 parent 62313f6 commit cf4a304
Show file tree
Hide file tree
Showing 11 changed files with 1,216 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.aws-sam
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ boto3 = "*"

[dev-packages]
pytest = "*"
aws-sam-cli = "*"

[requires]
python_version = "3.11"
Expand Down
1,092 changes: 1,085 additions & 7 deletions Pipfile.lock

Large diffs are not rendered by default.

36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![PyTest](https://github.com/socraticDevBlog/pastebin/actions/workflows/pytest.yml/badge.svg?branch=main)](https://github.com/socraticDevBlog/pastebin/actions/workflows/pytest.yml)

# poor man cloud-native pastebin

this project is about using AWS Free tier resources to host yourown pastebin
Expand Down Expand Up @@ -31,7 +32,37 @@ Install dependencies
pipenv install --deploy --dev
```

Start DynamoDB local instance
### Run Lambda locally

Using SAM (Serverless Application Model) CLI, you can easily execute the lambda
locally

We are using only one (1) lambda entrypoint to cover all http requests since
our use case is pretty simple.

Lambda code is located at `src/app.py`

Stubbed lambda _event_ input arguments are located at `local/events/`. There is
one file per http verb.

#### simulate a POST request locally

```bash
pipenv run sam build

pipenv run sam local invoke -e local/events/post.json

# > Invoking app.lambda_handler (python3.9)
# Local image is up-to-date
# Using local image: public.ecr.aws/lambda/python:3.9-rapid-x86_64.

# Mounting /Users/me/git/pastebin/.aws-sam/build/PastebinFunction as /var/task:ro,delegated, inside runtime container
# START RequestId: 378e46ab-dd9b-4a31-bec7-1cc1b4a06ae8 Version: $LATEST
# END RequestId: 378e46ab-dd9b-4a31-bec7-1cc1b4a06ae8
# REPORT RequestId: 378e46ab-dd9b-4a31-bec7-1cc1b4a06ae8 Init Duration: 1.52 ms Duration: 1021.66 ms Billed Duration: 1022 ms Memory Size: 128 MB Max Memory Used: 128 MB
```

### Start DynamoDB local instance

```bash
cd local
Expand Down Expand Up @@ -63,6 +94,8 @@ pipenv run test -v

## Tech Stack

**dependencies management** pipenv

**database:** aws DynamoDB

**computing:** aws Lambda, python, boto3
Expand All @@ -76,4 +109,3 @@ pipenv run test -v
- Docker
- DynamoDB local
- SAM (Lambda), awscli
- localStack (API Gateway)
14 changes: 8 additions & 6 deletions local/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ print out a list of tables in your DynamoDB instance:
aws dynamodb list-tables --endpoint-url http://localhost:8000
```

expect to find the "paste" table:

```json
{
"TableNames": ["paste"]
}
```

### stopping and removing dynamodb-local

**use `-v` flag to completely delete database table "paste"**
Expand All @@ -42,9 +50,3 @@ docker compose down -v
```

more info/examples: <https://github.com/aws-samples/aws-sam-java-rest>

## SAM (serverless application model)

````bash
brew install aws-sam-cli
````
4 changes: 4 additions & 0 deletions local/events/delete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "user are not allowed to delete their pastes",
"method": "DELETE"
}
4 changes: 4 additions & 0 deletions local/events/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "test paste number two",
"method": "GET"
}
4 changes: 4 additions & 0 deletions local/events/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "test paste number one",
"method": "POST"
}
31 changes: 31 additions & 0 deletions samconfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# More information about the configuration file can be found here:
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
version = 0.1

[default]
[default.global.parameters]
stack_name = "pastebin"

[default.build.parameters]
cached = true
parallel = true

[default.validate.parameters]
lint = true

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true
resolve_s3 = true

[default.package.parameters]
resolve_s3 = true

[default.sync.parameters]
watch = true

[default.local_start_api.parameters]
warm_containers = "EAGER"

[default.local_start_lambda.parameters]
warm_containers = "EAGER"
31 changes: 31 additions & 0 deletions src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json


def lambda_handler(event, context):
"""
lambda_handler
Handles all http requests (from API Gateway)
- receives requests
- dispatch request to appropriate function
- returns http "statusCode" + a "body" with at least:
a) "message" field
...
"""
method = event["method"]
message = event["message"]

if method == "GET":
return {
"statusCode": 200,
"body": json.dumps({"message": message}),
}
elif method == "POST":
return {
"statusCode": 201,
"body": json.dumps({"message": message}),
}
else:
return {"statusCode": 405, "body": json.dumps({"message": message})}
12 changes: 12 additions & 0 deletions template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Resources:
PastebinFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: app.lambda_handler
Runtime: python3.9
Architectures:
- x86_64

0 comments on commit cf4a304

Please sign in to comment.