page_type | languages | products | description | urlFragment | ||
---|---|---|---|---|---|---|
sample |
|
|
This sample shows how to use the Python SDK to create, retreive, and delete Azure Container Instances. |
container-instances-python-manage |
This sample shows how to use the Python SDK to create, retreive, and delete Azure Container Instances.
- Getting Started
- Install prerequisites
- Have an Azure security principal
- Setup The Sample
- Clone the repository
- Install packages
- Run The Sample
- Update the placeholders
- Execute example.py
- Understand The Code
- Defining an Azure execution context
- Constructing the Azure clients
- Execute activities
-
Python installed. See the the SDK for supported Python versions
-
A virtual environment initialized (optional)
TIP: using virtual environments is recommended, but not required. To initialize a virtual environment, open a terminal and run the following (replace
<myvirtualenv>
in the example below with the name of your virtual environment):pip install virtualenv virtualenv <myvirtualenv> cd <myvirtualenv> source bin/activate
-
Git installed. Download the latest client for your OS platform.
Get or create a security principal to authenticate with the Azure subscription that the sample will execute against. This can be done a few ways, including:
- the Azure CLI 2.0
- with PowerShell
- or using the portal
-
First, clone the repository. To do this, open a terminal, then type the following command:
git clone https://github.com/Azure-Samples/container-instances-python-manage.git
-
Next, install the Python packages the sample depends on. In the same terminal window, enter the following command:
cd container-instances-python-manage/src pip install -r requirements.txt
To run the sample, the information for the Azure security principal obtained from the Azure security principal section must be used to update the placeholder values in the sample.
- First, open
example.py
and locate the following codeblock at the top of the file:
azure_context = AzureContext(
subscription_id = '<SUBSCRIPTION ID>',
client_id = '<CLIENT ID>',
client_secret = '<CLIENT SECRET>',
tenant = '<TENANT ID (AZURE ACTIVE DIRECTORY)>'
)
-
Replace the values for
<SUBSCRIPTION ID>
,<CLIENT ID>
,<CLIENT SECRET>
, and<TENANT ID (AZURE ACTIVE DIRECTORY)>
. -
Save the changes made to
example.py
.
Using the terminal window from the previous step, or opening a new window and changing the directory to container-instances-python-manage/src
, and type the following command:
python example.py
TIP: if you decided to use virtual environnements as recommended, make sure the virtual environment has been activated as shown in step 3 of Getting Started above.
First, an Azure execution context is created with a subscription id and security principal credentials at the start of example.py
.
azure_context = AzureContext(
subscription_id = '<SUBSCRIPTION ID>',
client_id = '<CLIENT ID>',
client_secret = '<CLIENT SECRET>',
tenant = '<TENANT ID (AZURE ACTIVE DIRECTORY)>'
)
This helper class is used to demonstrate the concept of an Azure execution context.
TIP: There is no formal "execution context" object, but is conceptual. It is important to understand particularly when having to execute programs against multiple Azure subscriptions.
class AzureContext(object):
def __init__(self, subscription_id, client_id, client_secret, tenant):
self.credentials = ServicePrincipalCredentials(
client_id = client_id,
secret = client_secret,
tenant = tenant
)
self.subscription_id = subscription_id
Next, the clients are constructed. This is done outside main
method so the variables can be used in each function without having to define a client for every action.
resource_client = ResourceManagementClient(azure_context.credentials, azure_context.subscription_id)
client = ContainerInstanceManagementClient(azure_context.credentials, azure_context.subscription_id)
The execution steps of the code in example.py
performs the following actions:
- List all the container instances in the subscription
- Define attributes of the container instance
- Ensure the resource group is created
- Create the container
- Retrieve and show the newly created container instance
- Delete and clean up