<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: kosei</title>
    <description>The latest articles on DEV Community by kosei (@koseimori).</description>
    <link>https://dev.to/koseimori</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1328391%2F2a366f22-d317-4f1c-8552-24cfde1d602c.jpeg</url>
      <title>DEV Community: kosei</title>
      <link>https://dev.to/koseimori</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/koseimori"/>
    <language>en</language>
    <item>
      <title>Design Notes for the Claude Agent SDK</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Sun, 22 Nov 2026 14:17:08 +0000</pubDate>
      <link>https://dev.to/koseimori/design-notes-for-the-claude-agent-sdk-3p7</link>
      <guid>https://dev.to/koseimori/design-notes-for-the-claude-agent-sdk-3p7</guid>
      <description>&lt;h1&gt;
  
  
  Design Notes for the Claude Agent SDK
&lt;/h1&gt;

&lt;p&gt;I'm building an agent with the Claude Agent SDK for the first time, so I'm collecting what I learn and the design decisions I need to think through. I'll update these notes as I work on the implementation, learn more, or find something that needs correcting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key design considerations
&lt;/h2&gt;

&lt;p&gt;These notes cover termination conditions, permissions, delegation, failure handling, and evaluation when building agents with the Claude Agent SDK. Explanations about implementing your own loop with the Messages API are included separately.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Verify success:&lt;/strong&gt; Don't treat a successful SDK result as sufficient. Check the actual deliverables and the application's acceptance criteria.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make stopping possible:&lt;/strong&gt; Distinguish success, stopping without success, and human handoff. Set limits on time, turns, cost, and retries.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enforce permissions at execution time:&lt;/strong&gt; Combine tool configuration, runtime authorization, and backend permissions. Decide what happens when a permission check itself fails.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delegate independent work:&lt;/strong&gt; Define each subagent's scope and response format. Validate its results before starting dependent work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Isolate data:&lt;/strong&gt; Separate each user's workspace, credentials, conversations, and memory. Don't treat external content as authorization.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compare changes:&lt;/strong&gt; Use a fixed set of evaluation cases to measure success rates, prohibited actions, cost, and duration. Record the versions of the execution environment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Start by defining success and stopping conditions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;First, define what makes the agent loop succeed or fail. Then distinguish the conditions for success, stopping without success, and handing off to a human.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instructions such as "handle this properly" or "improve this" leave both the target outcome and the stopping point unclear.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the official documentation says
&lt;/h2&gt;

&lt;p&gt;The Claude Agent SDK repeats a cycle: the model decides what to do, a tool runs, and the model reassesses the result. The loop normally ends when the model responds without a tool call. You can inspect the final state through &lt;code&gt;ResultMessage.subtype&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Treat &lt;code&gt;success&lt;/code&gt; as successful SDK completion, and verify the application requirements separately. Turn limits can be configured with &lt;code&gt;max_turns / maxTurns&lt;/code&gt;, and budget limits with &lt;code&gt;max_budget_usd / maxBudgetUsd&lt;/code&gt;. The budget limit is not a strict spending limit enforced before each charge; it can be exceeded, so leave a margin.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/agent-loop" rel="noopener noreferrer"&gt;How the agent loop works&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying this in practice
&lt;/h2&gt;

&lt;p&gt;The following are design proposals based on the official documentation. They should be distinguished from behavior the SDK automatically guarantees.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Define three kinds of termination conditions
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;What to define&lt;/th&gt;
&lt;th&gt;Example: a customer support agent&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Success&lt;/td&gt;
&lt;td&gt;Deliverables and verifiable acceptance criteria&lt;/td&gt;
&lt;td&gt;Identify the applicable policy and generate a draft response with all required information&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop without success&lt;/td&gt;
&lt;td&gt;Conditions that end processing even if the task is incomplete&lt;/td&gt;
&lt;td&gt;Time limit reached, the same error recurring, or retry limit exceeded&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Handoff&lt;/td&gt;
&lt;td&gt;Conditions that require a human instead of further autonomous decisions&lt;/td&gt;
&lt;td&gt;Insufficient evidence, conflicting policies, or an operation outside the agent's authority&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An outcome such as "out of scope" or "cannot be fulfilled" can still be a valid business result if it is correctly identified and explained. Don't confuse that with a system failure.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Don't accept the agent's own success report as proof
&lt;/h3&gt;

&lt;p&gt;Write acceptance criteria in the prompt, and check those criteria in the calling code as well.&lt;/p&gt;

&lt;p&gt;For document extraction, receiving JSON is not enough. Separately validate required fields, correspondence with the source document, and consistency of monetary amounts. For code changes, verify that the changes actually exist and that the required tests have run with the expected results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One possible success rule: successful SDK completion + required output present + application validation passed.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Structured output can be validated against a format such as JSON Schema. However, valid structure does not guarantee correct content. The official documentation also explains that a &lt;code&gt;success&lt;/code&gt; result without the expected &lt;code&gt;structured_output&lt;/code&gt; should be treated as a failure.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/structured-outputs" rel="noopener noreferrer"&gt;Get structured output from agents&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Enforce limits in the execution environment, not just the prompt
&lt;/h3&gt;

&lt;p&gt;Don't rely only on an instruction such as "stop after a few attempts." Control turns, budget, execution time, and retries.&lt;/p&gt;

&lt;p&gt;In particular, define a stopping condition for repeated attempts with the same input and error when no progress is being made. Time limits and progress detection are application-level controls proposed here.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Define approval boundaries and when updates may be retried
&lt;/h3&gt;

&lt;p&gt;Hooks such as &lt;code&gt;PreToolUse&lt;/code&gt; can control actions before execution, while &lt;code&gt;PostToolUse&lt;/code&gt; can support monitoring and processing afterward.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/hooks" rel="noopener noreferrer"&gt;Intercept and control agent behavior with hooks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For reading, drafting, and committing changes to external systems, explicitly define which operations may run automatically and which require prior approval. Also define when updates may be retried. Section 8 covers checking external state after a timeout and preventing duplicate execution.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Make the next action clear even when processing stops early
&lt;/h3&gt;

&lt;p&gt;Record the termination reason, validation results, completed changes, unfinished work, and information needed to resume.&lt;/p&gt;

&lt;p&gt;For example, the application could return:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;status&lt;/code&gt;: completed / blocked / needs_human / failed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;evidence&lt;/code&gt;: supporting evidence, deliverables, and verification results&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;remaining_work&lt;/code&gt;: unfinished work&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;next_action&lt;/code&gt;: retry, ask a human, or another next step&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are application-defined fields, not standard SDK termination states.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Define when to delegate to subagents
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Decide whether to delegate based on whether a task can be completed independently with little information sharing.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The official guidance recommends delegation for parallelizable work, work that benefits from separate context, and tasks that can proceed independently without shared state. It favors direct handling by the parent for simple tasks, sequential work, edits to a single file, and tasks that need continuous context throughout the process. Avoid overusing subagents for things as small as a simple code search.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices#subagent-orchestration" rel="noopener noreferrer"&gt;Claude prompting best practices — Subagent orchestration&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following examples translate that guidance into practical design choices.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Consideration&lt;/th&gt;
&lt;th&gt;Better suited to delegation&lt;/th&gt;
&lt;th&gt;Better handled by the parent&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dependencies&lt;/td&gt;
&lt;td&gt;Research AWS, Azure, and GCP independently&lt;/td&gt;
&lt;td&gt;The scope of research B depends on the result of A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Context&lt;/td&gt;
&lt;td&gt;The task can be completed with the supplied documents and instructions&lt;/td&gt;
&lt;td&gt;The task requires a detailed understanding of the ongoing conversation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Work target&lt;/td&gt;
&lt;td&gt;Investigate separate modules independently&lt;/td&gt;
&lt;td&gt;Make closely related changes to the same file&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Workload&lt;/td&gt;
&lt;td&gt;Read large logs and return evidence and key findings&lt;/td&gt;
&lt;td&gt;Change a few lines or perform a single search&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Evaluation&lt;/td&gt;
&lt;td&gt;Review from another perspective against clear criteria&lt;/td&gt;
&lt;td&gt;Make the final decision based on the overall intent&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Delegation adds the work of writing instructions, passing information, checking results, and integrating them. Use it when the benefits of parallelism or context isolation outweigh that overhead. If the task is simply to read several files, the parent may be able to run independent tool calls concurrently. Parallel tool execution and delegation of work that involves judgment are different choices.&lt;/p&gt;

&lt;h4&gt;
  
  
  What to specify when delegating
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Purpose and scope: what to investigate and where responsibility ends&lt;/li&gt;
&lt;li&gt;Inputs and assumptions: required documents, constraints, and decision criteria&lt;/li&gt;
&lt;li&gt;Completion, stopping, and handoff conditions: what must be confirmed before finishing, and when to return control to the parent&lt;/li&gt;
&lt;li&gt;Response format: conclusions, evidence, unresolved questions, and changes made&lt;/li&gt;
&lt;li&gt;Permissions: research only, or permission to edit and execute as well&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Even after a subagent finishes its work, the parent should validate the evidence and deliverables before accepting the result.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;"The child finished its investigation" and "the overall objective was achieved" are different statements. The parent should check evidence, deliverables, and consistency with other results, then perform the necessary application-level validation before accepting the output.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Design tool availability separately from execution permissions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Give each role only the tools it needs, and separately validate which operations those tools may perform at runtime.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Giving every subagent the same large tool catalog forces each one to consider options outside its role. Make tool names and descriptions clear, and remove unnecessary tools from the configuration. An instruction saying "don't use this" should not be the only boundary.&lt;/p&gt;

