Skip to content

Commit

Permalink
Merge pull request #77 from ant-xuexiao/feat/support-lambda-deployment
Browse files Browse the repository at this point in the history
feat: migrate lambda deployment
  • Loading branch information
RaoHai authored Apr 9, 2024
2 parents 41dec72 + 1752e3c commit 1f98966
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 714 deletions.
437 changes: 0 additions & 437 deletions .aws/awsCloudFormation.yaml

This file was deleted.

10 changes: 10 additions & 0 deletions .aws/petercat-preview.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 0.1
[default.deploy.parameters]
stack_name = "petercat-api-preview"
resolve_s3 = true
s3_prefix = "petercat-api-preview"
region = "ap-northeast-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
disable_rollback = true
image_repositories = ["FastAPIFunction=654654285942.dkr.ecr.ap-northeast-1.amazonaws.com/samapp7427b055/fastapifunctionead79d0drepo"]
10 changes: 10 additions & 0 deletions .aws/petercat-prod.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version = 0.1
[default.deploy.parameters]
stack_name = "sam-app"
resolve_s3 = true
s3_prefix = "sam-app"
region = "ap-northeast-1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
disable_rollback = true
image_repositories = ["FastAPIFunction=654654285942.dkr.ecr.ap-northeast-1.amazonaws.com/samapp7427b055/fastapifunctionead79d0drepo"]
97 changes: 0 additions & 97 deletions .aws/task_definition.json

This file was deleted.

99 changes: 0 additions & 99 deletions .aws/task_definition_preview.json

This file was deleted.

42 changes: 42 additions & 0 deletions .github/workflows/aws-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Deploy Backend to Preview ECS

on:
push:
branches: ["main"]
paths:
- "server/**"
pull_request:
branches: [ "main" ]

env:
AWS_REGION: ap-northeast-1
ECR_REPOSITORY: petercat-lambda
ECR_REGISTRY: 654654285942.dkr.ecr.ap-northeast-1.amazonaws.com

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout

jobs:
deploy:
runs-on: ubuntu-latest
environment: production
strategy:
fail-fast: true

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::654654285942:role/Github-OIDC
audience: sts.amazonaws.com
aws-region: ${{ env.AWS_REGION }}

# Build inside Docker containers
- run: sam build --use-container --config-file .aws/petercat-preview.toml

# Prevent prompts and failure when the stack is unchanged
- run: sam deploy --no-confirm-changeset --no-fail-on-empty-changeset --config-file .aws/petercat-preview.toml
86 changes: 6 additions & 80 deletions .github/workflows/aws-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,83 +6,19 @@ on:

env:
AWS_REGION: ap-northeast-1
ECR_REPOSITORY: xuexiao
ECR_REPOSITORY: petercat-lambda
ECR_REGISTRY: 654654285942.dkr.ecr.ap-northeast-1.amazonaws.com
ECS_CLUSTER: ${{ secrets.ECS_CLUSTER }}
ECS_SERVICE: ${{ secrets.ECS_SERVICE }}

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout

jobs:
build_and_push:
name: Build and Push Docker Image
runs-on: ubuntu-latest
environment: production

steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::654654285942:role/Github-OIDC
audience: sts.amazonaws.com
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@2fc7aceee09e9e4a7105c0d060c656fad0b4f63d # v1

- name: Login to GitHub Container Registry
uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc # v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Login to Docker Hub
uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3

- name: Create Docker Cacha Storage Backend
run: |
docker buildx create --use --driver=docker-container
- name: See the file in the runner
run: |
ls -la
- name: Build, tag, and push image to Amazon ECR
id: build-image
uses: docker/build-push-action@4a13e500e55cf31b7a5d59a38ab2040ab0f42f56 # v5
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
with:
context: ./server/
push: true
platforms: linux/amd64,linux/arm64
tags: ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:${{ env.IMAGE_TAG }}, ${{ env.ECR_REGISTRY }}/${{ env.ECR_REPOSITORY }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max

deploy:
needs: build_and_push
runs-on: ubuntu-latest
environment: production
strategy:
fail-fast: false
matrix:
include:
- name: "xuexiao"
task_definition: ".aws/task_definition.json"
container: "petercat-web"
fail-fast: true

steps:
- name: Checkout
Expand All @@ -95,18 +31,8 @@ jobs:
audience: sts.amazonaws.com
aws-region: ${{ env.AWS_REGION }}

- name: Fill in the new image ID in the Amazon ECS task definition for ${{ matrix.name }}
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@4225e0b507142a2e432b018bc3ccb728559b437a # v1
with:
task-definition: ${{ matrix.task_definition }}
container-name: ${{ matrix.container }}
image: ${{env.ECR_REGISTRY}}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
# Build inside Docker containers
- run: sam build --use-container --config-file .aws/petercat-prod.toml

- name: Deploy Amazon ECS task definition for ${{ matrix.name }}
uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a # v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
# Prevent prompts and failure when the stack is unchanged
- run: sam deploy --no-confirm-changeset --no-fail-on-empty-changeset --config-file .aws/petercat-prod.toml
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ yarn-error.log*
next-env.d.ts

.yarn
/server/.aws-sam/*
.aws-sam/*
6 changes: 6 additions & 0 deletions doc/init_aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ Docker Compose's integration for ECS and ACI will be retired in November 2023. L
Successfully created ecs context "botmetaecscontext"
```

#### Test
```bash
curl -v -N --http2 --location 'https://7pgjnn7ecq7hs5jj6csocmt3rm0cyvxb.lambda-url.ap-northeast-1.on.aws/api/chat/stream' \
--header 'Content-Type: application/json' \
--data '{"messages":[{"role":"user","content":"彩蛋相关的issue"}],"prompt":""}'
```

#### REFERENCES
https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-private-integration.html
Loading

0 comments on commit 1f98966

Please sign in to comment.