-
Notifications
You must be signed in to change notification settings - Fork 0
Make serverless architecture with AWS Lambda
Sejoon Kim edited this page Sep 4, 2024
·
3 revisions
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.
- Backend Language: Go 1.22.2
- Serverless Computing: AWS Lambda
- API Management: Amazon API Gateway
- CI/CD: GitHub Actions
- External API: OpenAI GPT API
- Frontend: HTML, CSS, React
- Frontend (React application)
- API Gateway
- AWS Lambda functions (written in Go)
- OpenAI GPT API
- The user sends a request from the frontend.
- The request is forwarded to the Lambda function through API Gateway.
- The Lambda function calls the OpenAI GPT API as needed.
- The result is returned to the frontend through API Gateway.
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)
}
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
}
- Go to AWS Lambda console
- Click "Create function"
- Choose "Author from scratch"
- Enter function name (e.g., "EnglishCheckFunction")
- Choose "Go 1.x" for the runtime
- Click "Create function"
TODO :: Show Image
- Build your Go code locally: GOOS=linux go build -o main
- Zip the binary: zip function.zip main
- Upload the zip file in the Lambda console
TODO :: Show Image
- Go to API Gateway console
- Click "Create API"
- Choose "REST API" and click "Build"
- Create new resource (e.g., "/check")
- Create POST method for the resource
- Integrate the method with your Lambda function
TODO :: Show Image
Don't forget to enable CORS for your API:
- Sele1ct your API resource
- Click on "Actions" > "Enable CORS"
- Click on "Enable CORS and replace existing CORS headers"
TODO :: Show Image
We use GitHub Actions for our CI/CD pipeline. Here's how it's set up:
- Create a .github/workflows/deploy.yml file in your repository
- 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:
- Triggers on push to the main branch
- Sets up Go environment
- Builds the Lambda function
- Configures AWS credentials
- Deploys the function to AWS Lambda
To set up secrets:
- Go to your GitHub repository
- Click on "Settings" > "Secrets"
- 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.