-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcompute-instance-create.ts
More file actions
104 lines (84 loc) · 3.4 KB
/
compute-instance-create.ts
File metadata and controls
104 lines (84 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Session, waitForOperation } from '@yandex-cloud/nodejs-sdk';
import { networkService } from '@yandex-cloud/nodejs-sdk/vpc-v1';
import { imageService, instanceService, instance } from '@yandex-cloud/nodejs-sdk/compute-v1';
import { Instance } from '@yandex-cloud/nodejs-sdk/compute-v1/instance';
import { getEnv } from './utils/get-env';
import { log } from './utils/logger';
const IAM_TOKEN = getEnv('YC_IAM_TOKEN');
const FOLDER_ID = getEnv('YC_FOLDER_ID');
const TARGET_ZONE_ID = 'ru-central1-a';
(async () => {
const session = new Session({ iamToken: IAM_TOKEN });
const imageClient = session.client(imageService.ImageServiceClient);
const instanceClient = session.client(instanceService.InstanceServiceClient);
const networkClient = session.client(networkService.NetworkServiceClient);
const networkResponse = await networkClient.list(
networkService.ListNetworksRequest.fromPartial({
folderId: FOLDER_ID,
}),
);
log(`Found ${networkResponse.networks.length} networks in folder ${FOLDER_ID}`);
const network = networkResponse.networks.pop();
if (!network) {
throw new Error(`There are no networks in folder ${FOLDER_ID}`);
}
const subnetsResponse = await networkClient.listSubnets(
networkService.ListNetworkSubnetsRequest.fromPartial({
networkId: network.id,
}),
);
const subnet = subnetsResponse.subnets.find((s) => s.zoneId === TARGET_ZONE_ID);
if (!subnet) {
throw new Error(`There is no subnet in zone ${TARGET_ZONE_ID} in folder ${FOLDER_ID}`);
}
const image = await imageClient.getLatestByFamily(
imageService.GetImageLatestByFamilyRequest.fromPartial({
family: 'ubuntu-1804-lts',
folderId: 'standard-images',
}),
);
const createOp = await instanceClient.create(
instanceService.CreateInstanceRequest.fromPartial({
folderId: FOLDER_ID,
zoneId: TARGET_ZONE_ID,
platformId: 'standard-v2',
labels: { 'nodejs-sdk': 'yes' },
resourcesSpec: {
memory: 2 * 1024 * 1024 * 1024,
cores: 2,
},
bootDiskSpec: {
autoDelete: true,
diskSpec: {
size: 10 * 1024 * 1024 * 1024,
imageId: image.id,
},
},
networkInterfaceSpecs: [
{
subnetId: subnet.id,
primaryV4AddressSpec: {
oneToOneNatSpec: { ipVersion: instance.IpVersion.IPV4 },
},
},
],
}),
);
log(`Instance create operation id: ${createOp.id}`);
const finishedCreateOp = await waitForOperation(createOp, session);
if (finishedCreateOp.response) {
const instance = Instance.decode(finishedCreateOp.response.value);
log(`Instance ${instance.id} created`);
const removeOp = await instanceClient.delete(
instanceService.DeleteInstanceRequest.fromPartial({
instanceId: instance.id,
}),
);
const finishedRemoveOp = await waitForOperation(removeOp, session);
if (finishedRemoveOp.error) {
log(`Failed to remove instance ${instance.id}`);
} else {
log(`Successfully remove instance ${instance.id}`);
}
}
})();