From 1578f98d9381883489f9a2f2d39c8514c33fef5d Mon Sep 17 00:00:00 2001 From: Hatter1337 Date: Tue, 13 Aug 2024 11:04:06 +0100 Subject: [PATCH] Update documentation, add dokcer compose --- README.md | 31 ++++++++++++++++++++++++--- docker-compose.yaml | 12 +++++++++++ {doc => docs}/postman_collection.json | 0 {doc => docs}/swagger.yml | 0 run-local-api.sh | 11 ++++++++++ 5 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 docker-compose.yaml rename {doc => docs}/postman_collection.json (100%) rename {doc => docs}/swagger.yml (100%) create mode 100755 run-local-api.sh diff --git a/README.md b/README.md index e84799b..698f43a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,22 @@ This project demonstrates serverless architecture, offering scalable and cost-ef **Note:** `swagger.yml` and Postman collection are located in the `/doc` directory. +## Project structure +- `/docs` - API documentation with swagger file +- `/src` - Source code + - `/lambda` - Code base related to Lambda functions + - `/functions` - Lambda functions + - `/{name}` - Specific Lambda function with `handler.py` + - `/tests` - Unit tests for all Lambda functions + - `/layers` - Code base related to Lambda layers + - `/{name}_layer` - Lambda layer with `requirements.txt` and common code + - `/tests` - Unit tests for all Lambda layers + - `/resources` - AWS resources configuration with CloudFormation + - `template.yaml` - SAM template file with API resources: API Gateway, Lambda functions, etc. + - `samconfig.toml` - SAM configuration file with environment variables and parameters + - `docker-compose.yaml` - Docker Compose configuration for local development + - `run-local-api.sh` - Script to run SAM application locally in Docker + ## Deploy to your AWS account - First of all, you need to have an AWS account and AWS CLI installed on your machine. - Clone this repository and navigate to the root directory. @@ -24,8 +40,17 @@ This project demonstrates serverless architecture, offering scalable and cost-ef $ sam build $ sam deploy --config-env dev --profile {your-aws-profile} ``` -- Or run locally using **SAM CLI**: + +## Run locally using **SAM**: +- Configure your AWS CLI with the necessary credentials, for your AWS profile, before running SAM commands. +- Ensure Docker is running for `sam local start-api` to simulate the Lambda environment. +- Run the following commands to build and start the API locally: ```bash $ sam build -$ sam local start-api -``` \ No newline at end of file +$ sam local start-api --port 8000 --profile {your-aws-profile} +``` + +You also can use Dokcer Compose to run the application locally: +- Change the AWS profile name in `run-local-api.sh`. +- Grant execution rights to the script: `$ chmod +x run-local-api.sh` +- `$ docker-compose up` diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..8a8f2e5 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +services: + sam: + image: public.ecr.aws/sam/build-python3.12:1.112.0-20240313001228 + container_name: pokemon-api-local + ports: + - "8000:8000" + volumes: + - .:$PWD + - /var/run/docker.sock:/var/run/docker.sock + - ~/.aws/:/root/.aws:ro + working_dir: $PWD + entrypoint: ["/bin/sh", "-c", "./run-local-api.sh"] \ No newline at end of file diff --git a/doc/postman_collection.json b/docs/postman_collection.json similarity index 100% rename from doc/postman_collection.json rename to docs/postman_collection.json diff --git a/doc/swagger.yml b/docs/swagger.yml similarity index 100% rename from doc/swagger.yml rename to docs/swagger.yml diff --git a/run-local-api.sh b/run-local-api.sh new file mode 100755 index 0000000..de03e9a --- /dev/null +++ b/run-local-api.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +sam build + +sam local start-api \ + --profile {your-aws-profile} \ + --container-host host.docker.internal \ + --skip-pull-image \ + --host 0.0.0.0 \ + --port 8000 \ + --container-host-interface 0.0.0.0 \ No newline at end of file