<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Posts on Robin Su</title><link>https://robinsujob.github.io/post/</link><description>Recent content in Posts on Robin Su</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 22 Oct 2026 00:00:00 +0800</lastBuildDate><atom:link href="https://robinsujob.github.io/post/index.xml" rel="self" type="application/rss+xml"/><item><title>Building E2B Sandbox Resilience: Lessons from Nomad's Heartbeat Mechanism</title><link>https://robinsujob.github.io/p/e2b-sandbox-resilience-nomad-heartbeats/</link><pubDate>Thu, 22 Oct 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/e2b-sandbox-resilience-nomad-heartbeats/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building E2B Sandbox Resilience: Lessons from Nomad's Heartbeat Mechanism" /&gt;&lt;p&gt;Running a self-hosted E2B cluster is about more than getting Firecracker sandboxes to start. We also need to consider how to keep them stable once they host long-running workloads. As a single cluster supports more sandboxes, resilience becomes increasingly important. &lt;strong&gt;Our architecture needs to protect existing runtime state and user connections while limiting the impact on capacity for new sandboxes, so that a local interruption does not cause unnecessary disruption elsewhere.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This article starts with a single Nomad server becoming unavailable. It describes what we learned about Server / Client heartbeats and the changes we explored to task preservation, node failure detection, and blast-radius control.&lt;/p&gt;
&lt;p&gt;In this self-hosted environment, three Nomad servers form the control plane. The failure affected only the VM hosting one of them; the Clients running sandboxes and the other nodes remained healthy. Our initial expectation was straightforward: the remaining servers would keep the control plane available, Clients would switch to another healthy server, and workloads would continue running.&lt;/p&gt;
&lt;p&gt;The tests revealed something counterintuitive: &lt;strong&gt;only one server was lost, yet Orchestrator and other tasks on healthy Nomad Client hosts were replaced.&lt;/strong&gt; To understand why, we first needed to examine how Nomad servers and Clients use heartbeats to maintain contact, and what Nomad does with existing tasks when those heartbeats stop arriving.&lt;/p&gt;
&lt;h2 id="how-a-single-server-failure-affects-an-e2b-deployment"&gt;How a Single Server Failure Affects an E2B Deployment
&lt;/h2&gt;&lt;p&gt;First, consider the relationship between E2B and Nomad. In this deployment, Nomad schedules and manages services such as the API, Orchestrator, session-proxy, and client-proxy. Orchestrator, in turn, manages Firecracker sandboxes on its host. &lt;strong&gt;Sandbox creation and state management are handled by Orchestrator instances deployed on sandbox nodes and managed by Nomad.&lt;/strong&gt;&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;flowchart TB
 accTitle: Control and access paths in self-hosted E2B
 accDescr: Nomad manages infrastructure services, while Orchestrator manages sandboxes. Sandbox traffic does not pass through Nomad servers for every request.
 servers["3 x Nomad servers&lt;br/&gt;Leader / Follower"]

 subgraph host["Sandbox host"]
 agent["Nomad Client agent"]
 orchestrator["Orchestrator"]
 proxy["session-proxy"]
 sandboxes["Firecracker sandboxes&lt;br/&gt;Original processes / memory / files"]
 agent --&gt;|"Run tasks"| orchestrator
 agent --&gt;|"Run tasks"| proxy
 orchestrator --&gt;|"Create and manage"| sandboxes
 proxy --&gt;|"Forward workload requests"| sandboxes
 end

 agent -.-&gt;|"Node.UpdateStatus"| servers
 servers -.-&gt;|"Heartbeat response / allocation state"| agent
 user["E2B SDK / user"] --&gt; entry["client-proxy&lt;br/&gt;Shared proxy entry point"]
 entry --&gt; proxy&lt;/pre&gt;&lt;p&gt;A Nomad Client is the agent running on a host. It communicates with servers and executes tasks. When Nomad schedules a task group onto a Client node, the resulting execution instance is called an &lt;strong&gt;allocation&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;To observe whether a single server failure could trigger task replacement, we forcibly stopped one non-Leader server without configuring a disconnect retention policy. The other two servers maintained quorum, and we continued observing for approximately 11 minutes.&lt;/p&gt;
&lt;p&gt;Tracking the identities of the original allocations, we recorded the following replacements for selected tasks:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Task&lt;/th&gt;
 &lt;th style="text-align: right"&gt;Original allocations replaced / original total&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Orchestrator&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;7 / 20&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;session-proxy&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;7 / 20&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;client-proxy&lt;/td&gt;
 &lt;td style="text-align: right"&gt;&lt;strong&gt;2 / 3&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The experiment showed that &lt;strong&gt;the control plane can instruct a Client to abandon its original tasks even when the Client host has neither failed nor restarted.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For an E2B cluster, this is an important detail. Orchestrator manages sandbox runtime resources and participates in sandbox creation and state management. An unnecessary replacement can therefore affect sandboxes that are already running.&lt;/p&gt;
&lt;h2 id="the-unexpected-sequence-tasks-stop-after-heartbeats-recover"&gt;The Unexpected Sequence: Tasks Stop After Heartbeats Recover
&lt;/h2&gt;&lt;p&gt;Using the platform&amp;rsquo;s confirmation that the server had stopped as the observation baseline, we aligned Nomad Client logs with workload events:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;At approximately 21 seconds, we had captured a successful heartbeat from the Client to a healthy server.&lt;/li&gt;
&lt;li&gt;At approximately 39 seconds, the original task began &lt;code&gt;Killing&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;At approximately 44 seconds, the original application stream closed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The node and allocation states showed that &lt;strong&gt;the Client executed a previously made stop decision only after reconnecting and receiving an update.&lt;/strong&gt; The overall sequence was:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Heartbeat expires
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Server updates node status and triggers a scheduling evaluation
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Scheduler marks the original allocation as stop / lost
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Client reconnects and retrieves the allocation update
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Local task executes Killing
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;-&amp;gt; Replacement allocation starts
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="understanding-nomad-heartbeats-how-node-failure-detection-affects-workloads"&gt;Understanding Nomad Heartbeats: How Node Failure Detection Affects Workloads
&lt;/h2&gt;&lt;p&gt;With this question in mind, we examined Nomad&amp;rsquo;s GitHub source code and official documentation. We traced the following call chain:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Client sends a heartbeat -&amp;gt; RPC selects a server -&amp;gt; Follower forwards the request -&amp;gt; Leader renews the heartbeat timer -&amp;gt; Expiration updates node status -&amp;gt; Scheduler processes allocations -&amp;gt; Client applies task updates.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The sequence diagram below provides the full picture before we walk through the code. Its timestamps are illustrative and start from the last successfully processed heartbeat at the Leader. The log timestamps above start from the platform&amp;rsquo;s stop confirmation, so the two timelines do not correspond directly.&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: From heartbeat expiration to termination of the original task
 accDescr: t=0 is the last successful heartbeat at the Leader. With an illustrative TTL of 15 seconds and grace of 10 seconds, expiration occurs at about 25 seconds. If connectivity returns at 40 seconds, the Client can still execute a stop plan created earlier. These are illustrative, not measured, timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant F as Follower
 participant L as Leader
 participant S as Scheduler
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=10s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;F: Node.UpdateStatus
 F-&gt;&gt;L: t=0: Forward heartbeat and renew timer
 L--&gt;&gt;F: Return dynamic TTL=15s
 F--&gt;&gt;C: Heartbeat succeeds

 Note over Time: 5s
 Note over F: t=5s: Assume the server VM becomes unavailable
 Note over Time: About 15s
 C-xF: t~15s: Next heartbeat RPC does not return promptly
 Note over Time: About 25s&lt;br/&gt;15s TTL + 10s grace
 Note over L: t~25s (15+10): Heartbeat budget expires
 L-&gt;&gt;L: Update node status
 L-&gt;&gt;S: Trigger node-update evaluation
 Note over Time: About 25s+
 S-&gt;&gt;L: t~25s+: Submit stop plan for original allocation

 Note over Time: 40s&lt;br/&gt;Assumed recovery point
 C-&gt;&gt;L: t=40s: Assume heartbeat succeeds through a healthy server
 C-&gt;&gt;L: Retrieve allocation updates
 L--&gt;&gt;C: Original allocation is already marked to stop
 Note over Time: 40s+
 C-&gt;&gt;T: t=40s+: Local runner executes Killing
 Note over C,T: The stop plan predates reconnection&lt;br/&gt;A recovered heartbeat does not automatically cancel it&lt;/pre&gt;&lt;p&gt;The following sections walk through the key code paths. The snippets have been simplified.&lt;/p&gt;
&lt;h3 id="1-client-heartbeats-report-node-status"&gt;1. Client Heartbeats Report Node Status
&lt;/h3&gt;&lt;p&gt;On the Client, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L1870-L1923" target="_blank" rel="noopener"
 &gt;&lt;code&gt;registerAndHeartbeat()&lt;/code&gt;&lt;/a&gt; handles registration and periodic heartbeats. The actual request is constructed in &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L2236-L2274" target="_blank" rel="noopener"
 &gt;&lt;code&gt;updateNodeStatus()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeUpdateStatusRequest&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;NodeID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NodeID&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusReady&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeUpdateResponse&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RPC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Node.UpdateStatus&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heartbeatTTL&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;resp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HeartbeatTTL&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The request reports the &lt;strong&gt;NodeID and node status&lt;/strong&gt;, not the application health of a particular Job. After a successful response, the Client uses &lt;code&gt;HeartbeatTTL&lt;/code&gt; to schedule its next heartbeat. After a failure, the heartbeat loop schedules a retry and triggers server discovery.&lt;/p&gt;
&lt;p&gt;Multiple tasks on the same Client therefore share the node heartbeat. &lt;code&gt;Node.UpdateAlloc&lt;/code&gt; reports allocation state, while Orchestrator&amp;rsquo;s gRPC health check measures application health. Neither replaces this Client-to-Server heartbeat.&lt;/p&gt;
&lt;h3 id="2-one-server-per-rpc-attempt-switching-paths-waits-for-the-call-to-return"&gt;2. One Server per RPC Attempt: Switching Paths Waits for the Call to Return
&lt;/h3&gt;&lt;p&gt;Next, consider the Client&amp;rsquo;s &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/rpc.go#L83-L146" target="_blank" rel="noopener"
 &gt;&lt;code&gt;rpc()&lt;/code&gt;&lt;/a&gt;. The key sequence, simplified, is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindServer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;rpcErr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connPool&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RPC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Region&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Addr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;rpcErr&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;!=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NotifyFailedServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The Client first selects a server and then makes the RPC through the connection pool. &lt;strong&gt;Only after the current call returns an error&lt;/strong&gt; does it report the server failure and decide whether to retry at the RPC layer, based on the error type and remaining budget. If the error is not safe to retry automatically, it returns to the heartbeat loop above.&lt;/p&gt;
&lt;p&gt;The important delay here is how long it takes an RPC to return an error after a connection fails. Under the &lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/rpc" target="_blank" rel="noopener"
 &gt;default configuration&lt;/a&gt; and &lt;a class="link" href="https://github.com/hashicorp/yamux/blob/v0.1.2/session.go#L327-L379" target="_blank" rel="noopener"
 &gt;yamux implementation&lt;/a&gt;, when an established connection silently fails, the Keepalive interval is 30 seconds, and a Ping waits approximately 10 seconds for a Pong. Assuming the Ping write does not block and no Ping was already in flight before the failure, detecting the failure can take roughly 10-40 seconds, depending on the Keepalive phase.&lt;/p&gt;
&lt;p&gt;The retry budget is also checked only after the underlying call returns; it cannot interrupt that call early. This creates a possible race: &lt;strong&gt;the connection is still waiting to confirm a failure while the Leader&amp;rsquo;s node heartbeat budget has already expired.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="3-heartbeats-can-pass-through-a-follower-but-the-leader-renews-the-timer"&gt;3. Heartbeats Can Pass Through a Follower, but the Leader Renews the Timer
&lt;/h3&gt;&lt;p&gt;When a server receives the request, it first passes through the forwarding check in &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/node_endpoint.go#L655-L678" target="_blank" rel="noopener"
 &gt;&lt;code&gt;Node.UpdateStatus()&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forward&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;Node.UpdateStatus&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/rpc.go#L603-L618" target="_blank" rel="noopener"
 &gt;&lt;code&gt;forward()&lt;/code&gt;&lt;/a&gt; determines whether the current server should handle the request. If it is not the Leader, it forwards the request to the Leader.&lt;/p&gt;
&lt;p&gt;The Client does not need to contact the Leader directly every time, but its selected first-hop server must deliver the request promptly. &lt;strong&gt;If the Follower stops responding and the Client cannot switch paths in time, a healthy Leader may still fail to receive that Client&amp;rsquo;s heartbeat.&lt;/strong&gt;&lt;/p&gt;
&lt;h3 id="4-the-leader-waits-for-the-dynamic-ttl-plus-grace"&gt;4. The Leader Waits for the Dynamic TTL Plus Grace
&lt;/h3&gt;&lt;p&gt;Once the heartbeat successfully reaches the Leader, it calls &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L93-L135" target="_blank" rel="noopener"
 &gt;&lt;code&gt;resetHeartbeatTimer()&lt;/code&gt;&lt;/a&gt;. The function calculates an interval based on cluster size and then adds random jitter:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RateScaledInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MaxHeartbeatsPerSecond&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MinHeartbeatTTL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heartbeatTimers&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;helper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RandomStagger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resetHeartbeatTimerLocked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;srv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HeartbeatGrace&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ttl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;The returned &lt;code&gt;ttl&lt;/code&gt; determines the Client&amp;rsquo;s next heartbeat interval, while the Leader sets its expiration timer to &lt;code&gt;ttl + HeartbeatGrace&lt;/code&gt;.&lt;/strong&gt; &lt;code&gt;heartbeat_grace&lt;/code&gt; adds time for the Server to wait for the next successful heartbeat.&lt;/p&gt;
&lt;p&gt;Following the &lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#client-heartbeats" target="_blank" rel="noopener"
 &gt;official heartbeat documentation&lt;/a&gt;, the normal path can be understood as:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Reference node expiration time
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; = Time when the Leader last successfully processed a heartbeat and reset its timer
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; + Dynamic heartbeat TTL for that round
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; + heartbeat_grace
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;In this small cluster, the normal TTL was approximately 10-20 seconds. With the default 10-second grace, the Leader&amp;rsquo;s budget before declaring a Client&amp;rsquo;s heartbeat expired was approximately 20-30 seconds.&lt;/p&gt;
&lt;h3 id="5-timer-expiration-changes-node-status-before-triggering-scheduling"&gt;5. Timer Expiration Changes Node Status Before Triggering Scheduling
&lt;/h3&gt;&lt;p&gt;When the timer expires, the Leader runs &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L137-L181" target="_blank" rel="noopener"
 &gt;&lt;code&gt;invalidateHeartbeat()&lt;/code&gt;&lt;/a&gt;. The key node-status branch is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusDown&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;canDisconnect&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;hasPendingReconnects&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Status&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;structs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NodeStatusDisconnected&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Whether the node enters &lt;code&gt;disconnected&lt;/code&gt; depends on whether its allocations support reconnection and remain within their recovery windows. Not every heartbeat expiration leads to the same state transition.&lt;/p&gt;
&lt;p&gt;The &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/node_endpoint.go#L816-L873" target="_blank" rel="noopener"
 &gt;node-status update path&lt;/a&gt; then persists the necessary state changes through Raft and creates scheduling evaluations for the relevant transitions. The scheduler uses the Job type and disconnect policy to decide which allocations to retain temporarily, which to mark &lt;code&gt;lost&lt;/code&gt;, and whether to create replacements.&lt;/p&gt;
&lt;p&gt;For example, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/reconcile_cluster.go#L1097-L1127" target="_blank" rel="noopener"
 &gt;stop calculations for service allocations&lt;/a&gt; handle the lost set, while the &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/reconcile_node.go#L185-L263" target="_blank" rel="noopener"
 &gt;node reconciliation logic for system Jobs&lt;/a&gt; distinguishes reconnectable &lt;code&gt;unknown&lt;/code&gt; allocations from those that need to be treated as lost.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A heartbeat timeout does not mean the Leader directly sends a kill signal to an application process.&lt;/strong&gt; Node-status updates and scheduling plans sit between those events, and that is where the following configuration changes can intervene.&lt;/p&gt;
&lt;h3 id="6-after-reconnecting-the-client-applies-an-allocation-update-that-already-exists"&gt;6. After Reconnecting, the Client Applies an Allocation Update That Already Exists
&lt;/h3&gt;&lt;p&gt;The Client retrieves server-side allocation changes through &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L2482-L2545" target="_blank" rel="noopener"
 &gt;&lt;code&gt;watchAllocations()&lt;/code&gt;&lt;/a&gt;. Once an update reaches the local runner, &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/allocrunner/alloc_runner.go#L1078-L1101" target="_blank" rel="noopener"
 &gt;&lt;code&gt;handleAllocUpdate()&lt;/code&gt;&lt;/a&gt; checks whether the allocation is transitioning to a terminal state:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;stopping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Alloc&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;TerminalStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TerminalStatus&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setAlloc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;update&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;stopping&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;ar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;killTasks&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The terminal-state check also includes the Server&amp;rsquo;s desired state. Even if the original task is still running, the local runner stops it if the received update already requires termination.&lt;/p&gt;
&lt;p&gt;This connects the cause and effect seen in the logs: &lt;strong&gt;the Client did not switch heartbeat paths before its budget expired, so the Server updated node status and the scheduler created a stop plan. When the Client later reconnected, it applied the update and stopped the original task. Replacement means a new allocation starts, not that a task restarts within the original allocation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The community has also discussed similar symptoms in &lt;a class="link" href="https://github.com/hashicorp/nomad/issues/17974" target="_blank" rel="noopener"
 &gt;hashicorp/nomad#17974&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Understanding this chain reveals two intervention points: &lt;strong&gt;allow more time to switch heartbeat paths before declaring a node disconnected, or give the original tasks a chance to reconnect after that declaration.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="solution-1-preserve-original-tasks-and-access-paths-with-disconnect"&gt;Solution 1: Preserve Original Tasks and Access Paths with disconnect
&lt;/h2&gt;&lt;p&gt;For E2B, the first priority is to avoid unnecessarily replacing Orchestrator on a healthy Client because of a brief control-plane disconnection.&lt;/p&gt;
&lt;p&gt;We used Nomad&amp;rsquo;s &lt;code&gt;disconnect&lt;/code&gt; policy to separate &amp;ldquo;detect that the node is disconnected&amp;rdquo; from &amp;ldquo;immediately abandon its original tasks.&amp;rdquo; The configuration belongs in an existing Job&amp;rsquo;s &lt;strong&gt;task group&lt;/strong&gt;, for example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-hcl" data-lang="hcl"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;group&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;orchestrator&amp;#34;&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;disconnect&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; lost_after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;50s&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; replace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;false&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; reconcile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;keep_original&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&lt;span class="c1"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt; # Keep the existing task, network, and other definitions unchanged.
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Orchestrator, session-proxy, and client-proxy should each be evaluated and configured in their respective groups where protection is needed.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Setting&lt;/th&gt;
 &lt;th&gt;What it addresses&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;lost_after = &amp;quot;50s&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Provides a bounded reconnection window after an allocation enters &lt;code&gt;unknown&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;replace = false&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Prevents immediate replacement during the disconnect retention phase&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;reconcile = &amp;quot;keep_original&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Prefers the original instance during applicable reconnection reconciliation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Explicitly setting &lt;code&gt;replace = false&lt;/code&gt; prevents an immediate replacement from being created within the retention window. The corresponding &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/scheduler/reconciler/filters.go#L527-L544" target="_blank" rel="noopener"
 &gt;scheduler check&lt;/a&gt; is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;isDisconnecting&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;alloc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReplaceOnDisconnect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The other distinction is when the window starts. &lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/structs/alloc.go#L756-L779" target="_blank" rel="noopener"
 &gt;&lt;code&gt;Allocation.Expired()&lt;/code&gt;&lt;/a&gt; calculates expiration from the allocation&amp;rsquo;s most recent transition into &lt;code&gt;unknown&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;lastUnknown&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LastUnknown&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;lastUnknown&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetDisconnectLostAfter&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;expiry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Sub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Therefore, &lt;code&gt;lost_after = &amp;quot;50s&amp;quot;&lt;/code&gt; does not mean waiting 50 seconds from the server&amp;rsquo;s failure, and it is not equivalent to &lt;code&gt;heartbeat_grace = &amp;quot;50s&amp;quot;&lt;/code&gt;. Assuming a TTL of 15 seconds and a grace of 10 seconds, the approximate sequence is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t = 0 Last successful heartbeat at the Leader
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ 25s Heartbeat expires; node disconnection handling begins
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ 25s+ Relevant allocation enters unknown
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;t ~ (25+50)=75s+ If recovery has not completed, the 50-second retention window expires
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="disconnect-sequence-retain-the-original-task-then-wait-for-reconnection"&gt;disconnect Sequence: Retain the Original Task, Then Wait for Reconnection
&lt;/h3&gt;&lt;p&gt;The following diagram assumes that the original task on a healthy Client is still running. It shows how the &lt;code&gt;50s / false / keep_original&lt;/code&gt; policy handles a brief disconnection:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: How disconnect retains the original task after node disconnection
 accDescr: t=0 is the last successful heartbeat. With an illustrative TTL of 15 seconds and grace of 10 seconds, expiration occurs at about 25 seconds. A separate 50-second window starts when the allocation enters unknown. The example either reconnects at 40 seconds or proceeds to expiration handling after approximately 75 seconds. These are not measured timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant L as Leader
 participant S as Scheduler
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=10s, lost_after=50s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;L: t=0: Last successful heartbeat through a server
 L--&gt;&gt;C: Return dynamic TTL=15s
 Note over Time: 5s
 Note over C,L: t=5s: Assume the original first-hop server fails
 Note over Time: About 15s
 C-xL: t~15s: Heartbeat through the original path does not succeed
 Note over Time: About 25s&lt;br/&gt;Heartbeat budget expires
 Note over L: t~25s (15+10): Heartbeat budget expires
 L-&gt;&gt;L: Mark node as disconnected
 L-&gt;&gt;S: Trigger scheduling evaluation
 Note over Time: tU ~ 25s+&lt;br/&gt;Retention window starts
 S-&gt;&gt;L: tU~25s+: Mark original allocation as unknown
 Note over L,S: Start a separate 50s window at tU&lt;br/&gt;Expiration: tU+50s~75s+
 Note over S,T: replace = false: No immediate replacement
 Note over T: Original task keeps running on the healthy host

 alt Assume connectivity returns at t=40s within the window
 Note over Time: 40s&lt;br/&gt;Still within the retention window
 C-&gt;&gt;L: Restore heartbeat through another healthy server
 C-&gt;&gt;L: Synchronize original allocation runtime state
 L-&gt;&gt;S: Trigger reconnection reconciliation
 S-&gt;&gt;L: Retain original instance under keep_original
 Note over T: t=40s+: Preserve task identity without a disconnection-induced restart
 else Still disconnected at tU+50s~75s+
 Note over Time: tU + 50s ~ 75s+&lt;br/&gt;Retention window expires
 S-&gt;&gt;L: Window expires, proceed to lost and subsequent recovery
 end&lt;/pre&gt;&lt;p&gt;This does not extend the node&amp;rsquo;s heartbeat budget. Instead, it delays the decision to abandon the original allocation: &lt;strong&gt;enter &lt;code&gt;unknown&lt;/code&gt;, avoid immediate replacement, and wait for the original instance to reconnect.&lt;/strong&gt; The node may still not be &lt;code&gt;ready&lt;/code&gt; during this period, and retaining the original instance is no longer guaranteed after the window expires.&lt;/p&gt;
&lt;p&gt;Protection also needs to follow the user access path. Preserving Orchestrator alone does not necessarily preserve an existing connection. If client-proxy is replaced, the sandbox can remain alive while the connection breaks. We therefore included session-proxy and client-proxy in the validation.&lt;/p&gt;
&lt;p&gt;In one test with a 50-second disconnect window, Nomad Clients did briefly enter &lt;code&gt;disconnected&lt;/code&gt;. The observed &lt;code&gt;unknown&lt;/code&gt; intervals for their original allocations were approximately 1.7-31.6 seconds, followed by recovery within the window:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Validation item&lt;/th&gt;
 &lt;th&gt;Observation&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Orchestrator, session-proxy, and client-proxy&lt;/td&gt;
 &lt;td&gt;Original allocations preserved; no additional task restarts&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Original sandbox process identities, memory, and files&lt;/td&gt;
 &lt;td&gt;10 / 10 preserved; final independent checks completed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Original HTTP NDJSON streams&lt;/td&gt;
 &lt;td&gt;23 / 23 preserved&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Short requests during the formal observation period&lt;/td&gt;
 &lt;td&gt;12,581 requests, 0 errors&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;With disconnect configured, &lt;strong&gt;the Clients briefly lost control-plane contact, but their original tasks remained in place until reconnection, without being replaced.&lt;/strong&gt;&lt;/p&gt;