&lt;h4&gt;
  
  
  How the SDK settings differ
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;What it controls&lt;/th&gt;
&lt;th&gt;Important distinction&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;AgentDefinition.tools&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Tools available to a subagent&lt;/td&gt;
&lt;td&gt;Explicitly list the tools to provide. If omitted, the subagent inherits tools available to it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;allowedTools&lt;/code&gt; (&lt;code&gt;allowed_tools&lt;/code&gt; in Python)&lt;/td&gt;
&lt;td&gt;Automatic approval of tool execution&lt;/td&gt;
&lt;td&gt;This does not restrict which tools are available. Tools outside the list may still execute under other permission settings&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;permissionMode&lt;/code&gt;, allow/deny rules, and &lt;code&gt;canUseTool&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Allowing, denying, or requesting confirmation for execution&lt;/td&gt;
&lt;td&gt;Design this separately from tool availability. Define both automatic approval and decisions based on arguments and targets&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Sources: &lt;a href="https://code.claude.com/docs/en/agent-sdk/subagents#tool-restrictions" rel="noopener noreferrer"&gt;Subagents in the SDK — Tool restrictions&lt;/a&gt;, &lt;a href="https://code.claude.com/docs/en/agent-sdk/permissions" rel="noopener noreferrer"&gt;Configure permissions&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Example tool sets by role
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Role&lt;/th&gt;
&lt;th&gt;Example tools&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Researcher&lt;/td&gt;
&lt;td&gt;Web search and page retrieval&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Code reviewer&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Read&lt;/code&gt;, &lt;code&gt;Grep&lt;/code&gt;, and &lt;code&gt;Glob&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Implementer&lt;/td&gt;
&lt;td&gt;Reading, editing, and running required tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Integrator&lt;/td&gt;
&lt;td&gt;Read the supplied research results; return to the parent or researcher if more investigation is needed&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Refund handler&lt;/td&gt;
&lt;td&gt;Order lookup and a dedicated refund tool; validate amounts and target orders separately&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For a code reviewer, for example, explicitly set &lt;code&gt;tools: ["Read", "Grep", "Glob"]&lt;/code&gt;. Avoid providing unnecessary editing or search tools, and define where the agent should hand off when its tools are insufficient.&lt;/p&gt;

&lt;h4&gt;
  
  
  A proposed three-layer control model
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Tool configuration: expose only the tools needed for the role.&lt;/li&gt;
&lt;li&gt;Runtime checks: validate arguments, target resources, amounts, and approval status in code or hooks.&lt;/li&gt;
&lt;li&gt;Backend permissions: restrict operations through AWS IAM, read-only database users, API authorization, and similar controls.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Consider alternate paths through general-purpose tools.&lt;/strong&gt; If you provide &lt;code&gt;Bash&lt;/code&gt;, an agent may be able to perform an operation through a CLI or HTTP request even when the dedicated tool for that operation is absent. Not providing a particular tool does not necessarily make the operation impossible. Enforce prohibited operations in the execution environment and backend as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Recover locally when possible, and return decisions to the parent when necessary
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A subagent should handle temporary failures it can recover from within its scope. When it reaches a processing limit or needs a change in approach, it should return partial results and ask the parent to take over the decision.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are application design proposals, not behaviors the SDK automatically guarantees in every case.&lt;/p&gt;

&lt;h4&gt;
  
  
  Local recovery versus escalation
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Situation&lt;/th&gt;
&lt;th&gt;Example response&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Temporary search or read timeout&lt;/td&gt;
&lt;td&gt;Retry with backoff within attempt and time limits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Invalid arguments that can be corrected within the assigned scope&lt;/td&gt;
&lt;td&gt;Correct the arguments and retry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Permission or authentication failure&lt;/td&gt;
&lt;td&gt;Report the required intervention to the parent instead of repeating the same request&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retry limit or time budget reached&lt;/td&gt;
&lt;td&gt;Return partial results and unfinished work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A change in research scope, source, or overall approach is needed&lt;/td&gt;
&lt;td&gt;Return enough information for the parent to decide&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Waiting 1 second, then 2 seconds, then 4 seconds, with a maximum of three retries, is only an example configuration. A timeout is not guaranteed to resolve quickly. Set limits on total time and cost as well as retries, and account for automatic retries in the SDK or HTTP client so that the total number of attempts does not multiply unexpectedly.&lt;/p&gt;

&lt;p&gt;For billing, sending, or updating operations, the external operation may already have completed when a timeout occurs. Before retrying, prevent duplicate execution through state checks, idempotency keys, or similar mechanisms.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why local recovery helps, and what context isolation changes
&lt;/h4&gt;

&lt;p&gt;Ordinary subagents run in separate conversations. Intermediate tool calls and results remain with the subagent, and the parent normally receives its final message. Starting a new worker for every temporary failure can therefore introduce repeated investigation and additional handoff work.&lt;/p&gt;

&lt;p&gt;However, delegating again does not always mean starting from scratch. Supported subagents can resume with their history, and a fork may inherit the parent's conversation. Distinguish a new invocation, resuming an existing agent, and forking.&lt;/p&gt;

&lt;p&gt;Sources: &lt;a href="https://code.claude.com/docs/en/agent-sdk/subagents" rel="noopener noreferrer"&gt;Subagents in the SDK&lt;/a&gt;, &lt;a href="https://code.claude.com/docs/en/agent-sdk/subagents#resume-subagents" rel="noopener noreferrer"&gt;Resume subagents&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Return a structured report that supports the next decision, even on failure
&lt;/h4&gt;

&lt;p&gt;Reporting incomplete work as success can cause the parent to miss gaps in research or validation. Retrying indefinitely, on the other hand, blocks the overall workflow. When a limit is reached, distinguish complete, partial, and failed outcomes, and return both the findings and their limitations.&lt;/p&gt;

&lt;p&gt;The following is an application-specific output example, not a standard SDK termination state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"partial"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"error_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"timeout"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"attempts"&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 class="nl"&gt;"attempted_queries"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"The search conditions actually attempted"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"completed"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Research into sources A and B"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"missing"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"Verification of source C"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"partial_results"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nl"&gt;"finding"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"A verified fact"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Source URL"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"next_action"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Investigate an alternative to source C"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parent can request only the unfinished work, resume the existing worker, use another source, or provide a partial answer that explicitly states the gaps. It should verify coverage and evidence instead of accepting the child's report at face value.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Finish normalization in SDK hooks before the model reads the result
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;This section covers behavior relevant to developers implementing agents with the Claude Agent SDK.&lt;/strong&gt; Design the callbacks registered in &lt;code&gt;options.hooks&lt;/code&gt; (&lt;code&gt;hooks&lt;/code&gt; in the Python options) and their return values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A hook firing, or normalized data appearing in a log, does not prove that the result passed to the model was replaced.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Distinguish normal waiting behavior from asynchronous output
&lt;/h4&gt;

&lt;p&gt;By default, the SDK waits for a hook's return value. A &lt;code&gt;PostToolUse&lt;/code&gt; hook can transform a tool result and return a replacement before the model reads it.&lt;/p&gt;

&lt;p&gt;Returning &lt;code&gt;{ async: true }&lt;/code&gt; in TypeScript, or &lt;code&gt;{"async_": True}&lt;/code&gt; in Python, instead tells the SDK to continue without waiting. This is intended for side effects such as logging, metrics, and notifications. It cannot block the action, modify the result, or inject context.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A JavaScript &lt;code&gt;async&lt;/code&gt; function or Python &lt;code&gt;async def&lt;/code&gt; is not itself the problem.&lt;/strong&gt; If the callback awaits the transformation and returns a normal output object, the SDK waits for that return value. This is separate from the SDK's asynchronous output flag.&lt;/p&gt;

&lt;h4&gt;
  
  
  Input changes, additional context, and result replacement
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting or return field&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;th&gt;Normalization consideration&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;matcher&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Select the target tool&lt;/td&gt;
&lt;td&gt;If the hook already fires, changing the matcher alone will not fix an unapplied result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PreToolUse.updatedInput&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Change arguments passed to a tool&lt;/td&gt;
&lt;td&gt;Does not directly replace the result returned by an external server&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PostToolUse.additionalContext&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Add information to the result&lt;/td&gt;
&lt;td&gt;The original data remains; this is not result replacement&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;PostToolUse.updatedToolOutput&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Replace the tool result passed to the model&lt;/td&gt;
&lt;td&gt;Return it after transformation completes, using normal waiting behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;updatedMCPToolOutput&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Earlier field for replacing MCP tool results&lt;/td&gt;
&lt;td&gt;Check support in your version. The current documentation also describes &lt;code&gt;updatedToolOutput&lt;/code&gt; for all tools&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;{ async: true }&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Continue without waiting&lt;/td&gt;
&lt;td&gt;Do not use it for normalization or result replacement&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A &lt;code&gt;PostToolUse&lt;/code&gt; response containing &lt;code&gt;decision: "block"&lt;/code&gt; and a reason is also different from replacing the tool result. It does not undo side effects from a tool that has already executed.&lt;/p&gt;

