Skip to content

Commit 950d434

Browse files
authored
Merge pull request #294709 from craigshoemaker/aca/sessions-and-pools
[Container App] Restructure dynamic sessions content
2 parents 4fbf75a + f913449 commit 950d434

File tree

7 files changed

+828
-612
lines changed

7 files changed

+828
-612
lines changed

articles/container-apps/TOC.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@
6363
- name: Jobs
6464
href: jobs.md
6565
- name: Dynamic sessions
66-
href: sessions.md
66+
items:
67+
- name: Overview
68+
href: sessions.md
69+
- name: Usage
70+
href: sessions-usage.md
71+
- name: Session pools
72+
href: session-pool.md
6773
- name: Microservices
6874
href: microservices.md
6975
- name: Planned maintenance

articles/container-apps/session-pool.md

Lines changed: 318 additions & 0 deletions
Large diffs are not rendered by default.

articles/container-apps/sessions-code-interpreter.md

Lines changed: 224 additions & 76 deletions
Large diffs are not rendered by default.

articles/container-apps/sessions-custom-container.md

Lines changed: 6 additions & 364 deletions
Large diffs are not rendered by default.

articles/container-apps/sessions-tutorial-nodejs.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services: container-apps
55
author: IshitaAsthana
66
ms.service: azure-container-apps
77
ms.topic: tutorial
8-
ms.date: 11/08/2024
8+
ms.date: 02/19/2025
99
ms.author: iasthana
1010
---
1111

