This is an example for creating Azure container groups with enviroment variables using Typescript.

Authentication and creating client

Firstly we use "@azure/ms-rest-nodeauth" library to do the authentication using service principal.

1
msRestNodeAuth.loginWithServicePrincipalSecretWithAuthResponse(clientId, secrect, tenantId);

After authenticated successfully, we can get the credentials from the response and use it to create Container Instance client.

1
const client = new ContainerInstanceManagementClient(authRes.credentials, subscriptId);    

Create container group

We define the specs for the container going to be used in the container group. Also, we can specify the necessary environment variables here.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
const dataProcessContainer: Container = {
    name: config.containerName,
    image: config.imageName,
    resources: {
        requests: {
            memoryInGB: 1.5,
            cpu: 1
        }
    },
    environmentVariables: [
        {name: 'id', value: triggerParams['id']}
    ],
}

Then we pass this defined container as a parameter of the container group

1
2
3
4
5
6
7
const newContainerGroup: ContainerGroup = {
    containers: [dataProcessContainer],
    osType: 'Linux',
    location: 'southeastasia',
    imageRegistryCredentials: [imageRegistryCredential],
    restartPolicy: 'Never'
}

Finally, we use beginCreateOrUpdate method of the client to create and start the container group.

1
const response = await client.containerGroups.beginCreateOrUpdate(projectName, containerGroupsName, newContainerGroup);

Thoughts and conclusion

I was very struggle to find any resouces or example of using Typescript with Container group SDK as well as Azure SDK in general. So I had to investigate the source code of the library and spent a lot of time for searching on the Internet to get the job done, that makes me decided to write this blog with the hope that it will help someone who has the same problem.

A downside of this approach is that, for each request of updating environment variables, we need to create a new container instance group because currently Azure doesn’t support to update the information on the fly (link).