|
1 | 1 | --- |
2 | 2 | meta: |
3 | | - title: Create snapshots of an Instance with Serverless Jobs |
4 | | - description: This step-by-step tutorial will help you automate the creation of snapshots of your Instance using Serverless Jobs |
| 3 | + title: Create snapshots of an Instance with Serverless Jobs and the Scaleway CLI |
| 4 | + description: This step-by-step tutorial will help you automate the creation of snapshots of your Instance using Serverless Jobs and the Scaleway CLI |
5 | 5 | content: |
6 | | - h1: Create snapshots of an Instance with Serverless Jobs |
7 | | - paragraph: This step-by-step tutorial will help you automate the creation of snapshots of your Instance using Serverless Jobs |
8 | | -tags: serverless jobs instance snapshot backup image disk storage |
| 6 | + h1: Create snapshots of an Instance with Serverless Jobs and the Scaleway CLI |
| 7 | + paragraph: This step-by-step tutorial will help you automate the creation of snapshots of your Instance using Serverless Jobs and the Scaleway CLI |
| 8 | +tags: serverless jobs instance snapshot backup image disk storage cli |
9 | 9 | categories: |
10 | 10 | - instances |
11 | 11 | - jobs |
12 | 12 | dates: |
13 | 13 | validation: 2025-12-03 |
14 | 14 | posted: 2024-06-19 |
15 | 15 | --- |
16 | | - |
17 | 16 | [Scaleway Serverless Jobs](/serverless-jobs/quickstart/) allows you to create and automate recurring tasks. This tutorial will guide you through the process of creating snapshots of a [Scaleway Instance](/instances/quickstart/) on a recurring schedule using a Serverless Job. |
18 | 17 |
|
19 | | -Serverless Jobs are perfectly adapted for these autonomous tasks, as we do not need autoscaling or exposure via a web server. Refer to the [differences between jobs, containers and functions](/serverless-jobs/reference-content/difference-jobs-functions-containers/) for more information. |
| 18 | +Serverless Jobs are perfectly adapted for these autonomous tasks, as we do not need autoscaling or exposure via a web server. Refer to the [documentation on differences between jobs, containers, and functions](/serverless-jobs/reference-content/difference-jobs-functions-containers/) for more information. |
20 | 19 |
|
21 | 20 | <Macro id="requirements" /> |
22 | 21 |
|
23 | | -- A Scaleway account logged into the [console](https://console.scaleway.com) |
24 | | -- [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/). |
26 | | -- 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 |
| 22 | +- A Scaleway account logged into the [console](https://console.scaleway.com). |
| 23 | +- [Owner](/iam/concepts/#owner) status or [IAM permissions](/iam/concepts/#permission) allowing you to perform actions in the intended Organization. |
| 24 | +- Created an [Instance](/instances/how-to/create-an-instance/). |
135 | 25 |
|
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). |
194 | | - |
195 | | -## Creating the Job Definition |
| 26 | +## Creating the job definition |
196 | 27 |
|
197 | 28 | 1. In the [Scaleway console](https://console.scaleway.com), click **Jobs** in the **Serverless** section of the side menu. The jobs page displays. |
198 | 29 |
|
199 | 30 | 2. Click **Create job**. The job creation wizard displays. |
200 | 31 |
|
201 | | -3. Select the **Scaleway** Container Registry, then select your Container Registry namespace from the drop-down list, and the container image and tag. |
| 32 | +3. For **Container Image**, select **External**, and in **Image URL**, set: `scaleway/cli:latest`. |
202 | 33 |
|
203 | 34 | 4. Enter a name or use the automatically generated one. |
204 | 35 |
|
205 | 36 | 5. Select the region in which your job will be created. |
206 | 37 |
|
207 | | -6. Keep the default **resources** values, as this job requires little compute capabilities. |
| 38 | +6. Keep the default **resources** values, as this job requires little compute capability. |
208 | 39 |
|
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. |
| 40 | +7. Set **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. |
210 | 41 |
|
211 | 42 | 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`) |
214 | | - - `SCW_ACCESS_KEY`: your API access key |
215 | | - - `SCW_SECRET_KEY`: your API secret key |
216 | | - - `SCW_DEFAULT_ORGANIZATION_ID`: your Organization ID |
| 43 | + - `SCW_ACCESS_KEY`: your API access key. |
| 44 | + - `SCW_SECRET_KEY`: your API secret key. |
| 45 | + - `SCW_DEFAULT_PROJECT_ID`: your Project ID. |
| 46 | + - `SCW_DEFAULT_ORGANIZATION_ID`: your Organization ID. |
| 47 | + - `SCW_DEFAULT_REGION`: concerned region. |
| 48 | + |
| 49 | +<Message type="note"> |
| 50 | + We recommend using Secret Manager to store the `SCW_ACCESS_KEY` and `SCW_SECRET_KEY`. |
| 51 | +</Message> |
| 52 | + |
| 53 | +For more details about variables used by `cli`, refer to the [CLI config documentation](https://github.com/scaleway/scaleway-cli/blob/master/docs/commands/config.md). |
| 54 | + |
| 55 | +9. In the **Execution** tab, define the desired command: `/scw block snapshot create volume-id=11111111-1111-1111-1111-111111111111` (replace the ID with your desired volume ID). |
217 | 56 |
|
218 | | -9. Click **Create job**. |
| 57 | +10. Click **Create job**. |
219 | 58 |
|
220 | 59 | ## Running the job |
221 | 60 |
|
222 | | -From the **Overview** tab of the Serverless job you just created, click, **Actions**, then select **Run job** from the contextual menu. |
| 61 | +From the **Overview** tab of the Serverless job you just created, click **Actions**, then select **Run job** from the contextual menu. |
223 | 62 |
|
224 | 63 | The execution appears in the **Job runs** section. You can access the logs of your job by clicking <Icon name="more" /> next to the job run ID, and selecting **See on Cockpit**. |
225 | 64 |
|
226 | 65 | ## Possible improvements |
227 | 66 |
|
228 | 67 | This tutorial is a lightweight example of how to create recurring snapshots of an Instance. You can go further by: |
229 | | -- Using it to manage all your Instances snapshots |
230 | | -- Create backups of your storage disks |
231 | | -- Set up an alerting system in case of unexpected behavior |
| 68 | +- Using it to manage all your Instances' snapshots |
| 69 | +- Creating backups of your storage disks |
| 70 | +- Setting up an alerting system in case of unexpected behavior |
| 71 | +- Explore [scaleway/serverless-examples repository](https://github.com/scaleway/serverless-examples) for advanced automation examples |
232 | 72 |
|
233 | 73 | ## Additional resources |
234 | 74 |
|
|
0 commit comments