tonykang22 / chat

0 stars 0 forks source link

채팅 서비스 레퍼런스 분석3. Deploying a Group Chat App on a Serverless Architecture  #1

Open tonykang22 opened 1 year ago

tonykang22 commented 1 year ago

Deploying a Group Chat App on a Serverless Architecture

서버리스의 장점

image



서버리스 구축을 위해 필요한 서비스

image



Amazon S3



Amazon API Gateway



Amazon Lambda



DynamoDB




CloudFront



흐름


Retrieving the list of groups

image


"""

    Author | https://github.com/davykiash

"""

import os
import json
import boto3
import botocore
import requests
from datetime import datetime
from botocore.exceptions import ClientError
from boto3.dynamodb.conditions import Key

DYNAMODB_AWS_KEY = os.getenv('DYNAMODB_AWS_KEY')
DYNAMODB_AWS_SECRET = os.getenv('DYNAMODB_AWS_SECRET')
DYNAMODB_AWS_ENDPOINT = os.getenv('DYNAMODB_AWS_ENDPOINT')

def my_handler(event, context):
    # body = json.loads(event["body"])

    # store in dynamodb chat_messages user has joined
    list_of_groups = get_all_groups()
    if list_of_groups:

        return {
            'isBase64Encoded': False,
            'statusCode': 200,
            'headers': {"status": "success"},
            'body': json.dumps(list_of_groups)
        }

    else:
        return {
            'statusCode': 200,
            'body': "Connection Failed"
        }

def get_all_groups(dynamodb=None):
    if not dynamodb:
        dynamodb = boto3.resource('dynamodb',
                                  aws_access_key_id=DYNAMODB_AWS_KEY,
                                  aws_secret_access_key=DYNAMODB_AWS_SECRET,
                                  endpoint_url=DYNAMODB_AWS_ENDPOINT)

    table = dynamodb.Table('chat_groups')

    try:
        response = table.scan()
    except ClientError as e:
        print(e.response['Error']['Message'])
    else:
        if 'Items' in response.keys():
            return response['Items']
        else:
            return None



Joining and sending messages to all active members in the group.

image



Leave chat