Skip to content

Commit a033097

Browse files
feat(jobs): simplify jobs snapshot tutorial
1 parent ce03d6f commit a033097

File tree

1 file changed

+10
-174
lines changed

1 file changed

+10
-174
lines changed

tutorials/snapshot-instances-jobs/index.mdx

Lines changed: 10 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -22,200 +22,36 @@ Serverless Jobs are perfectly adapted for these autonomous tasks, as we do not n
2222

2323
- A Scaleway account logged into the [console](https://console.scaleway.com)
2424
- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization
25-
- Created a [Container Registry namespace](/container-registry/how-to/create-namespace/).
2625
- Created an [Instance](/instances/how-to/create-an-instance/)
27-
- Installed and started the [Docker daemon](https://docs.docker.com/engine/install/) to build the image.
28-
29-
## Creating the snapshot generator files
30-
31-
<Message type="note">
32-
You can also download the work files by cloning our [Scaleway Serverless examples repository](https://github.com/scaleway/serverless-examples/tree/main/jobs/instances-snapshot).
33-
</Message>
34-
35-
1. Create a file named `main.go`, and add the code below to it:
36-
37-
```go
38-
package main
39-
40-
import (
41-
"fmt"
42-
"os"
43-
"time"
44-
45-
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
46-
"github.com/scaleway/scaleway-sdk-go/scw"
47-
)
48-
49-
const (
50-
envOrgID = "SCW_DEFAULT_ORGANIZATION_ID"
51-
envAccessKey = "SCW_ACCESS_KEY"
52-
envSecretKey = "SCW_SECRET_KEY"
53-
envInstanceID = "INSTANCE_ID"
54-
envInstanceZone = "INSTANCE_ZONE"
55-
)
56-
57-
func main() {
58-
fmt.Println("creating snapshot of instance...")
59-
60-
// Create a Scaleway client with credentials from environment variables.
61-
client, err := scw.NewClient(
62-
// Get your organization ID at https://console.scaleway.com/organization/settings
63-
scw.WithDefaultOrganizationID(os.Getenv(envOrgID)),
64-
65-
// Get your credentials at https://console.scaleway.com/iam/api-keys
66-
scw.WithAuth(os.Getenv(envAccessKey), os.Getenv(envSecretKey)),
67-
68-
// Get more about our availability
69-
// zones at https://www.scaleway.com/en/docs/account/reference-content/products-availability/
70-
scw.WithDefaultRegion(scw.RegionFrPar),
71-
)
72-
if err != nil {
73-
panic(err)
74-
}
75-
76-
// Create SDK objects for Scaleway Instance product
77-
instanceAPI := instance.NewAPI(client)
78-
79-
if err := createSnapshots(instanceAPI); err != nil {
80-
panic(err)
81-
}
82-
}
83-
84-
func createSnapshots(instanceAPI *instance.API) error {
85-
gotInstance, err := instanceAPI.GetServer(&instance.GetServerRequest{
86-
ServerID: os.Getenv("INSTANCE_ID"),
87-
Zone: scw.Zone(os.Getenv("INSTANCE_ZONE")),
88-
})
89-
if err != nil {
90-
return fmt.Errorf("error while getting instance %w", err)
91-
}
92-
93-
now := time.Now().Format(time.DateOnly)
94-
95-
for _, volume := range gotInstance.Server.Volumes {
96-
snapshotName := fmt.Sprintf("snap-vol-%s-%s-%s",
97-
volume.VolumeType.String(),
98-
now,
99-
os.Getenv(envInstanceZone))
100-
101-
snapshotResp, err := instanceAPI.CreateSnapshot(&instance.CreateSnapshotRequest{
102-
Name: snapshotName,
103-
VolumeID: &volume.ID,
104-
VolumeType: instance.SnapshotVolumeType(volume.VolumeType),
105-
Zone: scw.Zone(os.Getenv(envInstanceZone)),
106-
})
107-
if err != nil {
108-
return fmt.Errorf("error while creating snapshot %w", err)
109-
}
110-
111-
fmt.Println("created snapshot ", snapshotResp.Snapshot.ID)
112-
}
113-
114-
return nil
115-
}
116-
117-
func init() {
118-
mandatoryVariables := [...]string{envOrgID, envAccessKey, envSecretKey, envInstanceID, envInstanceZone}
119-
120-
for idx := range mandatoryVariables {
121-
if os.Getenv(mandatoryVariables[idx]) == "" {
122-
panic("missing environment variable " + mandatoryVariables[idx])
123-
}
124-
}
125-
}
126-
127-
```
128-
129-
2. Create a file called `go.mod`, and add the code below to it:
130-
131-
```go
132-
module github.com/scaleway/serverless-examples/jobs/instances-snapshot
133-
134-
go 1.23
135-
136-
require github.com/scaleway/scaleway-sdk-go v1.0.0-beta.30
137-
138-
require gopkg.in/yaml.v2 v2.4.0 // indirect
139-
```
140-
141-
3. Run the following command to download the required dependencies:
142-
143-
```go
144-
go get
145-
```
146-
147-
## Building and pushing the image to Container Registry
148-
149-
Serverless Jobs rely on containers to run in the cloud and therefore require a [container image](/serverless-jobs/concepts/#container-image) hosted in the cloud using [Scaleway Container Registry](/container-registry/).
150-
151-
1. Create a `Dockerfile`, and add the following code to it:
152-
153-
```dockerfile
154-
# Using apline/golang image
155-
FROM golang:1.23-alpine
156-
157-
# Set destination for COPY
158-
WORKDIR /app
159-
160-
# Copy required files
161-
COPY go.mod ./
162-
COPY go.sum ./
163-
COPY *.go ./
164-
165-
# Build the executable
166-
RUN go build -o /jobs-snapshot
167-
168-
# Run the executable
169-
CMD [ "/jobs-snapshot" ]
170-
```
171-
172-
2. Run the following command in a terminal to connect to your Container Registry namespace. Do not forget to edit the command with your namespace name.
173-
174-
```shell
175-
docker login rg.fr-par.scw.cloud/your-namespace-name -u nologin --password-stdin <<< "$SCW_SECRET_KEY"
176-
```
177-
178-
3. Run the following command to build the container image locally:
179-
180-
```sh
181-
docker build -t rg.fr-par.scw.cloud/your-namespace-name/jobs-snapshot:v1 .
182-
183-
## TIP: for Apple Silicon or other ARM processors, please use the following command as Serverless Jobs supports amd64 architecture
184-
# docker build --platform linux/amd64 -t rg.fr-par.scw.cloud/jobs-snapshot/jobs-snapshot:v1 .
185-
```
186-
187-
4. Run the following command to push the container image to the registry:
188-
189-
```sh
190-
docker push rg.fr-par.scw.cloud/your-namespace-name/jobs-snapshot:v1
191-
```
192-
193-
Your image and its tag now appear in the [Container Registry in the Scaleway console](https://console.scaleway.com/registry/namespaces).
19426

19527
## Creating the Job Definition
19628

19729
1. In the [Scaleway console](https://console.scaleway.com), click **Jobs** in the **Serverless** section of the side menu. The jobs page displays.
19830

19931
2. Click **Create job**. The job creation wizard displays.
20032

201-
3. Select the **Scaleway** Container Registry, then select your Container Registry namespace from the drop-down list, and the container image and tag.
33+
3. **Container Image** select **External** and in **Image URL** set: `scaleway/cli:latest`
20234

20335
4. Enter a name or use the automatically generated one.
20436

20537
5. Select the region in which your job will be created.
20638

207-
6. Keep the default **resources** values, as this job requires little compute capabilities.
39+
6. Keep default **resources** values, as this job requires little compute capabilities.
20840

209-
7. Set the **cron schedule** to `0 2 * * *` and select the relevant time zone to run the job every day at 2:00 a.m. Refer to the [cron schedules documentation](/serverless-jobs/reference-content/cron-schedules/) for more information.
41+
7. Set **cron schedule** to `0 2 * * *` and select relevant time zone to run the job every day at 2:00 a.m. Refer to the [cron schedules documentation](/serverless-jobs/reference-content/cron-schedules/) for more information.
21042

21143
8. Define the following environment variables:
212-
- `INSTANCE_ID`: the ID of the Instance you want to snapshot
213-
- `INSTANCE_ZONE`: the [Availabilitiy Zone](/instances/concepts/#availability-zone) of your Instance (e.g. `fr-par-2`)
21444
- `SCW_ACCESS_KEY`: your API access key
21545
- `SCW_SECRET_KEY`: your API secret key
46+
- `SCW_DEFAULT_PROJECT_ID`: your Project ID
21647
- `SCW_DEFAULT_ORGANIZATION_ID`: your Organization ID
48+
- `SCW_DEFAULT_REGION`: Concerned region
49+
50+
For more details about variables used by `cli` please refer to [cli config documentation](https://github.com/scaleway/scaleway-cli/blob/master/docs/commands/config.md).
51+
52+
9. In **Execution** tab, define desired command `/scw block snapshot create volume-id=11111111-1111-1111-1111-111111111111` (replace the ID with your desired volume ID).
21753

218-
9. Click **Create job**.
54+
10. Click **Create job**.
21955

22056
## Running the job
22157

0 commit comments

Comments
 (0)