&lt;h4&gt;
  
  
  A schema mismatch can leave the original result visible to the model
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;In the Claude Agent SDK, if a replacement for a built-in tool does not match its output schema, the replacement is ignored and the original tool result is used.&lt;/strong&gt; This is behavior of the Claude Code runtime used by the SDK, and it is also documented in the Python SDK type definitions. It is not a general rule of the Claude API or MCP protocol.&lt;/p&gt;

&lt;p&gt;The sequence is: execute the tool, obtain its original result, return a replacement from &lt;code&gt;PostToolUse&lt;/code&gt;, validate the schema, then use the replacement if valid or the original result if invalid. A failed replacement does not discard the original result. The official sources referenced here do not explain the rationale behind this design.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool type&lt;/th&gt;
&lt;th&gt;How &lt;code&gt;updatedToolOutput&lt;/code&gt; is handled&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Built-in tools such as Bash, Read, and Edit&lt;/td&gt;
&lt;td&gt;The replacement must match the tool's output structure; otherwise, the original result is used&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MCP tools&lt;/td&gt;
&lt;td&gt;The replacement is passed through without the schema validation used for built-in tools&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For Bash, preserve the structure containing fields such as &lt;code&gt;stdout&lt;/code&gt;, &lt;code&gt;stderr&lt;/code&gt;, &lt;code&gt;interrupted&lt;/code&gt;, and &lt;code&gt;isImage&lt;/code&gt;, rather than returning only a replacement string. The following example follows the official documentation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"hookSpecificOutput"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"hookEventName"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"PostToolUse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"updatedToolOutput"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;"stdout"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Connected to [HOST]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"stderr"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"interrupted"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nl"&gt;"isImage"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the implementation, copy the original &lt;code&gt;tool_response&lt;/code&gt; and transform only the required fields. Match each tool's schema rather than reusing the Bash structure for other tools. A &lt;code&gt;PostToolUse&lt;/code&gt; response with &lt;code&gt;decision: "block"&lt;/code&gt; adds a reason alongside the original result; it cannot hide the original information. Likewise, &lt;code&gt;additionalContext&lt;/code&gt; only adds information.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do not rely solely on &lt;code&gt;PostToolUse&lt;/code&gt; replacement to redact sensitive information.&lt;/strong&gt; Because a schema mismatch can expose the original result to the model, information that must be hidden should be removed in the tool or a wrapper. If redaction fails, return an error rather than raw data. This is an application-level design proposal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;updatedToolOutput&lt;/code&gt; changes the result shown to the model. It does not undo completed file changes or network requests. The official documentation also explains that OpenTelemetry tool spans and analytics events record the original output before the hook runs.&lt;/p&gt;

&lt;p&gt;Test valid replacements, deliberately invalid replacements, and cases where sensitive information appears in both &lt;code&gt;stdout&lt;/code&gt; and &lt;code&gt;stderr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Sources: &lt;a href="https://code.claude.com/docs/en/agent-sdk/hooks" rel="noopener noreferrer"&gt;Agent SDK — Hooks&lt;/a&gt;, &lt;a href="https://github.com/anthropics/claude-agent-sdk-python/blob/main/src/claude_agent_sdk/types.py" rel="noopener noreferrer"&gt;Python SDK — PostToolUseHookSpecificOutput&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Conceptual normalization example
&lt;/h4&gt;

&lt;p&gt;Use code to normalize timestamps to ISO 8601 or map status codes to labels, rather than asking the model to do it. Define units, time zones, and how unknown codes are handled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Conceptual callback registered for PostToolUse.&lt;/span&gt;
&lt;span class="c1"&gt;// Implement normalizeToolResponse yourself, preserving the tool's output format.&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;normalizeHook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hook_event_name&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"PostToolUse"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;normalized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;normalizeToolResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tool_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;hookSpecificOutput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;hookEventName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"PostToolUse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;updatedToolOutput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;normalized&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;async&lt;/code&gt; function returns after the transformation completes; it does not return &lt;code&gt;{ async: true }&lt;/code&gt;. Preserve required output structures such as MCP's &lt;code&gt;content&lt;/code&gt;. For built-in tools, also match the output schema required by that tool.&lt;/p&gt;

&lt;p&gt;Verify the transformed values in the tool result passed to the model, not just in the hook's execution log.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/hooks" rel="noopener noreferrer"&gt;Claude Agent SDK — Hooks: Outputs / Asynchronous output&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Make the boundary between autonomous work and prior confirmation concrete
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Instead of only saying "ask before making dangerous changes," define what the agent may do autonomously and what requires human confirmation, using conditions and contrasting examples.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If an agent frequently asks about trivial changes but proceeds without asking about major ones, the decision boundary may be unclear. Consider reversibility, public interfaces, dependencies, and effects on shared systems, rather than only the number of changed lines or task difficulty. The following are example team rules, not criteria automatically guaranteed by the SDK.&lt;/p&gt;

&lt;h4&gt;
  
  
  Examples of proceeding versus asking
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Change&lt;/th&gt;
&lt;th&gt;Response&lt;/th&gt;
&lt;th&gt;Reason&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rename a variable used only inside a function&lt;/td&gt;
&lt;td&gt;Proceed autonomously&lt;/td&gt;
&lt;td&gt;No effect on external usage or behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reformat code without changing behavior&lt;/td&gt;
&lt;td&gt;Proceed autonomously&lt;/td&gt;
&lt;td&gt;A local, reversible change&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Change a public API name, required argument, or return contract&lt;/td&gt;
&lt;td&gt;Ask before changing it&lt;/td&gt;
&lt;td&gt;Affects compatibility with callers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Change behavior that other modules depend on&lt;/td&gt;
&lt;td&gt;Ask before changing it&lt;/td&gt;
&lt;td&gt;Effects extend beyond the edited location&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Delete a shared file or update shared infrastructure&lt;/td&gt;
&lt;td&gt;Ask before execution&lt;/td&gt;
&lt;td&gt;Affects other people's work or shared state&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Contrast an internal variable rename with a public API rename and include a short explanation. A few contrasting examples can communicate a decision pattern that applies to unfamiliar cases. Take existing explicit authorization into account, and avoid asking for the same approval repeatedly.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices" rel="noopener noreferrer"&gt;Claude prompting best practices — Use examples effectively / Balancing autonomy and safety&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Give prompts and runtime controls different responsibilities
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mechanism&lt;/th&gt;
&lt;th&gt;Responsibility&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Prompt&lt;/td&gt;
&lt;td&gt;Explain criteria that depend on the meaning of a change and its dependencies, such as whether it breaks compatibility&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Permission settings and a &lt;code&gt;PreToolUse&lt;/code&gt; hook implemented in code&lt;/td&gt;
&lt;td&gt;Deny actions or request confirmation based on conditions that can be evaluated, such as tool names, paths, commands, and arguments&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API diff checks, static analysis, and similar tools&lt;/td&gt;
&lt;td&gt;Detect changes such as public API modifications and provide evidence for hooks or CI&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A prompt guides model behavior but is not an execution control that guarantees compliance. Hooks implemented in code can inspect execution requests independently of the model's intent. However, their enforcement is limited to the checks that have actually been implemented.&lt;/p&gt;

&lt;p&gt;Formatting and a behavior change can affect the same file through the same editing tool. Paths and tool names alone cannot distinguish them. State the semantic boundary in the prompt, then reinforce machine-detectable conditions with permissions, hooks, and analysis. This does not mean hooks can never incorporate semantic judgments.&lt;/p&gt;

&lt;p&gt;Sources: &lt;a href="https://code.claude.com/docs/en/agent-sdk/hooks" rel="noopener noreferrer"&gt;Agent SDK — Hooks&lt;/a&gt;, &lt;a href="https://code.claude.com/docs/en/agent-sdk/permissions" rel="noopener noreferrer"&gt;Agent SDK — Configure permissions&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Explain exactly when confirmation is required
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Adding "IMPORTANT" to an ambiguous instruction does not define what makes a change dangerous.&lt;/li&gt;
&lt;li&gt;When prior confirmation is required, briefly state the proposed change, its impact, and why confirmation is needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  11. Design parallel execution and sequencing around dependencies and start conditions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Let agents choose flexible research procedures, and enforce mandatory dependencies in application code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following are proposals based on the official documentation. Distinguish prompt-based guidance from execution order guaranteed by code.&lt;/p&gt;

