Skip to content

Commit 0d5afc7

Browse files
thomas-tacquetLaure-di
authored andcommitted
feat(serverless): fix somesyntax highlighting (scaleway#3895)
* feat(serverless): fix somesyntax highlighting * missing syntax type * missing tags * json to hcl * more tf to hcl
1 parent 1937e7f commit 0d5afc7

File tree

8 files changed

+117
-117
lines changed

8 files changed

+117
-117
lines changed

serverless/containers/how-to/secure-a-container.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ secret:
5757

5858
Add the following [resource description](https://registry.terraform.io/providers/scaleway/scaleway/latest/docs/resources/container) in Terraform:
5959

60-
```
60+
```hcl
6161
secret_environment_variables = { "key" = "secret" }
6262
```
6363

tutorials/create-serverless-scraping/index.mdx

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ We start by creating the scraper program, or the "data producer".
4747

4848
SQS credentials and queue URL are read by the function from environment variables. Those variables are set by Terraform as explained in [one of the next sections](#create-a-terraform-file-to-provision-the-necessary-scaleway-resources). *If you choose another deployment method, such as the [console](https://console.scaleway.com/), do not forget to set them.*
4949
```python
50-
queue_url = os.getenv('QUEUE_URL')
50+
queue_url = os.getenv('QUEUE_URL')
5151
sqs_access_key = os.getenv('SQS_ACCESS_KEY')
5252
sqs_secret_access_key = os.getenv('SQS_SECRET_ACCESS_KEY')
5353
```
@@ -65,10 +65,10 @@ We start by creating the scraper program, or the "data producer".
6565
Using the AWS python sdk `boto3`, connect to the SQS queue and push the `title` and `url` of articles published less than 15 minutes ago.
6666
```python
6767
sqs = boto3.client(
68-
'sqs',
69-
endpoint_url=SCW_SQS_URL,
70-
aws_access_key_id=sqs_access_key,
71-
aws_secret_access_key=sqs_secret_access_key,
68+
'sqs',
69+
endpoint_url=SCW_SQS_URL,
70+
aws_access_key_id=sqs_access_key,
71+
aws_secret_access_key=sqs_secret_access_key,
7272
region_name='fr-par')
7373

7474
for age, titleline in zip(ages, titlelines):
@@ -117,7 +117,7 @@ Next, let's create our consumer function. When receiving a message containing th
117117
Lastly, we write the information into the database. *To keep the whole process completely automatic the* `CREATE_TABLE_IF_NOT_EXISTS` *query is run each time. If you integrate the functions into an existing database, there is no need for it.*
118118
```python
119119
conn = None
120-
try:
120+
try:
121121
conn = pg8000.native.Connection(host=db_host, database=db_name, port=db_port, user=db_user, password=db_password, timeout=15)
122122

123123
conn.run(CREATE_TABLE_IF_NOT_EXISTS)
@@ -136,7 +136,7 @@ As explained in the [Scaleway Functions documentation](/serverless/functions/how
136136

137137
## Create a Terraform file to provision the necessary Scaleway resources
138138

139-
For the purposes of this tutorial, we show how to provision all resources via Terraform.
139+
For the purposes of this tutorial, we show how to provision all resources via Terraform.
140140

141141
<Message type="tip">
142142
If you do not want to use Terraform, you can also create the required resources via the [console](https://console.scaleway.com/), the [Scaleway API](https://www.scaleway.com/en/developers/api/), or any other [developer tool](https://www.scaleway.com/en/developers/). Remember that if you do so, you will need to set up environment variables for functions as previously specified. The following documentation may help create the required resources:
@@ -149,7 +149,7 @@ If you do not want to use Terraform, you can also create the required resources
149149
1. Create a directory called `terraform` (at the same level as the `scraper` and `consumer` directories created in the previous steps).
150150
2. Inside it, create a file called `main.tf`.
151151
3. In the file you just created, add the code below to set up the [Scaleway Terraform provider](https://registry.terraform.io/providers/scaleway/scaleway/latest/docs) and your Project:
152-
```
152+
```hcl
153153
terraform {
154154
required_providers {
155155
scaleway = {
@@ -167,7 +167,7 @@ If you do not want to use Terraform, you can also create the required resources
167167
}
168168
```
169169
4. Still in the same file, add the code below to provision the SQS resources: SQS activation for the project, separate credentials with appropriate permissions for producer and consumer, and an SQS queue:
170-
```
170+
```hcl
171171
resource "scaleway_mnq_sqs" "main" {
172172
project_id = scaleway_account_project.mnq_tutorial.id
173173
}
@@ -202,7 +202,7 @@ If you do not want to use Terraform, you can also create the required resources
202202
}
203203
```
204204
5. Add the code below to provision the Managed Database for PostgreSQL resources. Note that here we are creating a random password and using it for the default and worker user:
205-
```
205+
```hcl
206206
resource "random_password" "dev_mnq_pg_exporter_password" {
207207
length = 16
208208
special = true
@@ -219,7 +219,7 @@ If you do not want to use Terraform, you can also create the required resources
219219
node_type = "db-dev-s"
220220
engine = "PostgreSQL-15"
221221
is_ha_cluster = false
222-
disable_backup = true
222+
disable_backup = true
223223
user_name = "mnq_initial_user"
224224
password = random_password.dev_mnq_pg_exporter_password.result
225225
}
@@ -240,7 +240,7 @@ If you do not want to use Terraform, you can also create the required resources
240240
}
241241
242242
resource "scaleway_rdb_database" "main" {
243-
instance_id = scaleway_rdb_instance.main.id
243+
instance_id = scaleway_rdb_instance.main.id
244244
name = "hn-database"
245245
}
246246
@@ -252,14 +252,14 @@ If you do not want to use Terraform, you can also create the required resources
252252
}
253253
254254
resource "scaleway_rdb_privilege" "mnq_user_role" {
255-
instance_id = scaleway_rdb_instance.main.id
255+
instance_id = scaleway_rdb_instance.main.id
256256
user_name = scaleway_rdb_user.worker.name
257257
database_name = scaleway_rdb_database.main.name
258258
permission = "all"
259259
}
260260
```
261261
6. Add the code below to provision the functions resources. First, activate the namespace, then locally zip the code and create the functions in the cloud. Note that we are referencing variables from other resources, to completely automate the deployment process:
262-
```
262+
```hcl
263263
locals {
264264
scraper_folder_path = "../scraper"
265265
consumer_folder_path = "../consumer"
@@ -354,17 +354,17 @@ If you do not want to use Terraform, you can also create the required resources
354354
}
355355
}
356356
```
357-
Note that a folder `archives` needs to be created manually if you started from scratch. It is included in the git repository.
358-
7. Add the code below to provision the triggers resources. The cron trigger activates at the minutes `[0, 15, 30, 45]` of every hour. No arguments are passed, but we could do so by specifying them in JSON format in the `args` parameter.
359-
```
357+
Note that a folder `archives` needs to be created manually if you started from scratch. It is included in the git repository.
358+
7. Add the code below to provision the triggers resources. The cron trigger activates at the minutes `[0, 15, 30, 45]` of every hour. No arguments are passed, but we could do so by specifying them in JSON format in the `args` parameter.
359+
```hcl
360360
resource "scaleway_function_cron" "scraper_cron" {
361-
function_id = scaleway_function.scraper.id
361+
function_id = scaleway_function.scraper.id
362362
schedule = "0,15,30,45 * * * *"
363363
args = jsonencode({})
364364
}
365365
366366
resource "scaleway_function_trigger" "consumer_sqs_trigger" {
367-
function_id = scaleway_function.consumer.id
367+
function_id = scaleway_function.consumer.id
368368
name = "hn-sqs-trigger"
369369
sqs {
370370
project_id = scaleway_mnq_sqs.main.project_id
@@ -378,22 +378,22 @@ Terraform makes this very straightforward. To provision all the resources and ge
378378
```
379379
cd terraform
380380
terraform init
381-
terraform plan
381+
terraform plan
382382
terraform apply
383383
```
384384

385385
### How to check that everything is working correctly
386386

387387
Go to the [Scaleway console](https://console.scaleway.com/), and check the logs and metrics for Serverless Functions' execution and Messaging and Queuing SQS queue statistics.
388388

389-
To make sure the data is correctly stored in the database, you can [connect to it directly](/managed-databases/postgresql-and-mysql/how-to/connect-database-instance/) via a CLI tool such as `psql`.
389+
To make sure the data is correctly stored in the database, you can [connect to it directly](/managed-databases/postgresql-and-mysql/how-to/connect-database-instance/) via a CLI tool such as `psql`.
390390
Retrieve the instance IP and port of your Managed Database from the console, under the [Managed Database section](https://console.scaleway.com/rdb/instances).
391391
Use the following command to connect to your database. When prompted for a password, you can find it by running `terraform output -json`.
392392
```
393393
psql -h <DB_INSTANCE_IP> --port <DB_INSTANCE_PORT> -d hn-database -U worker
394394
```
395395

396-
When you are done testing, don't forget to clean up! To do so, run:
396+
When you are done testing, don't forget to clean up! To do so, run:
397397
```
398398
cd terraform
399399
terraform destroy
@@ -405,7 +405,7 @@ We have shown how to asynchronously decouple the producer and the consumer using
405405
While the volume of data processed in this example is quite small, thanks to the Messaging and Queuing SQS queue's robustness and the auto-scaling capabilities of the Serverless Functions, you can adapt this example to manage larger workloads.
406406