@@ -131,7 +131,7 @@ To interact with the session pool's API, you must use an identity with the `Azur
131131
132132
## Get a bearer token
133133
134-
For direct access to the session pool’s API, generate an access token to include in the `Authorization` header of your requests. Ensure the token contains an audience (`aud`) claim with the value `https://dynamicsessions.io`. For more information, see [authentication and authorization](./sessions.md?tabs=azure-cli#authentication) rules.
134+
For direct access to the session pool’s API, generate an access token to include in the `Authorization` header of your requests. Ensure the token contains an audience (`aud`) claim with the value `https://dynamicsessions.io`. For more information, see [authentication and authorization](./sessions-usage.md?tabs=azure-cli#authentication) rules.
135135
136136
1. Get an access token.
137137
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
---
2+
title: Use dynamic sessions in Azure Container Apps
3+
description: Learn how to use dynamic sessions in Azure Container Apps.
4+
services: container-apps
5+
author: craigshoemaker
6+
ms.service: azure-container-apps
7+
ms.topic: conceptual
8+
ms.date: 04/07/2025
9+
ms.author: cshoe
10+
ms.custom: references_regions, ignite-2024
11+
---
12+
13+
# Use dynamic sessions in Azure Container Apps
14+
15+
Azure Container Apps dynamic [sessions](sessions.md) offer isolated, secure contexts when you need to run code or applications separately from other workloads. Sessions run inside a [session pool](session-pool.md) which provides immediate access to new and existing sessions. These sessions are ideal for scenarios where user-generated input needs to be processed in a controlled manner or when integrating third-party services that require executing code in an isolated environment.
16+
17+
This article shows you how to manage and interact with dynamic sessions.
18+
19+
## Session access
20+
21+
Your application interacts with a session using the session pool's management API.
22+
23+
A pool management endpoint follows this format:
24+
25+
```text
26+
https://<SESSION_POOL_NAME>.<ENVIRONMENT_ID>.<REGION>.azurecontainerapps.io
27+
```
28+
29+
For more information managing session pools, see [session pools management endpoint](./session-pool.md#management-endpoint)
30+
31+
## Forwarding requests to a session's container
32+
33+
To send a request into a session's container, you use the management endpoint as the root for your request. Anything in the path following the base pool management endpoint is forwarded to the session's container.
34+
35+
For example, if you make a call to: `<POOL_MANAGEMENT_ENDPOINT>/api/uploadfile`, the request is routed to the session's container at `0.0.0.0:<TARGET_PORT>/api/uploadfile`.
36+
37+
## Continuous interaction
38+
39+
As you continue to make calls to the same session, the session remains [allocated](sessions.md#session-lifecycle) in the pool. Once there are no requests to the session after the cooldown period has elapsed, the session is automatically destroyed.
40+
41+
## Sample request
42+
43+
The following example shows how to send request to a session using a user's ID as the session unique identifier.
44+
45+
Before you send the request, replace the placeholders between the `<>` brackets with values specific to your request.
46+
47+
```http
48+
POST <POOL_MANAGEMENT_ENDPOINT>/<API_PATH_EXPOSED_BY_CONTAINER>?identifier=<USER_ID>
49+
Authorization: Bearer <TOKEN>
50+
{
51+
"command": "echo 'Hello, world!'"
52+
}
53+
```
54+
55+
This request is forwarded to the container in the session with the identifier for the user's ID.
56+
57+
If the session isn't already running, Azure Container Apps automatically allocates a session from the pool before forwarding the request.
58+
59+
In this example, the session's container receives the request at `http://0.0.0.0:<INGRESS_PORT>/<API_PATH_EXPOSED_BY_CONTAINER>`.
60+
61+
## Identifiers
62+
63+
To send an HTTP request to a session, you must provide a session identifier in the request. You pass the session identifier in a query string parameter named `identifier` in the URL when you make a request to a session.
64+
65+
- If a session with the identifier already exists, the request is sent to the existing session.
66+
67+
- If a session with the identifier doesn't exist, a new session is automatically allocated before the request is sent to it.
68+
69+
:::image type="content" source="media/sessions/sessions-overview.png" alt-text="Screenshot of session pool and sessions usage.":::
70+
71+
### Identifier format
72+
73+
The session identifier is a free-form string, meaning you can define it in any way that suits your application's needs.
74+
75+
The session identifier is a string that you define that is unique within the session pool. If you're building a web application, you can use the user's ID as the session identifier. If you're building a chatbot, you can use the conversation ID.
76+
77+
The identifier must be a string that is 4 to 128 characters long and can contain only alphanumeric characters and special characters from this list: `|`, `-`, `&`, `^`, `%`, `$`, `#`, `(`, `)`, `{`, `}`, `[`, `]`, `;`, `<`, and `>`.
78+
79+
## Security
80+
81+
Dynamic sessions are built to run untrusted code and applications in a secure and isolated environment. While sessions are isolated from one another, anything within a single session, including files and environment variables, is accessible by users of the session.
82+
83+
Only configure or upload sensitive data to a session if you trust the users of the session.
84+
85+
By default, sessions are prevented from making outbound network requests. You can control network access by configuring network status settings on the session pool.
86+
87+
- **Use strong, unique session identifiers**: Always generate session identifiers that are long and complex to prevent brute-force attacks. Use cryptographic algorithms to create identifiers that are hard to guess.
88+
89+
- **Limit session visibility**: Set strict access controls to ensure that session identifiers are only visible to the session pool. Avoid exposing session IDs in URLs or logs.
90+
91+
- **Implement short expiration times**: Configure session identifiers to expire after a short period of inactivity. This approach minimizes the risk of sessions being hijacked after a user has finished interacting with your application.
92+
93+
- **Regularly rotate session credentials**: Periodically review and update the credentials associated with your sessions. Rotation decreases the risk of unauthorized access.
94+
95+
- **Utilize secure transmission protocols**: Always use HTTPS to encrypt data in transit, including session identifiers. This approach protects against man-in-the-middle attacks.
96+
97+
- **Monitor session activity**: Implement logging and monitoring to track session activities. Use these logs to identify unusual patterns or potential security breaches.
98+
99+
- **Validate user input**: Treat all user input as dangerous. Use input validation and sanitation techniques to protect against injection attacks and ensure that only trusted data is processed.
100+
101+
To fully secure your sessions, you can:
102+
103+
- [Use Microsoft Entra ID authentication and authorization](#authentication)
104+
- [Protect session identifiers](#protect-session-identifiers)
105+
106+
### <a name="authentication"></a>Authentication and authorization
107+
108+
When you send requests to a session using the pool management API, authentication is handled using Microsoft Entra tokens. Only Microsoft Entra tokens from an identity belonging to the *Azure ContainerApps Session Executor* role on the session pool are authorized to call the pool management API.
109+
110+
To assign the role to an identity, use the following Azure CLI command:
111+
112+
```azurecli
113+
az role assignment create \
114+
--role "Azure ContainerApps Session Executor" \
115+
--assignee <PRINCIPAL_ID> \
116+
--scope <SESSION_POOL_RESOURCE_ID>
117+
```
118+
119+
If you're using an [large language model (LLM) framework integration](sessions-code-interpreter.md#llm-framework-integrations), the framework handles the token generation and management for you. Ensure that the application is configured with a managed identity with the necessary role assignments on the session pool.
120+
121+
If you're using the pool's management API endpoints directly, you must generate a token and include it in the `Authorization` header of your HTTP requests. In addition to the role assignments previously mentioned, token needs to contain an audience (`aud`) claim with the value `https://dynamicsessions.io`.
122+
123+
##### [Azure CLI](#tab/cli)
124+
125+
To generate a token using the Azure CLI, run the following command:
126+
127+
```azurecli
128+
az account get-access-token --resource https://dynamicsessions.io
129+
```
130+
131+
##### [C#](#tab/csharp)
132+
133+
In C#, you can use the `Azure.Identity` library to generate a token. First, install the library:
134+
135+
```bash
136+
dotnet add package Azure.Identity
137+
```
138+
139+
Then, use the following code to generate a token:
140+
141+
```csharp
142+
using Azure.Identity;
143+
144+
var credential = new DefaultAzureCredential();
145+
var token = credential.GetToken(new TokenRequestContext(new[] { "https://dynamicsessions.io/.default" }));
146+
var accessToken = token.Token;
147+
```
148+
149+
##### [JavaScript](#tab/javascript)
150+
151+
In JavaScript, you can use the `@azure/identity` library to generate a token. First, install the library:
152+
153+
```bash
154+
npm install @azure/identity
155+
```
156+
157+
Then, use the following code to generate a token:
158+
159+
```javascript
160+
const { DefaultAzureCredential } = require("@azure/identity");
161+
const { TokenCredential } = require("@azure/core-auth");
162+
163+
const credential = new DefaultAzureCredential();
164+
const token = await credential.getToken("https://dynamicsessions.io/.default");
165+
const accessToken = token.token;
166+
```
167+
168+
##### [Python](#tab/python)
169+
170+
In Python, you can use the `azure-identity` library to generate a token. First, install the library:
171+
172+
```bash
173+
pip install azure-identity
174+
```
175+
176+
Then, use the following code to generate a token:
177+
178+
```python
179+
from azure.identity import DefaultAzureCredential
180+
181+
credential = DefaultAzureCredential()
182+
token = credential.get_token("https://dynamicsessions.io/.default")
183+
access_token = token.token
184+
```
185+
186+
---
187+
188+
> [!IMPORTANT]
189+
> A valid token is used to create and access any session in the pool. Keep your tokens secure and don't share them with untrusted parties. End users should never have direct access to tokens. Only make tokens available to the application, and never to end users.
190+
191+
### Protect session identifiers
192+
193+
The session identifier is sensitive information which you must manage securely. Your application needs to ensure each user or tenant only has access to their own sessions.
194+
195+
The specific strategies that prevent misuse of session identifiers differ depending on the design and architecture of your app. However, your app must always have complete control over the creation and use of session identifiers so that a malicious user can't access another user's session.
196+
197+
Example strategies include:
198+
199+
- **One session per user**: If your app uses one session per user, each user must be securely authenticated, and your app must use a unique session identifier for each logged in user.
200+
201+
- **One session per agent conversation**: If your app uses one session per AI agent conversation, ensure your app uses a unique session identifier for each conversation that can't be modified by the end user.
202+
203+
> [!IMPORTANT]
204+
> Failure to secure access to sessions could result in misuse or unauthorized access to data stored in your users' sessions.
205+
206+
## Logging
207+
208+
Console logs from containers running in a session are available in the Azure Log Analytics workspace associated with the Azure Container Apps environment in a table named `AppEnvSessionConsoleLogs_CL`.
209+
210+
## Related content
211+
212+
- **Session types**: Learn about the different types of dynamic sessions:
213+
- [Code interpreter sessions](./sessions-code-interpreter.md)
214+
- [Custom container sessions](./sessions-custom-container.md)
215+
216+
- **Tutorials**: Work directly with the REST API or via an LLM agent:
217+
- Use an LLM agent:
218+
- [AutoGen](./sessions-tutorial-autogen.md)
219+
- [LangChain](./sessions-tutorial-langchain.md)
220+
- [LlamaIndex](./sessions-tutorial-llamaindex.md)
221+
- [Semantic Kernel](./sessions-tutorial-semantic-kernel.md)
222+
- Use the REST API
223+
- [JavaScript Code interpreter](./sessions-tutorial-nodejs.md)

0 commit comments

Comments
 (0)