&lt;h4&gt;
  
  
  Start only independent tasks at the same time
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Task&lt;/th&gt;
&lt;th&gt;Required input&lt;/th&gt;
&lt;th&gt;Start condition&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;A: Find uses of deprecated APIs&lt;/td&gt;
&lt;td&gt;Source code&lt;/td&gt;
&lt;td&gt;Can start in parallel with B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B: Investigate module dependencies&lt;/td&gt;
&lt;td&gt;Source code&lt;/td&gt;
&lt;td&gt;Can start in parallel with A&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C: Create a migration plan&lt;/td&gt;
&lt;td&gt;Results from A and B&lt;/td&gt;
&lt;td&gt;Start after both final results arrive and pass acceptance checks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Listing multiple tool calls in the same response does not pass an earlier call's result to a later one. Even if the runtime executes them sequentially, the results of A and B are not automatically inserted into the already-generated arguments for C.&lt;/p&gt;

&lt;p&gt;Explicitly pass ordinary subagents the results, assumptions, evidence, and storage locations they need. They may obtain additional information through tools or inherit context through a fork, but they cannot access results that do not exist yet.&lt;/p&gt;

&lt;h4&gt;
  
  
  When to use prompts and when to use code
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Condition&lt;/th&gt;
&lt;th&gt;Control method&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Research procedures or delegates may change during the task&lt;/td&gt;
&lt;td&gt;Tell the parent agent the dependencies and start conditions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;A fixed workflow requires C to start only after A and B finish&lt;/td&gt;
&lt;td&gt;Separate the SDK calls and control sequencing with &lt;code&gt;await&lt;/code&gt; in code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Updates or messages may happen only after validation passes&lt;/td&gt;
&lt;td&gt;Validate results in code before starting the next stage&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For example, instruct the parent to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Run A and B in parallel because they are independent.&lt;/li&gt;
&lt;li&gt;Do not start C until both final reports have arrived.&lt;/li&gt;
&lt;li&gt;Pass C the results, evidence, and unresolved questions from A and B.&lt;/li&gt;
&lt;li&gt;If one fails, retry within the limits. If required inputs are missing, do not proceed to C; report partial results and the reason for stopping.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;A notification that background work has started does not mean the investigation is complete.&lt;/strong&gt; Acceptance of a background invocation is not its final report. Check the waiting and completion-notification behavior of your version, and wait for final results before starting dependent work.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/subagents" rel="noopener noreferrer"&gt;Subagents in the SDK&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Controlling a fixed workflow in code: pseudocode
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# run_agent is an application-defined function.&lt;/span&gt;
&lt;span class="c1"&gt;# It consumes Agent SDK query() to completion,&lt;/span&gt;
&lt;span class="c1"&gt;# checks successful completion and the required final output, then returns.&lt;/span&gt;
&lt;span class="n"&gt;api_report&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dependency_report&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;run_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Find uses of deprecated APIs"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;run_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Investigate dependencies"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Validate required fields, evidence, and unfinished work.&lt;/span&gt;
&lt;span class="c1"&gt;# Do not proceed if validation fails.&lt;/span&gt;
&lt;span class="nf"&gt;validate_reports&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_report&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dependency_report&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;plan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;run_agent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;make_plan_prompt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;api_report&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dependency_report&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the application coordinates multiple SDK sessions, rather than a parent agent internally delegating to subagents. Each agent can choose how to conduct its investigation, while code fixes the dependencies between stages.&lt;/p&gt;

&lt;p&gt;Define timeouts, concurrency limits, how to retry only failed investigations, and when partial results are sufficient to proceed. One exception in &lt;code&gt;asyncio.gather&lt;/code&gt; does not necessarily stop all other work, so also decide whether the remaining tasks should continue or be cancelled. For tasks that change the same file or external state, serialize them or isolate their workspaces to avoid conflicts.&lt;/p&gt;

&lt;p&gt;Sources: &lt;a href="https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices" rel="noopener noreferrer"&gt;Claude prompting best practices — Chain complex prompts&lt;/a&gt;, &lt;a href="https://www.anthropic.com/research/building-effective-agents" rel="noopener noreferrer"&gt;Building effective agents&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Distinguish Messages API result formatting from the Agent SDK's responsibilities
&lt;/h4&gt;

&lt;p&gt;If you implement the loop yourself with the Messages API, return the &lt;code&gt;tool_result&lt;/code&gt; for each &lt;code&gt;tool_use&lt;/code&gt; in the same assistant response together in the next user message. Match them using &lt;code&gt;tool_use_id&lt;/code&gt;, and put result blocks before explanatory text. Do not omit calls that failed or were not executed; return &lt;code&gt;is_error: true&lt;/code&gt; and a reason.&lt;/p&gt;

&lt;p&gt;With the Agent SDK, the SDK normally executes tools and returns their results. You do not need to construct &lt;code&gt;tool_result&lt;/code&gt; blocks manually each time. Also distinguish the events emitted by the SDK from the conversation-history format sent to the Messages API.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tool_choice: any&lt;/code&gt; requires at least one tool call in the Messages API. It does not guarantee a particular degree of parallelism or execution order.&lt;/p&gt;

&lt;p&gt;Sources: &lt;a href="https://platform.claude.com/docs/en/agents-and-tools/tool-use/parallel-tool-use" rel="noopener noreferrer"&gt;Parallel tool use&lt;/a&gt;, &lt;a href="https://code.claude.com/docs/en/agent-sdk/agent-loop" rel="noopener noreferrer"&gt;How the agent loop works&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  12. Plan for failures in permission-checking hooks
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Implementing a permission check does not automatically mean the operation will be denied if the check itself fails.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The current official documentation explains that, for many events, processing continues with a warning when a callback raises an exception, its output fails validation, or a return value is missing. If a &lt;code&gt;PreToolUse&lt;/code&gt; check fails, processing moves on to the normal permission evaluation. Registering the hook alone therefore does not guarantee denial.&lt;/p&gt;

&lt;p&gt;For a &lt;code&gt;PreToolUse&lt;/code&gt; hook protecting important actions, set &lt;code&gt;onFailure: "block"&lt;/code&gt; in TypeScript or &lt;code&gt;on_failure="block"&lt;/code&gt; in Python. The documented minimum versions are TypeScript Agent SDK v0.3.317 and Python Agent SDK v0.2.177. Support is also required in Claude Code; if it is unavailable, startup fails with an error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# check_policy is an application-defined permission-checking callback.&lt;/span&gt;
&lt;span class="c1"&gt;# Do not treat an indeterminate result or internal failure as permission.&lt;/span&gt;
&lt;span class="n"&gt;hooks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"PreToolUse"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nc"&gt;HookMatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;matcher&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"Bash"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;hooks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;check_policy&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;on_failure&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"block"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a uniform guarantee across all events. &lt;code&gt;PostToolUse&lt;/code&gt; cannot undo side effects that have already occurred, and events such as &lt;code&gt;Stop&lt;/code&gt; ignore this setting. Check timeout behavior for each event as well.&lt;/p&gt;

&lt;p&gt;Deliberately trigger exceptions, invalid return values, and timeouts, then verify that protected actions do not execute. If the check catches an exception and returns &lt;code&gt;{}&lt;/code&gt;, that is a normal response, so make sure failures are not treated as permission to proceed.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/hooks#block-the-action-when-a-callback-fails" rel="noopener noreferrer"&gt;Agent SDK — Block the action when a callback fails&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  13. Define user isolation and trust boundaries for external content
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Separate sessions or subagent conversations do not, by themselves, isolate files, credentials, or access to external services.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The official documentation warns against relying on the defaults of &lt;code&gt;query()&lt;/code&gt; for multi-user isolation, because some host settings and memory are loaded outside the scope of &lt;code&gt;settingSources&lt;/code&gt;. Separate filesystems by user, and combine &lt;code&gt;settingSources: []&lt;/code&gt; (&lt;code&gt;setting_sources=[]&lt;/code&gt; in Python) with &lt;code&gt;CLAUDE_CODE_DISABLE_AUTO_MEMORY=1&lt;/code&gt; in &lt;code&gt;env&lt;/code&gt;. This does not remove server-managed settings obtained through organization credentials.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/claude-code-features" rel="noopener noreferrer"&gt;Use Claude Code features — Settings and isolation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The following are application-level design proposals.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Area&lt;/th&gt;
&lt;th&gt;What to define&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Workspace&lt;/td&gt;
&lt;td&gt;Per-user filesystems, mount boundaries, and cleanup. Changing &lt;code&gt;cwd&lt;/code&gt; alone is not isolation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Conversations and resumption&lt;/td&gt;
&lt;td&gt;Map session IDs to users and authorize resumption so users cannot resume someone else's session&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory and settings&lt;/td&gt;
&lt;td&gt;Explicitly define what is shared and what is isolated between users&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Credentials&lt;/td&gt;
&lt;td&gt;Use minimum necessary permissions and lifetimes; where possible, attach credentials through a proxy outside the execution environment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Network access and tools&lt;/td&gt;
&lt;td&gt;Validate destinations, target resources, and user IDs at runtime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Logs and deliverables&lt;/td&gt;
&lt;td&gt;Define access permissions, retention, and removal of secrets&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Search results, documents, and MCP tool results can contain malicious instructions directed at the agent. Treat them as data to process, not as evidence of user approval or a grant of authority. Limit the impact through network restrictions, filesystem permissions, and API authorization rather than relying only on prompts.&lt;/p&gt;

