Skip to content

Commit a28a251

Browse files
feat(containers): memos example terraform
1 parent 49fda1b commit a28a251

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Memos application deployment using Terraform
2+
3+
[Memos](https://github.com/usememos/memos) is "an open-source, lightweight note-taking solution. The pain-less way to create your meaningful notes."
4+
5+
## Context
6+
7+
Memos requires a database and can be deployed on Serverless Containers. For cost optimised setup, for persistent storage this tutorial takes advantage of Scaleway Serverless SQL Database. Serverless Containers associated with Serverless Databases makes the setup cost efficient due to pay per use products.
8+
9+
## Requirements
10+
11+
- [Terraform installed](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli#install-terraform), more information about Scaleway Terraform Quickstart [here](https://www.scaleway.com/en/docs/terraform/quickstart/)
12+
- Terraform requires configuration to access Scaleway ressource, [follow this reference documentation](https://www.scaleway.com/en/docs/terraform/reference-content/scaleway-configuration-file/)
13+
14+
## Usage
15+
16+
`main.tf` will:
17+
* Create a new `memos` project
18+
* Create required IAM permissions and key
19+
* Create the Serverless SQL Database (postgres)
20+
* Create the Serverless Container running `memos` configured to use the created SQL Database
21+
22+
Deploy the project using Terraform:
23+
24+
```bash
25+
terraform init
26+
terraform apply
27+
```
28+
29+
## Delete changes
30+
31+
In case you need to delete everything created before (Database, Container, IAM configuration and Project), run:
32+
33+
```bash
34+
terraform destroy
35+
```

containers/memos-terraform/main.tf

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
terraform {
2+
required_providers {
3+
scaleway = {
4+
source = "scaleway/scaleway"
5+
}
6+
}
7+
required_version = ">= 0.13"
8+
}
9+
10+
# This Terraform configuration deploys a Memos instance on Scaleway.
11+
# Memos is an "open-source, lightweight note-taking solution. The pain-less way to create your meaningful notes. Your Notes, Your Way."
12+
#
13+
# This configuration creates the necessary resources, including:
14+
# - A Scaleway project
15+
# - An IAM application and policy for Serverless SQL Database access
16+
# - A Serverless SQL database (postgres)
17+
# - A Serverless Container to run Memos
18+
19+
# Create a new project "memos"
20+
resource "scaleway_account_project" "project" {
21+
name = "memos"
22+
}
23+
24+
# Create a new IAM Application called "memos"
25+
resource "scaleway_iam_application" "app" {
26+
name = "memos"
27+
}
28+
29+
# Create a new IAM Policy that will give the API Key access to the Database
30+
resource "scaleway_iam_policy" "db_access" {
31+
name = "memos_policy"
32+
description = "access to serverless database in project"
33+
application_id = scaleway_iam_application.app.id
34+
rule {
35+
project_ids = [scaleway_account_project.project.id]
36+
permission_set_names = ["ServerlessSQLDatabaseReadWrite"]
37+
}
38+
}
39+
40+
# Create API Key
41+
resource "scaleway_iam_api_key" "api_key" {
42+
application_id = scaleway_iam_application.app.id
43+
}
44+
45+
# Create a new SQL Serverless Database in the "memos" project
46+
resource "scaleway_sdb_sql_database" "database" {
47+
name = "memos"
48+
min_cpu = 0
49+
max_cpu = 1
50+
project_id = scaleway_account_project.project.id
51+
}
52+
53+
locals {
54+
# This connection string provides access to the Memos database.
55+
# It will be used to inject in the Serverless Container
56+
database_connection_string = format("postgres://%s:%s@%s",
57+
scaleway_iam_application.app.id,
58+
scaleway_iam_api_key.api_key.secret_key,
59+
trimprefix(scaleway_sdb_sql_database.database.endpoint, "postgres://"),
60+
)
61+
}
62+
63+
# Create namespace for the Serverless Container
64+
resource "scaleway_container_namespace" "main" {
65+
name = "memos-container"
66+
project_id = scaleway_account_project.project.id
67+
}
68+
69+
# Create the container with memos image. You can adjust cpu and memory
70+
# depending the traffic on your application.
71+
# Do not change the port
72+
resource "scaleway_container" "main" {
73+
name = "memos"
74+
namespace_id = scaleway_container_namespace.main.id
75+
registry_image = "neosmemo/memos:stable"
76+
port = 5230
77+
cpu_limit = 1000
78+
memory_limit = 2048
79+
min_scale = 0
80+
max_scale = 5
81+
privacy = "public"
82+
protocol = "http1"
83+
deploy = true
84+
85+
environment_variables = {
86+
# Specifies the database driver to use.
87+
"MEMOS_DRIVER" = "postgres"
88+
}
89+
secret_environment_variables = {
90+
# Provides the database connection string for Memos to connect to the database.
91+
"MEMOS_DSN" = local.database_connection_string
92+
}
93+
}

0 commit comments

Comments
 (0)