Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ahmadpayan/ahmad-web-shop


https://github.com/ahmadpayan/ahmad-web-shop

Last synced: about 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# Setup Instructions

# Overview
This project demonstrates the use of AWS Lambda, SQS, and CloudWatch Logs in a serverless architecture. The Lambda function processes messages from an SQS queue and logs the output to CloudWatch Logs.

# Architecture
- AWS Lambda: Processes messages from the SQS queue.
- AWS SQS: Message queue that triggers the Lambda function.
- AWS CloudWatch Logs: Stores logs generated by the Lambda function.

### Prerequisites

- Docker installed and running
- LocalStack installed
- https://github.com/localstack/awscli-local
- `pip install awscli-local`
- AWS CLI installed
- https://aws.amazon.com/cli/

- Configure AWS CLI for LocalStack:
```sh
aws configure --profile localstack-ahmad
```

- Region: `us-east-2`
- key and secret: `test`

### Setup Project

1. Build and Deploy the CloudFormation Stack

Run make command to setup the project and containers:
- `make`

Done! You only need to run the integration test to see if everything is set up correctly.

```sh
# Move the ZIP file to the local S3 bucket of LocalStack
awslocal s3 mb s3://local-lambda-bucket # Create S3 bucket if it doesn't exist
awslocal s3 cp WebShop.Lambda.zip s3://local-lambda-bucket/WebShop.Lambda.zip

# Deploy the CloudFormation stack
awslocal cloudformation create-stack --stack-name WebShopStack --template-body file://path/to/your/cloudformation-template.yml
```

2. Verify Lambda and SQS Creation
```sh
# List Lambda functions to verify creation
awslocal lambda list-functions --endpoint-url=http://localhost:4566 --profile localstack-ahmad

# List SQS queues to verify creation
awslocal sqs list-queues --endpoint-url=http://localhost:4566 --profile localstack-ahmad
```

## Viewing Logs

For simplicity, run the view_logs.sh file. But you need to install the `jq`. (`jq` is a lightweight and flexible command-line JSON processor).
In Mac:
- `brew install jq`

Then make it executable:
- `chmod +x view_logs.sh`

Now, you just need to run the integration this and then run the file to see the logs from the Lambda function
- `./view_logs.sh`

1. Describe Log Groups

```sh
awslocal logs describe-log-groups --endpoint-url=http://localhost:4566 --profile localstack-ahmad
```

2. Describe Log Streams
```sh
# Replace '/aws/lambda/YourLogGroupName' with the actual log group name from the previous step
awslocal logs describe-log-streams --log-group-name '/aws/lambda/YourLogGroupName' --endpoint-url=http://localhost:4566 --profile localstack-ahmad
```

3. Tail Logs
```sh
# Replace '/aws/lambda/YourLogGroupName' with the actual log group name
awslocal logs tail '/aws/lambda/YourLogGroupName' --endpoint-url=http://localhost:4566 --profile localstack-ahmad
```

## Additional Commands
1. Send a Message to SQS Queue
```sh
# Replace 'your-queue-url' with the actual queue URL
awslocal sqs send-message --queue-url your-queue-url --message-body 'Your message body' --endpoint-url=http://localhost:4566
```

2. Invoke Lambda Manually

```sh
# Replace 'YourLambdaFunctionName' with the actual Lambda function name
awslocal lambda invoke --function-name YourLambdaFunctionName --payload '{}' response.json --endpoint-url=http://localhost:4566
```

## Troubleshooting
1. List CloudFormation Stacks
```sh
1. awslocal cloudformation list-stacks --endpoint-url=http://localhost:4566
```

2. Describe CloudFormation Stack Resources

```sh
# Replace 'WebShopStack' with your stack name
awslocal cloudformation describe-stack-resources --stack-name WebShopStack --endpoint-url=http://localhost:4566
```

## Clean Up

1. Delete CloudFormation Stack
```sh
# Replace 'WebShopStack' with your stack name
awslocal cloudformation delete-stack --stack-name WebShopStack --endpoint-url=http://localhost:4566
```

2. Delete S3 Bucket
```sh
# Remove objects from the bucket before deleting
awslocal s3 rm s3://local-lambda-bucket --recursive
awslocal s3 rb s3://local-lambda-bucket
```