Skip to content

Make serverless architecture with AWS Lambda

Sejoon Kim edited this page Sep 4, 2024 · 3 revisions

Project Tech Stack and Architecture

This document explains the technology stack and architecture of our project. The backend is implemented using AWS Lambda and Go language, and is automatically deployed through a CI/CD pipeline.

Technology Stack

  1. Backend Language: Go 1.22.2
  2. Serverless Computing: AWS Lambda
  3. API Management: Amazon API Gateway
  4. CI/CD: GitHub Actions
  5. External API: OpenAI GPT API
  6. Frontend: HTML, CSS, React

Architecture Overview

Our application is structured as follows:

  1. Frontend (React application)
  2. API Gateway
  3. AWS Lambda functions (written in Go)
  4. OpenAI GPT API

Flow of user requests:

  1. The user sends a request from the frontend.
  2. The request is forwarded to the Lambda function through API Gateway.
  3. The Lambda function calls the OpenAI GPT API as needed.
  4. The result is returned to the frontend through API Gateway.

Using Lambda with Go and Calling GPT API

Setting up AWS Lambda with Go

To use AWS Lambda with Go, we need to implement the Handler function. Here's a basic structure:

package main

import (
    "github.com/aws/aws-lambda-go/lambda"
)

func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    // Your logic here
}

func main() {
    lambda.Start(Handler)
}

Calling GPT API from Go

To call the GPT API, we use the net/http package to make HTTP requests. Here's a simplified example:

import (
    "bytes"
    "encoding/json"
    "net/http"
    "os"
)

func CallGPTAPI(prompt string) (string, error) {
    apiKey := os.Getenv("OPENAI_API_KEY")
    url := "https://api.openai.com/v1/engines/davinci-codex/completions"
    
    requestBody, _ := json.Marshal(map[string]interface{}{
        "prompt": prompt,
        "max_tokens": 100,
    })
    
    req, _ := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
    req.Header.Set("Authorization", "Bearer " + apiKey)
    req.Header.Set("Content-Type", "application/json")
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()
    
    // Process the response here
    
    return "GPT response", nil
}

Configuring Lambda and API Gateway

Setting up Lambda

  1. Go to AWS Lambda console
  2. Click "Create function"
  3. Choose "Author from scratch"
  4. Enter function name (e.g., "EnglishCheckFunction")
  5. Choose "Go 1.x" for the runtime
  6. Click "Create function"

TODO :: Show Image

Uploading Go code to Lambda

  1. Build your Go code locally: GOOS=linux go build -o main
  2. Zip the binary: zip function.zip main
  3. Upload the zip file in the Lambda console

TODO :: Show Image

Configuring API Gateway

  1. Go to API Gateway console
  2. Click "Create API"
  3. Choose "REST API" and click "Build"
  4. Create new resource (e.g., "/check")
  5. Create POST method for the resource
  6. Integrate the method with your Lambda function

TODO :: Show Image

CORS Configuration

Don't forget to enable CORS for your API:

  1. Sele1ct your API resource
  2. Click on "Actions" > "Enable CORS"
  3. Click on "Enable CORS and replace existing CORS headers"

TODO :: Show Image

CI/CD Pipeline Configuration

We use GitHub Actions for our CI/CD pipeline. Here's how it's set up:

  1. Create a .github/workflows/deploy.yml file in your repository
  2. Add the following content:
name: Deploy to Lambda

on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.22.2
    - name: Build
      run: |
        cd lambda
        GOOS=linux go build -o main
        zip function.zip main
    - name: Deploy to Lambda
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: eu-central-1
    - run: |
        aws lambda update-function-code --function-name EnglishCheckFunction --zip-file fileb://lambda/function.zip

This workflow does the following:

  1. Triggers on push to the main branch
  2. Sets up Go environment
  3. Builds the Lambda function
  4. Configures AWS credentials
  5. Deploys the function to AWS Lambda

To set up secrets:

  1. Go to your GitHub repository
  2. Click on "Settings" > "Secrets"
  3. Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY

TODO :: Show Image

With this setup, every push to the main branch will automatically update your Lambda function.