407407
Here are some possible extensions to this basic example:
408-
- Replace the simple proposed logic with your own. What about counting how many times some keywords (e.g: copilot, serverless, microservice) appear in Hacker News articles?
408+
- Replace the simple proposed logic with your own. What about counting how many times some keywords (e.g: copilot, serverless, microservice) appear in Hacker News articles?
409409
- Define multiple cron triggers for different websites and pass the website as an argument to the function. Or, create multiple functions that feed the same queue.
410410
- Use a [Serverless Container](/serverless/containers/quickstart/) instead of the consumer function, and use a command line tool such as `htmldoc` or `pandoc` to convert the scraped articles to PDF and upload the result to a [Scaleway Object Storage](/storage/object/quickstart/) S3 bucket.
411411
- Replace the Managed Database for PostgreSQL with a [Scaleway Serverless Database](/serverless/sql-databases/quickstart/), so that all the infrastructure lives in the serverless ecosystem! *Note that at the moment there is no Terraform support for Serverless Database, hence the choice here to use Managed Database for PostgreSQL*.

tutorials/deploy-laravel-on-serverless-containers/index.mdx

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ content:
77
paragraph: This tutorial provides a step-by-step guide for deploying a containerized Laravel application on the Scaleway cloud platform.
88
tags: laravel php docker nginx fpm
99
hero: assets/scaleway-umami.webp
10-
categories:
10+
categories:
1111
- containers
1212
- container-registry
1313
dates:
@@ -42,7 +42,7 @@ Laravel applications make use of [queues](https://laravel.com/docs/10.x/queues)
4242
2. Create a queue. In this example, we create a `Standard` queue (At-least-once delivery, the order of messages is not preserved) with the default parameters. This queue will be the default queue used by our application.
4343

4444
<Lightbox src="scaleway-serverless-messaging-queue.webp" alt="Scaleway Console Messaging and Queuing Queue" />
45-
45+
4646
3. Generate credentials. In this example, we generate the credentials with `read` and `write` access.
4747

4848
<Lightbox src="scaleway-serverless-messaging-credential.webp" alt="Scaleway Console Messaging and Queuing Credential" />
@@ -53,7 +53,7 @@ In this section, we will focus on building the containerized image. With Docker,
5353

5454
1. Create the Dockerfile: we create a `Dockerfile` which is a text file that contains instructions for Docker to build the image. In this example, we specify the base image as `php:fpm-alpine`, install and enable the necessary php dependencies with [`install-php-extensions`](https://github.com/mlocati/docker-php-extension-installer), and determine the commands to be executed at startup.
5555

56-
```
56+
```dockerfile
5757
# Dockerfile
5858
FROM --platform=linux/amd64 php:8.2.6-fpm-alpine3.18
5959

@@ -84,9 +84,9 @@ In this section, we will focus on building the containerized image. With Docker,
8484
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]
8585
```
8686
2. Create the supervisor configuration file. [Supervisor](http://supervisord.org/) is a reliable and efficient process control system for managing and monitoring processes. This is used as multiple processes are running within the container. In this example, we create a `stubs/supervisor/supervisord.conf` file with the following configuration to start the web server Nginx, the php-fpm pool, and 5 workers:
87-
```
87+
```conf
8888
# stubs/supervisor/supervisord.conf
89-
[supervisord]
89+
[supervisord]
9090
nodaemon=true
9191
logfile=/dev/null
9292
logfile_maxbytes=0
@@ -128,43 +128,43 @@ In this section, we will focus on building the containerized image. With Docker,
128128

129129
3. Create web server configuration files. Nginx will be used to serve the static assets and to forward the requests to the php-fpm pool for processing. In this example, we create the following configuration files `stubs/nginx/http.d/default.conf` and `stubs/nginx/nginx.conf`.
130130

131-
```
131+
```hcl
132132
# stubs/nginx/http.d/default.conf
133133
server {
134134
listen 80;
135135
listen [::]:80;
136136
server_name _;
137137
root /var/www/html/public;
138-
138+
139139
add_header X-Frame-Options "SAMEORIGIN";
140140
add_header X-Content-Type-Options "nosniff";
141-
141+
142142
index index.php;
143-
143+
144144
charset utf-8;
145-
145+
146146
location / {
147147
try_files $uri $uri/ /index.php?$query_string;
148148
}
149-
149+
150150
location = /favicon.ico { access_log off; log_not_found off; }
151151
location = /robots.txt { access_log off; log_not_found off; }
152-
152+
153153
error_page 404 /index.php;
154-
154+
155155
location ~ \.php$ {
156156
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
157157
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
158158
include fastcgi_params;
159159
}
160-
160+
161161
location ~ /\.(?!well-known).* {
162162
deny all;
163163
}
164164
}
165165
```
166166

167-
```
167+
```hcl
168168
# stubs/nginx/nginx.conf
169169
error_log /var/log/nginx/error.log notice;
170170
events {
@@ -183,11 +183,11 @@ In this section, we will focus on building the containerized image. With Docker,
183183
pid /var/run/nginx.pid;
184184
user nginx;
185185
worker_processes auto;
186-
```
186+
```
187187

188188
4. Create the php-fpm configuration file. The configuration `stubs/php/php-fpm.d/zz-docker.conf` file should be created, and the php-fpm pool configured to render the dynamic pages of the Laravel application. Depending on the needs of your application, you might have to fine-tune the configuration of the process manager. Further information is available in the [php manual](https://www.php.net/manual/en/install.fpm.configuration.php).
189-
190-
```
189+
190+
```conf
191191
[global]
192192
daemonize = no
193193
@@ -197,27 +197,27 @@ In this section, we will focus on building the containerized image. With Docker,
197197
listen.group = www-data
198198
listen.mode = 0660
199199
200-
pm = dynamic
201-
pm.max_children = 75
202-
pm.start_servers = 10
203-
pm.min_spare_servers = 5
204-
pm.max_spare_servers = 20
200+
pm = dynamic
201+
pm.max_children = 75
202+
pm.start_servers = 10
203+
pm.min_spare_servers = 5
204+
pm.max_spare_servers = 20
205205
pm.process_idle_timeout = 10s
206206
```
207207

208208
5. Build the docker image.
209-
```
209+
```sh
210210
docker build -t my-image .
211211
```
212212

213-
## Creating Container Registry
213+
## Creating Container Registry
214214

215215
1. [Create a Scaleway Container Registry namespace](/containers/container-registry/how-to/create-namespace/) in the `PAR` region. Set the visibility to `Private` to avoid having your container retrieved without proper authentication and authorization.
216216

217217
<Lightbox src="scaleway-serverless-containers-namespace.webp" alt="Scaleway Console Container Registry Namespace" />
218218

219219
2. Run the following command in your local terminal to log in to the newly created Container Registry.
220-
```
220+
```sh
221221
docker login rg.fr-par.scw.cloud/namespace-zen-feistel -u nologin --password-stdin <<< "$SCW_SECRET_KEY"
222222
```
223223

@@ -226,8 +226,8 @@ In this section, we will focus on building the containerized image. With Docker,
226226
</Message>
227227

228228
3. Tag the image and push it to the Container Registry namespace.
229-
230-
```
229+
230+
```sh
231231
docker tag my-image rg.fr-par.scw.cloud/namespace-zen-feistel/my-image:v1
232232
docker push rg.fr-par.scw.cloud/namespace-zen-feistel/my-image:v1
233233
```
@@ -237,7 +237,7 @@ In this section, we will focus on building the containerized image. With Docker,
237237
The Scaleway documentation website provides a Quickstart on how to [create and manage a Serverless Container Namespace](/serverless/containers/quickstart/).
238238

239239
1. Create a Serverless Containers namespace. In this example, we create the `my-laravel-application` namespace and configure the environment variables and secrets necessary for our application. In particular, we must add all the variables needed to connect to the previously created SQS/SNS queue.
240-
240+
241241
By default, Laravel expects the following environment variables/secrets to be filled in for queues: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_DEFAULT_REGION`, `QUEUE_CONNECTION`, `SQS_PREFIX` and `SQS_QUEUE`.
242242

243243
2. Deploy the application. Click **+ Deploy a Container** once the namespace is created, and follow the instructions of the creation wizard. Select the registry namespace and the previously uploaded Docker image and configure the listening port (the Nginx web server is listening on port 80). For the CPU and memory, define at least 560mVPCU and 256 MB respectively. To reduce the limitations due to [cold start](/serverless/containers/concepts/#cold-start), we will run at least 1 instance.
@@ -274,7 +274,7 @@ By default, some metrics will be available in the Scaleway console. However, to
274274

275275
To test the load on the application, there is a basic test route that pushes a job into the queue and returns the welcome page.
276276

277-
``` php
277+
```php
278278
# routes/web.php
279279
use App\Jobs\ProcessPodcast;
280280

@@ -287,7 +287,7 @@ Route::get('/test', function () {
287287
```
288288
The job does nothing but wait for a couple of seconds.
289289

290-
``` php
290+
```php
291291
# app/Jobs/ProcessPodcast
292292

293293
class ProcessPodcast implements ShouldQueue
@@ -300,11 +300,11 @@ class ProcessPodcast implements ShouldQueue
300300
```
301301
Then, use `hey` to send 400 requests (20 concurrent requests) to this route.
302302

303-
```
303+
```sh
304304
hey -n 400 -q 20 https://example.com/test
305305
```
306306

307-
We can see that our deployment is not sufficiently sized to handle such workload and the response times are far from ideal.
307+
We can see that our deployment is not sufficiently sized to handle such workload and the response times are far from ideal.
308308

309309
```
310310
Response time histogram:

0 commit comments

Comments
 (0)