Skip to content

Commit

Permalink
feat: deploy on AWS Lambda (#703)
Browse files Browse the repository at this point in the history
* feat: aws-lambda compat code

* feat: Terraform code to deploy the function

* docs: how to use Terraform

* docs: do not set a dummy value of MONGOODB_URI

* docs: deploy backend on AWS Lambda

* lint: remove extra semicolons
  • Loading branch information
wzyboy authored May 22, 2024
1 parent adc03a1 commit 0cfdd9c
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [Zeabur 部署](#zeabur-部署) | ★☆☆☆☆ | 需要绑定支付宝或信用卡,部署简单,适合中国大陆访问,免费计划环境随时可能会被删除。 |
| [Netlify 部署](#netlify-部署) | ★★★★☆ | 有充足的免费额度,中国大陆访问速度不错。 |
| [Hugging Face 部署](#hugging-face-部署) | ★★★★☆ | 免费,中国大陆访问速度不错。 |
| [AWS Lambda 部署](#aws-lambda-部署) | ★★★☆☆ | 全球最大的云平台,适合已经使用 AWS 全家桶的用户。 |
| [私有部署](#私有部署) | ★★☆☆☆ | 适用于有服务器的用户,需要自行申请 HTTPS 证书。 |
| [私有部署 (Docker)](#私有部署-docker) | ★★★☆☆ | 适用于有服务器的用户,需要自行申请 HTTPS 证书。 |

Expand Down Expand Up @@ -245,6 +246,19 @@ EXPOSE 7860

![](./static/hugging-6.png)

## AWS Lambda 部署

1. 注册 AWS 账号并配置 Terraform CLI。
2. 参考 `src/server/aws-lambda/terraform` 目录中 Terraform 代码创建 AWS 资源。
3. 部署完成后,Terraform 会将 `lambda_function_url` 打印在屏幕上,您也可以使用 `terraform output` 获取这一 URL,如:

```
$ terraform output
lambda_function_url = "https://axtoiiithbcexamplegq7ozalu0cnkii.lambda-url.us-west-2.on.aws/"
```

该 URL 即为您的环境 ID,请记下这一 URL 用于前端配置。

## 私有部署

::: warning 注意
Expand Down
21 changes: 21 additions & 0 deletions src/server/aws-lambda/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020-present iMaeGoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions src/server/aws-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# AWS Lambda

Deploy Twikoo to [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html).

## Deploy with Terraform

```bash
cd terraform

# Init Terraform modules
terraform init

# Deploy to AWS
terraform apply
```
50 changes: 50 additions & 0 deletions src/server/aws-lambda/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const twikoo = require('twikoo-vercel')

/*
AWS Lambda compat layer for Vercel
https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html
*/

exports.handler = async function (event, context) {
process.env.VERCEL_URL = event.requestContext.domainName
process.env.TWIKOO_IP_HEADERS = JSON.stringify([
'headers.requestContext.http.sourceIp'
])
const result = {
statusCode: 204,
headers: {},
body: ''
}
const request = {
method: event.requestContext.http.method,
headers: event.headers,
body: {}
}
try {
if (event.isBase64Encoded) {
request.body = JSON.parse(Buffer.from(event.body, 'base64').toString('utf-8'))
} else {
request.body = JSON.parse(event.body)
}
} catch (e) {}
const response = {
status: function (code) {
result.statusCode = code
return this
},
json: function (json) {
result.headers['Content-Type'] = 'application/json'
result.body = JSON.stringify(json)
return this
},
end: function () {
return this
},
setHeader: function (k, v) {
result.headers[k] = v
return this
}
}
await twikoo(request, response)
return result
}
16 changes: 16 additions & 0 deletions src/server/aws-lambda/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "twikoo-aws-lambda",
"version": "1.6.35",
"description": "A simple comment system.",
"author": "imaegoo <hello@imaegoo.com> (https://github.com/imaegoo)",
"license": "MIT",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/twikoojs/twikoo.git"
},
"homepage": "https://twikoo.js.org",
"dependencies": {
"twikoo-vercel": "latest"
}
}
4 changes: 4 additions & 0 deletions src/server/aws-lambda/terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.terraform*
*.tfstate
*.tfstate.backup
builds/
34 changes: 34 additions & 0 deletions src/server/aws-lambda/terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0"
}
}
}

provider "aws" {
region = var.region
}

module "lambda_function" {
source = "terraform-aws-modules/lambda/aws"
version = "7.4.0"

function_name = "twikoo"
handler = "index.handler"
runtime = "nodejs20.x"
timeout = 10

source_path = "../src"

environment_variables = {
MONGODB_URI = var.mongodb_uri
}

create_lambda_function_url = true
}

output "lambda_function_url" {
value = module.lambda_function.lambda_function_url
}
9 changes: 9 additions & 0 deletions src/server/aws-lambda/terraform/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "region" {
description = "AWS region to deploy the function in."
default = "us-west-2"
}

variable "mongodb_uri" {
description = "MongoDB connection URI. The value will be passed to the Lambda function as environment variable MONGODB_URI."
sensitive = true
}

0 comments on commit 0cfdd9c

Please sign in to comment.