&lt;p&gt;Source: &lt;a href="https://code.claude.com/docs/en/agent-sdk/secure-deployment" rel="noopener noreferrer"&gt;Securely deploying AI agents&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  14. Run regression evaluations with fixed cases and record applicable versions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Don't judge the system by a single successful run. Compare the same evaluation cases before and after changing the model, prompt, tools, or SDK.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This section is a design proposal, not an automatic SDK guarantee. For each case, define inputs, required environment, expected results, allowed and prohibited operations, and acceptance criteria. Check execution traces and external state as well as the wording of the response.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Evaluation case&lt;/th&gt;
&lt;th&gt;What to check&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Normal operation&lt;/td&gt;
&lt;td&gt;Requirements, deliverables, and supporting evidence are satisfied&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insufficient or conflicting evidence&lt;/td&gt;
&lt;td&gt;The agent does not present guesses as facts and follows the defined stopping or handoff procedure&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Unauthorized actions or malicious tool results&lt;/td&gt;
&lt;td&gt;Prohibited actions are not executed, and external content does not expand permissions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Timeouts and retries&lt;/td&gt;
&lt;td&gt;Processing stops within limits and updates are not executed twice&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hook failures&lt;/td&gt;
&lt;td&gt;Protected actions do not execute when a critical pre-execution check fails&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User isolation&lt;/td&gt;
&lt;td&gt;Other users' files, conversations, and credentials are inaccessible&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Record business success rates, the number of prohibited actions executed, cost, duration, and the frequency of unnecessary confirmation requests. Model outputs vary, so repeat important cases and inspect failures. Define acceptance thresholds and tolerable variation for each use case.&lt;/p&gt;

&lt;p&gt;Include the SDK version, the actual Claude Code runtime version, model ID, versions of the prompt, tool definitions, and settings, and the execution date in evaluation records. Also record the verification date and applicable versions in specification notes, and revalidate them when updating.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pre-build checklist
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Have you defined the scope and deliverables, and distinguished success, stopping without success, and human handoff?&lt;/li&gt;
&lt;li&gt;[ ] Can you verify deliverable content and evidence, rather than relying only on successful SDK completion?&lt;/li&gt;
&lt;li&gt;[ ] Have you set turn, cost, time, and retry limits, with a way to stop when no progress is being made?&lt;/li&gt;
&lt;li&gt;[ ] Have you illustrated the boundary between automatic execution and prior approval, and enforced required restrictions through code or permissions?&lt;/li&gt;
&lt;li&gt;[ ] Have you provided only the tools needed for each role and checked alternate paths through tools such as Bash and backend permissions?&lt;/li&gt;
&lt;li&gt;[ ] Have you specified each delegated task's scope, inputs, permissions, and completion criteria, with the parent validating results before acceptance?&lt;/li&gt;
&lt;li&gt;[ ] Are only independent tasks parallelized, with dependent stages starting after all required results arrive and pass validation?&lt;/li&gt;
&lt;li&gt;[ ] Have you distinguished retries for temporary failures from human handoff and prevented duplicate updates?&lt;/li&gt;
&lt;li&gt;[ ] Have you verified both that the model receives normalized results and that protected actions are denied when critical pre-execution checks fail?&lt;/li&gt;
&lt;li&gt;[ ] Have you isolated user data, credentials, and conversations, and ensured that instructions in external documents or tool results cannot expand permissions?&lt;/li&gt;
&lt;li&gt;[ ] Can you record partial results and stopping reasons, and compare success rates, prohibited actions, cost, and time across fixed cases? Do you also record the SDK, model, and settings versions used for evaluation?&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>claude</category>
      <category>agents</category>
      <category>ai</category>
    </item>
    <item>
      <title>A Practical Guide to Continuous Delivery with GitHub Actions and AWS CDK</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Sun, 23 Feb 2025 15:41:23 +0000</pubDate>
      <link>https://dev.to/koseimori/a-practical-guide-to-continuous-delivery-with-github-actions-and-aws-cdk-435k</link>
      <guid>https://dev.to/koseimori/a-practical-guide-to-continuous-delivery-with-github-actions-and-aws-cdk-435k</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Previously, I wrote an article titled “&lt;a href="https://dev.to/koseimori/implementing-continuous-delivery-for-github-monorepos-and-microservices-with-github-actions-50i8"&gt;Implementing Continuous Delivery for Monorepo and Microservice with GitHub Actions&lt;/a&gt;.” In this article, I would like to dive deeper into a pattern that utilizes AWS as the infrastructure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment
&lt;/h2&gt;

&lt;p&gt;In this article, the following tools will be used:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub Actions&lt;/li&gt;
&lt;li&gt;AWS Account&lt;/li&gt;
&lt;li&gt;AWS CDK&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Infrastructure Definition (IaC)
&lt;/h2&gt;

&lt;p&gt;To perform Continuous Delivery (CD) on your infrastructure, you need to define your infrastructure as code (IaC). Although there are various tools available for IaC, here we will use AWS CDK.&lt;/p&gt;

&lt;h3&gt;
  
  
  AWS CDK
&lt;/h3&gt;

&lt;p&gt;Since in my previous article the repository was set up assuming TypeScript, we will also configure AWS CDK with TypeScript. However, as this article focuses on CD, please refer to another article for detailed explanations on AWS CDK.&lt;/p&gt;

&lt;p&gt;We aim for the following directory structure, so run the following command in the services/infrastructure directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./
├── .github/
│   └── workflows/
├── package.json
└── serivces/
    ├── infrastructure/
    ├── service-a/
    └── service-b/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute the following command to initialize AWS CDK:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx aws-cdk init app &lt;span class="nt"&gt;--language&lt;/span&gt; typescript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will generate the following files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-cdk-app/
├── bin/
│   └── infrastructure.ts
├── lib/
│   └── infrastructure-stack.ts
├── node_modules/
├── package.json
├── tsconfig.json
└── cdk.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a sample, the file infrastructure-stack.ts defines a stack that creates an S3 bucket:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-s3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyCdkAppStack&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StackProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Create an S3 Bucket&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;s3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Bucket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MyBucket&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;bucketName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;my-unique-bucket-name-12345&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// ※ Change this to a globally unique name&lt;/span&gt;
      &lt;span class="na"&gt;removalPolicy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;RemovalPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;DESTROY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Delete the bucket when the stack is deleted&lt;/span&gt;
      &lt;span class="na"&gt;autoDeleteObjects&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Delete objects in the bucket if removalPolicy is DESTROY&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Bootstrapping the Environment
&lt;/h3&gt;

&lt;p&gt;Before the initial deployment, you must bootstrap the target AWS account and region so that AWS CDK can deploy your stack. If you need to deploy to multiple regions, perform the bootstrap for each region. Since this is a one-time process per AWS account and region, it does not need to be managed via CD (GitHub) and can be executed from your local development machine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cdk bootstrap --region ap-northeast-1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Obtaining AWS Credentials
&lt;/h2&gt;

&lt;p&gt;In order to deploy resources to AWS from GitHub Actions, you need to obtain credentials from AWS. Although you could use AWS_ACCESS_KEY and AWS_SECRET_KEY (i.e., access keys), there are several disadvantages to that approach, which is why the OIDC method described later is recommended:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Management cost of secrets&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access keys require regular key rotation, leading to additional operational overhead.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Management cost for IAM users&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Since access keys are tied to IAM users, you would need to create IAM users for actions and periodically audit those accounts.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Security risks during operations&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access keys are intended for long-term use, so if they are compromised, they can be misused for an extended period.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Inability for fine-grained control on AWS side&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AWS cannot restrict access based on the repository or branch when using access keys, and it is difficult to change permissions on a per-workflow basis.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advantages of Using IAM Roles with OpenID Connect
&lt;/h2&gt;

&lt;p&gt;When using AWS from GitHub Actions, obtaining a role session via OpenID Connect (OIDC) offers the following advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;No need to manage long-term credentials&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With OIDC, temporary credentials are issued for each session. This eliminates the need to manage access keys on GitHub, reducing the risk of key leakage and the burden of key rotation.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Increased security and minimal permissions&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Temporary sessions obtained via OIDC have short validity. Once the session expires, no further actions can be taken, significantly lowering the security risk. By creating an AWS IAM role with only the necessary permissions and obtaining a session for that role, you can minimize the risk of permission leakage.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Seamless integration between GitHub Actions and AWS&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can restrict usage on a per-repository or per-branch basis—ensuring that only requests from specific repositories are valid, or even narrowing it down by branch or tag. Additionally, roles can be finely controlled per workflow (e.g., separate roles for deployment and testing), ensuring only the minimal required permissions are granted.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  OIDC Configuration (AWS)
&lt;/h2&gt;

