node.js - 如何使用 nodejs azure SDK 启动具有 azure VM 映像的新 VM?
问题描述
我使用了 AWS nodejs SDK 并使用 AMI(亚马逊机器映像)ID 创建了一个实例。现在,我需要为 Azure 复制相同的内容。我遇到了文档并找到了如下方法:
resourceClient.resourceGroups.createOrUpdate(resourceGroupName, groupParameters, callback);
但是,我不确定如何创建映像(如 AWS 中的 AMI),然后使用映像 ID 在 Azure 中启动 VM。该文档似乎非常直截了当,并且缺乏像我这样的初学者所需的解释。是否有任何代码示例/示例对刚接触 azure 的人有用?
解决方案
关于这个问题,请参考以下代码
az ad sp create-for-rbac --name YOUR-SERVICE-PRINCIPAL-NAME
- 代码
const { ClientSecretCredential } = require("@azure/identity");
const { NetworkManagementClient } = require("@azure/arm-network");
const { ComputeManagementClient } = require("@azure/arm-compute");
const { ResourceManagementClient } = require("@azure/arm-resources");
const clientId = "the appId of the sp";
const tenant = "tenant id";
const clientSecret = "the clientsecret of the sp";
const subscriptionId = "the id of your Azure subscription";
const creds = new ClientSecretCredential(tenant, clientId, clientSecret);
const resouceClient = new ResourceManagementClient(creds, subscriptionId);
const newtworkClient = new NetworkManagementClient(creds, subscriptionId);
const computeClient = new ComputeManagementClient(creds, subscriptionId);
async function main() {
try {
// create resource group
const group = await resouceClient.resourceGroups.createOrUpdate(
"testdf78",
{
location: "eastasia",
}
);
// create vnet and subnet
const vnet = await newtworkClient.virtualNetworks.createOrUpdate(
group.name,
"testdf1_vnet",
{
addressSpace: {
addressPrefixes: ["10.0.0.0/16"],
},
location: group.location,
subnets: [{ name: "default", addressPrefix: "10.0.0.0/24" }],
}
);
// create public ip
const ip = await newtworkClient.publicIPAddresses.createOrUpdate(
group.name,
"testdf1_ip",
{
location: group.location,
publicIPAllocationMethod: "Dynamic",
}
);
// create nic
const nic = await newtworkClient.networkInterfaces.createOrUpdate(
group.name,
"testdf1_nic",
{
location: group.location,
ipConfigurations: [
{
name: "test",
privateIPAllocationMethod: "Dynamic",
subnet: vnet.subnets[0],
publicIPAddress: ip,
},
],
}
);
// get you custom image
const image = await computeClient.images.get("<groupname>", "<image name>");
//create vm
computeClient.virtualMachines.createOrUpdate(group.name, "testdf1", {
location: group.location,
hardwareProfile: {
vmSize: "Standard_B1s",
},
storageProfile: {
imageReference: {
id: image.id,
},
osDisk: {
caching: "ReadWrite",
managedDisk: {
storageAccountType: "Standard_LRS",
},
name: "testdf1osdisk",
createOption: "FromImage",
},
},
osProfile: {
computerName: "testdf1",
adminUsername: "testqw",
adminPassword: "Password0123!",
linuxConfiguration: {
patchSettings: { patchMode: "ImageDefault" },
},
},
networkProfile: {
networkInterfaces: [
{
id: nic.id,
},
],
},
diagnosticsProfile: {
bootDiagnostics: {
enabled: true,
},
},
});
} catch (error) {
console.log(error);
}
}
main();
推荐阅读
- c# - 检测用户何时离开(即关闭浏览器)
- docker - 无法在第二个对等点上部署 fabcar 链代码
- sql - 如何使用 postgis 查询获取分组几何的 bbox?
- vue.js - 如何在 NuxtJS axios 中为 API 使用 env var?
- django - 我的更改密码表单错误在我的 html 页面中不起作用
- java - Android Studio:按几个按钮
- angular - Visual Studio Angular 组件服务不匹配
- regex - 拆分字符串,然后删除 Google 表格中的前缀(再次)
- python - 在 QPdfWriter 上结合 QPainter 和 QTextDocument
- python - 请求库与 Pyinstaller 冲突