0
# Geographic Routing
1
2
Geographic hierarchy management for geographic-based traffic routing including region codes and hierarchical geographic mappings. This enables routing users to the most appropriate endpoint based on their geographic location.
3
4
## Capabilities
5
6
### Get Geographic Hierarchy
7
8
Retrieves the default geographic hierarchy used by Traffic Manager for geographic traffic routing, containing all available region codes and their hierarchical relationships.
9
10
```python { .api }
11
def get_default() -> TrafficManagerGeographicHierarchy:
12
"""
13
Gets the default geographic hierarchy for Traffic Manager.
14
15
Returns:
16
TrafficManagerGeographicHierarchy: Complete geographic hierarchy structure
17
"""
18
```
19
20
**Usage Example:**
21
22
```python
23
# Get the geographic hierarchy
24
geo_hierarchy = client.geographic_hierarchies.get_default()
25
26
# Explore the hierarchy structure
27
def print_regions(region, indent=0):
28
prefix = " " * indent
29
print(f"{prefix}{region.name} ({region.code})")
30
31
if region.regions:
32
for child_region in region.regions:
33
print_regions(child_region, indent + 1)
34
35
# Print the complete hierarchy
36
root_region = geo_hierarchy.geographic_hierarchy
37
print("Geographic Hierarchy:")
38
print_regions(root_region)
39
40
# Find specific region codes
41
def find_region_codes(region, search_name):
42
codes = []
43
if search_name.lower() in region.name.lower():
44
codes.append(region.code)
45
46
if region.regions:
47
for child in region.regions:
48
codes.extend(find_region_codes(child, search_name))
49
50
return codes
51
52
# Find all US-related regions
53
us_codes = find_region_codes(root_region, "United States")
54
print(f"US region codes: {us_codes}")
55
56
# Find European regions
57
eu_codes = find_region_codes(root_region, "Europe")
58
print(f"European region codes: {eu_codes}")
59
```
60
61
## Geographic Data Models
62
63
### Geographic Hierarchy
64
65
```python { .api }
66
class TrafficManagerGeographicHierarchy:
67
"""Traffic Manager geographic hierarchy."""
68
geographic_hierarchy: Region # Root region containing all geographic mappings
69
70
class Region:
71
"""Geographic region in the hierarchy."""
72
code: str # Region code (e.g., "US", "FR", "WORLD-EUR")
73
name: str # Human-readable region name
74
regions: List[Region] # Child regions in the hierarchy
75
```
76
77
## Common Geographic Region Codes
78
79
### Continental Regions
80
81
```python
82
# Major continental regions
83
WORLD_REGIONS = [
84
"WORLD-AF", # Africa
85
"WORLD-AS", # Asia
86
"WORLD-EU", # Europe
87
"WORLD-NA", # North America
88
"WORLD-OC", # Oceania
89
"WORLD-SA" # South America
90
]
91
```
92
93
### Country Codes
94
95
```python
96
# Common country codes (ISO 3166-1 alpha-2)
97
COUNTRY_CODES = [
98
"US", # United States
99
"CA", # Canada
100
"MX", # Mexico
101
"BR", # Brazil
102
"GB", # United Kingdom
103
"FR", # France
104
"DE", # Germany
105
"IT", # Italy
106
"ES", # Spain
107
"NL", # Netherlands
108
"JP", # Japan
109
"CN", # China
110
"IN", # India
111
"AU", # Australia
112
"KR", # South Korea
113
"SG" # Singapore
114
]
115
```
116
117
### US State and Region Codes
118
119
```python
120
# United States regional codes
121
US_REGIONS = [
122
"US-AK", # Alaska
123
"US-AL", # Alabama
124
"US-AR", # Arkansas
125
"US-AZ", # Arizona
126
"US-CA", # California
127
"US-CO", # Colorado
128
"US-CT", # Connecticut
129
"US-DC", # District of Columbia
130
"US-DE", # Delaware
131
"US-FL", # Florida
132
"US-GA", # Georgia
133
"US-HI", # Hawaii
134
"US-IA", # Iowa
135
"US-ID", # Idaho
136
"US-IL", # Illinois
137
"US-IN", # Indiana
138
"US-KS", # Kansas
139
"US-KY", # Kentucky
140
"US-LA", # Louisiana
141
"US-MA", # Massachusetts
142
"US-MD", # Maryland
143
"US-ME", # Maine
144
"US-MI", # Michigan
145
"US-MN", # Minnesota
146
"US-MO", # Missouri
147
"US-MS", # Mississippi
148
"US-MT", # Montana
149
"US-NC", # North Carolina
150
"US-ND", # North Dakota
151
"US-NE", # Nebraska
152
"US-NH", # New Hampshire
153
"US-NJ", # New Jersey
154
"US-NM", # New Mexico
155
"US-NV", # Nevada
156
"US-NY", # New York
157
"US-OH", # Ohio
158
"US-OK", # Oklahoma
159
"US-OR", # Oregon
160
"US-PA", # Pennsylvania
161
"US-RI", # Rhode Island
162
"US-SC", # South Carolina
163
"US-SD", # South Dakota
164
"US-TN", # Tennessee
165
"US-TX", # Texas
166
"US-UT", # Utah
167
"US-VA", # Virginia
168
"US-VT", # Vermont
169
"US-WA", # Washington
170
"US-WI", # Wisconsin
171
"US-WV", # West Virginia
172
"US-WY" # Wyoming
173
]
174
```
175
176
## Geographic Routing Configuration
177
178
### Configure Geographic Routing on Profile
179
180
```python
181
from azure.mgmt.trafficmanager.models import Profile, TrafficRoutingMethod
182
183
# Create profile with geographic routing
184
profile = Profile(
185
location="global",
186
profile_status="Enabled",
187
traffic_routing_method=TrafficRoutingMethod.GEOGRAPHIC,
188
dns_config={
189
"relative_name": "geo-app",
190
"ttl": 60
191
},
192
monitor_config={
193
"protocol": "HTTPS",
194
"port": 443,
195
"path": "/health"
196
}
197
)
198
199
geo_profile = client.profiles.create_or_update(
200
resource_group_name="my-rg",
201
profile_name="geo-routing-profile",
202
parameters=profile
203
)
204
```
205
206
### Configure Geographic Mappings on Endpoints
207
208
```python
209
from azure.mgmt.trafficmanager.models import Endpoint
210
211
# North American endpoint
212
na_endpoint = Endpoint(
213
target="na-service.example.com",
214
endpoint_status="Enabled",
215
geo_mapping=[
216
"US", # United States
217
"CA", # Canada
218
"MX" # Mexico
219
]
220
)
221
222
# European endpoint
223
eu_endpoint = Endpoint(
224
target="eu-service.example.com",
225
endpoint_status="Enabled",
226
geo_mapping=[
227
"WORLD-EU" # All of Europe (includes all European countries)
228
]
229
)
230
231
# Specific European countries endpoint
232
eu_specific_endpoint = Endpoint(
233
target="eu-premium.example.com",
234
endpoint_status="Enabled",
235
geo_mapping=[
236
"GB", # United Kingdom
237
"FR", # France
238
"DE", # Germany
239
"NL" # Netherlands
240
]
241
)
242
243
# Asia-Pacific endpoint
244
apac_endpoint = Endpoint(
245
target="apac-service.example.com",
246
endpoint_status="Enabled",
247
geo_mapping=[
248
"JP", # Japan
249
"AU", # Australia
250
"SG", # Singapore
251
"KR", # South Korea
252
"IN" # India
253
]
254
)
255
256
# Default/fallback endpoint (no geo mapping = catches all unmapped traffic)
257
default_endpoint = Endpoint(
258
target="global-service.example.com",
259
endpoint_status="Enabled"
260
# No geo_mapping = default for unmapped regions
261
)
262
263
# Create endpoints
264
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "north-america", na_endpoint)
265
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "europe", eu_endpoint)
266
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "asia-pacific", apac_endpoint)
267
client.endpoints.create_or_update("my-rg", "geo-profile", "ExternalEndpoints", "default", default_endpoint)
268
```
269
270
## Geographic Routing Best Practices
271
272
### Hierarchy Planning
273
274
1. **Top-Down Approach**: Start with continental regions (`WORLD-*`) and drill down to specific countries/states as needed
275
2. **Overlap Prevention**: Each geographic code can only be mapped to one endpoint within a profile
276
3. **Default Endpoint**: Always include an endpoint with no geographic mapping as a fallback
277
4. **Hierarchical Specificity**: More specific mappings (country) take precedence over general ones (continent)
278
279
### Common Geographic Routing Patterns
280
281
#### 1. Continental Routing
282
283
```python
284
# Route by major continents
285
endpoints = [
286
{"name": "north-america", "geo_mapping": ["WORLD-NA"]},
287
{"name": "europe", "geo_mapping": ["WORLD-EU"]},
288
{"name": "asia", "geo_mapping": ["WORLD-AS"]},
289
{"name": "default", "geo_mapping": []} # Fallback
290
]
291
```
292
293
#### 2. Regulatory Compliance Routing
294
295
```python
296
# Separate endpoints for data sovereignty
297
endpoints = [
298
{"name": "eu-gdpr", "geo_mapping": ["WORLD-EU"]}, # EU for GDPR compliance
299
{"name": "us-cloud", "geo_mapping": ["US", "CA"]}, # North America
300
{"name": "apac", "geo_mapping": ["JP", "AU", "SG", "KR"]}, # Regulated APAC countries
301
{"name": "rest-of-world", "geo_mapping": []} # Everything else
302
]
303
```
304
305
#### 3. Performance-Focused Routing
306
307
```python
308
# Optimize for specific high-traffic regions
309
endpoints = [
310
{"name": "us-east", "geo_mapping": ["US-NY", "US-MA", "US-VA", "US-DC", "US-NJ"]},
311
{"name": "us-west", "geo_mapping": ["US-CA", "US-WA", "US-OR", "US-NV"]},
312
{"name": "uk-ireland", "geo_mapping": ["GB", "IE"]},
313
{"name": "central-eu", "geo_mapping": ["DE", "FR", "NL", "BE"]},
314
{"name": "nordic", "geo_mapping": ["SE", "NO", "DK", "FI"]},
315
{"name": "global", "geo_mapping": []} # Default
316
]
317
```
318
319
#### 4. Hybrid Geographic + Other Routing
320
321
```python
322
# Use nested profiles to combine geographic with other routing methods
323
# Parent profile: Geographic routing
324
# Child profiles: Performance or weighted routing within each region
325
326
parent_profile = Profile(
327
traffic_routing_method="Geographic",
328
# ... other config
329
)
330
331
# Child profile for North America with performance routing
332
na_child_profile = Profile(
333
traffic_routing_method="Performance",
334
# ... other config
335
)
336
337
# Nested endpoint pointing to NA child profile
338
na_nested_endpoint = Endpoint(
339
target_resource_id="/subscriptions/.../trafficManagerProfiles/na-performance-profile",
340
endpoint_status="Enabled",
341
geo_mapping=["US", "CA", "MX"]
342
)
343
```
344
345
### Geographic Code Discovery
346
347
```python
348
def discover_geographic_codes():
349
"""Helper function to explore available geographic codes."""
350
hierarchy = client.geographic_hierarchies.get_default()
351
352
def extract_codes(region, codes_dict, parent_path=""):
353
path = f"{parent_path}/{region.name}" if parent_path else region.name
354
codes_dict[region.code] = {
355
"name": region.name,
356
"path": path,
357
"parent": parent_path
358
}
359
360
if region.regions:
361
for child in region.regions:
362
extract_codes(child, codes_dict, path)
363
364
all_codes = {}
365
extract_codes(hierarchy.geographic_hierarchy, all_codes)
366
367
return all_codes
368
369
# Get all available codes
370
geo_codes = discover_geographic_codes()
371
372
# Find codes for a specific region
373
europe_codes = {k: v for k, v in geo_codes.items() if "Europe" in v["path"]}
374
us_codes = {k: v for k, v in geo_codes.items() if "United States" in v["path"]}
375
376
print(f"European region codes: {list(europe_codes.keys())}")
377
print(f"US region codes: {list(us_codes.keys())}")
378
```