-
Notifications
You must be signed in to change notification settings - Fork 12
feat(jobs): add Serverless Jobs Terraform example #75
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,47 @@ | ||
| # Serverless Jobs Hello World with Terraform | ||
|
|
||
| This example demonstrates how to set up a Scaleway [Serverless Job](https://www.scaleway.com/en/serverless-jobs/) using [Terraform](https://www.terraform.io/). | ||
|
|
||
| It builds a custom image locally, pushes this to the Scaleway registry, then creates a job that runs this image on a schedule. | ||
|
|
||
| ## Requirements | ||
|
|
||
| This example assumes you are familiar with how Serverless Jobs work. If needed, you can check [Scaleway's official documentation](https://www.scaleway.com/en/docs/serverless/jobs/quickstart/) | ||
|
|
||
| This example uses Terraform. Please set up your local environment as outlined in the docs for the [Scaleway Terraform Provider](https://registry.terraform.io/providers/scaleway/scaleway/latest/docs). | ||
|
|
||
| You will also need a Scaleway API key, which can be configured using [Scaleway IAM](https://www.scaleway.com/en/docs/identity-and-access-management/iam/how-to/create-api-keys/). If you are using IAM policies, make sure the key has the permissions: `ServerlessJobsFullAccess`, `ContainerRegistryFullAccess`. | ||
|
|
||
| ## Setup | ||
|
|
||
| Once your environment is set up, you need to export some environment variables to use your API key: | ||
|
|
||
| ```console | ||
| export TF_VAR_access_key=<your api key> | ||
| export TF_VAR_secret_key=<your secret key> | ||
| export TF_VAR_project_id=<your project ID> | ||
| ``` | ||
|
|
||
| From there, you can run the following to set up your job: | ||
|
|
||
| ```console | ||
| cd terraform | ||
|
|
||
| terraform init | ||
|
|
||
| terraform plan | ||
|
|
||
| terraform apply | ||
| ``` | ||
|
|
||
| You can then view your Job Definitions in the [Scaleway Console](https://console.scaleway.com/serverless-jobs/jobs). | ||
|
|
||
| The Job is set to run on a schedule, once every 5 minutes. Once the next schedule has run, you will see a "Job Run" listed for your Job Definition. | ||
|
|
||
| *Note* this job will keep running every 5 minutes, so you need to make sure you delete the job by running: | ||
|
|
||
| ```console | ||
| cd terraform | ||
|
|
||
| terraform destroy | ||
| ``` |
This file contains hidden or 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,7 @@ | ||
| FROM alpine:3.19 | ||
|
|
||
| RUN apk add --no-cache --upgrade bash | ||
|
|
||
| COPY hello.sh . | ||
|
|
||
| CMD ["sh", "hello.sh"] |
This file contains hidden or 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,6 @@ | ||
| #!/bin/bash | ||
|
|
||
| set -e | ||
|
|
||
| echo "-- Job run at $(date) --" | ||
| echo "$MESSAGE" |
This file contains hidden or 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 @@ | ||
| resource "scaleway_registry_namespace" "main" { | ||
| name = "jobs-tf-hello" | ||
| region = var.region | ||
| project_id = var.project_id | ||
| } | ||
|
|
||
| resource "docker_image" "main" { | ||
| name = "${scaleway_registry_namespace.main.endpoint}/jobs-hello:${var.image_version}" | ||
| build { | ||
| context = "${path.cwd}/../image" | ||
| } | ||
|
|
||
| provisioner "local-exec" { | ||
| command = "docker push ${docker_image.main.name}" | ||
| } | ||
| } |
This file contains hidden or 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,18 @@ | ||
| resource "scaleway_job_definition" "main" { | ||
| name = "hello_jobs" | ||
| cpu_limit = 1000 | ||
| memory_limit = 1024 | ||
|
|
||
| image_uri = docker_image.main.name | ||
|
|
||
| command = "sh hello.sh" | ||
|
|
||
| env = { | ||
| "MESSAGE" : "Hello from your Job!", | ||
| } | ||
|
|
||
| cron { | ||
| schedule = "*/5 * * * *" | ||
| timezone = "Europe/Paris" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| output "job_id" { | ||
| value = scaleway_job_definition.main.id | ||
| } | ||
|
|
This file contains hidden or 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 @@ | ||
| provider "scaleway" { | ||
| region = var.region | ||
| access_key = var.access_key | ||
| secret_key = var.secret_key | ||
| project_id = var.project_id | ||
| } | ||
|
|
||
| provider "docker" { | ||
| host = "unix:///var/run/docker.sock" | ||
|
|
||
| registry_auth { | ||
| address = scaleway_registry_namespace.main.endpoint | ||
| username = "nologin" | ||
| password = var.secret_key | ||
| } | ||
| } |
This file contains hidden or 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,22 @@ | ||
| variable "access_key" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "secret_key" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "project_id" { | ||
| type = string | ||
| } | ||
|
|
||
| variable "image_version" { | ||
| type = string | ||
| default = "0.0.3" | ||
| } | ||
|
|
||
| variable "region" { | ||
| type = string | ||
| default = "fr-par" | ||
| } | ||
|
|
This file contains hidden or 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,13 @@ | ||
| terraform { | ||
| required_providers { | ||
| scaleway = { | ||
| source = "scaleway/scaleway" | ||
| version = ">= 2.38.2" | ||
| } | ||
| docker = { | ||
| source = "kreuzwerker/docker" | ||
| version = "3.0.2" | ||
| } | ||
| } | ||
| required_version = ">= 0.13" | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clean-up