Created
November 17, 2017 15:28
-
-
Save nbellocam/03b7d38c01072c2c8883df406f66e7eb to your computer and use it in GitHub Desktop.
This gist show how to create a container instance in Azure using service principal authentication and the Azure SDK
This file contains 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
const msRestAzure = require('ms-rest-azure'); | |
const ContainerInstanceManagementClient = require("azure-arm-containerinstance"); | |
const AzureServiceClient = msRestAzure.AzureServiceClient; | |
const clientId = process.env['CLIENT_ID']; | |
const secret = process.env['APPLICATION_SECRET']; | |
const domain = process.env['DOMAIN']; //also known as tenantId | |
const subscriptionId = process.env['AZURE_SUBSCRIPTION_ID']; | |
const resourceGroup = 'YourResourceGroupName'; | |
const containerGroup = 'YourContainerGroupName'; | |
msRestAzure.loginWithServicePrincipalSecret(clientId, secret, domain) | |
.then(creds => { | |
creds.getToken((e, t) => { | |
let client = new ContainerInstanceManagementClient(creds, subscriptionId); | |
client.containerGroups.createOrUpdate(resourceGroup, containerGroup, { | |
location: 'westus', | |
name: 'demo1', | |
containers: [{ | |
name: 'demo1', | |
environmentVariables: [{ | |
name: 'SUBSCRIPTION_ID', | |
value: subscriptionId | |
}, { | |
name: 'RESOURCE_GROUP', | |
value: resourceGroup | |
}, { | |
name: 'CONTAINER_GROUP', | |
value: containerGroup | |
}, { | |
name: 'ACCESS_TOKEN', | |
value: t.accessToken | |
}], | |
image: 'nbellocam/autoclean-sh', | |
resources: { | |
limits: null, | |
requests: { | |
cpu: 1, | |
memoryInGB: 1.5 | |
} | |
} | |
}], | |
osType: 'Linux' | |
}).catch(err => { | |
console.log('An error ocurred'); | |
console.dir(err, { depth: null, colors: true }); | |
}); | |
}); | |
}).catch(err => { | |
console.dir(err, { depth: null, colors: true }); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment