Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deploy on AWS Lambda #703

Merged
merged 6 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Check failure on line 9 in src/server/aws-lambda/src/index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Extra semicolon
process.env.TWIKOO_IP_HEADERS = JSON.stringify([
'headers.requestContext.http.sourceIp'
wzyboy marked this conversation as resolved.
Show resolved Hide resolved
])
const result = {
statusCode: 204,
headers: {},
body: ''
}
const request = {
method: event.requestContext.http.method,
headers: event.headers,
wzyboy marked this conversation as resolved.
Show resolved Hide resolved
body: {}
}
try {
if (event.isBase64Encoded) {
request.body = JSON.parse(Buffer.from(event.body, 'base64').toString('utf-8'));

Check failure on line 25 in src/server/aws-lambda/src/index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Extra semicolon
} else {
request.body = JSON.parse(event.body);

Check failure on line 27 in src/server/aws-lambda/src/index.js

View workflow job for this annotation

GitHub Actions / build (18.x)

Extra semicolon
}
} 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
}
Loading