&lt;p&gt;To accept OIDC tokens from GitHub Actions, you must create an OpenID Connect Provider on the AWS side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating an IAM OIDC Provider with CDK
&lt;/h2&gt;

&lt;p&gt;In CDK, you can use the OpenIdConnectProvider from the @aws-cdk/aws-iam module. Open your CDK stack file (e.g. lib/github-oidc-stack.ts) and modify it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Construct&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;constructs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;aws-cdk-lib/aws-iam&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CdkGithubOidcStack&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Construct&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;StackProps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;scope&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="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Create the GitHub OIDC Provider&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gitHubOidcProvider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OpenIdConnectProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHubOidcProvider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://token.actions.githubusercontent.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;clientIds&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sts.amazonaws.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;thumbprints&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;6938fd4d98bab03faadb97b34396831e3780aea1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parameters are set as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;url: Specify the GitHub Actions OIDC token endpoint:

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://token.actions.githubusercontent.com" rel="noopener noreferrer"&gt;https://token.actions.githubusercontent.com&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;clientIds: Specify sts.amazonaws.com.&lt;/li&gt;

&lt;li&gt;thumbprints: Specify the SHA-1 fingerprint of GitHub’s root certificate. 

&lt;ul&gt;
&lt;li&gt;Although it changed once in the past, it is currently 6938fd4d98bab03faadb97b34396831e3780aea1.
This creates the OIDC provider required for federated authentication between GitHub and AWS.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Creating an IAM Role for Use from GitHub Actions
&lt;/h3&gt;

&lt;p&gt;Creating the OIDC provider alone does not grant access to AWS resources. You also need to create an IAM role that GitHub Actions jobs can assume. In the role’s trust policy (assume role policy), you must specify that the entity possessing the GitHub Actions OIDC token is trusted.&lt;/p&gt;

