-
Notifications
You must be signed in to change notification settings - Fork 325
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
8 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.terraform* | ||
*.tfstate | ||
*.tfstate.backup | ||
builds/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |