|
| 1 | +# Serverless Jobs for cleaning old snapshots |
| 2 | + |
| 3 | +This project shows how it's possible to automate tasks using Serverless Jobs. |
| 4 | + |
| 5 | +This simple example shows how to clean up snapshots after X days, it's useful to avoid a growing list of snapshots. |
| 6 | + |
| 7 | +# Set-up |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +- Scaleway Account |
| 12 | +- Docker daemon running to build the image |
| 13 | +- Container registry namespace created, for this example we assume that your namespace name is `jobs-snapshot-cleaner`: [doc here](https://www.scaleway.com/en/docs/containers/container-registry/how-to/create-namespace/) |
| 14 | + |
| 15 | +## Step 1 : Build and push to Container registry |
| 16 | + |
| 17 | +Serverless Jobs, like Serverless Containers (which are suited for HTTP applications), works |
| 18 | +with containers. So first, use your terminal reach this folder and run the following commands: |
| 19 | + |
| 20 | +```shell |
| 21 | +# First command is to login to container registry, you can find it in Scaleway console |
| 22 | +docker login rg.fr-par.scw.cloud/jobs-snapshot-cleaner -u nologin --password-stdin <<< "$SCW_SECRET_KEY" |
| 23 | + |
| 24 | +# Here we build the image to push |
| 25 | +docker build -t rg.fr-par.scw.cloud/jobs-snapshot-cleaner/jobs-snapshot-cleaner:v1 . |
| 26 | + |
| 27 | +## TIP: for Apple Silicon or other ARM processors, please use the following command as Serverless Jobs supports amd64 architecture |
| 28 | +# docker buildx build --platform linux/amd64 -t rg.fr-par.scw.cloud/jobs-snapshot-cleaner/jobs-snapshot-cleaner:v1 . |
| 29 | + |
| 30 | +# Push the image online to be used on Serverless Jobs |
| 31 | +docker push rg.fr-par.scw.cloud/jobs-snapshot-cleaner/jobs-snapshot-cleaner:v1 |
| 32 | +``` |
| 33 | +> [!TIP] |
| 34 | +> As we do not expose a web server and we do not require features such as auto-scaling, Serverless Jobs are perfect for this use case. |
| 35 | +
|
| 36 | +To check if everyting is ok, on the Scaleway Console you can verify if your tag is present in Container Registry. |
| 37 | + |
| 38 | +## Step 2: Creating the Job Definition |
| 39 | + |
| 40 | +On Scaleway Console on the following link you can create a new Job Definition: https://console.scaleway.com/serverless-jobs/jobs/create?region=fr-par |
| 41 | + |
| 42 | +1. On Container image, select the image you created in the step before. |
| 43 | +1. You can set the image name to something clear like `jobs-snapshot-cleaner` too. |
| 44 | +1. For the region you can select the one you prefer :) |
| 45 | +1. Regarding the resources you can keep the default values, this job is fast and do not require specific compute power or memory. |
| 46 | +1. To schedule your job for example every two days at 2am, you can set the cron to `0 2 */2 * *`. |
| 47 | +1. Important: advanced option, you need to set the following environment variables: |
| 48 | + |
| 49 | +> [!TIP] |
| 50 | +> For sensitive data like `SCW_ACCESS_KEY` and `SCW_SECRET_KEY` we recommend to inject them via Secret Manager, [more info here](https://www.scaleway.com/en/docs/serverless/jobs/how-to/reference-secret-in-job/). |
| 51 | +
|
| 52 | +- `SCW_DELETE_AFTER_DAYS`: number of days after the snapshots will be deleted |
| 53 | +- `SCW_PROJECT_ID`: project you want to clean up |
| 54 | +- `SCW_ZONE`: you need to give the ZONE of your snapshot you want to clean, like `fr-par-2` |
| 55 | +- `SCW_ACCESS_KEY`: your access key |
| 56 | +- `SCW_SECRET_KEY`: your secret key |
| 57 | +- `SCW_DEFAULT_ORGANIZATION_ID`: your organzation ID |
| 58 | + |
| 59 | +* Then click "create job" |
| 60 | + |
| 61 | +## Step 3: Run the job |
| 62 | + |
| 63 | +On your created Job Definition, just click the button "Run Job" and within seconds it should be successful. |
| 64 | + |
| 65 | +## Troubleshooting |
| 66 | + |
| 67 | +If your Job Run state goes in error, you can use the "Logs" tab in Scaleway Console to get more informations about the error. |
| 68 | + |
| 69 | +# Possible improvements |
| 70 | + |
| 71 | +You can exercice by adding the following features: |
| 72 | + |
| 73 | +- Add tags to exclude |
| 74 | +- Add alerts if a Job goes in error |
| 75 | +- Use secret manager |
| 76 | + |
| 77 | +# Additional content |
| 78 | + |
| 79 | +- [Jobs Documentation](https://www.scaleway.com/en/docs/serverless/jobs/how-to/create-job-from-scaleway-registry/) |
| 80 | +- [Other methods to deploy Jobs](https://www.scaleway.com/en/docs/serverless/jobs/reference-content/deploy-job/) |
| 81 | +- [Secret key / access key doc](https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/) |
| 82 | +- [CRON schedule help](https://www.scaleway.com/en/docs/serverless/jobs/reference-content/cron-schedules/) |
0 commit comments