&lt;p&gt;Edit your CDK stack file (e.g. lib/github-oidc-stack.ts) as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;githubRole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHubActionsRole&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;roleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;github-actions-deploy-role&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;assumedBy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WebIdentityPrincipal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;gitHubOidcProvider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;openIdConnectProviderArn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;StringEquals&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;token.actions.githubusercontent.com:aud&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sts.amazonaws.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="na"&gt;StringLike&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;token.actions.githubusercontent.com:sub&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;repo:your-org/your-repo:/*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;

  &lt;span class="c1"&gt;// Actions allowed for this role (e.g., S3 management permissions)&lt;/span&gt;
  &lt;span class="na"&gt;managedPolicies&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ManagedPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromAwsManagedPolicyName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AmazonS3FullAccess&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ManagedPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromAwsManagedPolicyName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AWSCloudFormationFullAccess&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Deploy the CDK stack and note the role ARN (e.g., arn:aws:iam::123456789012:role/github-actions-deploy-role).&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring GitHub Actions
&lt;/h3&gt;

&lt;p&gt;After configuring AWS, you need to set up GitHub Actions to assume the IAM role. GitHub Actions provides an official AWS action, aws-actions/configure-aws-credentials, which supports OIDC.&lt;/p&gt;

&lt;p&gt;You can choose the trigger conditions for the workflow; in this example, it will be triggered manually. For more details, see the GitHub documentation on manually running a workflow.&lt;/p&gt;

&lt;p&gt;Finally, don’t forget to add the permissions section.&lt;/p&gt;

&lt;p&gt;Your repository’s .github/workflows/deploy.yml file should be configured as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy to AWS&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;permissions&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;id-token&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;write&lt;/span&gt;
  &lt;span class="na"&gt;contents&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;read&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Checkout&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v4&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Configure AWS Credentials&lt;/span&gt;
        &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;aws-actions/configure-aws-credentials@v2&lt;/span&gt;
        &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="c1"&gt;# Specify the role ARN created earlier in CDK&lt;/span&gt;
          &lt;span class="na"&gt;role-to-assume&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;arn:aws:iam::your-aws-account-id:role/github-actions-push-image-role&lt;/span&gt;
          &lt;span class="na"&gt;aws-region&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ap-northeast-1&lt;/span&gt;

      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Run deploy command&lt;/span&gt;
        &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;npm run cdk deploy&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;role-to-assume: Specify the ARN of the IAM role you created.&lt;/li&gt;
&lt;li&gt;aws-region: Specify the AWS region to use (e.g., ap-northeast-1).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This configuration automatically obtains an OIDC token, allowing GitHub Actions to use AWS CLI with a temporary STS session. Since there is no need to store access keys (AccessKey/SecretKey) in the repository secrets, the process is both more secure and reduces the key management burden.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Environment Switching and Approval Steps Using GitHub Environments
&lt;/h2&gt;

&lt;p&gt;By using GitHub Environments, you can define settings and manage secrets for each environment (such as staging or production) and even implement an approval process.&lt;/p&gt;

&lt;p&gt;For more details, see Managing Environments for Deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up Environments
&lt;/h3&gt;

&lt;p&gt;Within your GitHub repository, define environments such as “staging” and “production,” and configure secrets (e.g., AWS account, region, parameters) for each environment.&lt;/p&gt;

&lt;p&gt;By setting up Required Reviewers, when a deployment workflow is executed, the workflow will pause until the specified reviewers approve it.&lt;/p&gt;

&lt;p&gt;For example, if you define an environment called production, add the following to your workflow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;production&lt;/span&gt; &lt;span class="c1"&gt;# Specify the environment name to use&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Switching Environments
&lt;/h3&gt;

&lt;p&gt;Since you will likely use multiple environments, you may want to switch between environments like “staging” and “production” within the same workflow.&lt;/p&gt;

&lt;p&gt;A workflow can accept an environment as an input, so you can switch environments (and thus the deployment target) by configuring it as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;

&lt;span class="na"&gt;inputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;env&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;AWS Environment&lt;/span&gt;
    &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;choice&lt;/span&gt;
    &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="s"&gt;dev&lt;/span&gt;
      &lt;span class="s"&gt;staging&lt;/span&gt;
      &lt;span class="s"&gt;production&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;deploy&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;runs-on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ubuntu-latest&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ inputs.env }}&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows you to dynamically switch settings such as the AWS account ID defined in GitHub environments, enabling you to manage multiple environments within a single workflow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Information
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Extending the Session Duration
&lt;/h3&gt;

&lt;p&gt;The AWS session obtained using the above configuration is valid for up to 1 hour. If your deployment workflow takes longer and the session expires, you can extend the session duration by adding additional settings to the role. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;githubRole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;iam&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Role&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;GitHubActionsRole&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;roleName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;github-actions-deploy-role&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;maxSessionDuration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;cdk&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Duration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hours&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// Valid for 2 hours&lt;/span&gt;
  &lt;span class="c1"&gt;// ... other configurations&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>aws</category>
      <category>devops</category>
      <category>cicd</category>
      <category>github</category>
    </item>
    <item>
      <title>Implementing Continuous Delivery for Monorepo and Microservice with GitHub Actions</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Sun, 17 Mar 2024 11:03:21 +0000</pubDate>
      <link>https://dev.to/koseimori/implementing-continuous-delivery-for-github-monorepos-and-microservices-with-github-actions-50i8</link>
      <guid>https://dev.to/koseimori/implementing-continuous-delivery-for-github-monorepos-and-microservices-with-github-actions-50i8</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this article, we explore how to implement CD (Continuous Delivery) using GitHub Actions in a monorepo + microservices setup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This section outlines the language and directory structure used as prerequisites.&lt;/p&gt;

&lt;h3&gt;
  
  
  Language
&lt;/h3&gt;

&lt;p&gt;The services within the repository are written in TypeScript and managed as workspaces with Yarn Workspaces. They are also assumed to be containerized. However, it is believed that the same mechanism could work even if services are written in different languages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Directory Structure
&lt;/h3&gt;

&lt;p&gt;The assumed directory structure is as follows: There is a services directory at the root, and each service is placed in its own directory within this.&lt;/p&gt;

&lt;p&gt;Infrastructure management tools like AWS are also managed within the same repository, under &lt;code&gt;services/infrastructure.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;./
├── .github/
│   └── workflows/
├── package.json
└── serivces/
    ├── infrastructure/
    ├── service-a/
    └── service-b/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;When adopting such a configuration, the scope of deployment becomes a problem. Workflows used in the repository are managed as common resources in &lt;code&gt;.github/workflows&lt;/code&gt;, and if you write the deployment workflows for &lt;code&gt;service-a&lt;/code&gt; and &lt;code&gt;service-b&lt;/code&gt; there, deploying only &lt;code&gt;service-a&lt;/code&gt; will inadvertently cause &lt;code&gt;service-b&lt;/code&gt; to be deployed as well. This can lead to increased workflow execution times and, depending on the configuration, unnecessary deployment actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;Therefore, let's consider a method to deploy only the services that have been changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution 1 (Using &lt;code&gt;paths&lt;/code&gt; Filters)
&lt;/h3&gt;

&lt;p&gt;A possible solution involves utilizing the functionality of including and excluding paths.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore" rel="noopener noreferrer"&gt;https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can define the relevant paths as follows and prepare a workflow for each service accordingly.&lt;/p&gt;

&lt;p&gt;For example, to deploy only &lt;code&gt;service-a&lt;/code&gt;, the configuration might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deploy service-a&lt;/span&gt;

&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;push&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;paths&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;serivces/service-a/**'&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# Steps to deploy service-a go here&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The disadvantages of this approach include the need to create a separate workflow file for each service, even when the deployment method is the same, which can make workflow file management cumbersome. Additionally, this method does not support dependencies between services.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution 2 (Using &lt;code&gt;git diff&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Another method is to use Git's capabilities to deploy only the services that have been changed.&lt;/p&gt;

&lt;p&gt;You can detect changes to the targeted services by using a command to get the differences from the last commit, and then deploy only the changed services.&lt;/p&gt;

&lt;p&gt;Let's explore this approach further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;p&gt;The workflow will be documented for manual execution (&lt;code&gt;workflow_dispatch&lt;/code&gt;), but it can also be set up to trigger automatically on &lt;code&gt;push&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If there are changes to the overall infrastructure managed by IaC, you'll want to deploy those changes before deploying any services, considering the deployment dependencies. This setup should also accommodate dependencies between services, allowing them to be expressed in the same system.&lt;/p&gt;

&lt;p&gt;Within the overall deployment workflow, implement the following jobs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Change detection job (&lt;code&gt;prepare&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Infrastructure deployment job&lt;/li&gt;
&lt;li&gt;Individual service deployment jobs&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This architecture allows for a more flexible deployment process, accommodating both the infrastructure and individual services, and addressing the need to manage dependencies between services.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feaxx0yna0dkgtw488a29.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feaxx0yna0dkgtw488a29.png" alt="Flowchart of deployment steps" width="528" height="586"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;graph TD;
    prepare--&amp;gt;infrastructure;
    infrastructure--&amp;gt;service-a;
    infrastructure--&amp;gt;service-b;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Indeed, having the ability to deploy services individually, in addition to deploying the entire system through one workflow, would offer greater flexibility and convenience. Thus, the solution should also enable workflows to deploy each service separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  Detailed Workflow
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Change Detection Job (&lt;code&gt;prepare&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;The change detection can be conducted as follows:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use a script within the &lt;code&gt;prepare&lt;/code&gt; job to run a &lt;code&gt;git diff&lt;/code&gt; command against the last commit to identify changed paths.&lt;/li&gt;
&lt;li&gt;Parse the output of the &lt;code&gt;git diff&lt;/code&gt; command to determine which services have been modified. This could involve checking if the changes are within the directories specific to each service.&lt;/li&gt;
&lt;li&gt;Set the output of this job to indicate which services need to be deployed. This can be achieved by setting environment variables or producing a file that subsequent jobs can read to determine whether they should proceed with deployment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By implementing this job at the beginning of the workflow, you can dynamically decide which subsequent jobs (e.g., infrastructure deployment or individual service deployments) need to run based on the changes detected. This approach minimizes unnecessary deployments, saves time, and ensures that resources are used efficiently.&lt;/p&gt;

&lt;p&gt;For individual service deployment workflows, you can adopt a similar change detection mechanism but scoped to the specific service's directory. This way, each service can have its workflow triggered either manually or automatically upon changes to its relevant files, offering a seamless and flexible deployment process tailored to the needs of each service within the monorepo.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jobs:
  prepare:
    outputs:
      infrastructure-diff-count: ${{ steps.infrastructure_changes.outputs.diff-count }}
      service-a-diff-count: ${{ steps.service_a_changes.outputs.diff-count }}
      service-b-diff-count: ${{ steps.service_b_changes.outputs.diff-count }}

    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 2
      - id: infrastructure_changes
        run: echo diff-count=`git diff HEAD~ --name-only --relative=services/infrastructure | wc -l` &amp;gt;&amp;gt; $GITHUB_OUTPUT
      - id: service_a_changes
        run: echo diff-count=`git diff HEAD~ --name-only --relative=services/service-a | wc -l` &amp;gt;&amp;gt; $GITHUB_OUTPUT
      - id: service_b_changes
        run: echo diff-count=`git diff HEAD~ --name-only --relative=services/service-b | wc -l` &amp;gt;&amp;gt; $GITHUB_OUTPUT
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are the key point:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Point&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Set fetch-depth to 2 in actions/checkout@v3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To obtain the differences from the previous commit using &lt;code&gt;git diff HEAD~&lt;/code&gt;, you need to set the &lt;code&gt;fetch-depth&lt;/code&gt; to 2. If this is not set, the previous commit cannot be fetched, and you will not be able to perform a diff comparison.&lt;br&gt;
This precaution is essential because the default behavior of &lt;code&gt;actions/checkout@v3&lt;/code&gt; is to fetch only the latest commit (i.e., with a &lt;code&gt;fetch-depth&lt;/code&gt; of 1). This shallow fetch is faster as it downloads less history, but it's not suitable for workflows that need to compare changes between commits. Setting &lt;code&gt;fetch-depth&lt;/code&gt; to 2 ensures that the action fetches the last two commits, allowing &lt;code&gt;git diff&lt;/code&gt; to correctly identify any changes made since the last commit. This setup is crucial for the change detection mechanism in CI/CD workflows that rely on comparing different versions of the codebase to determine the scope of changes and the necessity for deployments.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Infrastructure Deployment Job
&lt;/h3&gt;

&lt;p&gt;We won't detail the infrastructure deployment code, but we will mainly mention the parts related to the previous job, the change detection job.&lt;/p&gt;

&lt;p&gt;We define a deployment workflow for the infrastructure itself. By defining &lt;code&gt;workflow_dispatch&lt;/code&gt;, we can execute the workflow for the infrastructure itself. Also, to make it callable from the overall workflow, we define &lt;code&gt;workflow_call&lt;/code&gt; as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on:
  workflow_dispatch:
    inputs:
      stage:
        required: true
        type: string
  workflow_call:
    inputs:
      stage:
        required: true
        type: string

jobs:
  deploy:
    # setup, install, deploy, etc.
    # If using AWS CDK, deploy with commands like yarn workspace infrastructure cdk deploy --all --require-approval=never
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the workflow to deploy the infrastructure itself to the overall deployment workflow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jobs: # Written to align the indent, but it's the same job as prepare
  infrastructure_deploy:
    if: ${{ !cancelled() &amp;amp;&amp;amp; !failure() &amp;amp;&amp;amp; needs.prepare.outputs.infrastructure-diff-count &amp;gt; 0 }}
    needs:
      - prepare
    uses: '.github/workflows/deploy-infrastructure.yaml'
    with:
      stage: ${{ inputs.stage }}
    secrets: inherit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here are the key points:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Point 1&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Use if statements to determine the necessity of job execution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;needs.prepare.outputs.infrastructure-diff-count &amp;gt; 0&lt;/code&gt; to get the number of changed files obtained from the previous job and execute only if there are changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Point 2&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Use needs to indicate dependencies&lt;/strong&gt;&lt;/p&gt;


&lt;pre class="highlight plaintext"&gt;&lt;code&gt;needs: 
  - prepare
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;shows that it depends on the prepare job. It is possible to specify multiple dependencies, which can represent waiting until the deployment of the infrastructure or another dependent service is completed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Point 3&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Call the individual infrastructure workflow with uses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can call another workflow set up with &lt;code&gt;workflow_call&lt;/code&gt; using &lt;code&gt;uses: path to workflow file&lt;/code&gt;. If secrets are needed, they can be passed when calling. Specifying &lt;code&gt;inherit&lt;/code&gt; allows passing them collectively, but it's also possible to specify them individually.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Deployment Jobs for Each Service
&lt;/h3&gt;

&lt;p&gt;The deployment jobs for each service are almost the same as those for deploying infrastructure, so only the key points are described.&lt;/p&gt;

&lt;p&gt;We define a deployment workflow for each individual service. Services that can be deployed with the same workflow will reuse that workflow by accepting the service name as input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;on:
  workflow_dispatch:
    inputs:
      stage:
        required: true
        type: string
      service:
       required: true
       type: choice
       options:
         - service-a
         - service-b
  workflow_call:
    inputs:
      stage:
        required: true
        type: string
      service:
       required: true
       type: string

jobs:
  deploy:
    # setup, install, deploy, etc.
    # The workflow is reused by switching the target service based on inputs.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the workflow for deploying individual infrastructure to the overall deployment workflow.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;jobs: # Written to align the indentation, but it's the same job as prepare
  service_a_deploy:
    if: ${{ !cancelled() &amp;amp;&amp;amp; !failure() &amp;amp;&amp;amp; needs.prepare.outputs.service-a-diff-count &amp;gt; 0 }}
    needs:
      - prepare
      - infrastructure_deploy
    uses: '.github/workflows/deploy-service.yaml'
    with:
      stage: ${{ inputs.stage }}
      service: service-a
    secrets: inherit

  service_b_deploy:
    if: ${{ !cancelled() &amp;amp;&amp;amp; !failure() &amp;amp;&amp;amp; needs.prepare.outputs.service-b-diff-count &amp;gt; 0 }}
    needs:
      - prepare
      - infrastructure_deploy
      # - service_a_deploy
      # If dependent on service-a, specify as above to wait for the completion of service-a's deployment
    uses: '.github/workflows/deploy-service.yaml'
    with:
      stage: ${{ inputs.stage }}
      service: service-b
    secrets: inherit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key point is as follows:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Point&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Add cancelled(), failure() to the if statement to determine whether to execute the job&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;!cancelled() &amp;amp;&amp;amp; !failure() &amp;amp;&amp;amp;&lt;/code&gt; to the conditions of the if statement. This is to ensure that the workflow stops if it is cancelled, and also to ensure that this job does not skip if the jobs it depends on are skipped.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;With GitHub monorepo + microservices, it was possible to deploy only the services that have changes. The triggers for the workflow and the conditions for detecting changes can be adjusted according to each project.&lt;/p&gt;

</description>
      <category>github</category>
      <category>githubactions</category>
      <category>cicd</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Conditional Branching with workflow_dispatch and workflow_call in GitHub Actions</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Fri, 08 Mar 2024 17:41:05 +0000</pubDate>
      <link>https://dev.to/koseimori/conditional-branching-with-workflowdispatch-and-workflowcall-in-github-actions-oik</link>
      <guid>https://dev.to/koseimori/conditional-branching-with-workflowdispatch-and-workflowcall-in-github-actions-oik</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;There's a feature in GitHub Actions for reusing workflows. With workflow reuse, you can call another workflow from a workflow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.github.com/en/actions/using-workflows/reusing-workflows"&gt;https://docs.github.com/en/actions/using-workflows/reusing-workflows&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In doing so, there was a need to branch depending on whether the called workflow was called from another workflow, or if the workflow was executed standalone. This article describes how to do that.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to
&lt;/h2&gt;

&lt;p&gt;In GitHub Actions, you can obtain various information from the context at runtime.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.github.com/en/actions/learn-github-actions/contexts"&gt;https://docs.github.com/en/actions/learn-github-actions/contexts&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Among these, there exists a key named &lt;code&gt;github.event.workflow&lt;/code&gt;. This value contains the file path of the workflow.&lt;/p&gt;

&lt;p&gt;Depending on how it was called, the called workflow can obtain the following values (in the case where workflowA calls workflowB):&lt;/p&gt;

&lt;p&gt;When executing workflowB directly (&lt;code&gt;workflow_dispatch&lt;/code&gt;), the value of &lt;code&gt;github.event.workflow&lt;/code&gt;&lt;br&gt;
The file path of workflowB (&lt;code&gt;.github/workflows/workflow-b.yaml&lt;/code&gt;)&lt;br&gt;
When executed from workflowA (&lt;code&gt;workflow_call&lt;/code&gt;), the value of &lt;code&gt;github.event.workflow&lt;/code&gt;&lt;br&gt;
The file path of workflowA (&lt;code&gt;.github/workflows/workflow-a.yaml&lt;/code&gt;)&lt;br&gt;
With this, you can branch your process or steps.&lt;/p&gt;

&lt;p&gt;For example, if you want to execute a step only when workflowB is executed directly, you can add the following condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;if&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;github.event.workflow == '.github/workflows/workflow-b.yaml'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's also a &lt;code&gt;github.event_name&lt;/code&gt;, and I tried to see if it could be used for branching as well, but both the direct execution of workflowB and the execution from workflowA resulted in the same value as &lt;code&gt;workflow_dispatch&lt;/code&gt;, so it couldn't be used for branching.&lt;/p&gt;

&lt;h2&gt;
  
  
  About type: environment
&lt;/h2&gt;

&lt;p&gt;In practice, I think you can use the same workflow without branching by defining inputs with the same name as below, but since &lt;code&gt;type: environment&lt;/code&gt; exists only for &lt;code&gt;workflow_dispatch&lt;/code&gt;, if you are using this, you'll need to add a step to absorb the difference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;on&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;workflow_dispatch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;inputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;input1&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;    
  &lt;span class="na"&gt;workflow_call&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;inputs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;input1&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;string&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value in the environment (environment_name) can be used as is, but as shown above, this difference occurs, so it seems better to avoid it when making a &lt;code&gt;workflow_call&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;There seem to be various methods, but by checking &lt;code&gt;github.event.workflow&lt;/code&gt;, we were able to branch based on the method of executing the workflow.&lt;/p&gt;

</description>
      <category>githubactions</category>
      <category>cicd</category>
    </item>
    <item>
      <title>How to Set Dynamic Release URLs in Environment with GitHub Actions</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Thu, 07 Mar 2024 16:50:10 +0000</pubDate>
      <link>https://dev.to/koseimori/how-to-set-dynamic-release-urls-in-environment-with-github-actions-l83</link>
      <guid>https://dev.to/koseimori/how-to-set-dynamic-release-urls-in-environment-with-github-actions-l83</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article explains how to dynamically set a release URL in an Environment using GitHub Actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;GitHub Actions has a feature for defining environments called Environment.&lt;br&gt;
&lt;a href="https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment" rel="noopener noreferrer"&gt;Using environments for deployment - GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For example, by setting up environments like the following, it's possible to switch the release destination. (You can set any name you want.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Production environment&lt;/li&gt;
&lt;li&gt;Staging environment&lt;/li&gt;
&lt;li&gt;Testing environment&lt;/li&gt;
&lt;li&gt;Development environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Among these, there is a function to display the URL of the release destination after releasing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96fcdpl6ciekzdfqn609.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96fcdpl6ciekzdfqn609.png" alt="GitHub Actions Release URL"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there's only one fixed environment, it's not a problem. However, if the URL is determined dynamically during the release steps, it was unclear how to display it, so I'm documenting the method here.&lt;/p&gt;

&lt;p&gt;If it's a single environment, you can set the URL as follows.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;&lt;br&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;production&lt;/span&gt;&lt;br&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;&lt;a href="https://example.com" rel="noopener noreferrer"&gt;https://example.com&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  How To&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;The method is simple: save the value in an output during the step when the URL is confirmed, and then set that output as the environment's URL.&lt;/p&gt;

&lt;p&gt;Here is a sample Yaml. You need to assign a reference ID to the step. The format for referencing the output is &lt;code&gt;steps.[step_id].outputs.[output_name]&lt;/code&gt;.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;&lt;br&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;development&lt;/span&gt;&lt;br&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ steps.get_release_url.outputs.release_url }}&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;&lt;br&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set release url&lt;/span&gt;&lt;br&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;get_release_url&lt;/span&gt;&lt;br&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;echo release_url=&lt;a href="https://xxx.example.com" rel="noopener noreferrer"&gt;https://xxx.example.com&lt;/a&gt; &amp;gt;&amp;gt; $GITHUB_OUTPUT&lt;/span&gt;&lt;/p&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Conclusion&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;The method to dynamically set the URL of a release in GitHub Actions' Environment is to save a value in the output during the release step, and then set that output name as the URL in the environment.&lt;/p&gt;

</description>
      <category>githubactions</category>
      <category>cicd</category>
    </item>
    <item>
      <title>Removing "✨ Done in X.XXs." with Yarn</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Wed, 06 Mar 2024 16:33:56 +0000</pubDate>
      <link>https://dev.to/koseimori/removing-done-in-xxxs-with-yarn-2map</link>
      <guid>https://dev.to/koseimori/removing-done-in-xxxs-with-yarn-2map</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I wanted to remove the "✨ Done in X.XXs." message in Yarn. In the package.json, I defined the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "scripts": {
    "echo": "echo aaa"
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When executed, it displays the "✨ Done in X.XXs." message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn run &lt;span class="nb"&gt;echo
&lt;/span&gt;yarn run v1.22.10
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo &lt;/span&gt;aaa
aaa
✨  Done &lt;span class="k"&gt;in &lt;/span&gt;0.04s.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  --silent Option
&lt;/h2&gt;

&lt;p&gt;By adding the --silent option, it disappeared.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;yarn run &lt;span class="nt"&gt;--silent&lt;/span&gt; &lt;span class="nb"&gt;echo
&lt;/span&gt;aaa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>yarn</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>How to Reset Your User Password in Ubuntu on WSL if You Forget It</title>
      <dc:creator>kosei</dc:creator>
      <pubDate>Tue, 05 Mar 2024 15:31:09 +0000</pubDate>
      <link>https://dev.to/koseimori/how-to-reset-your-user-password-in-ubuntu-on-wsl-if-you-forget-it-48pe</link>
      <guid>https://dev.to/koseimori/how-to-reset-your-user-password-in-ubuntu-on-wsl-if-you-forget-it-48pe</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;After a long time, I tried to launch WSL and sudo on Ubuntu, but I forgot the password.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reset Method
&lt;/h2&gt;

&lt;p&gt;Log in as root with the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;wsl&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-u&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enter the username you want to reset in place of &lt;code&gt;[username]&lt;/code&gt; in the following command.&lt;br&gt;
You will be prompted for a new password, so reset it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;passwd&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>wsl</category>
      <category>windows</category>
      <category>beginners</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