&lt;h2 id="beyond-task-preservation-how-node-status-affects-sandbox-creation-capacity"&gt;Beyond Task Preservation: How Node Status Affects Sandbox Creation Capacity
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;disconnect&lt;/code&gt; preserves original tasks; it does not prevent a Client from being declared disconnected after its heartbeat expires. In other words, &lt;strong&gt;Orchestrator and existing sandboxes can remain running without the node remaining eligible to receive new sandbox placements.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In this E2B implementation, the API normally synchronizes node information from Nomad every 20 seconds, selecting nodes in the sandbox node pool whose status is &lt;code&gt;ready&lt;/code&gt;. The core filter is:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-go" data-lang="go"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nx"&gt;nomadapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;QueryOptions&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Filter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;`Status == &amp;#34;ready&amp;#34; and NodePool == &amp;#34;default&amp;#34;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;If the query succeeds but a previously known node no longer appears in the results, the API marks it &lt;code&gt;unhealthy&lt;/code&gt;, affecting subsequent placement.&lt;/p&gt;
&lt;p&gt;The public E2B runtime also has an implementation that discovers Orchestrators through allocations. Its &lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/shared/pkg/clusters/discovery/nomad.go#L29-L57" target="_blank" rel="noopener"
 &gt;discovery filter&lt;/a&gt; requires &lt;code&gt;ClientStatus == &amp;quot;running&amp;quot;&lt;/code&gt;. Its &lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/api/internal/orchestrator/cache.go#L195-L211" target="_blank" rel="noopener"
 &gt;API synchronization logic&lt;/a&gt; also removes an original instance from the local node pool when that instance no longer appears in valid discovery results.&lt;/p&gt;
&lt;p&gt;The two implementations filter different objects, but they highlight the same issue: &lt;strong&gt;placement decisions depend on Nomad&amp;rsquo;s state, not only on whether the original Orchestrator process is alive.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Under a high sandbox creation rate, some healthy hosts may continue running existing sandboxes while temporarily being unable to share the load of new requests. Creation pressure shifts to the remaining eligible nodes. If they lack sufficient capacity, creation can fail, queue, or time out.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;This is fundamentally a blast-radius problem.&lt;/strong&gt; The failure originates in a single server and directly disrupts communication for some Clients. Node state then affects API placement, transferring pressure to other healthy nodes. &lt;code&gt;disconnect&lt;/code&gt; contains the &amp;ldquo;destroy original tasks&amp;rdquo; path, but it does not automatically contain the &amp;ldquo;exclude healthy capacity&amp;rdquo; and &amp;ldquo;overload the remaining nodes&amp;rdquo; paths.&lt;/p&gt;
&lt;h2 id="solution-2-reduce-false-node-failure-detections-with-heartbeat_grace"&gt;Solution 2: Reduce False Node Failure Detections with heartbeat_grace
&lt;/h2&gt;&lt;p&gt;&lt;code&gt;disconnect&lt;/code&gt; addresses the situation after a node has been declared disconnected: do not abandon its original tasks immediately. Since node state also affects creation capacity, we need to look one step earlier. Can we give the Client enough time to switch paths before declaring it disconnected?&lt;/p&gt;
&lt;p&gt;This is where the Server-side &lt;strong&gt;&lt;code&gt;heartbeat_grace&lt;/code&gt;&lt;/strong&gt; setting applies:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-hcl" data-lang="hcl"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;server&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt; heartbeat_grace&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;50s&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;It adds time beyond the dynamic TTL. Changing the default grace from 10 to 50 seconds adds 40 seconds of headroom. Combined with the normal TTL of approximately 10-20 seconds, the reference budget becomes roughly 60-70 seconds from the last successful heartbeat.&lt;/p&gt;
&lt;h3 id="heartbeat_grace-sequence-allow-more-switching-time-before-declaring-a-node-disconnected"&gt;heartbeat_grace Sequence: Allow More Switching Time Before Declaring a Node Disconnected
&lt;/h3&gt;&lt;p&gt;Unlike disconnect, this changes how long the Leader waits for the next successful heartbeat. The example still assumes that the same Leader remains healthy throughout:&lt;/p&gt;
&lt;pre class="mermaid" style="visibility:hidden"&gt;sequenceDiagram
 accTitle: How heartbeat_grace provides switching time before node failure detection
 accDescr: t=0 is the last successful heartbeat. With an illustrative TTL of 15 seconds and grace of 50 seconds, expiration is at 65 seconds. A heartbeat restored at 40 seconds prevents this disconnection. If no heartbeat succeeds by 65 seconds, failure handling begins. These are illustrative, not measured, timestamps.
 autonumber
 participant Time as Relative timeline
 participant C as Nomad Client
 participant F as Original Follower
 participant L as Leader
 participant T as Original task

 Note over C,T: Illustrative: TTL=15s, grace=50s&lt;br/&gt;t=0: last successful heartbeat at Leader&lt;br/&gt;Not measured or to scale, scheduling delay omitted
 Note over Time: 0s&lt;br/&gt;Last successful heartbeat
 C-&gt;&gt;F: Node.UpdateStatus
 F-&gt;&gt;L: t=0: Forward heartbeat and renew timer
 L-&gt;&gt;L: Set expiration to t=65s (15+50)
 L--&gt;&gt;F: Return dynamic TTL=15s
 F--&gt;&gt;C: Schedule next heartbeat using the dynamic TTL

 Note over Time: 5s
 Note over F: t=5s: Assume the server fails
 Note over Time: About 15s
 C-xF: t~15s: Next heartbeat RPC does not return promptly
 Note over C: Wait for failure detection and switch to a healthy server
 Note over Time: About 25s&lt;br/&gt;Expiration with default grace
 Note over L: t~25s: Default grace would have expired&lt;br/&gt;The extended budget still runs until t=65s

 alt Assume heartbeat recovers at t=40s, before t=65s
 Note over Time: 40s&lt;br/&gt;Before the 65s expiration point
 C-&gt;&gt;L: Complete heartbeat through a healthy server
 L-&gt;&gt;L: Reset the TTL + grace timer
 Note over L,T: No node disconnection or task replacement triggered by this heartbeat failure
 else Still no heartbeat at t=65s, or the Client has actually failed
 Note over Time: About 65s&lt;br/&gt;15s TTL + 50s grace
 Note over L: t~65s (15+50): Heartbeat budget expires
 L-&gt;&gt;L: t~65s+: Declare node failure and begin subsequent task handling
 end&lt;/pre&gt;&lt;p&gt;Across two complete server-stop tests with grace set to 50 seconds and no disconnect policy on the key tasks, none of the 20 surviving Clients in either run was observed entering &lt;code&gt;down&lt;/code&gt; or &lt;code&gt;disconnected&lt;/code&gt;, and no original tasks were replaced. This matched the expected behavior: &lt;strong&gt;if switching completes before the budget expires, there is no need to enter the subsequent disconnection and task-handling paths.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;heartbeat_grace&lt;/code&gt; gives the Leader more time to wait.&lt;/strong&gt; If connectivity recovers in time, that heartbeat failure does not lead to a node-disconnection decision.&lt;/p&gt;
&lt;p&gt;However, the cost becomes clearer when a Client actually fails. When a Server fails but the Client is healthy, we want to wait a little longer. When the Client&amp;rsquo;s host really has failed, we want to detect it quickly and stop placing work there. The same heartbeat signal cannot guarantee both &amp;ldquo;never falsely mark a healthy node down&amp;rdquo; and &amp;ldquo;immediately detect a real failure.&amp;rdquo; A longer grace delays real Client-failure detection at the Nomad heartbeat layer, although application health checks may exclude the failed node earlier.&lt;/p&gt;
&lt;p&gt;From a blast-radius perspective, the two adjustments place the cost at different points:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Adjustment&lt;/th&gt;
 &lt;th&gt;Benefit&lt;/th&gt;
 &lt;th&gt;Cost and boundary&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Moderately increase &lt;code&gt;heartbeat_grace&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Give the Leader more time for the next heartbeat, reduce false failure detections on healthy Clients, and lower the risk of unnecessary task replacement or exclusion from the creation pool&lt;/td&gt;
 &lt;td&gt;Real Client failures also become visible later at the Nomad heartbeat layer&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Shorter grace + targeted &lt;code&gt;disconnect&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Expose disconnection sooner after a real Client failure while giving original tasks a short reconnection window&lt;/td&gt;
 &lt;td&gt;During a Server failure, some healthy Clients may still temporarily leave the creation pool, shifting pressure to the remaining nodes&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h2 id="summary-turning-heartbeat-findings-into-sandbox-resilience"&gt;Summary: Turning Heartbeat Findings into Sandbox Resilience
&lt;/h2&gt;&lt;p&gt;This investigation started with a simple question: why were tasks on healthy hosts rebuilt when only one Server was lost? Following the logs and code revealed a chain of architectural decisions that affect workloads: &lt;strong&gt;whether a heartbeat arrives in time, whether the node is declared disconnected, whether the original tasks are retained, and whether the layer above still treats the node as available capacity.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For E2B, we need to protect three goals separately:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Existing sandbox runtime state&lt;/strong&gt;: Separate node disconnection from immediately abandoning original tasks, leaving room to recover from brief control-plane failures.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Existing user connections&lt;/strong&gt;: Cover Orchestrator, session-proxy, and client-proxy rather than only verifying that the sandbox still exists.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Creation capacity and real-failure recovery&lt;/strong&gt;: Observe both the erroneous exclusion of healthy nodes and delays in detecting failed nodes or replacing tasks, while maintaining headroom on the remaining capacity.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Ultimately, resilience does not mean every failure causes zero interruption. It means preserving valuable runtime state during short disconnections, keeping recovery bounded when a real failure occurs, and limiting the knock-on effects to a level the workload can tolerate.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#heartbeat_grace" target="_blank" rel="noopener"
 &gt;Nomad Server Configuration: heartbeat_grace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/configuration/server#client-heartbeats" target="_blank" rel="noopener"
 &gt;Nomad Client Heartbeats&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://developer.hashicorp.com/nomad/docs/job-specification/disconnect" target="_blank" rel="noopener"
 &gt;Nomad disconnect Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/client/client.go#L1870-L1923" target="_blank" rel="noopener"
 &gt;Nomad Client Heartbeat Scheduling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/heartbeat.go#L56-L181" target="_blank" rel="noopener"
 &gt;Nomad Leader Heartbeat Timers and Expiration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/blob/bf24b0dd97430d3b364b68b1f7375e85e585778f/nomad/structs/alloc.go#L756-L779" target="_blank" rel="noopener"
 &gt;Nomad Allocation Disconnect Expiration&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/shared/pkg/clusters/discovery/nomad.go#L29-L57" target="_blank" rel="noopener"
 &gt;E2B Runtime: Nomad Discovery Filters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/e2b-dev/runtime/blob/afaaeaef96aae86f65ce60bf38fe083e7638a28e/packages/api/internal/orchestrator/cache.go#L53-L120" target="_blank" rel="noopener"
 &gt;E2B Runtime: API Node Synchronization&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/hashicorp/nomad/issues/17974" target="_blank" rel="noopener"
 &gt;hashicorp/nomad#17974: Non-Leader Connectivity Problems and Allocation Restarts&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>Building Resilient Azure AI Architecture with API Management Gateway (Part 2) — Retry Observability and Alerting</title><link>https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/</link><pubDate>Wed, 29 Apr 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building Resilient Azure AI Architecture with API Management Gateway (Part 2) — Retry Observability and Alerting" /&gt;&lt;p&gt;In the previous article &lt;a class="link" href="../azure-apim-ai-resilience/" &gt;&lt;em&gt;Building Resilient Azure AI Architecture with API Management Gateway&lt;/em&gt;&lt;/a&gt;, we built a resilient gateway architecture for multiple Microsoft Foundry resources using Azure API Management — Backend Load Balancing Pool for load distribution, and a &lt;code&gt;retry&lt;/code&gt; policy that automatically switches to healthy instances when backends return 4xx/5xx errors. This mechanism makes backend failures nearly invisible to clients.&lt;/p&gt;
&lt;p&gt;But &amp;ldquo;invisible&amp;rdquo; is precisely the challenge for operations teams: &lt;strong&gt;The Retry mechanism, while protecting clients, also hides the details of intermediate failures.&lt;/strong&gt; When a request goes through 3 retries and ultimately returns 200, operations staff have no way of knowing why the first two attempts failed — was it a 429 rate limit? A 404 deployment not found? Or a 500 server error? Which backend had the issue? What did the error body say?&lt;/p&gt;
&lt;p&gt;This article builds on the previous architecture to achieve complete Retry observability through &lt;strong&gt;Application Insights Diagnostic Logs&lt;/strong&gt;, &lt;strong&gt;without modifying any Policy code&lt;/strong&gt;, helping developers and operations teams:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;📋 &lt;strong&gt;Record detailed information for each Retry failure&lt;/strong&gt; — including the failed backend address, HTTP status code, error response body, and APIM request ID&lt;/li&gt;
&lt;li&gt;🔍 &lt;strong&gt;Reconstruct complete Retry chains via KQL queries&lt;/strong&gt; — using operation_Id to correlate all retry records for the same request&lt;/li&gt;
&lt;li&gt;🔔 &lt;strong&gt;Configure automated alerts&lt;/strong&gt; — notify the operations team via email when Retry failure counts exceed thresholds&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-architecture-overview"&gt;1. Architecture Overview
&lt;/h3&gt;&lt;p&gt;This article adds an Application Insights observability layer on top of the APIM + Foundry resilient architecture from the previous article. No changes to existing APIM Policies or Backend configurations are needed — Retry monitoring and alerting are achieved entirely through Diagnostic Logs configuration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Components from the previous article (unchanged):&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Component&lt;/th&gt;
 &lt;th&gt;Role&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Azure API Management&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Unified gateway, executing routing, Retry, and load balancing policies&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Backend Load Balancing Pool&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Pool of multiple Foundry resources, distributing requests via Round-Robin&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;New components in this article:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Component&lt;/th&gt;
 &lt;th&gt;Role&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Application Insights&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Receives APIM native telemetry (&lt;code&gt;requests&lt;/code&gt; / &lt;code&gt;dependencies&lt;/code&gt;), recording details of each Retry backend call&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Log Alert&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Automatically triggers alert notifications based on KQL queries when failure counts exceed thresholds&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Action Group&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Defines alert actions (Email/SMS/Webhook)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Core design principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Zero code changes&lt;/strong&gt; — No Policy modifications needed, fully implemented through APIM Diagnostic Logs configuration&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Native Telemetry&lt;/strong&gt; — APIM&amp;rsquo;s &lt;code&gt;dependencies&lt;/code&gt; table automatically records each &lt;code&gt;forward-request&lt;/code&gt; call (including intermediate Retry failures), combined with Backend Response Headers to log and Body bytes configuration to capture error details and Foundry request IDs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Full Retry chain visibility&lt;/strong&gt; — All Retry attempts for the same request share the same &lt;code&gt;operation_Id&lt;/code&gt;, making it easy to reconstruct the complete retry chain in KQL&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Proactive alerting&lt;/strong&gt; — Log Alert rules automatically detect failure trends and promptly notify the operations team&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The following steps will guide you through building the complete Retry observability solution:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Section&lt;/th&gt;
 &lt;th&gt;Action&lt;/th&gt;
 &lt;th&gt;Goal&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;2&lt;/td&gt;
 &lt;td&gt;Create Application Insights&lt;/td&gt;
 &lt;td&gt;Set up log storage and query infrastructure&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;3&lt;/td&gt;
 &lt;td&gt;Bind APIM Logger&lt;/td&gt;
 &lt;td&gt;Associate APIM with App Insights&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;4&lt;/td&gt;
 &lt;td&gt;Configure API Diagnostic Logs&lt;/td&gt;
 &lt;td&gt;Enable App Insights logging for the target API, configure Verbosity, Backend Response Headers and Body&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;5&lt;/td&gt;
 &lt;td&gt;Verify Data&lt;/td&gt;
 &lt;td&gt;Confirm Retry records in the dependencies table in App Insights&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;6&lt;/td&gt;
 &lt;td&gt;Configure Alert Rules&lt;/td&gt;
 &lt;td&gt;Create Log Alert + Action Group for automated email notifications&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="2-create-an-application-insights-instance"&gt;2. Create an Application Insights Instance
&lt;/h3&gt;&lt;p&gt;Create a workspace-based Application Insights instance in the same subscription and region as APIM to receive APIM telemetry data.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2-1) In the Azure Portal search bar, type Application Insights and select the &lt;strong&gt;Application Insights&lt;/strong&gt; service&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="401px" data-flex-grow="167" height="1408" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights_hu_acf1cf99e17b7e8b.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights_hu_61fc3fb0044de7d7.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-1-search-application-insights.png 2357w" width="2357"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2-2) Click &lt;strong&gt;+ Create&lt;/strong&gt;, fill in the following key configurations and complete creation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="232px" data-flex-grow="96" height="1442" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-2-create-app-insights.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-2-create-app-insights_hu_2128c60a8f4cd365.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/2-2-create-app-insights.png 1396w" width="1396"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription&lt;/td&gt;
 &lt;td&gt;Same as APIM&lt;/td&gt;
 &lt;td&gt;Keep in the same subscription for easier management&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Resource group&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;apim-ai-hub&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Same resource group as APIM&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-app-insight&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Specify an Application Insights instance name&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Region&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;Japan East&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Same region as APIM to reduce data transmission latency&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Workspace&lt;/td&gt;
 &lt;td&gt;Default&lt;/td&gt;
 &lt;td&gt;System automatically creates a Log Analytics workspace, or select one you&amp;rsquo;ve created&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Application Insights is workspace-based, with underlying data stored in a Log Analytics workspace. A workspace is automatically associated during creation — no manual creation needed.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="3-bind-apim-logger"&gt;3. Bind APIM Logger
&lt;/h3&gt;&lt;p&gt;Associate the Application Insights instance created in the previous step with APIM, enabling APIM to send telemetry data to App Insights.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;3-1) Navigate to the APIM instance → Left menu &lt;strong&gt;Monitoring&lt;/strong&gt; → &lt;strong&gt;Application Insights&lt;/strong&gt; → Click &lt;strong&gt;+ Add&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="440px" data-flex-grow="183" height="1451" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger_hu_cf14459b12773009.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger_hu_8a6c1a171bfb24d4.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger_hu_77c3530864ca1fd1.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/3-1-bind-apim-logger.png 2665w" width="2665"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Application Insights instance&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-app-insight&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Select the Application Insights instance just created&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;☐ Use as default and enable basic logging for all APIs&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Unchecked&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;When checked, this Logger becomes the global default, enabling basic logging for all APIs. Can reduce throughput by 40-50% under high traffic (&amp;gt;1000 req/s). Recommended to enable per-API in Step 4&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;☐ Add availability monitor&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Unchecked&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;When checked, App Insights periodically validates whether the APIM gateway endpoint is responding, with results displayed in App Insights&amp;rsquo; Availability panel. Not needed for this scenario&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="4-configure-api-diagnostic-logs"&gt;4. Configure API Diagnostic Logs
&lt;/h3&gt;&lt;p&gt;Enable Application Insights logging for the target AI API, configuring sampling rate, error recording, Backend Response, and other parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4-1) Navigate to APIM → APIs → Select target API → Switch to &lt;strong&gt;Settings&lt;/strong&gt; tab → Scroll down to &lt;strong&gt;Diagnostic Logs&lt;/strong&gt; area, click Enable&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="370px" data-flex-grow="154" height="1715" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs_hu_6a90258f10a6380d.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs_hu_79eb34df8dfd1a1c.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs_hu_7fe7c175d99acd5b.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-1-api-settings-diagnostic-logs.png 2647w" width="2647"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;4-2) Select the &lt;strong&gt;Application Insights&lt;/strong&gt; tab, check &lt;strong&gt;Enable&lt;/strong&gt;, and configure with the following parameters&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="267px" data-flex-grow="111" height="1212" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-2-diagnostic-logs-config.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-2-diagnostic-logs-config_hu_872049444957e930.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/4-2-diagnostic-logs-config.png 1350w" width="1350"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Destination&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-app-insight&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Select the bound App Insights instance. ⚠️ If it shows &amp;ldquo;No App Insights were configured&amp;rdquo;, go back to Step 3 to bind the Logger first&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Sampling (%)&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;100&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Sampling rate, 100 for full recording (can be adjusted to 25-50 for production)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Always log errors&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;✅ Enable&lt;/td&gt;
 &lt;td&gt;Ensures error requests are not filtered by sampling — all 4xx/5xx are recorded even when Sampling &amp;lt; 100%&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Log client IP address&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;✅ Enable&lt;/td&gt;
 &lt;td&gt;Records caller IP, useful for troubleshooting&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Support custom metrics&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Unchecked&lt;/td&gt;
 &lt;td&gt;Only needed when using the &lt;code&gt;emit-metric&lt;/code&gt; policy, not used in this article&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Verbosity&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;⚠️ &lt;code&gt;Information&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Controls the logging level for native telemetry, &lt;strong&gt;recommended to select Information or Verbose&lt;/strong&gt; (see explanation below)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Correlation protocol&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;W3C&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Modern standard, supports cross-service distributed tracing using &lt;code&gt;traceparent&lt;/code&gt; header to correlate frontend and backend requests&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;Verbosity Configuration Notes:&lt;/strong&gt;&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;❗ Important:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Verbosity should be set to &lt;code&gt;Information&lt;/code&gt; or &lt;code&gt;Verbose&lt;/code&gt;, not &lt;code&gt;Error&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Verbosity controls the logging level for the entire request chain&lt;/strong&gt; — successful final requests (200) are treated as information level, failed requests (4xx/5xx) are treated as error level.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Verbosity&lt;/th&gt;
 &lt;th&gt;Final Request Successful (200)&lt;/th&gt;
 &lt;th&gt;Final Request Failed (4xx/5xx)&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Information&lt;/strong&gt; ✅&lt;/td&gt;
 &lt;td&gt;✅ &lt;code&gt;requests&lt;/code&gt; recorded + ✅ All &lt;code&gt;dependencies&lt;/code&gt; recorded (including intermediate Retry failures)&lt;/td&gt;
 &lt;td&gt;✅ &lt;code&gt;requests&lt;/code&gt; recorded + ✅ All &lt;code&gt;dependencies&lt;/code&gt; recorded&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Error&lt;/strong&gt; ❌&lt;/td&gt;
 &lt;td&gt;❌ &lt;strong&gt;Nothing recorded&lt;/strong&gt; (both request and dependencies are lost)&lt;/td&gt;
 &lt;td&gt;✅ &lt;code&gt;requests&lt;/code&gt; recorded + ✅ Associated &lt;code&gt;dependencies&lt;/code&gt; recorded&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;⚠️ Warning:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When Verbosity=Error:&lt;/strong&gt; If 2 out of 3 Retry attempts fail (404) but the 3rd attempt succeeds (200), the entire request chain &lt;strong&gt;will not appear in any table&lt;/strong&gt; — including those 2 intermediate failed dependency records. This creates a blind spot for Retry observability.&lt;/p&gt;
&lt;p&gt;Selecting &lt;code&gt;Information&lt;/code&gt; ensures that regardless of whether the final result is success or failure, all Retry processes are fully recorded.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Additional settings — Backend Response Configuration:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This is the key configuration for achieving Retry observability. Check &lt;strong&gt;Backend Response&lt;/strong&gt;, configure Headers to log and Body bytes — APIM will include this information in the &lt;code&gt;Properties&lt;/code&gt; field of the &lt;code&gt;dependencies&lt;/code&gt; table.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Headers to log&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;apim-request-id&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Records the request tracking ID returned by Foundry, used for locating backend requests when submitting support tickets to Microsoft&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Body bytes to log&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;1024&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Records the first 1KB of response body, capturing error JSON details (e.g., &lt;code&gt;DeploymentNotFound&lt;/code&gt;, &lt;code&gt;Too Many Requests&lt;/code&gt;, etc.)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Headers to log and Body bytes apply to &lt;strong&gt;all sampled requests&lt;/strong&gt; (including successful ones), without distinguishing between success and failure. This data appears in two App Insights tables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;requests&lt;/code&gt; table&lt;/strong&gt; — Records the final request returned to the client (frontend perspective)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;dependencies&lt;/code&gt; table&lt;/strong&gt; — Records each &lt;code&gt;forward-request&lt;/code&gt; call (backend perspective, &lt;strong&gt;including each intermediate Retry attempt&lt;/strong&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The &lt;code&gt;dependencies&lt;/code&gt; table is the core data source for Retry observability.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;Click &lt;strong&gt;Save&lt;/strong&gt; to complete the configuration.&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;⚠️ Warning:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Configuration changes take &lt;strong&gt;several minutes&lt;/strong&gt; to propagate to APIM gateway instances. If you test immediately and see no data, please wait 5 minutes before checking.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="5-verify-data"&gt;5. Verify Data
&lt;/h3&gt;&lt;p&gt;After configuration is complete, send a few test requests that will trigger Retry (e.g., call a non-existent model deployment), wait approximately 5 minutes, then verify data in Application Insights.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5-1) Navigate to Application Insights → Monitoring → ① Logs, ② Switch to Tables view, ③ Find the &lt;code&gt;dependencies&lt;/code&gt; table under the Application Insights category and click Run&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="357px" data-flex-grow="149" height="1691" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records_hu_9e2b1c9bfe29f3e2.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records_hu_2d9636b048ff0ea2.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records_hu_8f8d132eb0a3836c.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-1-dependencies-retry-records.png 2522w" width="2522"&gt;&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;dependencies&lt;/code&gt; table generates an independent record for &lt;strong&gt;each &lt;code&gt;forward-request&lt;/code&gt; call&lt;/strong&gt; within the &lt;code&gt;retry&lt;/code&gt; block — including intermediate failed Retry attempts and the final successful request. In the results on the right, you can see multiple records pointing to different backends (e.g., &lt;code&gt;ai-hub-swedencentral-02&lt;/code&gt;, &lt;code&gt;ai-hub-eastus2-01&lt;/code&gt;, etc.), all belonging to the same request&amp;rsquo;s Retry chain.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;5-2) Expand a single record to view detailed fields and customDimensions&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="380px" data-flex-grow="158" height="1779" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions_hu_3ec02de0c7557683.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions_hu_da9d711ed95de465.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions_hu_4b398a8a1c40a260.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/5-2-dependency-custom-dimensions.png 2824w" width="2824"&gt;&lt;/p&gt;
&lt;p&gt;After expanding, you can see the complete information of the dependency record, with key troubleshooting fields highlighted:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Field&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;target&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Full address of the requested backend&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;resultCode&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;HTTP status code&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;duration&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Call duration&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;code&gt;success&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Whether successful&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;Response-Body&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Success/error response JSON details&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;&lt;code&gt;Response-apim-request-id&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Foundry-side request ID (different for each Retry), useful for submitting troubleshooting tickets&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;💡 Tip:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Use &lt;code&gt;operation_Id&lt;/code&gt; to link all Retry records for the same request together, reconstructing the complete retry chain. &lt;code&gt;Response-apim-request-id&lt;/code&gt; can be used to locate specific backend requests when submitting support tickets to Microsoft.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="6-configure-alert-rules"&gt;6. Configure Alert Rules
&lt;/h3&gt;&lt;p&gt;Automatically notify the operations team via email when Retry failure counts exceed thresholds. Alert rules are created on &lt;strong&gt;Application Insights&lt;/strong&gt; (not APIM).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-1) Write and verify KQL query on the Logs page&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Navigate to Application Insights → Monitoring → &lt;strong&gt;Logs&lt;/strong&gt;, ① create a new query tab, ② switch to &lt;strong&gt;KQL mode&lt;/strong&gt;, ③ enter the following query and ④ click &lt;strong&gt;Run&lt;/strong&gt; to verify results:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="375px" data-flex-grow="156" height="1172" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs_hu_642be3aedad56c4.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs_hu_cc2065b85fea4e56.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-1-kql-query-in-logs.png 1836w" width="1836"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where resultCode in (&amp;#34;429&amp;#34;, &amp;#34;500&amp;#34;, &amp;#34;502&amp;#34;, &amp;#34;503&amp;#34;, &amp;#34;504&amp;#34;) or toint(resultCode) &amp;gt;= 400
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize FailureCount = count()
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Adjust the status code filter as needed:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Scenario&lt;/th&gt;
 &lt;th&gt;Filter Condition&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;All failures&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where success == false&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Rate limiting only (429)&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where resultCode == &amp;quot;429&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;5xx server errors only&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where toint(resultCode) &amp;gt;= 500&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;429 + 5xx&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where resultCode == &amp;quot;429&amp;quot; or toint(resultCode) &amp;gt;= 500&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Exclude 404&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;where success == false and resultCode != &amp;quot;404&amp;quot;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Confirm the query returns the expected FailureCount value.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-2) Click the &lt;strong&gt;&lt;code&gt;...&lt;/code&gt;&lt;/strong&gt; menu in the top right → &lt;strong&gt;+ New alert rule&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="342px" data-flex-grow="142" height="647" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-2-new-alert-rule.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-2-new-alert-rule_hu_cc3b6550b8d4b269.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-2-new-alert-rule.png 922w" width="922"&gt;&lt;/p&gt;
&lt;p&gt;The system will automatically carry over the current KQL query into the Alert rule&amp;rsquo;s Condition configuration.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-3) Confirm query and Measurement configuration on the Condition tab&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="267px" data-flex-grow="111" height="1473" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement_hu_581e6c3507c648d5.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement_hu_918929b7c5602c7b.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-3-alert-condition-measurement.png 1642w" width="1642"&gt;&lt;/p&gt;
&lt;p&gt;Three key configurations highlighted in the screenshot:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Annotation&lt;/th&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;①&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Search query&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;KQL from previous step (auto-populated)&lt;/td&gt;
 &lt;td&gt;Contains &lt;code&gt;success == false&lt;/code&gt; + status code filter + &lt;code&gt;summarize FailureCount = count()&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;②&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Measure&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;FailureCount&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;⚠️ Must select &lt;code&gt;FailureCount&lt;/code&gt; (auto-detected from KQL&amp;rsquo;s summarize). If it defaults to &lt;code&gt;Table rows&lt;/code&gt;, switch manually&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;③&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Aggregation granularity&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;3 hours&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Aggregation granularity — aggregates FailureCount within this time range into a single data point for evaluation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Other settings keep defaults:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Query type&lt;/strong&gt;: Aggregated logs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aggregation type&lt;/strong&gt;: Total (sum of FailureCount)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;6-4) Scroll down to configure Alert logic&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="358px" data-flex-grow="149" height="1113" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold_hu_e8c567663cf0217.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold_hu_10bae5d7c67dab9d.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-4-alert-logic-threshold.png 1661w" width="1661"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Threshold type&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Static&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Use fixed threshold&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Operator&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Greater than&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Trigger when FailureCount exceeds threshold&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Threshold value&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;10&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Adjust based on business tolerance&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Frequency of evaluation&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;15 minutes&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Evaluate every 15 minutes&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;📊 The &lt;strong&gt;Preview&lt;/strong&gt; chart below displays the FailureCount trend and threshold line (dashed), helping you assess whether the threshold setting is reasonable. Areas where the blue solid line exceeds the dashed threshold line indicate periods that would trigger alerts.&lt;/p&gt;
&lt;p&gt;💰 Estimated monthly cost is approximately &lt;strong&gt;$0.50 USD&lt;/strong&gt; (depends on evaluation frequency).&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;Click &lt;strong&gt;Next: Actions &amp;gt;&lt;/strong&gt; to proceed.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-5) Configure alert actions on the Actions tab&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the Portal displays the &lt;strong&gt;Use quick actions (preview)&lt;/strong&gt; option (new UI), you can complete Action Group creation directly on this page without creating one separately:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="387px" data-flex-grow="161" height="1521" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview_hu_e24a82964c1c43b7.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview_hu_32781c44552b9774.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview_hu_ef08194da7af03db.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-5-quick-actions-preview.png 2453w" width="2453"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Annotation&lt;/th&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;①&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Use quick actions (preview)&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Select this option, and the Portal will automatically create an Action Group&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;②&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Quick actions panel&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Fill in Action group name, Display name, check Email and enter email address → &lt;strong&gt;Save&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;③&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Email subject&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Custom alert email subject, e.g., &lt;code&gt;AI Hub Retry Alert&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the Portal doesn&amp;rsquo;t show the quick actions option (legacy UI), select &lt;strong&gt;Use action groups&lt;/strong&gt; → &lt;strong&gt;+ Create action group&lt;/strong&gt;, and follow steps 6-6 ~ 6-8 below to create one manually.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;6-6) Fill in Action Group basic information&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="449px" data-flex-grow="187" height="843" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-6-action-group-basics.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-6-action-group-basics_hu_7bf86028e786560d.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-6-action-group-basics.png 1578w" width="1578"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Region&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Global&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Recommended to select Global for Action Groups&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Action group name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;ai-hub-retry-alert-action&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Unique identifier&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Display name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;AI-Hub-Retry&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Limited to 12 characters, displayed in notifications&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;6-7) Switch to the Notifications tab and configure email notifications&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="519px" data-flex-grow="216" height="1056" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications_hu_f73f8c20a5afca11.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications_hu_7e3cabadfe52490a.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-7-action-group-notifications.png 2286w" width="2286"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Notification type&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Email/SMS message/Push/Voice&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Select notification method&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Name&lt;/td&gt;
 &lt;td&gt;Recipient name&lt;/td&gt;
 &lt;td&gt;Notification recipient identifier&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Email&lt;/td&gt;
 &lt;td&gt;✅ Check and enter email&lt;/td&gt;
 &lt;td&gt;Alert email recipient address&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;The Actions tab allows configuration of richer automated actions (Automation Runbook, Azure Function, Logic App, Webhook, etc.). These are not used in this article — simply skip.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="421px" data-flex-grow="175" height="816" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-actions.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-actions_hu_c23fee3f12422516.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-actions.png 1434w" width="1434"&gt;&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;6-8) Switch to the Review + create tab, confirm configuration and click &lt;strong&gt;Create&lt;/strong&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="282px" data-flex-grow="117" height="1348" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-review-create.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-review-create_hu_ed0653e8c8c38395.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-review-create.png 1584w" width="1584"&gt;&lt;/p&gt;
&lt;p&gt;After creation, you&amp;rsquo;ll receive an Azure confirmation email indicating you&amp;rsquo;ve been added to the Action Group:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="333px" data-flex-grow="138" height="849" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-email-confirm.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-email-confirm_hu_113108bed65e44e4.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-8-action-group-email-confirm.png 1180w" width="1180"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-9) Return to the Alert rule&amp;rsquo;s Actions page, confirm the Action Group is associated, and fill in the email subject&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="279px" data-flex-grow="116" height="1351" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-9-alert-actions-email-subject.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-9-alert-actions-email-subject_hu_4deac364262f2076.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-9-alert-actions-email-subject.png 1576w" width="1576"&gt;&lt;/p&gt;
&lt;p&gt;Enter a custom alert email subject in the &lt;strong&gt;Email subject&lt;/strong&gt; field, e.g., &lt;code&gt;AI Hub Retry Alert&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-10) Switch to the Details tab and fill in Alert rule basic information&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="282px" data-flex-grow="117" height="1355" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-10-alert-details.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-10-alert-details_hu_dbcef4146041b689.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-10-alert-details.png 1595w" width="1595"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Severity&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;2 - Warning&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Alert severity level&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Alert rule name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;AI-Hub-Alert&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Alert rule name&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Region&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;Japan East&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Same region as App Insights&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Identity&lt;/td&gt;
 &lt;td&gt;&lt;strong&gt;Default&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Use default identity&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;6-11) Switch to Review + create to confirm all configurations, click &lt;strong&gt;Create&lt;/strong&gt; to complete&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="218px" data-flex-grow="91" height="1342" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-11-alert-review-create.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-11-alert-review-create_hu_a50e8bf7c746406e.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-11-alert-review-create.png 1222w" width="1222"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-12) Verify alert email&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Approximately 15 minutes after creation, when FailureCount exceeds the threshold (10), you&amp;rsquo;ll receive an alert email:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="326px" data-flex-grow="136" height="1123" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-12-alert-email-notification.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-12-alert-email-notification_hu_633484e3ccfa6737.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-12-alert-email-notification.png 1529w" width="1529"&gt;&lt;/p&gt;
&lt;p&gt;The email contains key information:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Field&lt;/th&gt;
 &lt;th&gt;Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Alert name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;AI-Hub-Alert&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Name of the triggered alert rule&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Severity&lt;/td&gt;
 &lt;td&gt;Warning&lt;/td&gt;
 &lt;td&gt;Alert level&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Metric value&lt;/td&gt;
 &lt;td&gt;15&lt;/td&gt;
 &lt;td&gt;Actual FailureCount&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Threshold&lt;/td&gt;
 &lt;td&gt;10&lt;/td&gt;
 &lt;td&gt;Configured threshold&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Fired time&lt;/td&gt;
 &lt;td&gt;Specific time (UTC)&lt;/td&gt;
 &lt;td&gt;When the alert was triggered&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Click &lt;strong&gt;View alert details&lt;/strong&gt; in the email to jump directly to Azure Portal for details; &lt;strong&gt;View search results&lt;/strong&gt; to view the KQL query results that triggered the alert.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;6-13) (Optional) Fine-tune alert parameters&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;After the alert rule is created, navigate to Application Insights → Monitoring → &lt;strong&gt;Alerts&lt;/strong&gt; → &lt;strong&gt;Alert rules&lt;/strong&gt; → Select the corresponding rule → Click the &lt;strong&gt;Edit&lt;/strong&gt; button:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="712px" data-flex-grow="297" height="606" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button_hu_caf882434c1c375a.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button_hu_3f71a1ceb886ff91.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-edit-button.png 1800w" width="1800"&gt;&lt;/p&gt;
&lt;p&gt;In the editing page&amp;rsquo;s &lt;strong&gt;Details tab → Advanced options&lt;/strong&gt;, fine-tune further:&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="452px" data-flex-grow="188" height="791" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-advanced-mute.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-advanced-mute_hu_42206efbc0e4a856.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience-part2/images/6-13-alert-advanced-mute.png 1491w" width="1491"&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;th&gt;Recommended Value&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Mute actions&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Enter a silent period after triggering, during which no repeat notifications are sent&lt;/td&gt;
 &lt;td&gt;✅ Check&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Mute actions for&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Silent duration — no notifications during this period even if conditions persist&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;3 hours&lt;/code&gt; (adjust as needed)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Automatically resolve alerts&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Automatically change alert status from Fired to Resolved when conditions are no longer met&lt;/td&gt;
 &lt;td&gt;Check as needed&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;💡 Tip:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If the evaluation frequency is 15 minutes and Mute is not configured, &lt;strong&gt;an email notification will be sent every 15 minutes&lt;/strong&gt; while alert conditions persist. Configuring Mute prevents repeated notifications after the first alert trigger, avoiding email flooding.&lt;/p&gt;
&lt;p&gt;Additionally, you can adjust &lt;strong&gt;Aggregation granularity&lt;/strong&gt;, &lt;strong&gt;Threshold value&lt;/strong&gt;, &lt;strong&gt;Frequency of evaluation&lt;/strong&gt;, and other parameters on the editing page to optimize alert sensitivity.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="7-appendix-advanced-kql-queries-and-client-ip-configuration-optional"&gt;7. Appendix: Advanced KQL Queries and Client IP Configuration (Optional)
&lt;/h3&gt;&lt;h4 id="7-1-enable-real-client-ip-recording"&gt;7-1) Enable Real Client IP Recording
&lt;/h4&gt;&lt;p&gt;Application Insights &lt;strong&gt;does not store client IP addresses by default&lt;/strong&gt; (privacy protection), and the &lt;code&gt;client_IP&lt;/code&gt; field displays as &lt;code&gt;0.0.0.0&lt;/code&gt;. Disable IP masking via CLI to record real IPs:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az resource update &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;app-insights-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-type &lt;span class="s2"&gt;&amp;#34;Microsoft.Insights/components&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --set properties.DisableIpMasking&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;⚠️ Warning:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Disabling IP masking will record real client IP addresses. Please ensure this complies with your organization&amp;rsquo;s privacy requirements.&lt;/p&gt;
&lt;p&gt;📖 Reference: Geolocation and IP address handling: &lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/app/ip-collection" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/app/ip-collection&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h4 id="7-2-correlate-client-ip-with-retry-failure-records"&gt;7-2) Correlate Client IP with Retry Failure Records
&lt;/h4&gt;&lt;p&gt;The &lt;code&gt;dependencies&lt;/code&gt; table itself does not contain client IP (it records APIM → backend outbound calls). You need to JOIN with the &lt;code&gt;requests&lt;/code&gt; table via &lt;code&gt;operation_Id&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(1h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| join kind=leftouter (
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; requests 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; | project operation_Id, req_client_IP = client_IP
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ) on operation_Id
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| project timestamp, target, resultCode, 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ClientIP = req_client_IP,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ErrorBody = tostring(customDimensions[&amp;#34;Response-Body&amp;#34;]),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; APIMRequestId = tostring(customDimensions[&amp;#34;Response-apim-request-id&amp;#34;]),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; operation_Id
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| order by timestamp desc
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;📝 Note:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Both &lt;code&gt;requests&lt;/code&gt; and &lt;code&gt;dependencies&lt;/code&gt; tables have a &lt;code&gt;client_IP&lt;/code&gt; field, but with different meanings:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;requests.client_IP&lt;/code&gt;&lt;/strong&gt; — IP of the client calling APIM (real client)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;dependencies.client_IP&lt;/code&gt;&lt;/strong&gt; — APIM gateway&amp;rsquo;s outbound IP (not the client)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;When JOINing, use renaming (e.g., &lt;code&gt;req_client_IP&lt;/code&gt;) to avoid field conflicts.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h4 id="7-3-more-useful-kql-queries"&gt;7-3) More Useful KQL Queries
&lt;/h4&gt;&lt;p&gt;&lt;strong&gt;Reconstruct complete Retry chain by operation_Id (including the successful attempt):&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;span class="lnt"&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(1h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize CallCount = count(),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Backends = make_list(target),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; StatusCodes = make_list(resultCode),
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; Durations = make_list(duration)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; by operation_Id
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where CallCount &amp;gt; 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| order by CallCount desc
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Failure trend chart:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(24h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize FailureCount = count() by bin(timestamp, 5m), target
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| render timechart
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;For alerting (grouped by backend + status code):&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;dependencies
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where timestamp &amp;gt; ago(2h)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where success == false
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| summarize FailureCount = count() by target, resultCode
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;| where FailureCount &amp;gt; 0
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="summary"&gt;Summary
&lt;/h3&gt;&lt;p&gt;Through this article&amp;rsquo;s implementation, we&amp;rsquo;ve built a complete Retry observability solution on top of the resilient gateway architecture from the previous article:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Capability&lt;/th&gt;
 &lt;th&gt;Implementation&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Each Retry failure recorded&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;APIM native &lt;code&gt;dependencies&lt;/code&gt; table automatically records each &lt;code&gt;forward-request&lt;/code&gt; call&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Error Body recording&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Backend Response Body bytes=1024, error JSON details appear in Properties&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Request tracking IDs&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;operation_Id&lt;/code&gt; (unchanged across Retries) + &lt;code&gt;Response-apim-request-id&lt;/code&gt; (Foundry-side ID)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Call duration&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Native &lt;code&gt;DurationMs&lt;/code&gt; field, precise to milliseconds&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;KQL queries&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Support aggregation analysis by backend, status code, and time dimensions&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;Automated alerting&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Log Alert rules + Action Group email notifications&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;For users who have already deployed the resilient architecture from the previous article, this article &lt;strong&gt;requires no Policy code changes&lt;/strong&gt; — complete Retry observability is achieved solely through APIM Diagnostic Logs configuration (binding App Insights + &lt;strong&gt;setting Verbosity to Information&lt;/strong&gt; + enabling Backend Response Headers/Body).&lt;/p&gt;
&lt;p&gt;Those errors hidden by the Retry mechanism are now clearly recorded in Application Insights&amp;rsquo; &lt;code&gt;dependencies&lt;/code&gt; table, ready for operations staff to review and address.&lt;/p&gt;
&lt;h3 id="references"&gt;References
&lt;/h3&gt;&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Document&lt;/th&gt;
 &lt;th&gt;Link&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;How to integrate Azure API Management with Azure Application Insights&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Diagnostic - Create Or Update REST API&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/rest/api/apimanagement/diagnostic/create-or-update" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/rest/api/apimanagement/diagnostic/create-or-update&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Application Insights telemetry data model&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/app/data-model-complete&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Geolocation and IP address handling (IP masking)&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/app/ip-collection" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/app/ip-collection&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscriptions in Azure API Management&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-subscriptions" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-subscriptions&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Tutorial: Create a log search alert for an Azure resource&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/alerts/tutorial-log-alert" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/alerts/tutorial-log-alert&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Create and manage action groups&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/azure-monitor/alerts/action-groups" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/azure-monitor/alerts/action-groups&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Kusto Query Language (KQL) overview&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/data-explorer/kusto/query/" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/data-explorer/kusto/query/&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Performance implications and log sampling&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights#performance-implications-and-log-sampling" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-howto-app-insights#performance-implications-and-log-sampling&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Azure Monitor pricing&lt;/td&gt;
 &lt;td&gt;&lt;a class="link" href="https://azure.microsoft.com/pricing/details/monitor/" target="_blank" rel="noopener"
 &gt;https://azure.microsoft.com/pricing/details/monitor/&lt;/a&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;</description></item><item><title>Building Resilient Azure AI Architecture with API Management Gateway</title><link>https://robinsujob.github.io/p/azure-apim-ai-resilience/</link><pubDate>Fri, 03 Apr 2026 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/azure-apim-ai-resilience/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Building Resilient Azure AI Architecture with API Management Gateway" /&gt;&lt;p&gt;As enterprise demand for AI capabilities grows, organizations are deeply integrating Microsoft Foundry model services into core business systems. As AI moves from experimentation to production, &lt;strong&gt;high availability, elastic scaling, unified governance, and security controls&lt;/strong&gt; become unavoidable themes.&lt;/p&gt;
&lt;p&gt;In production, a single subscription instance fails to meet requirements for &lt;strong&gt;continuity, stability, and throughput&lt;/strong&gt;. A mature architecture consolidates resources across subscriptions, &lt;strong&gt;aggregating AI quotas&lt;/strong&gt; to maximize throughput, with traffic distribution, automatic failover, and unified access control.&lt;/p&gt;
&lt;p&gt;Azure API Management (APIM), as Azure&amp;rsquo;s native API gateway, is naturally suited for this role. By deploying APIM as the unified entry point, combined with the following capabilities, we can build an &lt;strong&gt;enterprise-grade, highly resilient AI architecture&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;🔀 &lt;strong&gt;Backend Load Balancing Pool&lt;/strong&gt; — Load balancing and failover across subscriptions and Foundry instances, aggregating distributed quotas&lt;/li&gt;
&lt;li&gt;🔐 &lt;strong&gt;Managed Identity&lt;/strong&gt; — Zero-key secure authentication between APIM and backend AI services&lt;/li&gt;
&lt;li&gt;🛡️ &lt;strong&gt;Unified API Governance&lt;/strong&gt; — Request routing, traffic control, log auditing, and policy management at the gateway layer&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This article walks through a complete hands-on example: from resource deployment and identity configuration to load balancing policies, building a production-ready resilient gateway architecture targeting multiple Microsoft Foundry resources.&lt;/p&gt;
&lt;h2 id="architecture-overview"&gt;Architecture Overview
&lt;/h2&gt;&lt;p&gt;&lt;img alt="1-Architecture" class="gallery-image" data-flex-basis="289px" data-flex-grow="120" height="683" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/1-Architecture-en.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/1-Architecture-en_hu_10ff4595295ffe34.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/1-Architecture-en.png 823w" width="823"&gt;&lt;/p&gt;
&lt;p&gt;This architecture uses a &lt;strong&gt;centralized API gateway + multi-subscription backend pattern&lt;/strong&gt;, with APIM as the unified entry point, distributing requests round-robin to Foundry instances across multiple subscriptions and regions.&lt;/p&gt;
&lt;p&gt;Core design principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Unified Entry Point&lt;/strong&gt; — Clients only need the APIM endpoint; the pool uses Round-Robin by default&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simplified Security Authentication&lt;/strong&gt; — Zero-key communication via Managed Identity; clients only need a single API Key&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-Subscription Quota Aggregation&lt;/strong&gt; — Aggregate quotas across subscriptions into a unified capacity pool&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cross-Region Disaster Recovery&lt;/strong&gt; — APIM automatically routes to healthy instances when a region becomes unavailable&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;APIM&amp;rsquo;s Policy engine also provides flexible traffic control:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pinned Routing&lt;/strong&gt; — via &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt; header&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Auto Failover&lt;/strong&gt; — Auto-retry with exponential backoff on 4xx/5xx&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Request Tracing&lt;/strong&gt; — &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; response header for tracing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Architecture setup steps:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Section&lt;/th&gt;
 &lt;th&gt;Action&lt;/th&gt;
 &lt;th&gt;Goal&lt;/th&gt;
 &lt;th&gt;Platform&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;1&lt;/td&gt;
 &lt;td&gt;Create APIM instance&lt;/td&gt;
 &lt;td&gt;Set up the unified gateway entry point and enable Managed Identity&lt;/td&gt;
 &lt;td&gt;Azure Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;2&lt;/td&gt;
 &lt;td&gt;Create Foundry resources&lt;/td&gt;
 &lt;td&gt;Build the backend resource pool across multiple subscriptions and regions&lt;/td&gt;
 &lt;td&gt;Azure Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;3&lt;/td&gt;
 &lt;td&gt;Deploy AI models&lt;/td&gt;
 &lt;td&gt;Enable AI service capability on backend resources (using Sora2 as an example)&lt;/td&gt;
 &lt;td&gt;Foundry Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;4&lt;/td&gt;
 &lt;td&gt;Configure RBAC&lt;/td&gt;
 &lt;td&gt;Grant APIM Managed Identity permission to access backends&lt;/td&gt;
 &lt;td&gt;Azure Portal&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;5&lt;/td&gt;
 &lt;td&gt;Configure Backend + LB Pool&lt;/td&gt;
 &lt;td&gt;Register backends and create the load balancing pool&lt;/td&gt;
 &lt;td&gt;Azure Portal (APIM)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;6&lt;/td&gt;
 &lt;td&gt;Create API + Policy&lt;/td&gt;
 &lt;td&gt;Define the API entry point and configure routing and failover&lt;/td&gt;
 &lt;td&gt;Azure Portal (APIM)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;7&lt;/td&gt;
 &lt;td&gt;Test and validate&lt;/td&gt;
 &lt;td&gt;Verify load balancing and pinned backend routing&lt;/td&gt;
 &lt;td&gt;curl / Postman&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="create-apim-instance"&gt;Create APIM Instance
&lt;/h2&gt;&lt;p&gt;Create an API Management instance in Azure Portal, selecting v2 Tier, and enable &lt;strong&gt;System Assigned Managed Identity&lt;/strong&gt; during creation.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; Search &lt;code&gt;API Management Service&lt;/code&gt; → + Create → Fill in configuration → Enable on the Managed identity tab → Review + install&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="185px" data-flex-grow="77" height="1506" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-3-basics-config.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-3-basics-config_hu_14e08ccbc5610987.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-3-basics-config.png 1163w" width="1163"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="475px" data-flex-grow="198" height="562" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-4-managed-identity.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-4-managed-identity_hu_4043f457949cc774.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-4-managed-identity.png 1114w" width="1114"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az rest --method PUT &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --url &lt;span class="s2"&gt;&amp;#34;https://management.azure.com/subscriptions/&amp;lt;subscription-id&amp;gt;/resourceGroups/&amp;lt;resource-group&amp;gt;/providers/Microsoft.ApiManagement/service/&amp;lt;apim-name&amp;gt;?api-version=2024-05-01&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --body &lt;span class="s1"&gt;&amp;#39;{
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;location&amp;#34;: &amp;#34;&amp;lt;region&amp;gt;&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;sku&amp;#34;: { &amp;#34;name&amp;#34;: &amp;#34;&amp;lt;BasicV2|StandardV2|PremiumV2&amp;gt;&amp;#34;, &amp;#34;capacity&amp;#34;: 1 },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;identity&amp;#34;: { &amp;#34;type&amp;#34;: &amp;#34;SystemAssigned&amp;#34; },
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;properties&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;publisherName&amp;#34;: &amp;#34;&amp;lt;organization-name&amp;gt;&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;publisherEmail&amp;#34;: &amp;#34;&amp;lt;admin-email&amp;gt;&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;After Creation:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Status: &lt;strong&gt;Online&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Record &lt;strong&gt;Gateway URL&lt;/strong&gt; (for example: &lt;code&gt;https://&amp;lt;apim-name&amp;gt;.azure-api.net&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Record the Managed Identity &lt;strong&gt;Object (principal) ID&lt;/strong&gt; for RBAC&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="728px" data-flex-grow="303" height="644" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard_hu_2b05d081fb5fa317.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard_hu_e21fb9797c99f7a2.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/2-6-apim-dashboard.png 1956w" width="1956"&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="create-foundry-resources"&gt;Create Foundry Resources
&lt;/h2&gt;&lt;p&gt;Create Foundry resources across multiple subscriptions and regions, each with a corresponding project.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; Search &lt;code&gt;Microsoft Foundry&lt;/code&gt; → Create a resource → Fill in Subscription / Region / Name / Project name → Create&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="220px" data-flex-grow="91" height="1512" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/3-3-create-foundry-resource.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/3-3-create-foundry-resource_hu_e040e90bd75b1db3.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/3-3-create-foundry-resource.png 1386w" width="1386"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --location &amp;lt;region-code&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --kind AIServices --sku S0 &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --custom-domain &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --allow-project-management &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account project create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --project-name &amp;lt;project-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --location &amp;lt;region-code&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Example Resource Distribution:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Subscription&lt;/th&gt;
 &lt;th&gt;Region&lt;/th&gt;
 &lt;th&gt;Resource Name&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription A&lt;/td&gt;
 &lt;td&gt;East US 2&lt;/td&gt;
 &lt;td&gt;foundry-eastus2-01&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription A&lt;/td&gt;
 &lt;td&gt;Sweden Central&lt;/td&gt;
 &lt;td&gt;foundry-swedencentral-01&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription B&lt;/td&gt;
 &lt;td&gt;East US 2&lt;/td&gt;
 &lt;td&gt;foundry-eastus2-02&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Subscription B&lt;/td&gt;
 &lt;td&gt;Sweden Central&lt;/td&gt;
 &lt;td&gt;foundry-swedencentral-02&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="deploy-models"&gt;Deploy Models
&lt;/h2&gt;&lt;p&gt;Deploy AI models in each Foundry resource via the Foundry Portal.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; In &lt;a class="link" href="https://ai.azure.com/nextgen/" target="_blank" rel="noopener"
 &gt;Foundry Portal&lt;/a&gt;, select the target project → Build → Models → Deploy a base model → Search for a model (for example &lt;code&gt;sora-2&lt;/code&gt;) → Deploy → Default settings&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="491px" data-flex-grow="204" height="924" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2_hu_e7087db313cdf721.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2_hu_956f92a0935b1b90.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-2-search-sora2.png 1891w" width="1891"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="455px" data-flex-grow="189" height="1006" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground_hu_b6ecf169c4389d93.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground_hu_50733a65436bad42.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/4-5-playground.png 1911w" width="1911"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account deployment create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --deployment-name &amp;lt;deployment-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --model-name &amp;lt;model-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --model-version &amp;lt;model-version&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --model-format &amp;lt;model-format&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --sku-name &amp;lt;sku-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --sku-capacity &amp;lt;capacity&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;Model names, versions, and SKUs vary by region. See: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure" target="_blank" rel="noopener"
 &gt;Models and Region Availability&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="configure-rbac"&gt;Configure RBAC
&lt;/h2&gt;&lt;p&gt;Grant the &lt;code&gt;Cognitive Services OpenAI User&lt;/code&gt; role to APIM&amp;rsquo;s Managed Identity on &lt;strong&gt;each Foundry resource&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; Go to the Foundry resource → Access control (IAM) → + Add → Add role assignment → Search &lt;code&gt;Cognitive Services OpenAI User&lt;/code&gt; → In Members choose Managed identity → Select the API Management service → Review + assign&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="296px" data-flex-grow="123" height="1042" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-3-select-role.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-3-select-role_hu_7d310acac9aba761.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-3-select-role.png 1287w" width="1287"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="378px" data-flex-grow="157" height="1166" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity_hu_d617d34d874084d4.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity_hu_66bfce957da67b8d.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/5-4-select-managed-identity.png 1840w" width="1840"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Get APIM Managed Identity Principal ID&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az apim show &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;apim-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --query &lt;span class="s2"&gt;&amp;#34;identity.principalId&amp;#34;&lt;/span&gt; -o tsv
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Assign role for each Foundry resource&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az role assignment create &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --assignee &amp;lt;principal-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --role &lt;span class="s2"&gt;&amp;#34;Cognitive Services OpenAI User&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --scope /subscriptions/&amp;lt;subscription-id&amp;gt;/resourceGroups/&amp;lt;resource-group&amp;gt;/providers/Microsoft.CognitiveServices/accounts/&amp;lt;foundry-resource-name&amp;gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
 &lt;blockquote&gt;
 &lt;p&gt;&lt;code&gt;Cognitive Services OpenAI User&lt;/code&gt; is the minimum permission role—allows inference calls but not resource configuration changes.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="configure-backend--lb-pool"&gt;Configure Backend + LB Pool
&lt;/h2&gt;&lt;h3 id="create-backend"&gt;Create Backend
&lt;/h3&gt;&lt;p&gt;Create a Backend for each Foundry resource with Managed Identity authentication.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Get OpenAI Endpoint:&lt;/strong&gt; On the project Home page in &lt;a class="link" href="https://ai.azure.com/nextgen/" target="_blank" rel="noopener"
 &gt;Foundry Portal&lt;/a&gt;, find &lt;strong&gt;Azure OpenAI endpoint&lt;/strong&gt;, or use the CLI:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az cognitiveservices account show &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --subscription &amp;lt;subscription-id&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --resource-group &amp;lt;resource-group&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --name &amp;lt;foundry-resource-name&amp;gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --query &lt;span class="s2"&gt;&amp;#34;properties.endpoints.\&amp;#34;OpenAI Language Model Instance API\&amp;#34;&amp;#34;&lt;/span&gt; -o tsv
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; APIM → Backends → + Create new backend → Fill in Name / Runtime URL → Managed Identity tab → Client identity = System assigned → Resource ID = &lt;code&gt;https://cognitiveservices.azure.com&lt;/code&gt; → Create&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="299px" data-flex-grow="124" height="1647" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend_hu_6b4ff1c24b7d40ce.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend_hu_3c50fe44b4ab8742.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-3-create-backend.png 2052w" width="2052"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Key Configuration:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Configuration&lt;/th&gt;
 &lt;th&gt;Example Value&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Name&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;&amp;lt;foundry-resource-name&amp;gt;&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Recommended to match the Foundry resource name&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Runtime URL&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;https://&amp;lt;foundry-resource-name&amp;gt;.openai.azure.com/openai/v1&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Foundry OpenAI endpoint (+ &lt;code&gt;openai/v1&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Client identity&lt;/td&gt;
 &lt;td&gt;System assigned identity&lt;/td&gt;
 &lt;td&gt;Use APIM system-assigned managed identity&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;Resource ID&lt;/td&gt;
 &lt;td&gt;&lt;code&gt;https://cognitiveservices.azure.com&lt;/code&gt;&lt;/td&gt;
 &lt;td&gt;Token audience, &lt;strong&gt;fixed value&lt;/strong&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;&lt;strong&gt;CLI Alternative:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;az rest --method PUT &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --url &lt;span class="s2"&gt;&amp;#34;https://management.azure.com/subscriptions/&amp;lt;subscription-id&amp;gt;/resourceGroups/&amp;lt;resource-group&amp;gt;/providers/Microsoft.ApiManagement/service/&amp;lt;apim-name&amp;gt;/backends/&amp;lt;backend-name&amp;gt;?api-version=2024-05-01&amp;#34;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --body &lt;span class="s1"&gt;&amp;#39;{
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;properties&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;url&amp;#34;: &amp;#34;https://&amp;lt;foundry-resource-name&amp;gt;.openai.azure.com/openai/v1&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;protocol&amp;#34;: &amp;#34;http&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;credentials&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;managedIdentity&amp;#34;: {
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;clientId&amp;#34;: null,
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; &amp;#34;resource&amp;#34;: &amp;#34;https://cognitiveservices.azure.com&amp;#34;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s1"&gt; }&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="create-lb-pool"&gt;Create LB Pool
&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; APIM → Backends → Load balancer tab → + Create new pool → Fill in Pool Name → Select all Backends → Send requests evenly (Round-robin) → Create&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="375px" data-flex-grow="156" height="774" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-6-create-pool.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-6-create-pool_hu_3af9a6228e0fc588.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/6-6-create-pool.png 1212w" width="1212"&gt;&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Portal provides two options: &lt;strong&gt;Send requests evenly&lt;/strong&gt; (Round-robin) or &lt;strong&gt;Customize weight and priority&lt;/strong&gt;.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2 id="create-api--policy"&gt;Create API + Policy
&lt;/h2&gt;&lt;h3 id="create-api"&gt;Create API
&lt;/h3&gt;&lt;p&gt;&lt;strong&gt;Portal Steps:&lt;/strong&gt; APIM → APIs → + Add API → HTTP → Fill in Display name / API URL suffix (&lt;code&gt;openai/v1&lt;/code&gt;) → Leave Web service URL empty → Create&lt;/p&gt;
&lt;p&gt;Then add Operations: create &lt;code&gt;/*&lt;/code&gt; wildcard path for POST, GET, and DELETE.&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;This example uses wildcard &lt;code&gt;/*&lt;/code&gt; to proxy all requests. For finer control, create operations for specific paths like &lt;code&gt;/videos/*&lt;/code&gt; for Sora2.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h3 id="configure-policy"&gt;Configure Policy
&lt;/h3&gt;&lt;p&gt;Select All operations → click &lt;code&gt;&amp;lt;/&amp;gt;&lt;/code&gt; in Inbound processing → paste the policy below, update the pool name → Save&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;span class="lnt"&gt;25
&lt;/span&gt;&lt;span class="lnt"&gt;26
&lt;/span&gt;&lt;span class="lnt"&gt;27
&lt;/span&gt;&lt;span class="lnt"&gt;28
&lt;/span&gt;&lt;span class="lnt"&gt;29
&lt;/span&gt;&lt;span class="lnt"&gt;30
&lt;/span&gt;&lt;span class="lnt"&gt;31
&lt;/span&gt;&lt;span class="lnt"&gt;32
&lt;/span&gt;&lt;span class="lnt"&gt;33
&lt;/span&gt;&lt;span class="lnt"&gt;34
&lt;/span&gt;&lt;span class="lnt"&gt;35
&lt;/span&gt;&lt;span class="lnt"&gt;36
&lt;/span&gt;&lt;span class="lnt"&gt;37
&lt;/span&gt;&lt;span class="lnt"&gt;38
&lt;/span&gt;&lt;span class="lnt"&gt;39
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-xml" data-lang="xml"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nt"&gt;&amp;lt;policies&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;inbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c"&gt;&amp;lt;!-- Read request header so callers can pin a backend --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-variable&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;target&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Request.Headers.GetValueOrDefault(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;X-AI-Foundry-Target&amp;#34;,&lt;/span&gt; &lt;span class="err"&gt;&amp;#34;&amp;#34;))&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c"&gt;&amp;lt;!-- Backend load balancing pool name --&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-variable&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;pool_name&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&amp;lt;your-ai-hub-pool-name&amp;gt;&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;when&lt;/span&gt; &lt;span class="na"&gt;condition=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Variables.GetValueOrDefault&amp;lt;string&amp;gt;(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;target&amp;#34;)&lt;/span&gt; &lt;span class="err"&gt;!=&lt;/span&gt; &lt;span class="err"&gt;&amp;#34;&amp;#34;)&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-backend-service&lt;/span&gt; &lt;span class="na"&gt;backend-id=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Variables.GetValueOrDefault&amp;lt;string&amp;gt;(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;target&amp;#34;))&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/when&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-backend-service&lt;/span&gt; &lt;span class="na"&gt;backend-id=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@((string)context.Variables[&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;pool_name&amp;#34;])&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/inbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;outbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-header&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;X-AI-Foundry-Backend&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;exists-action=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;override&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;value&amp;gt;&lt;/span&gt;@(context.Request.Url.Host.Split(&amp;#39;.&amp;#39;)[0])&lt;span class="nt"&gt;&amp;lt;/value&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/set-header&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/outbound&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;on-error&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;base&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/on-error&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;backend&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;when&lt;/span&gt; &lt;span class="na"&gt;condition=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Variables.GetValueOrDefault&amp;lt;string&amp;gt;(&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;target&amp;#34;)&lt;/span&gt; &lt;span class="err"&gt;!=&lt;/span&gt; &lt;span class="err"&gt;&amp;#34;&amp;#34;)&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;forward-request&lt;/span&gt; &lt;span class="na"&gt;buffer-request-body=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;true&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/when&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;retry&lt;/span&gt; &lt;span class="na"&gt;condition=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@(context.Response == null || context.Response.StatusCode &amp;gt;= 400)&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;count=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;3&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;interval=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;1&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;delta=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;1&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;max-interval=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;5&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;first-fast-retry=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;true&amp;#34;&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;set-backend-service&lt;/span&gt; &lt;span class="na"&gt;backend-id=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;@((string)context.Variables[&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;pool_name&amp;#34;]&lt;/span&gt; &lt;span class="err"&gt;)&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;forward-request&lt;/span&gt; &lt;span class="na"&gt;buffer-request-body=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;true&amp;#34;&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/retry&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/otherwise&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/choose&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;lt;/backend&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nt"&gt;&amp;lt;/policies&amp;gt;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="370px" data-flex-grow="154" height="1848" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en_hu_bf4c944bd3751b2f.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en_hu_82283df76f27d876.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en_hu_fc16f38167941c1d.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/7-5-policy-editor-en.png 2851w" width="2851"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Policy Logic:&lt;/strong&gt;&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Area&lt;/th&gt;
 &lt;th&gt;Function&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;inbound&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Read &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt;; if present, route to the specified backend, otherwise use the LB Pool round-robin&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;outbound&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Return the actual backend name in the &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; response header&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;&lt;strong&gt;backend&lt;/strong&gt;&lt;/td&gt;
 &lt;td&gt;Forward directly for pinned routing; in pool mode, auto-retry and switch backend on failure (up to 3 attempts with exponential backoff)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="test-and-validate"&gt;Test and Validate
&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Prerequisites:&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Gather the following before testing:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Get Gateway URL: APIM → Overview → Gateway URL&lt;/li&gt;
&lt;li&gt;Get API Key: APIM → Subscriptions → Built-in all-access → Show keys → Primary key&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="506px" data-flex-grow="211" height="1116" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key_hu_73484fdc0f89d8c9.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key_hu_a8585cc5e1155571.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-2-subscription-key.png 2355w" width="2355"&gt;&lt;/p&gt;
&lt;h3 id="test-1-load-balancing"&gt;Test 1: Load Balancing
&lt;/h3&gt;&lt;p&gt;Send consecutive requests without &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt;, and observe &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; rotating across backends:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -D - -o /dev/null --location &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;https://&amp;lt;apim-gateway-url&amp;gt;/openai/v1/videos&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;Ocp-Apim-Subscription-Key: &amp;lt;your-subscription-key&amp;gt;&amp;#39;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Expected: &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; changes with each request, confirming Round-robin is working.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="295px" data-flex-grow="123" height="1961" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test_hu_7f93d3fde652e814.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test_hu_a47e59e6a4cc2c60.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test_hu_54b55a659d650932.png 2400w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-3-round-robin-test.png 2415w" width="2415"&gt;&lt;/p&gt;
&lt;h3 id="test-2-pinned-backend-routing"&gt;Test 2: Pinned Backend Routing
&lt;/h3&gt;&lt;p&gt;Use &lt;code&gt;X-AI-Foundry-Target&lt;/code&gt; header to pin to a specific backend:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;curl -s -D - -o /dev/null --location &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s1"&gt;&amp;#39;https://&amp;lt;apim-gateway-url&amp;gt;/openai/v1/responses&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;Content-Type: application/json&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;Ocp-Apim-Subscription-Key: &amp;lt;your-subscription-key&amp;gt;&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --header &lt;span class="s1"&gt;&amp;#39;X-AI-Foundry-Target: &amp;lt;backend-name&amp;gt;&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --data &lt;span class="s1"&gt;&amp;#39;{&amp;#34;model&amp;#34;: &amp;#34;&amp;lt;model-name&amp;gt;&amp;#34;, &amp;#34;input&amp;#34;: &amp;#34;Hello&amp;#34;}&amp;#39;&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;|&lt;/span&gt; grep -i X-AI-Foundry-Backend
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Expected: &lt;code&gt;X-AI-Foundry-Backend&lt;/code&gt; consistently returns the specified backend name.&lt;/p&gt;
&lt;p&gt;&lt;img class="gallery-image" data-flex-basis="423px" data-flex-grow="176" height="1056" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test.png" srcset="https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test_hu_dfc5dea219569ee5.png 800w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test_hu_f665edd6a0ac1aeb.png 1600w, https://robinsujob.github.io/p/azure-apim-ai-resilience/images/8-4-pinned-backend-test.png 1865w" width="1865"&gt;&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="faq"&gt;FAQ
&lt;/h2&gt;&lt;p&gt;&lt;strong&gt;Q: Why not use APIM Backend&amp;rsquo;s Circuit Breaker?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This architecture targets load balancing and quota aggregation. Circuit Breaker would &amp;ldquo;break&amp;rdquo; on backend failure, but a 429 on one model shouldn&amp;rsquo;t block other models on the same backend. Simple failover via &lt;code&gt;retry&lt;/code&gt; + &lt;code&gt;set-backend-service&lt;/code&gt; is more appropriate.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Q: Is this architecture only for Sora2?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;No. It applies to any Foundry / Azure OpenAI model service (Responses API, Chat Completions, Embeddings, Image Generation, etc.)—just point the Backend URL to the corresponding endpoint.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="reference-documentation"&gt;Reference Documentation
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;Microsoft Foundry Overview: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/what-is-foundry" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/what-is-foundry&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Foundry RBAC: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry-classic/openai/how-to/role-based-access-control" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry-classic/openai/how-to/role-based-access-control&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;APIM v2 Tier: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/v2-service-tiers-overview" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/v2-service-tiers-overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Backends &amp;amp; LB Pool: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/backends" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/backends&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Managed Identity: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/api-management-howto-use-managed-service-identity" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/api-management-howto-use-managed-service-identity&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Retry Policy: &lt;a class="link" href="https://learn.microsoft.com/azure/api-management/retry-policy" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/api-management/retry-policy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Video Generation / Sora2: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/openai/concepts/video-generation" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/openai/concepts/video-generation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Models &amp;amp; Regions: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/foundry-models/concepts/models-sold-directly-by-azure&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Error Codes: &lt;a class="link" href="https://learn.microsoft.com/azure/foundry/openai/supported-languages#error-handling" target="_blank" rel="noopener"
 &gt;https://learn.microsoft.com/azure/foundry/openai/supported-languages#error-handling&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;</description></item><item><title>China CloudFront SSL Plugin V2 - One-Stop Free SSL Certificate Management</title><link>https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/</link><pubDate>Tue, 19 Aug 2025 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post China CloudFront SSL Plugin V2 - One-Stop Free SSL Certificate Management" /&gt;&lt;p&gt;In today&amp;rsquo;s digital age, website security is paramount. SSL certificates, as a critical tool for protecting data transmission, have always been a key concern for developers and operations teams. In 2023, we released the China CloudFront SSL Plugin to facilitate the integration of free SSL certificates with CloudFront in the China region. We have continued to iterate on our solution. Now, we are pleased to announce the official release of the second generation of the China CloudFront SSL Plugin.&lt;/p&gt;
&lt;h3 id="major-upgrades"&gt;Major Upgrades
&lt;/h3&gt;&lt;p&gt;China CloudFront SSL Plugin V2 introduces three core upgrades, making certificate management simpler, safer, and more efficient.&lt;/p&gt;
&lt;h4 id="1-breaking-through-the-single-project-limitation-with-unified-multi-project-management"&gt;1. Breaking Through the Single-Project Limitation with Unified Multi-Project Management
&lt;/h4&gt;&lt;p&gt;In this new version, we have completely restructured the project management architecture:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Before (V1)&lt;/strong&gt;: Each stack could only manage a single project&amp;rsquo;s domain set. Managing multiple projects required deploying multiple stacks.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;After (V2)&lt;/strong&gt;: A single stack can now manage domain sets for multiple projects, significantly simplifying operations.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="2-a-brand-new-interactive-experience--no-more-complex-operations"&gt;2. A Brand-New Interactive Experience — No More Complex Operations
&lt;/h4&gt;&lt;p&gt;To improve the user experience, we have completely redesigned the management interface:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Before (V1)&lt;/strong&gt;: An API management page built on Swagger UI that required manually constructing and sending API requests.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;After (V2)&lt;/strong&gt;: An intuitive graphical user interface where all operations can be completed with simple button clicks, greatly lowering the barrier to entry.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="3-enhanced-security-mechanisms-for-safer-operations"&gt;3. Enhanced Security Mechanisms for Safer Operations
&lt;/h4&gt;&lt;p&gt;Security has been comprehensively strengthened:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Before (V1)&lt;/strong&gt;: External APIs lacked effective security validation mechanisms.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;After (V2)&lt;/strong&gt;: An Access Key verification mechanism has been introduced — all API operations now require authentication, ensuring secure and controlled access.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While introducing these new capabilities, we have preserved and reinforced the most popular features of the solution:&lt;/p&gt;
&lt;p&gt;✓ &lt;strong&gt;Cost Advantage&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Fully serverless architecture with pay-per-invocation pricing&lt;/li&gt;
&lt;li&gt;Certificate renewal triggers every 30 days by default&lt;/li&gt;
&lt;li&gt;Only minimal storage and logging costs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;✓ &lt;strong&gt;Operational Automation&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Seamless integration with CloudFront&lt;/li&gt;
&lt;li&gt;Automatic replacement of associated CloudFront certificates after renewal&lt;/li&gt;
&lt;li&gt;Scheduled automatic renewal via Amazon EventBridge&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;✓ &lt;strong&gt;Operational Transparency&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Comprehensive email notification mechanism covering the entire certificate lifecycle&lt;/li&gt;
&lt;li&gt;Intuitive graphical dashboard for real-time certificate status monitoring&lt;/li&gt;
&lt;li&gt;Unified multi-project management and monitoring&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;✓ &lt;strong&gt;Open Source&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;All code is provided as open source&lt;/li&gt;
&lt;li&gt;Supports customization on top of the source code&lt;/li&gt;
&lt;li&gt;Easy to extend for enterprise-specific requirements&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These upgrades not only enhance the user experience but also provide more powerful support for China CloudFront certificate management. Whether managing certificates for a single website or multiple projects, V2 can handle it with ease.&lt;/p&gt;
&lt;h2 id="architecture"&gt;Architecture
&lt;/h2&gt;&lt;p&gt;As a certificate management solution for CloudFront in the China region, we designed a complete architecture based on serverless services, with automated deployment via Amazon CloudFormation templates.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Architecture" class="gallery-image" data-flex-basis="555px" data-flex-grow="231" height="731" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/architecture-v2.png" srcset="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/architecture-v2_hu_77ffabdb655b00f5.png 800w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/architecture-v2_hu_a1659a5a8d55333.png 1600w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/architecture-v2.png 1691w" width="1691"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Core Components:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Let&amp;rsquo;s Encrypt&lt;/strong&gt;: A free, open, and automated certificate authority that provides free SSL certificate support for the solution&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Certbot&lt;/strong&gt;: A free open-source tool for automated certificate acquisition, deployment, and renewal&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon Lambda&lt;/strong&gt;: The core compute component responsible for running the certificate issuance program, frontend interface, and certificate management API&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon API Gateway&lt;/strong&gt;: Provides a secure certificate management API interface — all operations require Access Key verification&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;2. Storage &amp;amp; Database:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Amazon DynamoDB&lt;/strong&gt;: A serverless database dedicated to storing project certificate issuance status and error information&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon S3&lt;/strong&gt;: Used for storing backup SSL certificates, supporting certificate download and backup management&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IAM SSL Certificate Storage&lt;/strong&gt;: Dedicated storage for SSL certificates associated with CloudFront — a special requirement for the China region&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;3. Automation &amp;amp; Notifications:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Amazon SNS&lt;/strong&gt;: Sends email notifications about certificate issuance status, ensuring operations teams are promptly informed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Amazon EventBridge&lt;/strong&gt;: Triggers certificate renewal on a schedule, defaulting to automatic renewal every 30 days&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="deployment-demo"&gt;Deployment Demo
&lt;/h2&gt;&lt;h4 id="create-management-stack"&gt;Create Management Stack
&lt;/h4&gt;&lt;p&gt;Creating the management stack is the first step in deploying China CloudFront SSL Plugin V2. Using an Amazon CloudFormation template, you can deploy the main stack — including Lambda functions, API Gateway, DynamoDB, and other core components — with a single click. During deployment, you need to specify a stack name and an Access Key as the API Gateway security key, ensuring only authorized users can access the certificate management features. The entire deployment process takes approximately 3–5 minutes, after which you will receive an access link to the certificate management frontend.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Create Stack" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="720" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/new-stack.gif" srcset="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/new-stack_hu_801afb176afd33fd.gif 800w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/new-stack.gif 1280w" width="1280"&gt;&lt;/p&gt;
&lt;h4 id="create-certificate-project"&gt;Create Certificate Project
&lt;/h4&gt;&lt;p&gt;Creating a certificate project is the core step in using China CloudFront SSL Plugin V2. In the graphical management interface, you can easily create new certificate projects to request free Let&amp;rsquo;s Encrypt SSL certificates for specified domain sets. Each project supports multiple domains (comma-separated), and you can configure the notification email address and automatic renewal interval (30 days recommended). Once a project is created, the system automatically deploys a dedicated sub-stack containing a certificate issuance Lambda function and an EventBridge scheduling rule for automated certificate management.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Create Project" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="720" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/create-project.gif" srcset="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/create-project_hu_7162baf9cd7a6cb.gif 800w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/create-project.gif 1280w" width="1280"&gt;&lt;/p&gt;
&lt;h4 id="attach-the-auto-issued-certificate-to-cloudfront"&gt;Attach the Auto-Issued Certificate to CloudFront
&lt;/h4&gt;&lt;p&gt;Attaching the auto-issued certificate to CloudFront is the key step for enabling HTTPS access. After a certificate is successfully issued, the system automatically uploads it to IAM SSL certificate storage. You can then find and select the corresponding certificate in the CloudFront console&amp;rsquo;s distribution settings. Under &amp;ldquo;Alternate Domain Names and SSL Certificate,&amp;rdquo; enter the domain name for which you requested the certificate, then select the corresponding certificate from the custom SSL certificate dropdown to complete the binding. Once bound, subsequent automatic certificate renewals will seamlessly replace the certificate in CloudFront without any manual intervention.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Attach SSL" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="720" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/attach-ssl.gif" srcset="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/attach-ssl_hu_635e22f67d718779.gif 800w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/attach-ssl.gif 1280w" width="1280"&gt;&lt;/p&gt;
&lt;h4 id="update-certificate-project-domains--auto-renewal-interval"&gt;Update Certificate Project (Domains / Auto-Renewal Interval)
&lt;/h4&gt;&lt;p&gt;The update certificate project feature allows you to flexibly adjust project configurations to adapt to business changes. By selecting a project and clicking the &amp;ldquo;Modify Project&amp;rdquo; button, you can update the domain list and adjust the SSL certificate auto-renewal interval. Each project modification triggers a new certificate issuance, ensuring configuration changes take effect immediately. The system automatically handles CloudFront certificate updates and old certificate cleanup — the entire process is transparent to the user and reliably secure.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Update Project" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="720" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/update-project.gif" srcset="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/update-project_hu_e4ce74cd9263387c.gif 800w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/update-project.gif 1280w" width="1280"&gt;&lt;/p&gt;
&lt;h4 id="re-issue-certificate--force-certificate-update"&gt;Re-issue Certificate / Force Certificate Update
&lt;/h4&gt;&lt;p&gt;The re-issue certificate feature gives you manual control over certificate updates. When certificate issuance fails or you need an immediate certificate update, you can select the corresponding certificate from the certificate list and click the &amp;ldquo;Re-issue&amp;rdquo; button to manually trigger the certificate update process. The system re-executes the full certificate request, validation, issuance, and CloudFront update workflow, and notifies you of the result via email. This feature is particularly useful for fault recovery, emergency updates, or testing scenarios, ensuring your SSL certificates are always up to date.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Re-issue" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="720" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/recert.gif" srcset="https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/recert_hu_884274259bf0fe5a.gif 800w, https://robinsujob.github.io/p/china-cloudfront-ssl-plugin-v2/images/recert.gif 1280w" width="1280"&gt;&lt;/p&gt;
&lt;p&gt;To experience the full feature set, please refer to our solution page for deployment.&lt;/p&gt;
&lt;p&gt;Deploy: &lt;a class="link" href="https://console.amazonaws.cn/cloudformation/home?#/stacks/create/template?templateURL=https://aws-cn-getting-started.s3.cn-northwest-1.amazonaws.com.cn/china-cloudfront-ssl-plugin_v2/ChinaCloudFrontSslPluginStackV2.template.json" target="_blank" rel="noopener"
 &gt;One-Click Deploy China CloudFront SSL Plugin V2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Tutorial: &lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin V2 Full Tutorial&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;China CloudFront SSL Plugin V2 delivers a more convenient and comprehensive certificate solution for CloudFront users in the China region through its brand-new multi-project management architecture, intuitive graphical interface, and enhanced security mechanisms.&lt;/p&gt;
&lt;h2 id="resources-and-documentation"&gt;Resources and Documentation
&lt;/h2&gt;&lt;h3 id="tutorials--deployment"&gt;Tutorials &amp;amp; Deployment
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin V2 Full Tutorial&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Detailed deployment and usage guide with complete step-by-step instructions and troubleshooting&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://console.amazonaws.cn/cloudformation/home?#/stacks/create/template?templateURL=https://aws-cn-getting-started.s3.cn-northwest-1.amazonaws.com.cn/china-cloudfront-ssl-plugin_v2/ChinaCloudFrontSslPluginStackV2.template.json" target="_blank" rel="noopener"
 &gt;One-Click Deploy V2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Deploy the latest version directly via CloudFormation template&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="open-source-repositories"&gt;Open Source Repositories
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://github.com/aws-samples/sample-China-CloudFront-SSL-Plugin-V2" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin V2&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Complete V2 source code, supporting custom development and contributions&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://github.com/aws-samples/China-CloudFront-SSL-Plugin" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin V1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;First-generation source code for comparison and reference&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="mailto:china-cloudfront-ssl-plugin@amazon.com" &gt;Email the Solution Team&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Contact the China CloudFront SSL Plugin solution team directly for professional support&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://www.amazonaws.cn/support/icp/" target="_blank" rel="noopener"
 &gt;ICP Filing Guide&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Required ICP filing process for website deployment in the China region&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="related-aws-service-documentation"&gt;Related AWS Service Documentation
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://docs.amazonaws.cn/aws/latest/userguide/cloudfront.html#feature-diff" target="_blank" rel="noopener"
 &gt;Amazon CloudFront China Region Feature Differences&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Understand the special requirements and limitations of CloudFront in the China region&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://docs.amazonaws.cn/IAM/latest/UserGuide/id_credentials_server-certs.html" target="_blank" rel="noopener"
 &gt;IAM SSL Certificate Management&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Official documentation for China region CloudFront SSL certificate storage&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/migrate-domain-to-amazon-route53/" target="_blank" rel="noopener"
 &gt;Amazon Route 53 Domain Migration&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Detailed steps for migrating domain resolution to Route 53&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/divert-website-access-traffic-from-ec2-to-amazon-cloudfront/" target="_blank" rel="noopener"
 &gt;Amazon CloudFront Deployment Guide&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;CloudFront Deployment Mini-Guide — Deploying Free Certificates with China Region CloudFront and SSL Plugin&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="other-technical-references"&gt;Other Technical References
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://letsencrypt.org/docs/" target="_blank" rel="noopener"
 &gt;Let&amp;rsquo;s Encrypt Documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Learn about how the Let&amp;rsquo;s Encrypt certificate authority works and its limitations&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://certbot.eff.org/pages/about" target="_blank" rel="noopener"
 &gt;Certbot Documentation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Detailed usage instructions for the Certbot tool&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://certbot-dns-route53.readthedocs.io/en/stable/" target="_blank" rel="noopener"
 &gt;Amazon Route 53 DNS Validation&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Technical documentation for the Route 53 DNS validation plugin&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="contact-us"&gt;Contact Us
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="https://webchat-aws.clink.cn/chat.html?accessId=35d2504b-57b6-4093-97fd-81a6198bbbfa&amp;amp;language=zh_CN" target="_blank" rel="noopener"
 &gt;Contact AWS Technical Support&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Get official professional technical support and answers&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a class="link" href="mailto:china-cloudfront-ssl-plugin@amazon.com" &gt;Email the Solution Team&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Contact the China CloudFront SSL Plugin solution team directly for professional support&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/china-cloudfront-ssl-plugin-v2-a-one-stop-certificate-solution-for-cloudfront-in-china-region/" target="_blank" rel="noopener"
 &gt;AWS Blog&lt;/a&gt; | &lt;a class="link" href="https://github.com/aws-samples/sample-China-CloudFront-SSL-Plugin-V2" target="_blank" rel="noopener"
 &gt;GitHub&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>Deploy GGUF Models on Amazon SageMaker with llama.cpp</title><link>https://robinsujob.github.io/p/sagemaker-gguf-model-inference/</link><pubDate>Wed, 12 Feb 2025 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/sagemaker-gguf-model-inference/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Deploy GGUF Models on Amazon SageMaker with llama.cpp" /&gt;&lt;p&gt;With the rapid development of artificial intelligence, LLM models have demonstrated powerful capabilities in natural language processing, machine translation, and intelligent assistants. These models are typically developed using frameworks like PyTorch, with pre-trained results stored in specific binary formats such as PyTorch&amp;rsquo;s .pt files. However, as model sizes continue to grow, traditional file formats face challenges in storage, transmission, and loading — large file sizes, slow loading speeds, and poor cross-platform compatibility.&lt;/p&gt;
&lt;p&gt;To address these challenges, &lt;a class="link" href="https://github.com/ggerganov/ggml/blob/master/docs/gguf.md" target="_blank" rel="noopener"
 &gt;GGUF&lt;/a&gt; (GPT-Generated Unified Format) was created. GGUF is a binary file format designed specifically for LLM models, proposed by Georgi Gerganov, the founder of the llama.cpp open-source project. It aims to improve the storage and exchange efficiency of large models through optimized data structures, compact binary encoding, and memory mapping. GGUF format is now widely used in deploying and sharing various large models, especially popular in open-source communities like Hugging Face.&lt;/p&gt;
&lt;p&gt;Its core features enable developers to manage and use LLM models more efficiently while reducing resource consumption and improving performance. Through conversion tools, original model pre-training results can be easily converted to GGUF format for a more efficient experience.&lt;/p&gt;
&lt;p&gt;In this article, you will learn how to deploy LLM models in GGUF file format using Amazon SageMaker AI real-time inference endpoints.&lt;/p&gt;
&lt;h2 id="amazon-sagemaker-ai-model-deployment-options"&gt;Amazon SageMaker AI Model Deployment Options
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="https://aws.amazon.com/cn/sagemaker-ai/" target="_blank" rel="noopener"
 &gt;Amazon SageMaker AI&lt;/a&gt; is a fully managed service that brings together a broad set of tools to enable high-performance, low-cost machine learning (ML) for any use case. With SageMaker AI, you can build, train, and deploy ML models at scale using notebooks, debuggers, profilers, pipelines, MLOps, and more. It supports built-in algorithms and pre-built Docker images for common ML frameworks (TensorFlow, PyTorch, ONNX, XGBoost), and provides specialized Deep Learning Containers (DLC), libraries, and tools for model parallelism and Large Model Inference (LMI).&lt;/p&gt;
&lt;p&gt;If the pre-built Docker images don&amp;rsquo;t meet your needs, you can build your own Docker container (BYOC – Bring Your Own Container) for inference. To be compatible with SageMaker AI, your container must:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Have a web server listening on port 8080.&lt;/li&gt;
&lt;li&gt;Accept POST requests to /invocations and /ping endpoints. Requests must return within 60 seconds with a maximum size of 6 MB.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To deploy and run GGUF models in SageMaker AI, you need to build a custom Docker container. This approach integrates the llama.cpp project to run GGUF models for effective deployment and inference on the SageMaker AI platform.&lt;/p&gt;
&lt;h2 id="llamacpp-and-gguf"&gt;llama.cpp and GGUF
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="https://github.com/ggerganov/llama.cpp" target="_blank" rel="noopener"
 &gt;llama.cpp&lt;/a&gt; is a C/C++ LLM inference project designed for local or cloud-based LLM inference with minimal setup and optimal performance across various hardware. Key advantages include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Pure C/C++ implementation with no dependencies&lt;/li&gt;
&lt;li&gt;First-class support for Apple silicon, optimized via ARM NEON, Accelerate, and Metal frameworks&lt;/li&gt;
&lt;li&gt;AVX, AVX2, AVX512, and AMX support for x86 architectures&lt;/li&gt;
&lt;li&gt;Multiple quantization options (from 5-bit to 8-bit) for faster inference and lower memory usage&lt;/li&gt;
&lt;li&gt;Multiple GPU backend support including NVIDIA (CUDA), AMD (HIP), and Moore Threads MTT (MUSA)&lt;/li&gt;
&lt;li&gt;CPU+GPU hybrid inference, enabling partial acceleration for models larger than VRAM capacity&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To run in llama.cpp, models must be stored in GGUF format. You can run GGUF models through llama.cpp containers, leveraging its full capabilities. When running GGUF models in SageMaker, combining them with llama.cpp maximizes its efficient inference capabilities and multi-hardware architecture support.&lt;/p&gt;
&lt;h2 id="hosting-gguf-models-in-sagemaker-ai-using-byoc"&gt;Hosting GGUF Models in SageMaker AI Using BYOC
&lt;/h2&gt;&lt;p&gt;The following section details how to combine GGUF with llama.cpp using Amazon SageMaker AI. We&amp;rsquo;ll demonstrate how to build a dedicated GGUF Docker container for SageMaker AI and apply it to model inference.&lt;/p&gt;
&lt;p&gt;Using the &lt;a class="link" href="https://docs.aws.amazon.com/sagemaker/latest/dg/adapt-inference-container.html" target="_blank" rel="noopener"
 &gt;Bring Your Own Container (BYOC)&lt;/a&gt; approach, Amazon SageMaker AI provides great flexibility. Regardless of programming language, runtime, framework, or dependencies, you can integrate virtually any model and code into the SageMaker AI ecosystem.&lt;/p&gt;
&lt;p&gt;This article uses &lt;a class="link" href="https://aws.amazon.com/sagemaker-ai/notebooks/" target="_blank" rel="noopener"
 &gt;Amazon SageMaker AI Notebook&lt;/a&gt; to build and deploy the GGUF model runtime framework. The solution architecture is shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Solution Architecture" class="gallery-image" data-flex-basis="344px" data-flex-grow="143" height="680" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai1.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai1_hu_77d54a03e10f814.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai1.jpg 976w" width="976"&gt;&lt;/p&gt;
&lt;p&gt;The main steps are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;From HuggingFace, download a GGUF model and upload it to S3. This blog uses Llama 3 8B GGUF as an example.&lt;/li&gt;
&lt;li&gt;Prepare the key BYOC files in the Notebook: Dockerfile, main.py, requirements.txt, serve, server.sh.&lt;/li&gt;
&lt;li&gt;Create a custom Docker image and push it to Amazon ECR.&lt;/li&gt;
&lt;li&gt;Create an Amazon SageMaker Model in the Notebook and deploy it to an inference endpoint.&lt;/li&gt;
&lt;li&gt;During endpoint startup, download the GGUF model from S3 to the specified location in the container.&lt;/li&gt;
&lt;li&gt;Test the endpoint by invoking inference with the SageMaker SDK.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-create-amazon-sagemaker-ai-notebook"&gt;1. Create Amazon SageMaker AI Notebook
&lt;/h3&gt;&lt;p&gt;You can create a SageMaker AI Notebook instance in the AWS console, selecting an appropriate instance type and storage. Note that disk space of 30GB or more is recommended, primarily for container building and model storage.&lt;/p&gt;
&lt;p&gt;When selecting or creating an IAM role, ensure the role has permissions to push images to Amazon ECR.&lt;/p&gt;
&lt;p&gt;If the permissions are missing, add the following inline policy:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Notebook Creation" class="gallery-image" data-flex-basis="242px" data-flex-grow="101" height="1703" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2_hu_f3ed2e041fdb6436.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2_hu_37b333e052ca44e0.jpg 1600w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai2.jpg 1722w" width="1722"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="IAM Permissions" class="gallery-image" data-flex-basis="292px" data-flex-grow="121" height="1249" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai3.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai3_hu_8314466b84e5192e.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai3.jpg 1521w" width="1521"&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-json" data-lang="json"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;Effect&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;Allow&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;Action&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:CompleteLayerUpload&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:UploadLayerPart&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:InitiateLayerUpload&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:BatchCheckLayerAvailability&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:PutImage&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;ecr:BatchGetImage&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;],&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nt"&gt;&amp;#34;Resource&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;arn:aws:ecr:*:*:*&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="2-build-byoc-code"&gt;2. Build BYOC Code
&lt;/h3&gt;&lt;p&gt;The complete code is available in the &lt;a class="link" href="https://github.com/aws-samples/deploy-gguf-model-to-sagemaker" target="_blank" rel="noopener"
 &gt;aws-samples repository&lt;/a&gt;. This article covers key code explanations only.&lt;/p&gt;
&lt;p&gt;To run GGUF models in SageMaker, prepare the code with the following file structure:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;workspace
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- Dockerfile
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- main.py
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- requirements.txt
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- serve
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;|-- server.sh
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The runtime trigger logic and HTTP requests are shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Runtime Logic" class="gallery-image" data-flex-basis="287px" data-flex-grow="119" height="401" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai4.png" width="481"&gt;&lt;/p&gt;
&lt;h4 id="21-dockerfile"&gt;2.1 Dockerfile
&lt;/h4&gt;&lt;p&gt;The Dockerfile defines a container environment based on the llama.cpp CUDA version, designed specifically for running GGUF models on SageMaker.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;span class="lnt"&gt;25
&lt;/span&gt;&lt;span class="lnt"&gt;26
&lt;/span&gt;&lt;span class="lnt"&gt;27
&lt;/span&gt;&lt;span class="lnt"&gt;28
&lt;/span&gt;&lt;span class="lnt"&gt;29
&lt;/span&gt;&lt;span class="lnt"&gt;30
&lt;/span&gt;&lt;span class="lnt"&gt;31
&lt;/span&gt;&lt;span class="lnt"&gt;32
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-dockerfile" data-lang="dockerfile"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;ghcr.io/ggerganov/llama.cpp:full-cuda&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;PYTHONUNBUFFERED&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;MODELPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/app/llm_model.bin&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$PATH&lt;/span&gt;:/app&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;BUCKET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENV&lt;/span&gt; &lt;span class="nv"&gt;BUCKET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&amp;#34;&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;/app&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; apt-get update &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get install -y &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; unzip &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; libcurl4-openssl-dev &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; python3 &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; python3-pip &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; python3-dev &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; git &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; psmisc &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; pciutils&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; requirements.txt ./requirements.txt&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; main.py /app/&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; serve /app/&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;COPY&lt;/span&gt; server.sh /app/&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; chmod u+x serve&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; chmod u+x server.sh&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; pip3 install -r requirements.txt&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;RUN&lt;/span&gt; &lt;span class="nb"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;PATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/app:&lt;span class="nv"&gt;$PATH&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;ENTRYPOINT&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/bin/bash&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;8080&lt;/span&gt;&lt;span class="err"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;When building the Dockerfile, pay attention to these key environment variables:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;MODELPATH&lt;/strong&gt;: Specifies the model filename used at startup.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;BUCKET&lt;/strong&gt; and &lt;strong&gt;BUCKET_KEY&lt;/strong&gt;: Define the Amazon S3 bucket name and object key for the model file.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="22-mainpy"&gt;2.2 main.py
&lt;/h4&gt;&lt;p&gt;This file implements an HTTP API server for interacting with llama.cpp. Key modifications for SageMaker deployment include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Port configuration: main.py defaults to port 8080, connecting to llama.cpp on port 8081&lt;/li&gt;
&lt;li&gt;Route changes: /v1/completions changed to /invocations, added /ping health check&lt;/li&gt;
&lt;li&gt;Model loading: Added update_model function for automatic S3 model download&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;s3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;s3&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;s3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;download_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bucket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;MODELPATH&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;subprocess&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/app/server.sh&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;MODELPATH&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="ne"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;traceback&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;format_exc&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;/ping&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;GET&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ping&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nd"&gt;@app.route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/invocations&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;methods&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;POST&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;completion&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;is_present&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;update_model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;bucket&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;key&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h4 id="23-sagemaker-inference-entry-point-serve-file"&gt;2.3 SageMaker Inference Entry Point: serve File
&lt;/h4&gt;&lt;p&gt;SageMaker inference nodes execute &lt;code&gt;docker run image serve&lt;/code&gt; by default when starting containers.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#!/bin/sh
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;serve&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;uvicorn &lt;span class="s1"&gt;&amp;#39;main:asgi_app&amp;#39;&lt;/span&gt; --host 0.0.0.0 --port &lt;span class="m"&gt;8080&lt;/span&gt; --workers &lt;span class="m"&gt;8&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h4 id="24-llamacpp-startup-script-serversh"&gt;2.4 llama.cpp Startup Script: server.sh
&lt;/h4&gt;&lt;p&gt;server.sh starts the llama.cpp service. See the &lt;a class="link" href="https://github.com/ggerganov/llama.cpp/blob/master/examples/server/README.md" target="_blank" rel="noopener"
 &gt;llama.cpp documentation&lt;/a&gt; for available runtime parameters.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#!/bin/sh
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;server.sh&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;args: &lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;GPU Layer: &lt;/span&gt;&lt;span class="nv"&gt;$GPU_LAYERS&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;if&lt;/span&gt; lspci &lt;span class="p"&gt;|&lt;/span&gt; grep -i nvidia &lt;span class="p"&gt;&amp;amp;&lt;/span&gt;&amp;gt; /dev/null&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;NVIDIA GPU is available.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;NGL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="nv"&gt;$GPU_LAYERS&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;CPU_PER_SLOT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;else&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;No NVIDIA GPU found.&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;NGL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;CPU_PER_SLOT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;killall llama-server
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;/app/llama-server -m &lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt; -c &lt;span class="m"&gt;2048&lt;/span&gt; -t &lt;span class="k"&gt;$(&lt;/span&gt;nproc --all&lt;span class="k"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; --host 0.0.0.0 --port &lt;span class="m"&gt;8081&lt;/span&gt; -cb &lt;span class="se"&gt;\
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; -np &lt;span class="k"&gt;$(($(&lt;/span&gt;nproc --all&lt;span class="k"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nv"&gt;$CPU_PER_SLOT&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt; -ngl &lt;span class="nv"&gt;$NGL&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="3-model-deployment-and-testing"&gt;3. Model Deployment and Testing
&lt;/h3&gt;&lt;p&gt;During model deployment, specify the ECR image, instance type, and corresponding environment variables.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;container_uri&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ECR_REPOSITORY_URI&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;IMAGE_TAG&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;instance_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;ml.g5.2xlarge&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utils&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name_from_base&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;llama-cpp-gguf-byoc&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;image_uri&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;container_uri&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;iam_role&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;MODELPATH&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;/app/&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;MODEL_NAME&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;BUCKET&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;S3_BUCKET_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;BUCKET_KEY&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MODEL_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;GPU_LAYERS&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;32&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;deploy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;instance_type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;instance_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;initial_instance_count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;During inference, you can pass parameters to get model output. Standard invocation:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;invoke_sagemaker_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker_runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoke_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;EndpointName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;response_body&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Body&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response_body&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;prompt&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;What are the most popular tourist attractions in Beijing?&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_tokens&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;temperature&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;repeat_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;frequency_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;top_p&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;inference&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;invoke_sagemaker_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inference&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;choices&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;text&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;Streaming output:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;span class="lnt"&gt;13
&lt;/span&gt;&lt;span class="lnt"&gt;14
&lt;/span&gt;&lt;span class="lnt"&gt;15
&lt;/span&gt;&lt;span class="lnt"&gt;16
&lt;/span&gt;&lt;span class="lnt"&gt;17
&lt;/span&gt;&lt;span class="lnt"&gt;18
&lt;/span&gt;&lt;span class="lnt"&gt;19
&lt;/span&gt;&lt;span class="lnt"&gt;20
&lt;/span&gt;&lt;span class="lnt"&gt;21
&lt;/span&gt;&lt;span class="lnt"&gt;22
&lt;/span&gt;&lt;span class="lnt"&gt;23
&lt;/span&gt;&lt;span class="lnt"&gt;24
&lt;/span&gt;&lt;span class="lnt"&gt;25
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;invoke_sagemaker_streaming_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker_runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoke_endpoint_with_response_stream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;EndpointName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;event_stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Body&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;event_stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;itm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;PayloadPart&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;Bytes&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;strict&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;choices&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;text&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;llama_args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;prompt&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;&amp;#34;What are the most popular tourist attractions in Beijing?&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;max_tokens&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;temperature&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;repeat_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;frequency_penalty&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;top_p&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;stream&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;invoke_sagemaker_streaming_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llama_args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;You can pass S3 bucket name and object key to the SageMaker AI inference instance for dynamic model replacement at runtime:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt; 1
&lt;/span&gt;&lt;span class="lnt"&gt; 2
&lt;/span&gt;&lt;span class="lnt"&gt; 3
&lt;/span&gt;&lt;span class="lnt"&gt; 4
&lt;/span&gt;&lt;span class="lnt"&gt; 5
&lt;/span&gt;&lt;span class="lnt"&gt; 6
&lt;/span&gt;&lt;span class="lnt"&gt; 7
&lt;/span&gt;&lt;span class="lnt"&gt; 8
&lt;/span&gt;&lt;span class="lnt"&gt; 9
&lt;/span&gt;&lt;span class="lnt"&gt;10
&lt;/span&gt;&lt;span class="lnt"&gt;11
&lt;/span&gt;&lt;span class="lnt"&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;configure&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;bucket&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;S3_BUCKET_NAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s2"&gt;&amp;#34;key&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MODEL_NAME&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sagemaker_runtime&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;invoke_endpoint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;EndpointName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;endpoint_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;ContentType&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;application/json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;response: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The inference results after deployment are shown below.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Inference Results" class="gallery-image" data-flex-basis="483px" data-flex-grow="201" height="1648" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5.jpg" srcset="https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5_hu_8c8e6cd9d8315636.jpg 800w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5_hu_96443b5ae743182f.jpg 1600w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5_hu_31cf501a671b939e.jpg 2400w, https://robinsujob.github.io/p/sagemaker-gguf-model-inference/images/practice-running-gguf-format-model-inference-using-sagemaker-ai5.jpg 3322w" width="3322"&gt;&lt;/p&gt;
&lt;p&gt;After testing, if you no longer need the inference endpoint, delete the Endpoint resources to avoid unnecessary charges.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;This article demonstrated how to deploy GGUF format models on Amazon SageMaker AI inference endpoints using the Bring Your Own Container (BYOC) approach via SageMaker AI Notebook. After successful deployment, you can seamlessly integrate the model with other AWS services and embed it into your applications to build feature-rich AI applications.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ol&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/ggerganov/ggml/blob/master/docs/gguf.md" target="_blank" rel="noopener"
 &gt;GGUF Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/ggerganov/llama.cpp" target="_blank" rel="noopener"
 &gt;llama.cpp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://aws.amazon.com/cn/sagemaker/" target="_blank" rel="noopener"
 &gt;Amazon SageMaker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/sagemaker/latest/dg/adapt-inference-container.html" target="_blank" rel="noopener"
 &gt;Adapting Your Own Inference Container&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://sagemaker-examples.readthedocs.io/en/latest/advanced_functionality/scikit_bring_your_own/scikit_bring_your_own.html" target="_blank" rel="noopener"
 &gt;SageMaker BYOC Examples&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/ggerganov/llama.cpp/blob/gguf-python/examples/server/api_like_OAI.py" target="_blank" rel="noopener"
 &gt;llama.cpp API Example&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/aws-samples/genai-llm-cpu-sagemaker/tree/main/docker" target="_blank" rel="noopener"
 &gt;GenAI LLM CPU SageMaker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://community.aws/content/2eazHYzSfcY9flCGKsuGjpwqq1B" target="_blank" rel="noopener"
 &gt;Community AWS Article&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original post: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/practice-running-gguf-format-model-inference-using-sagemaker-ai/" target="_blank" rel="noopener"
 &gt;Practice Running GGUF Format Model Inference Using SageMaker AI&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;GitHub repo: &lt;a class="link" href="https://github.com/aws-samples/deploy-gguf-model-to-sagemaker" target="_blank" rel="noopener"
 &gt;aws-samples/deploy-gguf-model-to-sagemaker&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>From GPT-SoVITS to AWS MagicVoice: Building a Custom-Voice TTS Solution</title><link>https://robinsujob.github.io/p/aws-magicvoice-gpt-sovits-solution/</link><pubDate>Sun, 01 Dec 2024 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/aws-magicvoice-gpt-sovits-solution/</guid><description>&lt;p&gt;Around 2024, GPT-SoVITS quickly gained traction in the open-source speech community. It made custom voice creation—previously a highly specialized undertaking—accessible to everyday developers. The official project supports &lt;strong&gt;zero-shot TTS from roughly five seconds of reference audio&lt;/strong&gt;, as well as &lt;strong&gt;few-shot fine-tuning with about one minute of speech&lt;/strong&gt;. It can also use a reference voice to synthesize speech across languages, including Chinese, English, and Japanese.&lt;/p&gt;
&lt;p&gt;Its popularity stems not only from its output quality, but also from a WebUI that brings together data segmentation, speech recognition, text annotation, training, and inference tools. This toolchain is highly efficient for model developers. However, new challenges emerge when the system is handed to real business users: the interface is complex, model files require manual management, the frontend and backend are tightly coupled, and deployment and operations require expertise in GPUs, Linux, and model directory structures.&lt;/p&gt;
&lt;p&gt;This was the starting point for &lt;strong&gt;MagicVoice&lt;/strong&gt;: packaging GPT-SoVITS as a ready-to-use, manageable reference solution deployed in the customer&amp;rsquo;s own AWS account.&lt;/p&gt;
&lt;h2 id="from-gpt-sovitss-popularity-to-an-aws-solution"&gt;From GPT-SoVITS&amp;rsquo;s Popularity to an AWS Solution
&lt;/h2&gt;&lt;p&gt;The customer wanted to use custom voices for audiobook production and similar content workflows. The requirement went well beyond simply “reading text aloud”:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Provide multiple characters and emotions within one system;&lt;/li&gt;
&lt;li&gt;Create character voices quickly from a small number of samples;&lt;/li&gt;
&lt;li&gt;Generate Chinese, English, or mixed Chinese-English content with the same character;&lt;/li&gt;
&lt;li&gt;Keep end users from having to understand model weights or complex inference parameters;&lt;/li&gt;
&lt;li&gt;Allow model developers to train, update, and manage voices independently;&lt;/li&gt;
&lt;li&gt;Enable rapid environment deployment, with straightforward maintenance or decommissioning after testing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The native GPT-SoVITS WebUI is built with Gradio and offers a comprehensive feature set, but it combines model development, data processing, and end-user inference in a single interface. Nontechnical users face too many options, while solution maintainers cannot readily host it as a static website on Amazon S3 and Amazon CloudFront.&lt;/p&gt;
&lt;p&gt;MagicVoice therefore focused on four goals:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;One-click infrastructure deployment&lt;/strong&gt;: Use AWS CDK to generate an AWS CloudFormation template.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;A user-friendly interface&lt;/strong&gt;: Build a new frontend that exposes only essential options.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cloud-based model management&lt;/strong&gt;: Host models in Amazon S3 to simplify the deployment of characters, emotions, and reference voices.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Separation of developers and users&lt;/strong&gt;: Developers handle training, models, and voice licensing; users simply choose a voice and generate audio.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This adds a productization layer beyond merely moving the WebUI to Amazon EC2—and that layer is central to the value of the solution.&lt;/p&gt;
&lt;h2 id="magicvoice-architecture-on-aws"&gt;MagicVoice Architecture on AWS
&lt;/h2&gt;&lt;p&gt;The following diagram shows the overall MagicVoice architecture on AWS and its three core flows: frontend access, speech inference, and model updates.&lt;/p&gt;
&lt;figure&gt;
 &lt;img src="https://robinsujob.github.io/media/magicvoice/architecture-original.png" alt="Overall MagicVoice architecture on AWS" loading="lazy"&gt;
 &lt;figcaption&gt;Overall MagicVoice architecture on AWS&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The architecture consists of three main components: &lt;strong&gt;the web frontend, the inference backend, and the model synchronization and update service&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id="1-web-frontend"&gt;1. Web Frontend
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;The frontend is built with React and AWS Cloudscape;&lt;/li&gt;
&lt;li&gt;Compiled static assets are stored in Amazon S3;&lt;/li&gt;
&lt;li&gt;Amazon CloudFront serves as the unified access point and distributes the frontend content;&lt;/li&gt;
&lt;li&gt;The interface exposes only text, character, emotion, language, speed, and a small set of advanced inference options;&lt;/li&gt;
&lt;li&gt;The “Reload TTS Service” action enters the backend event flow through Amazon API Gateway.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cloudscape offers more than visual consistency. It allows the frontend to be released independently of Gradio and the model process, so end users never need to see developer-oriented functions such as training, audio segmentation, or weight paths.&lt;/p&gt;
&lt;h3 id="2-inference-backend"&gt;2. Inference Backend
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;The GPT-SoVITS inference service runs on a GPU-enabled Amazon EC2 instance;&lt;/li&gt;
&lt;li&gt;CloudFront forwards inference requests to the EC2 backend;&lt;/li&gt;
&lt;li&gt;The EC2 instance reads characters, emotions, model weights, and reference audio from the model S3 bucket;&lt;/li&gt;
&lt;li&gt;The inference API supports both standard responses and streaming output.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A proof-of-concept environment can start with a &lt;code&gt;g4dn.xlarge&lt;/code&gt; instance. Production instance sizing, however, should be benchmarked against the model version, concurrency, time to first audio output, audio duration, and GPU memory consumption.&lt;/p&gt;
&lt;h3 id="3-model-synchronization-and-update-service"&gt;3. Model Synchronization and Update Service
&lt;/h3&gt;&lt;p&gt;Model updates can be triggered in two ways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A model developer uploads or deletes model files in Amazon S3, and the resulting S3 event is sent to Amazon EventBridge;&lt;/li&gt;
&lt;li&gt;An authorized user selects the reload action in the frontend, causing Amazon API Gateway to send an event to EventBridge.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;EventBridge then invokes AWS Systems Manager Run Command. Using a predefined Systems Manager document, Run Command executes synchronization and reload scripts on the EC2 instance. The scripts synchronize the latest models from Amazon S3 to the inference instance and then refresh the service.&lt;/p&gt;
&lt;p&gt;As a result, model developers do not need to sign in to the EC2 instance or copy weights manually. The inference instance does not need to expose SSH, and routine management is handled through Amazon S3, EventBridge, and Systems Manager.&lt;/p&gt;
&lt;h2 id="why-separate-model-developers-from-end-users"&gt;Why Separate Model Developers from End Users?
&lt;/h2&gt;&lt;p&gt;A custom-voice solution serves two fundamentally different groups:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Role&lt;/th&gt;
 &lt;th&gt;Responsibilities&lt;/th&gt;
 &lt;th&gt;What They Do Not Need to Handle&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;Model developer&lt;/td&gt;
 &lt;td&gt;Prepare licensed source material, train models, organize directories, upload to Amazon S3, and validate voice quality&lt;/td&gt;
 &lt;td&gt;The business-facing interface and routine text generation&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;End user&lt;/td&gt;
 &lt;td&gt;Select a character, emotion, and language; enter text; preview or download the result&lt;/td&gt;
 &lt;td&gt;Weight files, training parameters, GPUs, or server operations&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;This separation improves usability and clarifies responsibility for voice rights and operations. Whether voice material is properly licensed and whether a model may be used commercially should be addressed in the model release process—not decided ad hoc by end users at inference time.&lt;/p&gt;
&lt;p&gt;The MagicVoice frontend therefore exposes only the options required by the business workflow:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;TTS Text;&lt;/li&gt;
&lt;li&gt;Character;&lt;/li&gt;
&lt;li&gt;Emotion;&lt;/li&gt;
&lt;li&gt;Language;&lt;/li&gt;
&lt;li&gt;Speed;&lt;/li&gt;
&lt;li&gt;Inference Batch;&lt;/li&gt;
&lt;li&gt;Top K, Top P, Temperature;&lt;/li&gt;
&lt;li&gt;Streaming;&lt;/li&gt;
&lt;li&gt;Reload TTS Service.&lt;/li&gt;
&lt;/ul&gt;
&lt;figure&gt;
 &lt;img src="https://robinsujob.github.io/media/magicvoice/demo-ui.png" alt="MagicVoice demo interface" loading="lazy"&gt;
 &lt;figcaption&gt;The streamlined MagicVoice user interface built with AWS Cloudscape&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;h2 id="the-model-lifecycle-from-training-to-production"&gt;The Model Lifecycle: From Training to Production
&lt;/h2&gt;&lt;p&gt;GPT-SoVITS models used to generate different character voices require a consistent directory structure for GPT weights, SoVITS weights, reference audio, reference text, and language metadata. The basic workflow is:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Prepare and train a voice model in a separate training environment;&lt;/li&gt;
&lt;li&gt;Organize the model according to the MagicVoice directory convention;&lt;/li&gt;
&lt;li&gt;Upload the model directory to the model storage S3 bucket;&lt;/li&gt;
&lt;li&gt;An S3 event triggers EventBridge;&lt;/li&gt;
&lt;li&gt;EventBridge invokes Systems Manager Run Command;&lt;/li&gt;
&lt;li&gt;The EC2 instance synchronizes the model and reloads the inference service;&lt;/li&gt;
&lt;li&gt;After refreshing the character list, users can select the new voice.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Training and online inference should run in separate environments. Start GPU instances on demand for model training, then publish the resulting models to Amazon S3 so that training workloads do not consume online inference capacity.&lt;/p&gt;
&lt;p&gt;For consistent management of character voices and emotions, organize directories by character and model version, and store the model weights, reference audio, and inference configuration together in each directory.&lt;/p&gt;
&lt;figure&gt;
 &lt;img src="https://robinsujob.github.io/media/magicvoice/model-directory.png" alt="Example MagicVoice character model directory containing reference audio, infer_config.json, GPT weights, and SoVITS weights" loading="lazy"&gt;
 &lt;figcaption&gt;Example character model directory&lt;/figcaption&gt;
&lt;/figure&gt;
&lt;p&gt;The &lt;code&gt;infer_config.json&lt;/code&gt; file is the entry point for each character model&amp;rsquo;s inference configuration. It records the locations of the GPT and SoVITS weight files, while &lt;code&gt;emotion_list&lt;/code&gt; maps emotion labels such as “default” and “angry” to the corresponding reference audio (&lt;code&gt;ref_wav_path&lt;/code&gt;), reference text (&lt;code&gt;prompt_text&lt;/code&gt;), and language (&lt;code&gt;prompt_language&lt;/code&gt;). After a user selects a character and emotion in the frontend, the inference service uses this configuration to load the appropriate model weights and reference audio, eliminating the need to maintain each file path in application code.&lt;/p&gt;
&lt;p&gt;Each model version should include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An &lt;code&gt;infer_config.json&lt;/code&gt; inference configuration file;&lt;/li&gt;
&lt;li&gt;GPT and SoVITS model weights;&lt;/li&gt;
&lt;li&gt;Reference audio for each emotion, together with its text and language metadata;&lt;/li&gt;
&lt;li&gt;The model version and release date.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="ready-to-use-deployment-with-infrastructure-as-code"&gt;Ready-to-Use Deployment with Infrastructure as Code
&lt;/h2&gt;&lt;p&gt;MagicVoice uses Amazon CloudFront, Amazon S3, Amazon EC2, Amazon API Gateway, Amazon EventBridge, AWS Systems Manager, and AWS Identity and Access Management (IAM). Creating these resources manually in the console would not only slow deployment, but also make it difficult to maintain consistency across environments. This solution uses infrastructure as code (IaC) to deploy and manage the complete solution framework.&lt;/p&gt;
&lt;p&gt;The infrastructure template creates and connects:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A frontend S3 bucket;&lt;/li&gt;
&lt;li&gt;A model S3 bucket;&lt;/li&gt;
&lt;li&gt;A CloudFront distribution;&lt;/li&gt;
&lt;li&gt;A GPU-enabled EC2 instance, Amazon EBS volumes, a security group, and an instance role;&lt;/li&gt;
&lt;li&gt;An API Gateway reload API;&lt;/li&gt;
&lt;li&gt;EventBridge rules;&lt;/li&gt;
&lt;li&gt;A Systems Manager document;&lt;/li&gt;
&lt;li&gt;The required IAM roles and policies.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Permissions follow the principle of least privilege: the EC2 instance can read only the designated model bucket; EventBridge can invoke only the designated Systems Manager document; Run Command targets only the intended instance; and the frontend reload API must not allow anonymous access.&lt;/p&gt;
&lt;h2 id="demo-from-text-input-to-speech-generation"&gt;Demo: From Text Input to Speech Generation
&lt;/h2&gt;&lt;p&gt;The following video demonstrates the complete MagicVoice workflow in about three minutes, including character and parameter selection, speech generation, and output preview.&lt;/p&gt;
&lt;video controls preload="metadata" poster="/media/magicvoice/demo-ui.png" style="width:100%;max-width:1100px"&gt;
 &lt;source src="https://robinsujob.github.io/media/magicvoice/magicvoice-demo.mp4" type="video/mp4"&gt;
 &lt;a href="https://robinsujob.github.io/media/magicvoice/magicvoice-demo.mp4"&gt;Download the MagicVoice demo video&lt;/a&gt;
&lt;/video&gt;
&lt;p&gt;If your browser does not support embedded playback, you can &lt;a class="link" href="https://robinsujob.github.io/media/magicvoice/magicvoice-demo.mp4" &gt;download the demo video directly&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="conclusion"&gt;Conclusion
&lt;/h2&gt;&lt;p&gt;GPT-SoVITS lowered the barrier to few-shot voice cloning and cross-lingual speech synthesis. MagicVoice takes the next step by packaging these capabilities as a solution that can be rapidly deployed, easily used, and centrally managed within a customer&amp;rsquo;s AWS account.&lt;/p&gt;
&lt;p&gt;The value of MagicVoice lies not in any single model parameter, but in its end-to-end design for real-world usage:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Build a clean, intuitive frontend with AWS Cloudscape;&lt;/li&gt;
&lt;li&gt;Host and distribute the frontend through Amazon S3 and Amazon CloudFront;&lt;/li&gt;
&lt;li&gt;Run the GPT-SoVITS inference service on a GPU-enabled EC2 instance;&lt;/li&gt;
&lt;li&gt;Centrally manage characters, emotions, and model files in Amazon S3;&lt;/li&gt;
&lt;li&gt;Automate model synchronization and service reloads with Amazon EventBridge and AWS Systems Manager;&lt;/li&gt;
&lt;li&gt;Standardize repeatable deployment with AWS CDK and AWS CloudFormation;&lt;/li&gt;
&lt;li&gt;Separate model development from routine use to reduce complexity for end users.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Together, these design choices transform the developer-oriented GPT-SoVITS WebUI into an AWS TTS reference architecture that is easier to deploy, operate, and maintain.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/RVC-Boss/GPT-SoVITS" target="_blank" rel="noopener"
 &gt;GPT-SoVITS GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Introduction.html" target="_blank" rel="noopener"
 &gt;Amazon CloudFront Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/EventBridge.html" target="_blank" rel="noopener"
 &gt;Using Amazon S3 with Amazon EventBridge&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/systems-manager/latest/userguide/run-command.html" target="_blank" rel="noopener"
 &gt;AWS Systems Manager Run Command&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/cdk/v2/guide/home.html" target="_blank" rel="noopener"
 &gt;AWS CDK Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://calculator.aws/" target="_blank" rel="noopener"
 &gt;AWS Pricing Calculator&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</description></item><item><title>API Gateway CORS Configuration Guide</title><link>https://robinsujob.github.io/p/api-gateway-cors-configuration/</link><pubDate>Mon, 28 Oct 2024 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/api-gateway-cors-configuration/</guid><description>&lt;p&gt;CORS, or Cross-Origin Resource Sharing, is an HTTP header-based mechanism that allows servers to indicate which external origins (domains, protocols, or ports) can access their resources. CORS addresses the inconvenience caused by the same-origin policy — a browser security mechanism that restricts web pages from loading resources from different origins to prevent malicious websites from stealing sensitive data. However, this restriction also blocks legitimate cross-origin requests. Complex applications often reference third-party APIs and resources in their client-side code, and CORS is needed when the client domain does not match the server domain. Through CORS, browsers can safely perform cross-origin data transfers, bypassing the limitations of the same-origin policy. CORS allows client browsers to check with third-party servers whether a request is authorized before any data is transferred. Due to the complexity of CORS configuration, developers often set overly permissive cross-origin access permissions — for example, using &amp;ldquo;*&amp;rdquo; in allowed headers to permit all access — which can compromise application security.&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://aws.amazon.com/cn/api-gateway/?nc1=h_ls" target="_blank" rel="noopener"
 &gt;Amazon API Gateway&lt;/a&gt; is a fully managed service that helps developers easily manage APIs at any scale. It is commonly used for backend service management and routing. Therefore, configuring CORS on API Gateway is a very common requirement. In Amazon API Gateway, different API types have different CORS configuration methods, which adds to the complexity of CORS configuration.&lt;/p&gt;
&lt;p&gt;This article explains how to configure cross-origin resource sharing in Amazon API Gateway&amp;rsquo;s &lt;a class="link" href="https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/how-to-cors.html" target="_blank" rel="noopener"
 &gt;REST API&lt;/a&gt; and &lt;a class="link" href="https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/http-api-cors.html" target="_blank" rel="noopener"
 &gt;HTTP API&lt;/a&gt;, with a focus on the differences between REST API and HTTP API in CORS configuration, as well as how to configure CORS when authorization is enabled. By reading this article, you will master the CORS configuration techniques in Amazon API Gateway, enabling cross-origin access configurations that meet the &lt;a class="link" href="https://docs.aws.amazon.com/zh_cn/wellarchitected/latest/framework/sec_permissions_least_privileges.html" target="_blank" rel="noopener"
 &gt;principle of least privilege&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="how-cors-works"&gt;How CORS Works
&lt;/h2&gt;&lt;p&gt;CORS is an HTTP header-based mechanism where servers use header information to restrict access. CORS works primarily through the following &lt;a class="link" href="https://developer.mozilla.org/zh-CN/docs/Glossary/CORS#cors_%E6%A0%87%E5%A4%B4" target="_blank" rel="noopener"
 &gt;key HTTP headers&lt;/a&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Origin&lt;/strong&gt;: Indicates the request origin.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access-Control-Allow-Origin&lt;/strong&gt;: Specifies which origins can access the resource.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access-Control-Allow-Methods&lt;/strong&gt;: Lists the HTTP methods allowed to access the resource.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access-Control-Allow-Headers&lt;/strong&gt;: Indicates which custom request headers are allowed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access-Control-Allow-Credentials&lt;/strong&gt;: Indicates whether credentials such as cookies are allowed.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Access-Control-Max-Age&lt;/strong&gt;: Indicates how long preflight request results can be cached.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Based on the complexity of the request, CORS divides them into two categories:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Simple requests&lt;/strong&gt;: No preflight is needed. These requests use GET/POST/HEAD methods and only use specific safe headers (such as Accept, Content-Type).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-simple requests&lt;/strong&gt;: Preflight is required. Any cross-origin request that is not a simple request is considered a non-simple request. Before sending the actual request, the browser first sends an OPTIONS request to confirm whether the server allows the actual request. This preflight mechanism prevents unauthorized cross-origin operations from having side effects on server data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For non-simple requests, the server relies on the browser to send a &lt;a class="link" href="https://developer.mozilla.org/zh-CN/docs/Glossary/Preflight_request" target="_blank" rel="noopener"
 &gt;CORS preflight&lt;/a&gt; or OPTIONS request. A complete request flow is as follows:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Complete CORS Request Flow" class="gallery-image" data-flex-basis="649px" data-flex-grow="270" height="335" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-1.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-1_hu_90f15b3de6560802.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-1.png 906w" width="906"&gt;&lt;/p&gt;
&lt;p&gt;The complete CORS flow for non-simple requests:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Client application initiates the request&lt;/li&gt;
&lt;li&gt;Browser sends the preflight request&lt;/li&gt;
&lt;li&gt;Server returns the preflight response&lt;/li&gt;
&lt;li&gt;Browser sends the actual request&lt;/li&gt;
&lt;li&gt;Server returns the actual response&lt;/li&gt;
&lt;li&gt;Client receives the actual response&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id="introduction-to-api-gateway"&gt;Introduction to API Gateway
&lt;/h2&gt;&lt;p&gt;&lt;a class="link" href="https://aws.amazon.com/cn/api-gateway/?nc1=h_ls" target="_blank" rel="noopener"
 &gt;Amazon API Gateway&lt;/a&gt; is a fully managed service that helps developers easily create, publish, maintain, monitor, and secure APIs at any scale. Without the need to maintain the underlying infrastructure, API Gateway can accept and process thousands of concurrent API calls. API Gateway supports both RESTful and WebSocket API types, as well as multiple backend integration types such as containerized services, serverless workloads, and HTTP endpoints. Other key features of API Gateway include traffic management, CORS support, authorization and access control, throttling, monitoring, and API version management.&lt;/p&gt;
&lt;h3 id="rest-api-vs-http-api"&gt;REST API vs HTTP API
&lt;/h3&gt;&lt;p&gt;REST API and HTTP API are both RESTful API products of Amazon API Gateway. Their differences are as follows.&lt;/p&gt;
&lt;h4 id="features"&gt;Features
&lt;/h4&gt;&lt;p&gt;REST API&amp;rsquo;s unique features include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;API key support&lt;/li&gt;
&lt;li&gt;Per-client throttling&lt;/li&gt;
&lt;li&gt;Request validation&lt;/li&gt;
&lt;li&gt;AWS WAF integration&lt;/li&gt;
&lt;li&gt;Private API endpoints&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;HTTP API is designed with a minimalist feature set. Its characteristics include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Automatic deployment&lt;/li&gt;
&lt;li&gt;Default stage support&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="performance-and-pricing"&gt;Performance and Pricing
&lt;/h4&gt;&lt;p&gt;Because HTTP API has a more streamlined feature set, it offers faster speeds and lower prices.&lt;/p&gt;
&lt;h2 id="configuring-cors-in-api-gateway"&gt;Configuring CORS in API Gateway
&lt;/h2&gt;&lt;p&gt;To resolve cross-origin issues in Amazon API Gateway, you need to perform CORS configuration. API Gateway will automatically use the OPTIONS method and attempt to add CORS headers to existing method integration responses, which helps you handle preflight requests and actual responses. In Amazon API Gateway, REST API and HTTP API have different CORS configuration methods.&lt;/p&gt;
&lt;h3 id="rest-api-cors-configuration"&gt;REST API CORS Configuration
&lt;/h3&gt;&lt;ul&gt;
&lt;li&gt;As shown in the following figure, the REST API has a default / resource, under which we created a POST method integrated with a Lambda function as the backend service.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="REST API Resource Configuration" class="gallery-image" data-flex-basis="488px" data-flex-grow="203" height="703" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-2.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-2_hu_b125e11b39350d.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-2.png 1431w" width="1431"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;To configure CORS for the POST method under the / resource, we need to go back to the resource overview page and click Enable CORS.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Enable CORS Button" class="gallery-image" data-flex-basis="521px" data-flex-grow="217" height="658" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-3.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-3_hu_d101f4d084b25eb2.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-3.png 1431w" width="1431"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On the Enable CORS page, first configure the allowed methods for access control. To correctly respond to preflight requests, the OPTIONS method is enabled by default. We select the POST method that needs CORS configuration. Then, we configure the allowed headers and origins for access control — the specific allowed values depend on your backend application. Click Save after configuration.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="CORS Configuration Page" class="gallery-image" data-flex-basis="282px" data-flex-grow="117" height="1218" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-4.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-4_hu_8f5620fe90f1d472.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-4.png 1432w" width="1432"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;After saving, on the POST method&amp;rsquo;s integration response page, you can see that in the header mapping, the Access-Control-Allow-Origin mapping value is the allowed origin configured in the previous step.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Integration Response Header Mapping" class="gallery-image" data-flex-basis="405px" data-flex-grow="169" height="846" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-5.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-5_hu_3e8077200db8ada.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-5.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the resource list, we can also see that after enabling CORS, API Gateway automatically created an OPTIONS method for us. This method is used to correctly respond to preflight requests. The OPTIONS method&amp;rsquo;s integration type is mock integration, which returns a 200 status code for requests and returns all relevant preflight response headers. The header mapping values are determined by the settings in step 3.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="OPTIONS Method Configuration" class="gallery-image" data-flex-basis="408px" data-flex-grow="170" height="839" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-6.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-6_hu_cdf3509b959456e6.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-6.png 1429w" width="1429"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Testing a cross-origin POST request from the allowed origin endpoint configured in step 3, we can see that for both the preflight request and the actual request, the REST API returns the corresponding headers.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Preflight Request Response" class="gallery-image" data-flex-basis="1424px" data-flex-grow="593" height="241" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-7.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-7_hu_d46b95e540d3781a.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-7.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Actual Request Response" class="gallery-image" data-flex-basis="1926px" data-flex-grow="802" height="178" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-8.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-8_hu_dc031d25f9d16a4a.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-8.png 1429w" width="1429"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;For non-root paths, you can also check CORS (Cross-Origin Resource Sharing) when creating the resource, so that API Gateway automatically creates an OPTIONS method under this resource to handle preflight requests.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Check CORS When Creating Resource" class="gallery-image" data-flex-basis="486px" data-flex-grow="202" height="706" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-9.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-9_hu_6be09ed5e09f6b5f.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-9.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;p&gt;Note that the OPTIONS method created using this approach will accept cross-origin requests from all origins and all methods by default. If you need to tighten the permissions, you can click Edit in the upper right corner to modify it.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Edit OPTIONS Method" class="gallery-image" data-flex-basis="612px" data-flex-grow="255" height="561" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-10.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-10_hu_c3791a9f73aa0d53.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-10.png 1431w" width="1431"&gt;&lt;/p&gt;
&lt;p&gt;After checking CORS (Cross-Origin Resource Sharing) when creating the resource, you still need to click Enable CORS in the upper right corner of the resource overview page (following the same method as step 3) to configure the CORS-related headers for the specific request method (such as POST).&lt;/p&gt;
&lt;p&gt;&lt;img alt="Configure CORS Headers for Specific Methods" class="gallery-image" data-flex-basis="1635px" data-flex-grow="681" height="210" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-11.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-11_hu_16695efaae835e00.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-11.png 1431w" width="1431"&gt;&lt;/p&gt;
&lt;h4 id="cors-configuration-with-authorization"&gt;CORS Configuration with Authorization
&lt;/h4&gt;&lt;p&gt;You can integrate &lt;a class="link" href="https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html" target="_blank" rel="noopener"
 &gt;Lambda authorizers&lt;/a&gt; or &lt;a class="link" href="https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html" target="_blank" rel="noopener"
 &gt;Cognito authorizers&lt;/a&gt; with your REST API. When enabling authorization, be sure to only enable it for the methods that need it — do not enable authorization for the OPTIONS method, otherwise preflight requests (which are unauthorized) will result in 401 errors.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Enable Authorization for POST Method" class="gallery-image" data-flex-basis="469px" data-flex-grow="195" height="730" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-12.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-12_hu_f126b76e649ca0a7.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-12.png 1428w" width="1428"&gt;&lt;/p&gt;
&lt;p&gt;Enable authorization for the POST method.&lt;/p&gt;
&lt;p&gt;&lt;img alt="OPTIONS Authorization Error" class="gallery-image" data-flex-basis="2365px" data-flex-grow="985" height="145" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-13.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-13_hu_1c92ddbc0a1b9ce1.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-13.png 1429w" width="1429"&gt;&lt;/p&gt;
&lt;p&gt;If authorization is enabled for the OPTIONS method, preflight requests will fail with errors.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Do Not Authorize OPTIONS" class="gallery-image" data-flex-basis="469px" data-flex-grow="195" height="731" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-14.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-14_hu_9207afd8a90f3ad.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-14.png 1429w" width="1429"&gt;&lt;/p&gt;
&lt;p&gt;Do not enable authorization for the OPTIONS method.&lt;/p&gt;
&lt;h3 id="http-api-cors-configuration"&gt;HTTP API CORS Configuration
&lt;/h3&gt;&lt;p&gt;In this section, we continue to use configuring CORS for the POST method under the / resource as an example to demonstrate how to configure CORS for HTTP API.&lt;/p&gt;
&lt;p&gt;HTTP API CORS configuration is relatively simple because once you enable CORS for your HTTP API, API Gateway will automatically respond to preflight requests from browsers and automatically add CORS-related headers to your backend responses. You will not see the configuration of the OPTIONS method that API Gateway uses to automatically respond to preflight requests in your API resources, nor will you see the response header configuration for the POST method.&lt;/p&gt;
&lt;p&gt;The following are the detailed steps to enable HTTP API CORS:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Go to the HTTP API page, click CORS in the left navigation bar, then click Configure in the upper right corner.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="HTTP API CORS Configuration Entry" class="gallery-image" data-flex-basis="590px" data-flex-grow="246" height="581" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-15.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-15_hu_e81433b783b1027a.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-15.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On the CORS configuration page, enter the allowed origins, headers, and methods for access control. In our example, we need to allow POST method access. Depending on your needs, you can also optionally configure exposed headers, max age, and credentials. After configuration, click Save in the lower right corner.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="HTTP API CORS Configuration Page" class="gallery-image" data-flex-basis="382px" data-flex-grow="159" height="897" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-16.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-16_hu_54f563eb5ffd4d90.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-16.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;After configuration, we cannot see the OPTIONS method configuration on the HTTP API routes page.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="HTTP API Routes Page" class="gallery-image" data-flex-basis="437px" data-flex-grow="182" height="785" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-17.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-17_hu_6a3deb6db50e33bd.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-17.png 1432w" width="1432"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Testing a cross-origin POST request from the allowed origin endpoint of the HTTP API, we can see that both the preflight OPTIONS request and the actual POST request receive correct responses. This is because API Gateway handles the HTTP API&amp;rsquo;s response to preflight requests and adds the CORS-related headers for us.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="HTTP API CORS Test Results" class="gallery-image" data-flex-basis="1875px" data-flex-grow="781" height="183" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-18.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-18_hu_63a7ec1f82bd643a.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-18.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;h4 id="cors-configuration-with-authorization-1"&gt;CORS Configuration with Authorization
&lt;/h4&gt;&lt;p&gt;Similar to REST API, HTTP API supports authorization for specific methods through &lt;a class="link" href="https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html" target="_blank" rel="noopener"
 &gt;Lambda&lt;/a&gt;, &lt;a class="link" href="https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-jwt-authorizer.html" target="_blank" rel="noopener"
 &gt;JWT&lt;/a&gt;, and &lt;a class="link" href="https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-access-control-iam.html" target="_blank" rel="noopener"
 &gt;IAM&lt;/a&gt; authorizers. If you want to enable CORS for an HTTP API with integrated authorizers, in most cases, HTTP API will automatically handle preflight requests for you. Therefore, no special configuration is needed — simply follow the steps above to enable CORS for an HTTP API with integrated authorizers. The only exception is when you create an authorized ANY method (as shown below). In this case, note that preflight requests will be handled by the ANY method rather than by HTTP API. Since preflight requests do not contain authorization information, the authorized ANY method will not return the correct status code and CORS headers, and the preflight request will fail with a 401 error.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Authorized ANY Method" class="gallery-image" data-flex-basis="724px" data-flex-grow="301" height="474" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-19.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-19_hu_6d078dcc831f02c8.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-19.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="ANY Method 401 Error" class="gallery-image" data-flex-basis="1383px" data-flex-grow="576" height="248" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-20.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-20_hu_722d8b7eaa811aee.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-20.png 1430w" width="1430"&gt;&lt;/p&gt;
&lt;p&gt;In this case, there are two solutions:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;(Recommended)&lt;/strong&gt; Create a specific route for each method and resource as needed, and avoid using the ANY method. For example, in our case, we only need to create a route for the POST method.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Create Route for Specific Method" class="gallery-image" data-flex-basis="723px" data-flex-grow="301" height="475" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-21.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-21_hu_a4f4c7744f5d9fe0.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-21.png 1432w" width="1432"&gt;&lt;/p&gt;
&lt;ol start="2"&gt;
&lt;li&gt;Create an unauthorized OPTIONS method route under the same resource path, and add any backend integration to the OPTIONS method (as shown below). This way, the unauthorized OPTIONS route will capture preflight requests.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Create Unauthorized OPTIONS Route" class="gallery-image" data-flex-basis="504px" data-flex-grow="210" height="680" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-22.png" srcset="https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-22_hu_1e0063c55466db2f.png 800w, https://robinsujob.github.io/p/api-gateway-cors-configuration/images/cors-config-22.png 1428w" width="1428"&gt;&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;This article introduced the CORS configuration methods for REST API and HTTP API in Amazon API Gateway, with special attention to how to configure both API types to ensure preflight requests are correctly responded to when authorization is enabled. The differences in CORS configuration between the two API types are as follows:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;API Type&lt;/th&gt;
 &lt;th&gt;CORS Configuration Method&lt;/th&gt;
 &lt;th&gt;Configuration Features&lt;/th&gt;
 &lt;th&gt;Authorization Notes&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;REST API&lt;/td&gt;
 &lt;td&gt;Configure CORS for specific routes in the REST API by clicking Enable CORS in the upper right corner of the route.&lt;/td&gt;
 &lt;td&gt;You can see the OPTIONS method used to respond to preflight requests and its response header settings under the configured route. You can also see the CORS header settings in the response of the methods that allow cross-origin access.&lt;/td&gt;
 &lt;td&gt;Do not enable authorization for the OPTIONS method.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;HTTP API&lt;/td&gt;
 &lt;td&gt;Configure CORS for the entire HTTP API by clicking CORS in the left navigation bar to enter the CORS configuration page.&lt;/td&gt;
 &lt;td&gt;Preflight requests are automatically handled by API Gateway, and the automatically responding OPTIONS method configuration is not visible in the console. CORS headers are automatically added by API Gateway, and the CORS header settings are also not visible in the response of the methods that allow cross-origin access.&lt;/td&gt;
 &lt;td&gt;Avoid using the ANY method. If you must use it, create an additional unauthorized OPTIONS method for handling preflight requests.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;To enable CORS for other AWS resources, refer to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/several-solutions-to-cloudfront-cross-domain-problem-cors/" target="_blank" rel="noopener"
 &gt;Enable CORS for CloudFront&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/several-solutions-to-cloudfront-cross-domain-problem-cors/" target="_blank" rel="noopener"
 &gt;Enable CORS for Lambda Function URLs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.aws.amazon.com/zh_cn/AmazonS3/latest/userguide/cors.html" target="_blank" rel="noopener"
 &gt;Enable CORS for S3 Static Website Hosting&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original article: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/amazon-api-gateway-cross-origin-resource-sharing-configuration/" target="_blank" rel="noopener"
 &gt;Amazon API Gateway CORS Configuration - AWS Blog&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>WordPress GenAI Plus - AI Content Generation Plugin</title><link>https://robinsujob.github.io/p/wordpress-genai-plus/</link><pubDate>Mon, 29 Apr 2024 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/wordpress-genai-plus/</guid><description>&lt;p&gt;Generative AI (GenAI) has risen rapidly since 2022, sweeping the globe and demonstrating enormous potential across multiple domains — text-to-image, text-to-text, image-to-image, and more. Models like Stable Diffusion and DALL-E can generate realistic images from natural-language descriptions, opening new possibilities for creative design and visual arts. Meanwhile, large language models such as Claude and GPT enable natural, fluent conversational interactions, empowering content creation and Q&amp;amp;A applications.&lt;/p&gt;
&lt;p&gt;WordPress is a widely used open-source content management system (CMS) ideal for creating and managing all types of websites — blogs, corporate sites, personal pages, and online stores. First released in 2003, it was designed to give users an easy-to-use platform for publishing content, even without a technical background.&lt;/p&gt;
&lt;p&gt;Now, leveraging the powerful AI capabilities of AWS, you can experience an intelligent AI writing assistant directly inside WordPress to accelerate your content creation. The newly launched WordPress GenAI Plus solution seamlessly integrates Claude 3 (via Amazon Bedrock) and Stable Diffusion into the WordPress editor. Simply provide a prompt or topic direction, and the AI will automatically generate article content and images. You can iterate as many times as needed until you achieve the ideal result.&lt;/p&gt;
&lt;p&gt;The main features provided by WordPress GenAI Plus writing assistant include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Text Generation&lt;/strong&gt;: Generate articles based on user prompts&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Image Generation&lt;/strong&gt;: Generate images based on user prompts&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Translation&lt;/strong&gt;: Currently supports Chinese, English, French, Japanese, and Korean as target languages; source language is unrestricted&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar Check&lt;/strong&gt;: Provides grammar correction suggestions for selected English paragraphs (English only)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This solution supports one-click deployment via an Amazon CloudFormation template. By deploying the stack, users get the complete infrastructure — WordPress site, database, CDN, and AI capabilities — greatly simplifying setup and usage.&lt;/p&gt;
&lt;h2 id="architecture-overview"&gt;Architecture Overview
&lt;/h2&gt;&lt;p&gt;The WordPress GenAI Plus solution can be deployed in any AWS global region. It offers both a two-tier architecture (separating web and database layers) and a traditional LNMP (Linux, Nginx, MySQL, PHP) all-in-one architecture. The two-tier architecture is shown below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Two-tier architecture" class="gallery-image" data-flex-basis="297px" data-flex-grow="124" height="2451" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img1.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img1_hu_ef986cc92121e38c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img1_hu_1f38168aeec108de.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img1_hu_9cb7cba0cd6786a8.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img1.jpg 3042w" width="3042"&gt;&lt;/p&gt;
&lt;p&gt;Architecture highlights: Two-tier (web + data), WordPress on public subnet, RDS on private subnet, multi-AZ option, CloudFront CDN, Lambda + Bedrock integration for GenAI capabilities. EC2, RDS, and Lambda can be deployed in any region; Bedrock is available in us-east-1 or us-west-2.&lt;/p&gt;
&lt;h2 id="deployment-process"&gt;Deployment Process
&lt;/h2&gt;&lt;h3 id="model-access-preparation"&gt;Model Access Preparation
&lt;/h3&gt;&lt;p&gt;Before getting started, you need to request model access permissions in Amazon Bedrock.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select the Bedrock service in the AWS global console&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Select Bedrock service" class="gallery-image" data-flex-basis="423px" data-flex-grow="176" height="1632" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img2.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img2_hu_ff7adb1de6e28a7c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img2_hu_ed60cc0f95369656.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img2_hu_eb0db7bf6f95213e.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img2.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select the model deployment region (us-east-1 or us-west-2) in the top-right corner&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Select region" class="gallery-image" data-flex-basis="424px" data-flex-grow="176" height="1628" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img3.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img3_hu_c6dbf5acc69a53a.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img3_hu_690ac4e1519353c2.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img3_hu_7ffe6cbb83d97648.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img3.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In the left sidebar, select &amp;ldquo;Model access&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Model access" class="gallery-image" data-flex-basis="426px" data-flex-grow="177" height="1622" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img4.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img4_hu_eb5eae4fa56907c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img4_hu_2084e03a8b9945a9.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img4_hu_8bbe8fe229be6d5f.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img4.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Click the &amp;ldquo;Manage model access&amp;rdquo; button&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Manage model access" class="gallery-image" data-flex-basis="602px" data-flex-grow="250" height="1148" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img5.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img5_hu_4c7e1fcce0773894.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img5_hu_914817e58e4a3a46.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img5_hu_9c6dc810c37010b5.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img5.jpg 2880w" width="2880"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check the boxes for SDXL1.0, Claude 3 Sonnet, and Claude 3 Haiku, then click &amp;ldquo;Save changes&amp;rdquo;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Select models" class="gallery-image" data-flex-basis="462px" data-flex-grow="192" height="1368" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img6.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img6_hu_78eff3ca9133ee4f.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img6_hu_d3ad62343717f74c.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img6_hu_cc484afaeb398a9b.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img6.jpg 2634w" width="2634"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Wait for permission to be granted (status changes to &amp;ldquo;Access granted&amp;rdquo;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="create-key-pair"&gt;Create Key Pair
&lt;/h3&gt;&lt;p&gt;A key pair allows you to securely connect to your Amazon EC2 instance via SSH. Before deployment, make sure you have created a key pair.&lt;/p&gt;
&lt;p&gt;If you haven&amp;rsquo;t created one yet, &lt;a class="link" href="https://console.aws.amazon.com/ec2/home?#CreateKeyPair:" target="_blank" rel="noopener"
 &gt;click here to create a key pair&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Create key pair" class="gallery-image" data-flex-basis="421px" data-flex-grow="175" height="1617" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img7.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img7_hu_cad5c45fa3e0a90d.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img7_hu_f6cce1f57c16990f.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img7_hu_94281de5e7298609.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img7.jpg 2842w" width="2842"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Confirm the deployment region, enter a key pair name, click &amp;ldquo;Create key pair&amp;rdquo; and save it securely.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="create-cloudformation-stack"&gt;Create CloudFormation Stack
&lt;/h3&gt;&lt;p&gt;Click the link above to enter the stack deployment page.&lt;/p&gt;
&lt;p&gt;&lt;a class="link" href="https://console.aws.amazon.com/cloudformation/home?#stacks/create/template?templateURL=https://aws-cn-getting-started.s3.us-west-2.amazonaws.com/wordpress-plus/WordpressTwoLayer.template.json" target="_blank" rel="noopener"
 &gt;Click to deploy&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Confirm the deployment region in the top-right corner (must match your key pair region), then specify the following parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Custom stack name&lt;/li&gt;
&lt;li&gt;EC2 instance type&lt;/li&gt;
&lt;li&gt;EC2 disk size&lt;/li&gt;
&lt;li&gt;EC2 key pair&lt;/li&gt;
&lt;li&gt;Database username&lt;/li&gt;
&lt;li&gt;Database password (min 8 chars, no /, quotes, or @)&lt;/li&gt;
&lt;li&gt;Database instance type&lt;/li&gt;
&lt;li&gt;Database storage size&lt;/li&gt;
&lt;li&gt;Multi-AZ deployment option&lt;/li&gt;
&lt;li&gt;Bedrock deployment region&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Click Next.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Parameter configuration 1" class="gallery-image" data-flex-basis="437px" data-flex-grow="182" height="1482" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img8.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img8_hu_7d2b2c9cef5c41c6.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img8_hu_421e9f77cd018aa8.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img8_hu_439213e3bdf1cd13.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img8.jpg 2701w" width="2701"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Parameter configuration 2" class="gallery-image" data-flex-basis="340px" data-flex-grow="141" height="1481" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img9.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img9_hu_6288bdd7fec4c900.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img9_hu_b4fcc6939852e6fa.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img9.jpg 2103w" width="2103"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On step 3 &amp;ldquo;Configure stack options&amp;rdquo;, click Next&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Configure stack options" class="gallery-image" data-flex-basis="468px" data-flex-grow="195" height="1267" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img10.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img10_hu_95518abbf47e5ca7.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img10_hu_fb67f51952fa9f00.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img10_hu_2115bc5a21c35a03.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img10.jpg 2471w" width="2471"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On step 4, check the IAM acknowledgments and CAPABILITY_AUTO_EXPAND, then click Submit&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Confirm and submit" class="gallery-image" data-flex-basis="1388px" data-flex-grow="578" height="334" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img11.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img11_hu_3d90c68d964ed45.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img11_hu_ff83e94cfa74f7ac.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img11.jpg 1933w" width="1933"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Wait about 10 minutes for the stack status to become CREATE_COMPLETE&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Creating" class="gallery-image" data-flex-basis="286px" data-flex-grow="119" height="946" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img12.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img12_hu_81f94874089ae4dd.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img12.jpg 1129w" width="1129"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Creation complete" class="gallery-image" data-flex-basis="1107px" data-flex-grow="461" height="273" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img13.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img13_hu_3781969cab94306a.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img13.jpg 1260w" width="1260"&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Check the Outputs tab for GenAI Function URL, WordPress install URL, admin URL, and site URL&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Outputs" class="gallery-image" data-flex-basis="496px" data-flex-grow="206" height="537" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img14.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img14_hu_353ab9d564899a99.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img14.jpg 1111w" width="1111"&gt;&lt;/p&gt;
&lt;h2 id="start-using-wordpress-ai-capabilities"&gt;Start Using WordPress AI Capabilities
&lt;/h2&gt;&lt;h3 id="initialize-wordpress"&gt;Initialize WordPress
&lt;/h3&gt;&lt;p&gt;After deployment, find the InstallWordPress field in the CloudFormation stack outputs and open the URL to configure WordPress.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Install WordPress" class="gallery-image" data-flex-basis="746px" data-flex-grow="310" height="926" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img15.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img15_hu_bf06d4c5c6828201.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img15_hu_481036577f33f7d8.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img15_hu_2d7b049f3e10d176.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img15.jpg 2879w" width="2879"&gt;&lt;/p&gt;
&lt;p&gt;In the one-click deployment, database information is pre-configured. You only need to set up your site title, username, and password, then click &amp;ldquo;Install WordPress&amp;rdquo; and log into the admin panel.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Configure site info" class="gallery-image" data-flex-basis="223px" data-flex-grow="93" height="1758" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img16.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img16_hu_86e385ae7faafc9c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img16_hu_641f58c03ebd0476.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img16.jpg 1639w" width="1639"&gt;&lt;/p&gt;
&lt;h3 id="enable-the-genai-plugin"&gt;Enable the GenAI Plugin
&lt;/h3&gt;&lt;p&gt;In the WordPress admin panel, click &amp;ldquo;Plugins&amp;rdquo;, find &amp;ldquo;Amazon AI writing assistant&amp;rdquo; and click Activate.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Enable plugin" class="gallery-image" data-flex-basis="400px" data-flex-grow="166" height="1083" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img17.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img17_hu_efdbd2773c46eb06.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img17_hu_e62b2ca4d64d37f7.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img17.jpg 1805w" width="1805"&gt;&lt;/p&gt;
&lt;p&gt;The Lambda URL and Region are pre-configured. Verify and click Save.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Plugin configuration" class="gallery-image" data-flex-basis="456px" data-flex-grow="190" height="1388" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img18.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img18_hu_56798583b4336e7f.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img18_hu_a787804102fa7bcf.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img18_hu_f4020940aeba884f.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img18.jpg 2641w" width="2641"&gt;&lt;/p&gt;
&lt;h3 id="experience-genai-capabilities"&gt;Experience GenAI Capabilities
&lt;/h3&gt;&lt;p&gt;In the WordPress admin, click &amp;ldquo;New → Post&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="New post" class="gallery-image" data-flex-basis="457px" data-flex-grow="190" height="586" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img19.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img19_hu_824c0100d2ea8ab7.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img19.jpg 1118w" width="1118"&gt;&lt;/p&gt;
&lt;p&gt;In the editor body, type &lt;code&gt;/ask&lt;/code&gt; to trigger the Ask AI plugin. Click it to start using the AI features.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Ask AI plugin" class="gallery-image" data-flex-basis="658px" data-flex-grow="274" height="426" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img20.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img20_hu_71405a2822eef0f3.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img20.jpg 1168w" width="1168"&gt;&lt;/p&gt;
&lt;p&gt;You can enter text-to-text or text-to-image prompts in Chinese or English in the popup input box.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Text generation example" class="gallery-image" data-flex-basis="766px" data-flex-grow="319" height="432" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img21.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img21_hu_e2be7a30c6459c1b.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img21.jpg 1380w" width="1380"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Image generation example" class="gallery-image" data-flex-basis="778px" data-flex-grow="324" height="401" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img22.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img22_hu_21aebfa7b3e7210c.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img22.jpg 1300w" width="1300"&gt;&lt;/p&gt;
&lt;p&gt;See the demo below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="GenAI demo" class="gallery-image" data-flex-basis="368px" data-flex-grow="153" height="521" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/gif1.gif" width="800"&gt;&lt;/p&gt;
&lt;p&gt;The GenAI plugin also supports translation and grammar checking:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Translation&lt;/strong&gt;: Enter text in any language, click the pen icon in the floating toolbar → Translate Text. Target language is set in the plugin configuration page. Supported: ZH_CN, EN_US, FR, JA, KO.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Translation demo" class="gallery-image" data-flex-basis="417px" data-flex-grow="173" height="345" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/gif2.gif" width="600"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Translation config" class="gallery-image" data-flex-basis="428px" data-flex-grow="178" height="1400" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img25.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img25_hu_6826f283d45e92c0.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img25_hu_c1733b4c18dafc5e.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img25_hu_6e5b049473cfecbd.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img25.jpg 2500w" width="2500"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Grammar Check&lt;/strong&gt;: For text you want to correct, click the pen icon in the floating toolbar → Correct Text.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Grammar check demo" class="gallery-image" data-flex-basis="417px" data-flex-grow="173" height="345" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/gif3.gif" width="600"&gt;&lt;/p&gt;
&lt;h2 id="alternative-architecture"&gt;Alternative Architecture
&lt;/h2&gt;&lt;p&gt;You can configure your own domain name in Amazon CloudFront, use Certificate Manager to request a free public certificate for HTTPS, and update the site address in the WordPress admin panel.&lt;/p&gt;
&lt;p&gt;This solution also supports a traditional LNMP all-in-one WordPress architecture:&lt;/p&gt;
&lt;p&gt;&lt;img alt="LNMP architecture" class="gallery-image" data-flex-basis="709px" data-flex-grow="295" height="1259" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/wordpress-genai-plus/images/img27.jpg" srcset="https://robinsujob.github.io/p/wordpress-genai-plus/images/img27_hu_1a9e40034b81b345.jpg 800w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img27_hu_959d415c4d7de88c.jpg 1600w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img27_hu_b44b6ce634ac4395.jpg 2400w, https://robinsujob.github.io/p/wordpress-genai-plus/images/img27.jpg 3721w" width="3721"&gt;&lt;/p&gt;
&lt;p&gt;Deploy the LNMP architecture: &lt;a class="link" href="https://console.aws.amazon.com/cloudformation/home?#stacks/create/template?templateURL=https://aws-cn-getting-started.s3.us-west-2.amazonaws.com/wordpress-plus/WordpressInOne.template.json" target="_blank" rel="noopener"
 &gt;Click to deploy&lt;/a&gt;&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;Through this tutorial, you can deploy WordPress GenAI Plus on AWS with one click, integrating the power of generative AI into the WordPress editor for a brand-new AI-powered content creation experience.&lt;/p&gt;
&lt;p&gt;Related Resources:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://aws.amazon.com/cn/campaigns/aigc" target="_blank" rel="noopener"
 &gt;AWS AIGC&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original article: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/wordpress-genai-plus-accelerate-your-website-content-creation-with-generative-ai/" target="_blank" rel="noopener"
 &gt;WordPress GenAI Plus: Accelerate Your Website Content Creation with Generative AI&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>China CloudFront SSL Plugin Deployment Guide</title><link>https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/</link><pubDate>Wed, 15 Nov 2023 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post China CloudFront SSL Plugin Deployment Guide" /&gt;&lt;p&gt;This article applies to AWS China Region services. You will learn how to use Amazon CloudFront in the China Region to accelerate and distribute traffic from websites deployed on EC2, and use the &lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin&lt;/a&gt; to deploy free SSL certificates to protect your site. The plugin helps you generate, renew, and download free SSL certificates in the AWS China Regions, with Amazon CloudFront integration and automatic certificate renewal. You can also refer to the EC2-CloudFront integration section for configuring CloudFront to distribute EC2 website traffic in AWS Global Regions.&lt;/p&gt;
&lt;h2 id="why-choose-amazon-cloudfront"&gt;Why Choose Amazon CloudFront?
&lt;/h2&gt;&lt;p&gt;Amazon CloudFront is a Content Delivery Network (CDN) service provided by AWS. Using a CDN reduces website load times, lowers bounce rates, and increases traffic and revenue, while also reducing the load on origin servers, improving website stability, security, availability, and reliability. Amazon CloudFront securely delivers data, videos, applications, and APIs to customers globally with low latency and high transfer speeds in a developer-friendly environment, and integrates seamlessly with Amazon S3, Elastic Load Balancer, Amazon EC2, and more.&lt;/p&gt;
&lt;p&gt;Amazon CloudFront not only accelerates your website and reduces origin server load, but also lowers the data transfer costs for traffic leaving your AWS-hosted website to the internet. For example, compared to EC2&amp;rsquo;s outbound data transfer costs, using Amazon CloudFront in the AWS China Regions can help you reduce outbound data transfer costs by at least 67%. For specific pricing, refer to the AWS China Region official website &lt;a class="link" href="https://www.amazonaws.cn/ec2/pricing/#Amazon_EC2_.E6.95.B0.E6.8D.AE.E4.BC.A0.E8.BE.93" target="_blank" rel="noopener"
 &gt;EC2 Data Transfer Pricing&lt;/a&gt; and &lt;a class="link" href="https://www.amazonaws.cn/cloudfront/pricing/#On-demand_Pricing" target="_blank" rel="noopener"
 &gt;CloudFront Data Transfer Pricing&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="considerations-for-using-cloudfront-in-china-regions"&gt;Considerations for Using CloudFront in China Regions
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;ICP Filing and Domain Registration:&lt;/strong&gt; According to relevant laws and regulations, if you need to host a website in mainland China, you must first complete ICP filing. Domains without ICP filing cannot serve web content. Please prepare an ICP-filed domain. Refer to the AWS China &lt;a class="link" href="https://www.amazonaws.cn/support/icp/" target="_blank" rel="noopener"
 &gt;ICP Filing Support Documentation&lt;/a&gt; for more details on obtaining the required ICP filing for your domain.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;CloudFront Provided Domain:&lt;/strong&gt; CloudFront automatically assigns a &lt;code&gt;*.cloudfront.cn&lt;/code&gt; domain. However, due to ICP filing requirements, you cannot directly use this domain for website services in China. You must add an ICP-filed domain as an alternate domain name (also known as CNAME) to your CloudFront distribution, and then use the alternate domain name to access your website.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;CloudFront SSL/TLS Certificate Integration:&lt;/strong&gt; Amazon CloudFront in the China Regions does not currently support &lt;a class="link" href="https://www.amazonaws.cn/certificate-manager/features/" target="_blank" rel="noopener"
 &gt;Amazon Certificate Manager&lt;/a&gt;. If you need to use SSL/TLS certificates with CloudFront, you can obtain them from third-party certificate authorities and &lt;a class="link" href="https://docs.amazonaws.cn/AmazonCloudFront/latest/DeveloperGuide/cnames-and-https-procedures.html#cnames-and-https-uploading-certificates" target="_blank" rel="noopener"
 &gt;import SSL/TLS certificates&lt;/a&gt;. You can also use the &lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin&lt;/a&gt; to obtain free SSL/TLS certificates, which will be covered in detail in the &amp;ldquo;Deploy Free Certificates with China CloudFront SSL Plugin&amp;rdquo; section of this article.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Feature Differences Between China and Global Regions:&lt;/strong&gt; Amazon CloudFront in the China Regions does not currently support Lambda@Edge, Amazon WAF, and other features. See the &lt;a class="link" href="https://docs.amazonaws.cn/aws/latest/userguide/cloudfront.html#feature-diff" target="_blank" rel="noopener"
 &gt;documentation&lt;/a&gt; for more details.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="prerequisites"&gt;Prerequisites
&lt;/h2&gt;&lt;p&gt;To complete all sections of this article and get a consistent experience with AWS services, please ensure you meet the following prerequisites.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Your AWS China Region account has completed ICP filing, with ports 80/443 open&lt;/li&gt;
&lt;li&gt;You have an ICP-filed domain&lt;/li&gt;
&lt;li&gt;Use &lt;a class="link" href="https://www.amazonaws.cn/route53/" target="_blank" rel="noopener"
 &gt;Amazon Route 53&lt;/a&gt; as your DNS service. If you haven&amp;rsquo;t migrated your DNS to Amazon Route 53 yet, see the &lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/migrate-domain-to-amazon-route53/" target="_blank" rel="noopener"
 &gt;reference documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="integrating-ec2-with-cloudfront"&gt;Integrating EC2 with CloudFront
&lt;/h2&gt;&lt;p&gt;In the following sections, you will launch an EC2 instance, set up a web server, integrate EC2 with CloudFront, and deploy a free SSL certificate using the China CloudFront SSL Plugin.&lt;/p&gt;
&lt;p&gt;You will also learn some tips for using EC2 and CloudFront, such as using Elastic IP to fix your EC2 public DNS hostname for CloudFront origins, or using AWS-managed prefix lists to enhance EC2 security.&lt;/p&gt;
&lt;p&gt;The deployment architecture is shown below:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Deployment Architecture" class="gallery-image" data-flex-basis="279px" data-flex-grow="116" height="1090" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/architecture.png" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/architecture_hu_7853b82d2810f37e.png 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/architecture.png 1268w" width="1268"&gt;&lt;/p&gt;
&lt;h3 id="create-ec2-and-install-web-server"&gt;Create EC2 and Install Web Server
&lt;/h3&gt;&lt;p&gt;In this section, you will create an EC2 instance with a web server, then bind an Elastic IP to fix the EC2 public DNS hostname.&lt;/p&gt;
&lt;h4 id="1-launch-ec2-instance"&gt;1. Launch EC2 Instance
&lt;/h4&gt;&lt;p&gt;You can launch an EC2 instance from the EC2 console.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Launch EC2 Instance" class="gallery-image" data-flex-basis="398px" data-flex-grow="166" height="1183" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/ec2-create.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/ec2-create_hu_4bad2cd2999e9a49.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/ec2-create_hu_524d8415a1264fef.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/ec2-create.jpg 1965w" width="1965"&gt;&lt;/p&gt;
&lt;p&gt;Name your instance, e.g., &amp;ldquo;web-server&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;You can keep the default AMI and instance type.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;AMI: Amazon Linux 2023&lt;/li&gt;
&lt;li&gt;Instance type: micro&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="AMI and Instance Type" class="gallery-image" data-flex-basis="398px" data-flex-grow="166" height="1183" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-2.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-2_hu_4bad2cd2999e9a49.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-2_hu_524d8415a1264fef.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-2.jpg 1965w" width="1965"&gt;&lt;/p&gt;
&lt;p&gt;Under &amp;ldquo;Key pair&amp;rdquo;, select an existing key pair or create a new one for subsequent system administration.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Key Pair Settings" class="gallery-image" data-flex-basis="639px" data-flex-grow="266" height="651" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-3.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-3_hu_9f6ea6be1004f543.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-3_hu_cd1424e493dd60e4.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-3.jpg 1734w" width="1734"&gt;&lt;/p&gt;
&lt;p&gt;For network settings, ensure your EC2 instance is deployed in a public subnet, and check &amp;ldquo;Allow HTTP traffic from the internet&amp;rdquo; to make port 80 accessible from the internet.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Network Configuration" class="gallery-image" data-flex-basis="260px" data-flex-grow="108" height="1532" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-4.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-4_hu_6eb00b634c3b6715.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-4_hu_daaf7b7b2d61cf19.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-4.jpg 1663w" width="1663"&gt;&lt;/p&gt;
&lt;p&gt;Expand the &amp;ldquo;Advanced details&amp;rdquo; section at the bottom:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Advanced Details" class="gallery-image" data-flex-basis="592px" data-flex-grow="246" height="622" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-5.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-5_hu_b78cc41ef9237432.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-5.jpg 1536w" width="1536"&gt;&lt;/p&gt;
&lt;p&gt;At the bottom of the page under &amp;ldquo;User data&amp;rdquo;, paste the following script to install a web server on first boot. Then click &amp;ldquo;Launch instance&amp;rdquo; to start the instance.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#!/bin/bash
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;yum install -y httpd
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;systemctl &lt;span class="nb"&gt;enable&lt;/span&gt; httpd
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;systemctl start httpd
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="User Data" class="gallery-image" data-flex-basis="274px" data-flex-grow="114" height="1378" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-6.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-6_hu_7ced7558e6bc727.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-6.jpg 1576w" width="1576"&gt;&lt;/p&gt;
&lt;h4 id="2-create-elastic-ip-and-associate-with-ec2"&gt;2. Create Elastic IP and Associate with EC2
&lt;/h4&gt;&lt;p&gt;&lt;a class="link" href="https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html" target="_blank" rel="noopener"
 &gt;Elastic IP&lt;/a&gt; is a static IPv4 address designed for cloud computing. Using an Elastic IP ensures your EC2 instance&amp;rsquo;s public IP address and public DNS hostname remain unchanged. In this section, you will learn how to associate an Elastic IP with your EC2 instance and access your site using the EC2 instance&amp;rsquo;s public DNS hostname.&lt;/p&gt;
&lt;p&gt;Find Elastic IP under &amp;ldquo;Network &amp;amp; Security&amp;rdquo; in the EC2 console left menu.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Elastic IP Menu" class="gallery-image" data-flex-basis="1917px" data-flex-grow="799" height="201" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-7.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-7_hu_dcee5b048214ea12.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-7_hu_a09c30cd09c9bc23.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-7.jpg 1606w" width="1606"&gt;&lt;/p&gt;
&lt;p&gt;Click &amp;ldquo;Allocate Elastic IP address&amp;rdquo; in the upper right, then click &amp;ldquo;Allocate&amp;rdquo; to obtain an Elastic IP.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Allocate Elastic IP" class="gallery-image" data-flex-basis="542px" data-flex-grow="226" height="1058" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-8.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-8_hu_ef1e899f2d63465d.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-8_hu_77727674391f39a8.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-8.jpg 2392w" width="2392"&gt;&lt;/p&gt;
&lt;p&gt;Select your Elastic IP, click the &amp;ldquo;Actions&amp;rdquo; button in the upper right, and choose &amp;ldquo;Associate Elastic IP address&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Associate Elastic IP" class="gallery-image" data-flex-basis="423px" data-flex-grow="176" height="1375" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-9.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-9_hu_c73c28e244703e66.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-9_hu_db6574310cf99be4.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-9_hu_152a186740569800.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-9.jpg 2426w" width="2426"&gt;&lt;/p&gt;
&lt;p&gt;Select your previously created EC2 instance and click &amp;ldquo;Associate&amp;rdquo; to ensure the EC2 instance&amp;rsquo;s public IP address remains stable.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Select EC2 Instance" class="gallery-image" data-flex-basis="338px" data-flex-grow="141" height="1256" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-10.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-10_hu_bf400582c310b418.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-10_hu_d5b93b8eec98cf94.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-10.jpg 1772w" width="1772"&gt;&lt;/p&gt;
&lt;p&gt;Click the &amp;ldquo;Instances&amp;rdquo; menu on the left to return to the instance list, select your instance, and copy the public IPv4 DNS.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Copy Public DNS" class="gallery-image" data-flex-basis="856px" data-flex-grow="356" height="782" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-11.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-11_hu_e13e176090677f6.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-11_hu_2eacf8c682b2ccda.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-11_hu_1b2b525001061f85.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-11.jpg 2791w" width="2791"&gt;&lt;/p&gt;
&lt;p&gt;Access the public DNS hostname in your browser to verify the web server is installed. You can see the site is currently using port 80 without an SSL/TLS certificate.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Browser Verification" class="gallery-image" data-flex-basis="274px" data-flex-grow="114" height="1485" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-12.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-12_hu_4b723108790f6ed8.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-12_hu_da4cc718ccb5b425.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-12.jpg 1699w" width="1699"&gt;&lt;/p&gt;
&lt;p&gt;In the following sections, you will use this hostname as the CloudFront origin to divert website traffic from EC2 to Amazon CloudFront.&lt;/p&gt;
&lt;h3 id="create-cloudfront-distribution"&gt;Create CloudFront Distribution
&lt;/h3&gt;&lt;p&gt;In this section, you will create a CloudFront distribution and associate it with the previously deployed EC2 instance, routing traffic through CloudFront to accelerate content delivery.&lt;/p&gt;
&lt;h4 id="1-create-cloudfront-distribution-and-associate-ec2"&gt;1. Create CloudFront Distribution and Associate EC2
&lt;/h4&gt;&lt;p&gt;Next, you will quickly set up a CloudFront distribution. Go to the CloudFront console and create a distribution:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Create CloudFront Distribution" class="gallery-image" data-flex-basis="606px" data-flex-grow="252" height="1325" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-13.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-13_hu_a8367bef1aae0926.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-13_hu_4d508bb4a235ad8d.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-13_hu_5453637f6d5fbdeb.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-13.jpg 3351w" width="3351"&gt;&lt;/p&gt;
&lt;p&gt;On the CloudFront configuration page, you will see the basic configuration elements that a CloudFront distribution requires:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Origin&lt;/strong&gt; – The origin server, supporting multiple AWS services including but not limited to S3, EC2, ELB, API Gateway, as well as third-party origins&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache Behavior&lt;/strong&gt; – Define caching behavior through path matching, enable transfer compression, allowed request methods, access control, etc.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Settings&lt;/strong&gt; – Alternate domain names (CNAME), SSL certificate, default root object, logging, etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For the origin, use the EC2 public DNS (with Elastic IP) as the origin domain. Ensure the origin protocol is HTTP.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Origin Settings" class="gallery-image" data-flex-basis="414px" data-flex-grow="172" height="978" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-14.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-14_hu_c12930d6d67ea46.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-14_hu_8583eeaa9f5891e9.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-14.jpg 1690w" width="1690"&gt;&lt;/p&gt;
&lt;p&gt;For the default cache behavior, you can keep the default settings:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Cache Behavior Settings" class="gallery-image" data-flex-basis="711px" data-flex-grow="296" height="909" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-15.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-15_hu_12c7444888d92324.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-15_hu_65ebbbf0958e115e.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-15_hu_20aef0f435f57651.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-15.jpg 2694w" width="2694"&gt;&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Due to compliance requirements, the default CloudFront distribution domain in the Beijing/Ningxia regions cannot be accessed directly. You must complete domain ICP filing and set up CNAME DNS resolution before access is possible.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;p&gt;In the settings section, enter your ICP-filed domain as the alternate domain name (CNAME):&lt;/p&gt;
&lt;p&gt;&lt;img alt="Alternate Domain Name Settings" class="gallery-image" data-flex-basis="286px" data-flex-grow="119" height="1438" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-16.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-16_hu_14196fd7b272dfcd.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-16_hu_94de9764e2460cf6.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-16.jpg 1716w" width="1716"&gt;&lt;/p&gt;
&lt;p&gt;After creation, wait for deployment to complete. Once deployed, check your alternate domain name — if the domain has passed ICP filing, you should see a &amp;ldquo;green checkmark&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ICP Filing Green Checkmark" class="gallery-image" data-flex-basis="153px" data-flex-grow="63" height="1698" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-17.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-17_hu_acad7026d9b16373.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-17.jpg 1086w" width="1086"&gt;&lt;/p&gt;
&lt;p&gt;After deployment, go to Route 53 (or your DNS provider) to update the DNS configuration. Using Route 53 as an example, enter the corresponding domain&amp;rsquo;s hosted zone and create a new record.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Route 53 Create Record" class="gallery-image" data-flex-basis="405px" data-flex-grow="168" height="956" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-18.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-18_hu_970077f08855e81d.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-18_hu_995f1b9e1c60399.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-18.jpg 1615w" width="1615"&gt;&lt;/p&gt;
&lt;p&gt;Fill in the DNS record:&lt;/p&gt;
&lt;p&gt;&lt;img alt="DNS Record Configuration" class="gallery-image" data-flex-basis="693px" data-flex-grow="289" height="1085" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-19.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-19_hu_b37ebdfd46e25cb.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-19_hu_490015ade77f4a67.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-19_hu_d75cfeda12c8ce18.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-19.jpg 3136w" width="3136"&gt;&lt;/p&gt;
&lt;p&gt;After completing the DNS update, open your browser and access the ICP-filed domain:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Browser Access Domain" class="gallery-image" data-flex-basis="539px" data-flex-grow="224" height="1010" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-20.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-20_hu_36b565ec92948586.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-20_hu_58b3a2674e642329.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-20.jpg 2270w" width="2270"&gt;&lt;/p&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If you encounter a 502 error, check whether EC2 has an SSL certificate installed or whether the origin port is correct. See the &lt;a class="link" href="https://docs.amazonaws.cn/AmazonCloudFront/latest/DeveloperGuide/http-502-bad-gateway.html" target="_blank" rel="noopener"
 &gt;documentation&lt;/a&gt;. If you encounter a 403 error, ensure you are accessing via the ICP-filed domain.&lt;/p&gt;

 &lt;/blockquote&gt;
&lt;h4 id="2-enhance-ec2-and-website-security"&gt;2. Enhance EC2 and Website Security
&lt;/h4&gt;&lt;p&gt;In this section, you will use &lt;a class="link" href="https://docs.amazonaws.cn/vpc/latest/userguide/working-with-aws-managed-prefix-lists.html#available-aws-managed-prefix-lists" target="_blank" rel="noopener"
 &gt;AWS-managed prefix lists&lt;/a&gt; to enhance EC2 security. You can use the Amazon CloudFront managed prefix list to restrict inbound HTTP/HTTPS traffic to only the IP addresses of CloudFront origin-facing servers.&lt;/p&gt;
&lt;p&gt;Select your instance, go to the EC2 security group, and edit the inbound rules: add an HTTP rule, enter &amp;ldquo;cloudfront&amp;rdquo; in the source field, select the &lt;code&gt;com.amazonaws.global.cloudfront.origin-facing&lt;/code&gt; prefix list, and remove the existing &lt;code&gt;0.0.0.0/0&lt;/code&gt; rule.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Security Group Prefix List Configuration" class="gallery-image" data-flex-basis="382px" data-flex-grow="159" height="1065" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-21.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-21_hu_f948369aa9eb93d9.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-21_hu_aa508796cb61f82d.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-21.jpg 1696w" width="1696"&gt;&lt;/p&gt;
&lt;p&gt;You can also associate SSL/TLS certificates with CloudFront to enhance website security. In the following section, you will deploy free SSL certificates using the China CloudFront SSL Plugin.&lt;/p&gt;
&lt;h2 id="deploy-free-certificates-with-china-cloudfront-ssl-plugin"&gt;Deploy Free Certificates with China CloudFront SSL Plugin
&lt;/h2&gt;&lt;p&gt;Before starting, please confirm once again that you are using Amazon Route 53 for DNS resolution. This section covers the deployment process only. For more details, see the &lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/?nc1=h_ls" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin&lt;/a&gt;.&lt;/p&gt;
&lt;h3 id="1-initialize-deployment"&gt;1. Initialize Deployment
&lt;/h3&gt;&lt;p&gt;Deploy via CloudFormation. Click the &lt;a class="link" href="https://console.amazonaws.cn/cloudformation/home?#/stacks/create/template?templateURL=https://aws-cn-getting-started.s3.cn-northwest-1.amazonaws.com.cn/china-cloudfront-ssl-plugin/ChinaCloudFrontSslPluginStack.json" target="_blank" rel="noopener"
 &gt;link&lt;/a&gt; to launch the stack in the CloudFormation console.&lt;/p&gt;
&lt;p&gt;&lt;img alt="CloudFormation Deployment" class="gallery-image" data-flex-basis="381px" data-flex-grow="158" height="898" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-22.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-22_hu_c48fc7de493a412d.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-22.jpg 1426w" width="1426"&gt;&lt;/p&gt;
&lt;h3 id="2-enter-deployment-parameters"&gt;2. Enter Deployment Parameters
&lt;/h3&gt;&lt;p&gt;Specify stack details with the following parameters:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Stack Name:&lt;/strong&gt; Name your stack&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Email:&lt;/strong&gt; Enter your email for SNS email notifications (only one email supported)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Domain Name:&lt;/strong&gt; Enter the domain names for the SSL certificate. Supports wildcard domains and multiple domains (comma-separated)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SSL Renew Interval Days:&lt;/strong&gt; Enter the certificate renewal interval. Let&amp;rsquo;s Encrypt certificates are valid for 90 days; enter a number between 1 and 89. Recommended default: 80 days.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Stack Parameters" class="gallery-image" data-flex-basis="547px" data-flex-grow="228" height="1408" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-23.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-23_hu_df0d35d2e1320a42.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-23_hu_fb9028b1c9581b35.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-23_hu_98d013332fd98823.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-23.jpg 3211w" width="3211"&gt;&lt;/p&gt;
&lt;h3 id="3-confirm-deployment"&gt;3. Confirm Deployment
&lt;/h3&gt;&lt;p&gt;Review the deployment details on the current page, check the &amp;ldquo;I acknowledge&amp;rdquo; box at the bottom, and click &lt;strong&gt;Submit&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Confirm Deployment" class="gallery-image" data-flex-basis="540px" data-flex-grow="225" height="1500" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-24.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-24_hu_97040fb6988a5f8.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-24_hu_3530ff7150daad0c.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-24_hu_8c5bc23ad109306f.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-24.jpg 3378w" width="3378"&gt;&lt;/p&gt;
&lt;p&gt;After submission, you will see resources being created in the stack events tab. Wait approximately 3 minutes.&lt;/p&gt;
&lt;h3 id="4-subscribe-to-sns-notifications-promptly"&gt;4. Subscribe to SNS Notifications Promptly
&lt;/h3&gt;&lt;p&gt;During the stack deployment process, check your email promptly. You will receive an SNS subscription confirmation request from &lt;a class="link" href="mailto:no-reply@sns.amazonaws.com" &gt;no-reply@sns.amazonaws.com&lt;/a&gt;. Please click the confirmation link as soon as possible.&lt;/p&gt;
&lt;p&gt;&lt;img alt="SNS Subscription Email" class="gallery-image" data-flex-basis="753px" data-flex-grow="313" height="1579" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-25.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-25_hu_cdde9165045830e8.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-25_hu_bb88619825b58bb2.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-25_hu_f203472c7d3c1c50.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-25.jpg 4956w" width="4956"&gt;&lt;/p&gt;
&lt;p&gt;Successful subscription confirmation:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Subscription Successful" class="gallery-image" data-flex-basis="1331px" data-flex-grow="554" height="566" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-26.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-26_hu_745d769f4f147290.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-26_hu_af2c5877f6070985.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-26_hu_2b7bfecfa67898b.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-26.jpg 3140w" width="3140"&gt;&lt;/p&gt;
&lt;h3 id="5-check-stack-deployment-progress"&gt;5. Check Stack Deployment Progress
&lt;/h3&gt;&lt;p&gt;When the stack status changes to green &lt;code&gt;CREATE_COMPLETE&lt;/code&gt;, the deployment is finished.&lt;/p&gt;
&lt;p&gt;Check the &amp;ldquo;Outputs&amp;rdquo; tab for quick links:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CloudfrontConsole:&lt;/strong&gt; Access the CloudFront console to quickly bind the issued certificate&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ManagementWebURL:&lt;/strong&gt; Access SwaggerUI to view or delete existing SSL certificates in IAM&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;S3BucketURL:&lt;/strong&gt; Access the S3 console to download the issued SSL certificates&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Stack Outputs" class="gallery-image" data-flex-basis="1089px" data-flex-grow="453" height="526" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-27.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-27_hu_e4a40fc578f3a1ff.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-27_hu_d7c6996c56f5d4b4.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-27.jpg 2387w" width="2387"&gt;&lt;/p&gt;
&lt;h3 id="6-bind-ssl-certificate-in-the-amazon-cloudfront-console"&gt;6. Bind SSL Certificate in the Amazon CloudFront Console
&lt;/h3&gt;&lt;p&gt;After stack deployment is complete, an SSL certificate will be automatically requested for your domain. If you have subscribed to the SNS topic, you will receive an email notification confirming the certificate has been issued. The certificate name consists of the stack name and expiry time, e.g., &lt;code&gt;Certbot-2023-11-14-1540&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img alt="SSL Certificate Email Notification" class="gallery-image" data-flex-basis="532px" data-flex-grow="221" height="542" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-28.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-28_hu_eea6111e60dfb38a.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-28.jpg 1202w" width="1202"&gt;&lt;/p&gt;
&lt;p&gt;Open the CloudFront console, select your distribution, and find the option to edit alternate domain names and SSL certificate.&lt;/p&gt;
&lt;p&gt;&lt;img alt="CloudFront Edit Settings" class="gallery-image" data-flex-basis="551px" data-flex-grow="229" height="1403" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-29.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-29_hu_9832edb84636551.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-29_hu_e1912aa46e2c65d8.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-29_hu_ab611836b6a34937.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-29.jpg 3222w" width="3222"&gt;&lt;/p&gt;
&lt;p&gt;Select the corresponding SSL certificate from the custom SSL certificate dropdown, then save changes.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Select SSL Certificate" class="gallery-image" data-flex-basis="444px" data-flex-grow="185" height="1743" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-30.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-30_hu_5dae4bd224493381.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-30_hu_26ea4d3b5146beb3.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-30_hu_4517dfc367337036.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-30.jpg 3226w" width="3226"&gt;&lt;/p&gt;
&lt;p&gt;After deployment, you can access your CloudFront-accelerated site via HTTPS in the browser and verify the SSL certificate issued by Let&amp;rsquo;s Encrypt, valid for 90 days.&lt;/p&gt;
&lt;p&gt;&lt;img alt="HTTPS Access Verification" class="gallery-image" data-flex-basis="437px" data-flex-grow="182" height="1492" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-31.jpg" srcset="https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-31_hu_1eee86b177ad311b.jpg 800w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-31_hu_3c5134eb5ca760eb.jpg 1600w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-31_hu_532535a6568d9bb.jpg 2400w, https://robinsujob.github.io/p/cloudfront-ssl-plugin-deployment-guide/images/cloudfront-ssl-31.jpg 2720w" width="2720"&gt;&lt;/p&gt;
&lt;p&gt;For more information on using the China CloudFront SSL Plugin, see the &lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/?nc1=h_ls" target="_blank" rel="noopener"
 &gt;plugin documentation&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="summary"&gt;Summary
&lt;/h2&gt;&lt;p&gt;Through this article, you have learned the basics of Amazon CloudFront and how to divert website traffic deployed on EC2 to CloudFront for acceleration and cost savings. You have also learned to use AWS-managed prefix lists in EC2 security groups to ensure your EC2 only accepts traffic originating from CloudFront, and to deploy free SSL certificates using the China CloudFront SSL Plugin to protect your site with HTTPS.&lt;/p&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://www.amazonaws.cn/getting-started/tutorials/create-ssl-with-cloudfront/?nc1=h_ls" target="_blank" rel="noopener"
 &gt;China CloudFront SSL Plugin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.amazonaws.cn/AmazonCloudFront/latest/DeveloperGuide/Introduction.html" target="_blank" rel="noopener"
 &gt;Amazon CloudFront Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://docs.amazonaws.cn/aws/latest/userguide/cloudfront.html#feature-diff" target="_blank" rel="noopener"
 &gt;Amazon CloudFront China Region Differences&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://www.amazonaws.cn/getting-started/use-cases/cloudfront-network-acceleration/?nc1=h_ls" target="_blank" rel="noopener"
 &gt;Network Acceleration with Amazon CloudFront in China&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;&lt;strong&gt;Original Post:&lt;/strong&gt; &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/divert-website-access-traffic-from-ec2-to-amazon-cloudfront/" target="_blank" rel="noopener"
 &gt;Amazon CloudFront Deployment Mini-Guide (Part 8) - Deploy Free Certificates with China CloudFront and CloudFront SSL Plugin&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a class="link" href="https://github.com/aws-samples/China-CloudFront-SSL-Plugin" target="_blank" rel="noopener"
 &gt;aws-samples/China-CloudFront-SSL-Plugin&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item><item><title>Art QR Code with ControlNet and Stable Diffusion</title><link>https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/</link><pubDate>Fri, 01 Sep 2023 00:00:00 +0800</pubDate><guid>https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/</guid><description>&lt;img src="https://robinsujob.github.io/" alt="Featured image of post Art QR Code with ControlNet and Stable Diffusion" /&gt;&lt;h2 id="background"&gt;Background
&lt;/h2&gt;&lt;p&gt;Over the past few months, there has been growing interest in combining Stable Diffusion with ControlNet to create artistic QR codes. This post was originally developed as part of an AWS workshop demonstrating how to deploy and use Stable Diffusion on AWS infrastructure.&lt;/p&gt;
&lt;p&gt;This post shares practical tips for working with Stable Diffusion and walks through the best practices for generating artistic QR codes.&lt;/p&gt;
&lt;p&gt;We will use QR codes as ControlNet input, integrating QR code data points into artistic images while keeping them scannable by QR code readers. With this technology, you can transform any QR code into a unique piece of art, expressing and conveying information in an entirely new way. Here are some examples:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Sample 1" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="2000" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1_hu_55ce0a96d66b54da.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1_hu_76a851db4e696861.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample1.jpg 2000w" width="2000"&gt;
&lt;img alt="Sample 2" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="2000" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2_hu_58726bd2cf5fe70d.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2_hu_4c6be33718f82540.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample2.jpg 2000w" width="2000"&gt;
&lt;img alt="Sample 3" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="2000" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3_hu_aa92c1059d5785be.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3_hu_52c82c010083f7a4.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/sample3.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h2 id="stable-diffusion-practical-tips"&gt;Stable Diffusion Practical Tips
&lt;/h2&gt;&lt;p&gt;As the ancient Chinese saying goes: &amp;ldquo;The beginning is always the hardest&amp;rdquo; and &amp;ldquo;Seek breadth while perfecting the details.&amp;rdquo; These correspond to the two most common challenges when working with Stable Diffusion: first, how to choose the right prompt strategy to generate images that meet expectations; and second, how to fine-tune image details so the final output meets production-quality requirements.&lt;/p&gt;
&lt;p&gt;Based on our experience helping users work with Stable Diffusion, we have compiled the following best practices for reference.&lt;/p&gt;
&lt;h3 id="prompt-engineering"&gt;Prompt Engineering
&lt;/h3&gt;&lt;p&gt;As Stable Diffusion versions continue to evolve and AI&amp;rsquo;s understanding of semantics gets closer to &amp;ldquo;common sense,&amp;rdquo; the requirements for effective prompts are becoming increasingly demanding. Many common prompt misconceptions can sometimes have adverse effects on image generation.&lt;/p&gt;
&lt;h4 id="basic-concepts"&gt;Basic Concepts
&lt;/h4&gt;&lt;p&gt;Prompts are divided into &lt;strong&gt;positive prompts&lt;/strong&gt; (what you want) and &lt;strong&gt;negative prompts&lt;/strong&gt; (what you don&amp;rsquo;t want), telling the AI what to include and what to exclude.&lt;/p&gt;
&lt;h4 id="common-misconceptions"&gt;Common Misconceptions
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Precision over quantity — describing a scene with the shortest words is more effective than natural language.&lt;/li&gt;
&lt;li&gt;Quality-improving descriptors should not be mindlessly stacked; more is not better.&lt;/li&gt;
&lt;li&gt;Common starters like &amp;ldquo;masterpiece&amp;rdquo; and &amp;ldquo;best quality&amp;rdquo; often become dead weight in prompts. These words were meaningful in the NovelAI era, when they were heavily used during model training for image evaluation. However, after continuous model refinement by authors on Civitai, these prompts barely show their intended effect in modern models.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="adjusting-prompt-weights"&gt;Adjusting Prompt Weights
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;The default weight of each token is 1, decreasing from left to right.&lt;/li&gt;
&lt;li&gt;Prompt weights significantly affect the generated image.&lt;/li&gt;
&lt;li&gt;Use parentheses + colon + number to specify weight, e.g., &lt;code&gt;(one girl:1.5)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="mind-the-prompt-order"&gt;Mind the Prompt Order
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Landscape tags first = smaller characters; character tags first = larger characters or half-body shots.&lt;/li&gt;
&lt;li&gt;Choose the correct order and syntax for prompts to more effectively render the desired scene.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="emojis-in-prompts"&gt;Emojis in Prompts
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Prompts support emojis, and they work with good expressiveness.&lt;/li&gt;
&lt;li&gt;For specific facial expressions or actions, adding emojis can achieve the desired effect.&lt;/li&gt;
&lt;li&gt;To prevent semantic drift, prefer emojis and minimize unnecessary complex syntax.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="recommended-camera-angle-prompts"&gt;Recommended Camera Angle Prompts
&lt;/h4&gt;&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th&gt;Parameter&lt;/th&gt;
 &lt;th&gt;Description&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td&gt;extreme closeup&lt;/td&gt;
 &lt;td&gt;Face close-up&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;close up&lt;/td&gt;
 &lt;td&gt;Head&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;medium close up&lt;/td&gt;
 &lt;td&gt;ID photo&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;medium shot&lt;/td&gt;
 &lt;td&gt;Half body&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;cowboy shot&lt;/td&gt;
 &lt;td&gt;No legs&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;medium full shot&lt;/td&gt;
 &lt;td&gt;No feet&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td&gt;full shot&lt;/td&gt;
 &lt;td&gt;Full body&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="image-optimization"&gt;Image Optimization
&lt;/h3&gt;&lt;p&gt;Often we generate an image that&amp;rsquo;s not quite satisfactory and want to further optimize it, but don&amp;rsquo;t know where to start. Here are some best practices for parameter tuning:&lt;/p&gt;
&lt;h4 id="which-parameters-to-adjust"&gt;Which Parameters to Adjust
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CFG Scale&lt;/strong&gt;: The correlation between the image and the prompt. Higher values mean greater prompt influence and closer adherence to the prompt.
&lt;ul&gt;
&lt;li&gt;CFG 2–6: Creative but may be too distorted, not following the prompt. Fun for short prompts.&lt;/li&gt;
&lt;li&gt;CFG 7–10: Recommended for most prompts.&lt;/li&gt;
&lt;li&gt;CFG 10–15: Use when the prompt is detailed and clear.&lt;/li&gt;
&lt;li&gt;CFG 16–20: Not recommended unless the prompt is very detailed.&lt;/li&gt;
&lt;li&gt;CFG &amp;gt;20: Almost unusable.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling Steps&lt;/strong&gt;: More steps mean smaller, more precise adjustments per step, but proportionally increase generation time. For most samplers, more iterations produce better results, but beyond 50 steps, improvements become negligible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling Method&lt;/strong&gt;: Different methods have different optimal step counts; comprehensive comparison is needed.
&lt;ul&gt;
&lt;li&gt;Euler a: Creative; different step counts produce different images. An efficient method for quickly testing prompt effects.&lt;/li&gt;
&lt;li&gt;DPM2 a Karras: Good for realistic models, hard to control after 30 steps.&lt;/li&gt;
&lt;li&gt;DPM++ 2M Karras: Excellent at high step counts, more details.&lt;/li&gt;
&lt;li&gt;DDIM: Fast convergence but low efficiency; good for inpainting.&lt;/li&gt;
&lt;li&gt;Different model and sampler combinations produce different results; the above is for reference only. When choosing sampling methods, it&amp;rsquo;s best to use X/Y/Z plots for comparison.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seed&lt;/strong&gt;: The random seed often has a huge impact on composition and is the main source of randomness in SD. With the same seed and all other parameters fixed, you can reproduce (nearly) identical images. Once you find a suitable composition, fix the seed and fine-tune details — this is the recommended approach.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="how-to-find-optimal-parameters"&gt;How to Find Optimal Parameters
&lt;/h4&gt;&lt;p&gt;Use X/Y/Z plots to clearly compare results under different parameters, quickly locate suitable parameter ranges, and perform further generation control.&lt;/p&gt;
&lt;p&gt;&lt;img alt="X/Y/Z Plot Comparison" class="gallery-image" data-flex-basis="353px" data-flex-grow="147" height="1358" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4_hu_77502e0855ea324.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4_hu_f3805d232f87f8e3.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image4.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h4 id="image-size-optimization"&gt;Image Size Optimization
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Image quality is not directly tied to image size.&lt;/li&gt;
&lt;li&gt;However, size does affect the subject and content to some extent.&lt;/li&gt;
&lt;li&gt;When the output size is too wide, multiple subjects may appear.&lt;/li&gt;
&lt;li&gt;Sizes above 1024 may produce undesirable results, and GPU memory pressure becomes significant. Small resolution + upscaling is recommended.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="optimizing-multi-character-and-wide-format-generation"&gt;Optimizing Multi-Character and Wide-Format Generation
&lt;/h4&gt;&lt;ul&gt;
&lt;li&gt;Using txt2img alone cannot effectively specify individual character features in multi-character scenarios.&lt;/li&gt;
&lt;li&gt;The recommended approach is to create a draft + img2img or ControlNet.&lt;/li&gt;
&lt;li&gt;For wide-format single-character generation, draft a sketch with color blocking to determine the main subject, or use ControlNet&amp;rsquo;s OpenPose for character skeleton.&lt;/li&gt;
&lt;li&gt;For multi-character scenes, use ControlNet&amp;rsquo;s OpenPose to specify character count; this also works for three-view drawings of the same character.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="hand-repair"&gt;Hand Repair
&lt;/h4&gt;&lt;p&gt;Send the image to img2img inpaint with similar prompts, placing &amp;ldquo;hand&amp;rdquo; prompts at the front. Set denoising strength based on how much you want hand features to change (below 0.25 if you just want completeness), keeping steps and CFG the same as txt2img. For more precise hand control, find a satisfactory hand reference image and use ControlNet&amp;rsquo;s Canny or OpenPose_hands preprocessor + model combined with inpaint.&lt;/p&gt;
&lt;h4 id="face-repair"&gt;Face Repair
&lt;/h4&gt;&lt;p&gt;When drawing images with small character subjects, facial distortion frequently occurs. Especially in the artistic QR code generation process described later, faces often break due to QR code data points. For facial inpainting, the &lt;strong&gt;ADetailer&lt;/strong&gt; plugin is recommended. It uses the YOLO algorithm for object detection — we configure it to detect character faces and provide prompts and models for facial inpainting. The plugin performs localized inpainting at detected face locations to complete the repair. ADetailer can handle both face and hand detection and repair, and can also reference LoRA models for localized inpainting.&lt;/p&gt;
&lt;p&gt;&lt;img alt="ADetailer Face Repair" class="gallery-image" data-flex-basis="445px" data-flex-grow="185" height="1078" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5_hu_6367fc31bcade539.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5_hu_5676d0dce49e6a81.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image5.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h2 id="generating-artistic-qr-codes-with-controlnet"&gt;Generating Artistic QR Codes with ControlNet
&lt;/h2&gt;&lt;h3 id="step-1-optimize-the-qr-code"&gt;Step 1: Optimize the QR Code
&lt;/h3&gt;&lt;p&gt;A QR code is a pattern of black and white geometric shapes distributed in 2D space that records symbolic data. There are multiple encoding methods; we use the most universal and fundamental one: QR Code.&lt;/p&gt;
&lt;p&gt;The input QR code is one of the most important factors when generating artistic QR codes with SD. We mainly care about two characteristics:&lt;/p&gt;
&lt;p&gt;&lt;img alt="QR Code Structure" class="gallery-image" data-flex-basis="463px" data-flex-grow="193" height="1035" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6_hu_b52fdd4bc2347dd6.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6_hu_4f11b8a8789a497c.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image6.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1. Information contained in the QR code&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Regardless of the encoding method, the more character information a QR code carries, the more complex its visual black-and-white structure becomes. Complex structures heavily constrain artistic creativity during generation. Therefore, we first need to simplify the content length.&lt;/p&gt;
&lt;p&gt;For the most common use case — web links — shorten the URL first. There are many URL shortening tools available; choose freely. Note that within mainland China, use platforms with registered domain names, otherwise links may be blocked by WeChat, browsers, etc.&lt;/p&gt;
&lt;p&gt;&lt;img alt="URL Length Comparison" class="gallery-image" data-flex-basis="434px" data-flex-grow="180" height="1105" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7_hu_994cc7c8be691b7a.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7_hu_5ecf3e0fd8a69b85.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image7.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;2. How the QR code is presented&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With technological development, QR codes no longer only support black and white square patterns; both positioning points and modules support diverse presentation styles.&lt;/p&gt;
&lt;p&gt;In practice, try different module styles to achieve the desired generation effect. The following images show how different QR code styles affect the final result:&lt;/p&gt;
&lt;p&gt;&lt;img alt="Different Module Styles" class="gallery-image" data-flex-basis="665px" data-flex-grow="277" height="721" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8_hu_f5428857b68a719e.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8_hu_e03ceb67ae15f93d.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image8.jpg 2000w" width="2000"&gt;
&lt;img alt="Different QR Code Style Comparison" class="gallery-image" data-flex-basis="474px" data-flex-grow="197" height="1012" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9_hu_19fbc491720a35dd.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9_hu_299366d2f08403a0.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image9.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;Generation parameters:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;span class="lnt"&gt;6
&lt;/span&gt;&lt;span class="lnt"&gt;7
&lt;/span&gt;&lt;span class="lnt"&gt;8
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Prompt: mountain, green grassland, sky, cloud, bird, blue sky, no human, day, wide shot, flying, border, outdoors, white bird, scenery
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Negative prompt: easynegative
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Steps: 40, Sampler: DPM++ 2M Karras, CFG scale: 6, Seed: 3943213078, Size: 872x872,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Model hash: 876b4c7ba5, Model: cetusMix_Whalefall2, Clip skip: 2,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;ControlNet: &amp;#34;preprocessor: none, model: control_v1p_sd15_qrcode_monster [a6e58995],
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;weight: 1.35-1.5, starting/ending: (0.05, 1), resize mode: Resize and Fill,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;pixel perfect: True, control mode: Balanced, preprocessor params: (512, 64, 64)&amp;#34;,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Version: v1.3.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="step-2-create-the-base-qr-code"&gt;Step 2: Create the Base QR Code
&lt;/h3&gt;&lt;p&gt;After understanding the key points above, use a QR code generation tool to create a base QR code for SD input. There are many web-based generators available; choose freely. For convenience, here is a useful WebUI plugin:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Anthony&amp;rsquo;s QR Toolkit&lt;/strong&gt;: Integrated QR code generation and optimization tool for WebUI
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://github.com/antfu/sd-webui-qrcode-toolkit" target="_blank" rel="noopener"
 &gt;https://github.com/antfu/sd-webui-qrcode-toolkit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="QR Toolkit Configuration" class="gallery-image" data-flex-basis="352px" data-flex-grow="146" height="1363" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10_hu_7dd3f5c4533a8b1f.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10_hu_a9e7cdd31e40fc49.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image10.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;After creating the QR code, click &amp;ldquo;Download&amp;rdquo; to save it locally, or click &amp;ldquo;Send to ControlNet&amp;rdquo; to pass it directly for the next step.&lt;/p&gt;
&lt;h3 id="step-3-determine-the-art-style"&gt;Step 3: Determine the Art Style
&lt;/h3&gt;&lt;p&gt;The core of creating art with Stable Diffusion is choosing the right model + prompts. Before creating artistic QR codes, we recommend generating an image without ControlNet first to test the style.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Test Generation Result" class="gallery-image" data-flex-basis="506px" data-flex-grow="210" height="948" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11_hu_67bbdab4025fc3f5.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11_hu_c83f3b551e0924d7.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image11.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;Generation parameters:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;span class="lnt"&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Prompt: mountain, green grassland, sky, cloud, bird, blue sky, no human, day, wide shot, flying, border, outdoors, white bird, scenery
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Negative prompt: easynegative
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 4078355702,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Face restoration: CodeFormer, Size: 512x512,
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Model hash: 876b4c7ba5, Model: cetusMix_Whalefall2, Clip skip: 2, Version: v1.3.2
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="step-4-import-qr-code-into-controlnet"&gt;Step 4: Import QR Code into ControlNet
&lt;/h3&gt;&lt;p&gt;After confirming the image style, upload the unprocessed QR code to ControlNet. Pay attention to these configuration options:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&amp;ldquo;Enable&amp;rdquo; button&lt;/strong&gt;: Check to ensure ControlNet is active during generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Model selector&lt;/strong&gt;: Select &lt;code&gt;control_v1p_sd15_qrcode_monster&lt;/code&gt; to strengthen QR code control&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control weight&lt;/strong&gt;: For the qrcode_monster model, we recommend setting it between 1.1–1.6&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Guidance start/end timing&lt;/strong&gt;: Start timing recommended between 0–0.1, end timing recommended at 1&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="ControlNet Configuration" class="gallery-image" data-flex-basis="240px" data-flex-grow="100" height="1997" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12_hu_25f078fb5a3943d7.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12_hu_f4879ef6b64e3a2e.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image12.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;In the txt2img configuration, adjust two key values:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sampling steps&lt;/strong&gt;: Recommended 30–50 (the default of 20 is insufficient for generating high-quality QR code images)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Width/Height&lt;/strong&gt;: Match the original QR code aspect ratio from ControlNet&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="txt2img Parameter Adjustment" class="gallery-image" data-flex-basis="251px" data-flex-grow="104" height="1909" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13_hu_ed705bdb20a2cff1.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13_hu_e4b0ba433aa581d9.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image13.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;After all parameters are configured, click generate. Here we can see a good result — the QR code passes scanning tests on mobile phones.&lt;/p&gt;
&lt;p&gt;If the generated QR code doesn&amp;rsquo;t meet expectations, try fine-tuning the following parameters and increase the total batch count, iterating to approach the desired result:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Prompts&lt;/li&gt;
&lt;li&gt;Sampling method&lt;/li&gt;
&lt;li&gt;ControlNet control weight&lt;/li&gt;
&lt;li&gt;ControlNet guidance start/end timing&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;img alt="Generation Result" class="gallery-image" data-flex-basis="259px" data-flex-grow="108" height="1849" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14_hu_748dcd2b923a108f.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14_hu_2ed397a762cf9367.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image14.jpg 2000w" width="2000"&gt;
&lt;img alt="Parameter Fine-tuning" class="gallery-image" data-flex-basis="418px" data-flex-grow="174" height="1146" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15_hu_ad317a8ce958a578.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15_hu_e79e5ee60f7554ba.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image15.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;p&gt;When necessary, use the X/Y/Z Plot in &amp;ldquo;Scripts&amp;rdquo; to compare QR code generation effects under different parameters. Here we compared ControlNet&amp;rsquo;s control weight and guidance start timing:&lt;/p&gt;
&lt;p&gt;&lt;img alt="X/Y/Z Plot Comparison" class="gallery-image" data-flex-basis="740px" data-flex-grow="308" height="648" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16.jpg" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16_hu_30abd11948c76d54.jpg 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16_hu_ee7d7898db9411be.jpg 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/image16.jpg 2000w" width="2000"&gt;&lt;/p&gt;
&lt;h2 id="enhancing-qr-codes-with-openpose"&gt;Enhancing QR Codes with OpenPose
&lt;/h2&gt;&lt;p&gt;Building on the QR code techniques above, we can take it further by combining &lt;strong&gt;OpenPose&lt;/strong&gt; with QR codes to create even more dynamic and artistic results. OpenPose generates skeleton maps of human poses, which serve as an additional ControlNet input to control character poses in the generated image.&lt;/p&gt;
&lt;h3 id="example-showcase"&gt;Example Showcase
&lt;/h3&gt;&lt;p&gt;Here are some example results combining OpenPose pose control with QR code generation:&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: center"&gt;&lt;/th&gt;
 &lt;th style="text-align: center"&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: center"&gt;&lt;img alt="OpenPose + QR Code" class="gallery-image" data-flex-basis="400px" data-flex-grow="166" height="540" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/1-open-pose-code.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/1-open-pose-code_hu_1827644c6b490e79.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/1-open-pose-code.png 900w" width="900"&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;&lt;img alt="Girl with Car" class="gallery-image" data-flex-basis="401px" data-flex-grow="167" height="536" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/2-girl-with-car.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/2-girl-with-car_hu_d779f7fe9c1958a9.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/2-girl-with-car.png 896w" width="896"&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;The prompt used for the example above:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(school girl:1.6, walking), (a car, side view, to left), (residential area, cherry trees)
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Negative prompt: (worst quality:1.6),(low quality:1.6), (inaccurate limb:1.2),bad composition, inaccurate eyes, easynegative
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;Steps: 50, Sampler: Euler a, CFG scale: 7, Seed: 3408317133, Size: 900x540, Model: cetusMix_Whalefall2
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h3 id="environment-setup-enable-multiple-controlnet-units"&gt;Environment Setup: Enable Multiple ControlNet Units
&lt;/h3&gt;&lt;p&gt;To use OpenPose and QR Code ControlNet simultaneously, you need at least &lt;strong&gt;2 ControlNet Units&lt;/strong&gt; enabled:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;Settings&lt;/strong&gt; → &lt;strong&gt;ControlNet&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Set the &lt;strong&gt;Multi ControlNet&lt;/strong&gt; unit count to &lt;strong&gt;2&lt;/strong&gt; (or more)&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Apply Settings&lt;/strong&gt;, then &lt;strong&gt;Reload UI&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Settings" class="gallery-image" data-flex-basis="753px" data-flex-grow="313" height="801" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting_hu_b5e6a63896ebda6.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting_hu_8b6d41a6102ea411.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting_hu_ecc7edb38c7f398b.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/3-setting.png 2514w" width="2514"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Set Multi ControlNet Units" class="gallery-image" data-flex-basis="910px" data-flex-grow="379" height="855" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit_hu_b5d1b2f4387cd450.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit_hu_26545b7bcc39a476.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit_hu_1bdf0964a8df41e6.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/4-set-multi-unit.png 3245w" width="3245"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Reload UI" class="gallery-image" data-flex-basis="534px" data-flex-grow="222" height="1486" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui_hu_3b3ab1cb73bf46f3.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui_hu_e849a8fb6bba1f41.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui_hu_f3f3f518f7878675.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/5-reload-ui.png 3310w" width="3310"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Verify Result" class="gallery-image" data-flex-basis="376px" data-flex-grow="156" height="1030" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result_hu_1ab84bcffc2484d0.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result_hu_85b1c58013b0518d.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/6-check-result.png 1616w" width="1616"&gt;&lt;/p&gt;
&lt;h3 id="import-skeleton-data-and-enable-openpose"&gt;Import Skeleton Data and Enable OpenPose
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;Open the &lt;strong&gt;OpenPose Editor&lt;/strong&gt; extension&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Load JSON&lt;/strong&gt; to import your prepared skeleton pose data&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Send to txt2img&lt;/strong&gt; to transfer the pose to ControlNet&lt;/li&gt;
&lt;li&gt;In ControlNet &lt;strong&gt;Unit 0&lt;/strong&gt;, enable ControlNet and select the model &lt;strong&gt;control_v11p_sd15_openpose&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Import Pose Data" class="gallery-image" data-flex-basis="547px" data-flex-grow="227" height="1448" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose_hu_b2a0db80c07e8fdd.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose_hu_536c91b4faef4e8e.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose_hu_e0f0e9ff4141a491.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/7-import-pose.png 3301w" width="3301"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Enable OpenPose ControlNet" class="gallery-image" data-flex-basis="220px" data-flex-grow="91" height="1735" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/8-enable-openpose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/8-enable-openpose_hu_f0fb744ed7c07583.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/8-enable-openpose.png 1595w" width="1595"&gt;&lt;/p&gt;
&lt;h3 id="import-qr-code-and-configure-generation"&gt;Import QR Code and Configure Generation
&lt;/h3&gt;&lt;ol&gt;
&lt;li&gt;Switch to &lt;strong&gt;ControlNet Unit 1&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Upload your QR code image&lt;/li&gt;
&lt;li&gt;Select the model &lt;strong&gt;control_v1p_sd15_qrcode_monster&lt;/strong&gt; with a &lt;strong&gt;Control Weight of 1.7&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Set &lt;strong&gt;Sampling Steps&lt;/strong&gt; to 30–50 for better quality&lt;/li&gt;
&lt;li&gt;Adjust &lt;strong&gt;Width/Height&lt;/strong&gt; to match your desired output resolution&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img alt="Enable QR Code Monster" class="gallery-image" data-flex-basis="233px" data-flex-grow="97" height="1716" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster_hu_48432b44047ff4a8.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster_hu_5ed8c8f73f0276dc.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/9-enable-qr-monster.png 1668w" width="1668"&gt;&lt;/p&gt;
&lt;p&gt;&lt;img alt="Set Width and Height" class="gallery-image" data-flex-basis="228px" data-flex-grow="95" height="1737" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw_hu_e0393aac628b989a.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw_hu_f56c4be88d52317e.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/10-set-hw.png 1656w" width="1656"&gt;&lt;/p&gt;
&lt;h3 id="prompts-and-generation"&gt;Prompts and Generation
&lt;/h3&gt;&lt;p&gt;Set your prompts for the final generation:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Positive prompt:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;(1girl:1.6, side lying sleep, on the garden, sunflowers), wooden floor
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;strong&gt;Negative prompt:&lt;/strong&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;extra hands, extra fingers, extra legs, fewer fingers, (low quality, worst quality:1.4), (bad anatomy), (inaccurate limb:1.2),bad composition, inaccurate eyes, extra digit,fewer digits,(extra arms:1.2), signature, easynegative
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;&lt;img alt="Set Prompts" class="gallery-image" data-flex-basis="1108px" data-flex-grow="461" height="719" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt_hu_140869ed3fe0cac5.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt_hu_811ca3a0bd1bbe33.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt_hu_c793fa54f7ad3c77.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/11-set-prompt.png 3320w" width="3320"&gt;&lt;/p&gt;
&lt;h3 id="results"&gt;Results
&lt;/h3&gt;&lt;p&gt;Here is the final generated result combining OpenPose and QR code:&lt;/p&gt;
&lt;p&gt;&lt;img alt="OpenPose + QR Code Result" class="gallery-image" data-flex-basis="453px" data-flex-grow="189" height="1739" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode_hu_1dda73f82d03b7f1.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode_hu_559208d515c67a7d.png 1600w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode_hu_a9a88a4430cb2aa3.png 2400w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/12-openpose-qrcode.png 3288w" width="3288"&gt;&lt;/p&gt;
&lt;p&gt;By cleverly offsetting the QR code position and applying composition techniques, the viewer&amp;rsquo;s focus is effectively shifted from the QR code to the character itself, creating a more natural and harmonious overall image.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: center"&gt;&lt;img class="gallery-image" data-flex-basis="355px" data-flex-grow="148" height="675" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/13-lying-sleep-openpose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/13-lying-sleep-openpose_hu_aea52875040ebca6.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/13-lying-sleep-openpose.png 1000w" width="1000"&gt;&lt;/th&gt;
 &lt;th style="text-align: center"&gt;&lt;img class="gallery-image" data-flex-basis="357px" data-flex-grow="148" height="672" loading="lazy" sizes="(max-width: 767px) calc(100vw - 30px), (max-width: 1023px) 700px, (max-width: 1279px) 950px, 1232px" src="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/14-openpose.png" srcset="https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/14-openpose_hu_3e8ab8bf2aeed12d.png 800w, https://robinsujob.github.io/p/art-qr-code-controlnet-stable-diffusion/images/14-openpose.png 1000w" width="1000"&gt;&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="fine-tuning-tips"&gt;Fine-Tuning Tips
&lt;/h3&gt;&lt;p&gt;To achieve the best results when combining OpenPose with QR codes, consider adjusting:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Prompts&lt;/strong&gt;: Experiment with different scene descriptions and character poses&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sampling method&lt;/strong&gt;: Try different samplers (Euler a, DPM++ 2M Karras, etc.)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Skeleton structure and position&lt;/strong&gt;: Adjust the pose skeleton to avoid overlapping with QR code critical areas&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control weight&lt;/strong&gt;: Balance between OpenPose (pose accuracy) and QR code (scannability) weights&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Guidance start/end timing&lt;/strong&gt;: Fine-tune when each ControlNet kicks in during the denoising process&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="appendix-controlnet-qrcode-model-selection"&gt;Appendix: ControlNet QRCode Model Selection
&lt;/h2&gt;&lt;p&gt;To date, &lt;strong&gt;QRCode Monster&lt;/strong&gt; is the model with the highest QR code control success rate and best image fusion effect. It can be downloaded from HuggingFace:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster" target="_blank" rel="noopener"
 &gt;QRCode Monster - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Another option is &lt;strong&gt;QR Pattern v2.0&lt;/strong&gt;, best used with IoC Lab&amp;rsquo;s Brightness model as an auxiliary to improve local contrast. However, based on our testing, this model introduces more interference that may significantly alter the image style. Downloads:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://civitai.com/models/90940/controlnet-qr-pattern-qr-codes" target="_blank" rel="noopener"
 &gt;QR Pattern v2.0 - Civitai&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/ioclab/ioc-controlnet" target="_blank" rel="noopener"
 &gt;IoC Lab ControlNet - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="references"&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class="link" href="https://catalog.us-east-1.prod.workshops.aws/workshops/facdf921-2eea-4638-bc01-522e1eef3dc5" target="_blank" rel="noopener"
 &gt;AIGC Stable Diffusion Workshop&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://antfu.me/posts/ai-qrcode-101" target="_blank" rel="noopener"
 &gt;QR Toolkit by Anthony Fu&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster" target="_blank" rel="noopener"
 &gt;QRCode Monster - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="link" href="https://huggingface.co/ioclab/ioc-controlnet" target="_blank" rel="noopener"
 &gt;IoC Lab ControlNet - HuggingFace&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;

 &lt;blockquote&gt;
 &lt;p&gt;Original post: &lt;a class="link" href="https://aws.amazon.com/cn/blogs/china/art-qr-code-generation-with-controlnet-ai-painting-solution-based-on-stable-diffusion/" target="_blank" rel="noopener"
 &gt;Art QR Code Generation with ControlNet - AI Painting Solution Based on Stable Diffusion&lt;/a&gt;&lt;/p&gt;

 &lt;/blockquote&gt;</description></item></channel></rss>