<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming &#8211; Rafael Bernard Araujo</title>
	<atom:link href="https://rafael.bernard-araujo.com/categoria/technology/programming/feed" rel="self" type="application/rss+xml" />
	<link>https://rafael.bernard-araujo.com</link>
	<description>desenvolvendo... while(!success){  try(); }</description>
	<lastBuildDate>Tue, 19 May 2026 00:58:20 +0000</lastBuildDate>
	<language>pt-BR</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">21941730</site>	<item>
		<title>Building Evolutionary Architectures &#8211; Chapter 2: Fitness Functions</title>
		<link>https://rafael.bernard-araujo.com/building-evolutionary-architectures-chapter-2-fitness-functions.php</link>
					<comments>https://rafael.bernard-araujo.com/building-evolutionary-architectures-chapter-2-fitness-functions.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Tue, 19 May 2026 00:58:20 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[fitness functions]]></category>
		<category><![CDATA[software architecture]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2376</guid>

					<description><![CDATA[Chapter 2 introduces the concept of architectural fitness functions, the mechanism that makes &#34;evolutionary&#34; more than a buzzword. The origin: borrowing from evolutionary computing The term comes from genetic algorithm design. In evolutionary computing, a fitness function defines what &#34;better&#34; means so that solutions can gradually emerge through small changes across generations. The classic example: [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Chapter 2 introduces the concept of <strong>architectural fitness functions</strong>, the mechanism that makes &quot;evolutionary&quot; more than a buzzword.</p>
<h2>The origin: borrowing from evolutionary computing</h2>
<p>The term comes from genetic algorithm design. In evolutionary computing, a fitness function defines what &quot;better&quot; means so that solutions can gradually emerge through small changes across generations. The classic example: when using a genetic algorithm to optimise wing design, the fitness function assesses wind resistance, weight, air flow, and other desirable characteristics. At each generation, the engineer asks: is this closer to or further away from the goal?</p>
<p>Ford, Parsons and Kua borrow this concept for software:</p>
<blockquote>
<p><strong>An architectural fitness function provides an objective integrity assessment of some architectural characteristic(s).</strong></p>
</blockquote>
<p>In software, fitness functions check that developers preserve important architectural characteristics; the &quot;-ilities&quot; architects care about: scalability, security, performance, maintainability, resilience.</p>
<h2>The core idea</h2>
<p>An evolutionary architecture supports <em>guided</em>, incremental change across multiple dimensions. The key word is <strong><em>guided</em></strong>. Without guidance, incremental change is just drift. Fitness functions are what provide the guidance.</p>
<p>The fitness function protects the various architectural characteristics required for the system. These requirements differ greatly across systems and organisations: some require intense security; others require significant throughput or low latency; others need resilience to failure. A crucial early architecture decision is to define which dimensions matter most for a given system, based on business drivers, technical capabilities, and scale.</p>
<h2>Why this matters</h2>
<p>Most teams have implicit architectural goals: &quot;the system should be fast&quot;, &quot;services should be loosely coupled&quot;, &quot;we should be secure&quot;. The problem is that implicit goals erode. Nobody notices the slow degradation until a characteristic has already failed.</p>
<p>Fitness functions make the implicit explicit. They turn architectural aspirations into verifiable checks. Automated where possible, manual where necessary.</p>
<p>A key insight: improving one architectural dimension can accidentally harm another. Improving performance with caching might harm data freshness or security. Fitness functions act as guardrails that detect these tradeoff violations before they reach production.</p>
<h2>Categorising fitness functions</h2>
<p>The book defines several dimensions for classifying fitness functions:</p>
<h3>Atomic vs Holistic</h3>
<ul>
<li><strong>Atomic</strong> — tests one particular aspect of the architecture in isolation. Example: a unit test checking for cyclic dependencies in a package, or a code metric that checks cyclomatic complexity.</li>
<li><strong>Holistic</strong> — tests a combination of architectural aspects, assessing interactions between different concerns. Example: testing the number of concurrent users within a certain latency range while caching is enabled — this simultaneously checks scalability and data freshness. Holistic functions are harder to build but capture what atomic ones miss.</li>
</ul>
<h3>Triggered vs Continuous vs Temporal</h3>
<ul>
<li><strong>Triggered</strong> — executed in response to a specific event: a developer running a unit test, a CI pipeline stage, a QA person performing exploratory testing.</li>
<li><strong>Continuous</strong> — constant verification of architectural aspects. Monitoring and alerting are the classic examples. Netflix's Chaos Monkey — which runs in production and randomly terminates instances — is a continuous holistic fitness function that forces teams to build resilient services.</li>
<li><strong>Temporal</strong> — have a particular time component. Example: a reminder to check whether important security updates have been performed, or a scheduled dependency check that alerts on outdated libraries.</li>
</ul>
<h3>Static vs Dynamic</h3>
<ul>
<li><strong>Static</strong> — fixed predefined acceptable values. Binary pass/fail (a unit test), or a threshold (latency must be &lt; 200ms).</li>
<li><strong>Dynamic</strong> — acceptable values depend on context. Acceptable latency might depend on actual system scale; security requirements might vary based on the regulatory environment.</li>
</ul>
<h3>Automated vs Manual</h3>
<ul>
<li><strong>Automated</strong> — unit tests, deployment pipeline checks, stress tests, chaos engineering. Ideally as much automation as possible.</li>
<li><strong>Manual</strong> — some things can't be automated (legal approval requirements, certain QA processes). Some things aren't automated <em>yet</em>. The goal is to push the boundary toward automation over time.</li>
</ul>
<h2>What fitness functions look like in practice</h2>
<p>Fitness functions encompass existing engineering practices but also extend beyond them:</p>
<table>
<thead>
<tr>
<th>Category</th>
<th>Examples</th>
<th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Architecture tests</strong></td>
<td>phpat (PHP/PHPStan) or ts-arch (TypeScript) rules checking component dependencies, layer violations, naming conventions, import directionality</td>
<td>Atomic, triggered</td>
</tr>
<tr>
<td><strong>Code metrics</strong></td>
<td>Cyclomatic complexity thresholds, afferent/efferent coupling limits</td>
<td>Atomic, triggered</td>
</tr>
<tr>
<td><strong>Contract tests</strong></td>
<td>API contract verification ensuring requirements are met</td>
<td>Atomic, triggered</td>
</tr>
<tr>
<td><strong>Security scanning</strong></td>
<td>Vulnerability scanning, licence compliance checks for open-source dependencies</td>
<td>Atomic, triggered</td>
</tr>
<tr>
<td><strong>Performance testing</strong></td>
<td>Load tests validating latency SLOs under expected concurrency</td>
<td>Holistic, triggered</td>
</tr>
<tr>
<td><strong>Monitoring &amp; alerting</strong></td>
<td>p99 latency monitors, error rate thresholds, SLO compliance dashboards</td>
<td>Atomic/holistic, continuous</td>
</tr>
<tr>
<td><strong>Chaos engineering</strong></td>
<td>Netflix Simian Army — randomly terminating instances, availability zones, or entire regions</td>
<td>Holistic, continuous</td>
</tr>
<tr>
<td><strong>Security reviews</strong></td>
<td>Quarterly security audits, penetration testing</td>
<td>Holistic, manual/temporal</td>
</tr>
<tr>
<td><strong>Dependency freshness</strong></td>
<td>Scheduled checks for outdated libraries or security patches</td>
<td>Atomic, temporal</td>
</tr>
</tbody>
</table>
<p><strong>The best fitness functions are</strong> <strong>automated and triggered</strong>: they give feedback at the point of change, not weeks later. Place them in the deployment pipeline. Fast atomic functions early, slow holistic functions later.</p>
<h2>Deployment pipelines as the enforcement mechanism</h2>
<p>Fitness functions only work if they're part of the delivery workflow. The deployment pipeline is where they live:</p>
<ol>
<li><strong>Early stages</strong> — fast, atomic checks: architecture tests (phpat, ts-arch), code metrics, linting, security scanning, contract tests.</li>
<li><strong>Middle stages</strong> — integration and performance tests, holistic triggered functions.</li>
<li><strong>Later stages / production</strong> — continuous monitoring, chaos engineering, temporal reminders.</li>
</ol>
<p>As Thoughtworks puts it: <em>&quot;creating the desired fitness functions — and including them in appropriate delivery pipelines — communicates these metrics as an important aspect of enterprise architecture.&quot;</em></p>
<h2>The four layers of fitness (from NILUS)</h2>
<p>A useful framing from practice splits fitness functions across four layers:</p>
<ol>
<li><strong>Structural fitness</strong> — code dependencies, database access patterns, API contracts, service boundaries.</li>
<li><strong>Behavioural fitness</strong> — latency, resilience, throughput, consistency, recovery behaviour.</li>
<li><strong>Operational fitness</strong> — deployment independence, observability coverage, runbook readiness, SLO compliance.</li>
<li><strong>Semantic fitness</strong> — bounded context integrity, event naming quality, policy ownership, domain model consistency.</li>
</ol>
<p>Most teams start at structural (the easiest to automate) and never reach semantic. But <strong>semantic fitness functions</strong> (checking that your domain model remains coherent as it evolves) <strong>are often the most valuable for long-lived systems</strong>.</p>
<h2>Systems thinking</h2>
<p>Dr. Russell Ackoff's quote captures the deeper point:</p>
<blockquote>
<p>A system is never the sum of its parts. It is the product of the interaction of its parts.</p>
</blockquote>
<p>Fitness functions that only measure individual components miss the point. The interesting failures happen at integration boundaries — between services, between teams, between intentions and reality. Holistic fitness functions (end-to-end latency, deployment frequency, change failure rate) capture what atomic ones cannot.</p>
<h2>How I'm applying this</h2>
<p>This connects directly to work I care about:</p>
<ul>
<li><strong>Platform modernisations</strong> I've designed and implemented were operational fitness: bringing reliability through automated deployment pipelines, observability and monitoring, and runbook readiness. I just called it &quot;keeping things running.&quot;</li>
<li><strong>ADRs</strong> capture the <em>decisions</em>. Fitness functions verify those decisions are still holding. Decisions and verification go hand in hand.</li>
<li><strong>Kent Beck's Test Desiderata</strong> is itself a fitness function for test quality — a checklist of characteristics that tests should exhibit (isolated, deterministic, fast, behavioural, structure-insensitive, specific, predictive).</li>
<li><strong>DORA metrics</strong> (deployment frequency, lead time, change failure rate, MTTR) are fitness functions for delivery capability.</li>
<li><strong>Code health metrics</strong> (as described in the Loveholidays case from <a href="https://rafael.bernard-araujo.com/tropecando-120.php">Tropeçando 120</a>) are fitness functions that enabled their AI-first shift — they invested in code health metrics <em>before</em> adopting AI, which is exactly the fitness-function-first approach.</li>
<li><strong>phpat</strong> (PHP, as a PHPStan extension) and <strong>ts-arch</strong> (TypeScript) — writing architecture rules as unit tests that run in CI is the purest implementation of triggered atomic fitness functions.</li>
</ul>
<p>The pattern: define what matters, measure it, enforce it automatically, and revisit periodically. Architecture that can't be verified can't evolve — it can only decay.</p>
<h2>Further reading</h2>
<ul>
<li><a href="https://martinfowler.com/articles/evo-arch-forward.html">Foreword to Building Evolutionary Architectures</a> — Martin Fowler's foreword to the book, framing fitness functions as the mechanism to monitor architectural state in an evolutionary style.</li>
<li><a href="https://www.thoughtworks.com/en-gb/insights/articles/fitness-function-driven-development">Fitness function-driven development</a> — Paula Paul &amp; Rosemary Wang apply the TDD mindset to architecture: write the fitness function first, then develop to pass it.</li>
<li><a href="https://martinfowler.com/articles/fitness-functions-data-products.html">Governing data products using fitness functions</a> — Extending fitness functions into Data Mesh governance (2024).</li>
<li><a href="https://www.thoughtworks.com/en-ca/insights/books/building-evolutionaryarchitectures-second-edition">Building Evolutionary Architectures, 2nd Edition</a> — The book.</li>
</ul>
<hr />
<p><em>Part of my reading notes on <a href="https://rafael.bernard-araujo.com/building-evolutionary-architectures-notes.php">Building Evolutionary Architectures</a> (Ford, Parsons, Kua).</em></p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/building-evolutionary-architectures-chapter-2-fitness-functions.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2376</post-id>	</item>
		<item>
		<title>Building Evolutionary Architectures Notes</title>
		<link>https://rafael.bernard-araujo.com/building-evolutionary-architectures-notes.php</link>
					<comments>https://rafael.bernard-araujo.com/building-evolutionary-architectures-notes.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Tue, 19 May 2026 00:30:15 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[software architecture]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2378</guid>

					<description><![CDATA[Notes Chapter 1: Software Architecture Despite our best efforts, software becomes harder to change over time. For a variety of reasons, the parts that comprise software systems defy easy modifications, becoming more brittle and intractable over time. Changes in software projects are usually driven by a reevaluation of functionality and/or scope. But another type of [&#8230;]]]></description>
										<content:encoded><![CDATA[<h2>Notes</h2>
<h3>Chapter 1: Software Architecture</h3>
<blockquote>
<p>Despite our best efforts, software becomes harder to change over time. For a variety of reasons, the parts that comprise software systems defy easy modifications, becoming more brittle and intractable over time. Changes in software projects are usually driven by a reevaluation of functionality and/or scope. But another type of change occurs outside the control of architects and long-term planners. Though architects like to be able to strategically plan for the future, the constantly changing software development ecosystem makes that difficult. Since we can't avoid change, we need to exploit it.</p>
</blockquote>
<p>— On <em>Evolutionary Architecture</em></p>
<blockquote>
<p>An evolutionary architecture supports guided, incremental changes across multiple dimensions.</p>
</blockquote>
<p>— Definition of <em>Evolutionary Architecture</em></p>
<h3>Related: Ralph Johnson on Architecture (via Fowler, 2003)</h3>
<p>These quotes from Ralph Johnson (from [[2026-04-24 - Martin Fowler - Who Needs an Architect|Who Needs an Architect?]]) are foundational to the ideas in this book:</p>
<blockquote>
<p>&quot;In most successful software projects, the expert developers working on that project have a shared understanding of the system design. This shared understanding is called 'architecture.' [...] the architecture only includes the components and interfaces that are understood by all the developers.&quot;</p>
</blockquote>
<p>— Architecture as a social construct, not a diagram.</p>
<blockquote>
<p>&quot;There is no theoretical reason that anything is hard to change about software. If you pick any one aspect of software then you can make it easy to change, but we don't know how to make everything easy to change. Making something easy to change makes the overall system a little more complex, and making everything easy to change makes the entire system very complex. Complexity is what makes software hard to change. That, and duplication.&quot;</p>
</blockquote>
<p>— The fundamental tension that evolutionary architectures try to navigate: change vs complexity.</p>
<blockquote>
<p>&quot;Software is not limited by physics, like buildings are. It is limited by imagination, by design, by organization. In short, it is limited by properties of people, not by properties of the world. 'We have met the enemy, and he is us.'&quot;</p>
</blockquote>
<p>— The constraint is us, not the technology.</p>
<h3>Chapter 2: Fitness Functions</h3>
<blockquote>
<p>An evolutionary architecture supports <em>guided</em>, incremental change across multiple dimensions.</p>
</blockquote>
<p>-- on FItness Functions, chapter 2</p>
<blockquote>
<p>The fitness function protects the various archutectural characteristics required for the system. The specific architectural requirements differ greatly across systems and organizations, based on business drivers, technical capabilities, and a host of other factors. Some systems require intense security; others require significant throughput factors.</p>
</blockquote>
<p>-- on Fitness Functions, chapter 2</p>
<blockquote>
<p>A system is never the sum of its parts. It is the product of the interaction of its parts.</p>
</blockquote>
<p>-- Dr. Russel Ackoff</p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/building-evolutionary-architectures-notes.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2378</post-id>	</item>
		<item>
		<title>Introduce Parameter Object &#124; Refactoring Patterns</title>
		<link>https://rafael.bernard-araujo.com/introduce-parameter-object-refactoring-patterns.php</link>
					<comments>https://rafael.bernard-araujo.com/introduce-parameter-object-refactoring-patterns.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Wed, 22 Apr 2026 05:45:10 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[refactoring patterns]]></category>
		<category><![CDATA[rust]]></category>
		<category><![CDATA[software architecture]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2357</guid>

					<description><![CDATA[This refactoring pattern involves grouping parameters that naturally go together into a single object. When you see a group of data items that regularly travel together, appearing in function after function, it's a sign they should be combined into a single object. Check https://rafael.bernard-araujo.com/refactoring-patterns/introduce-parameter-object There are PHP and Rust implemenation examples.]]></description>
										<content:encoded><![CDATA[<p>This refactoring pattern involves grouping parameters that naturally go together into a single object. When you see a group of data items that regularly travel together, appearing in function after function, it's a sign they should be combined into a single object.</p>
<p>Check <a href="https://rafael.bernard-araujo.com/refactoring-patterns/introduce-parameter-object">https://rafael.bernard-araujo.com/refactoring-patterns/introduce-parameter-object</a></p>
<p>There are PHP and Rust implemenation examples.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/introduce-parameter-object-refactoring-patterns.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2357</post-id>	</item>
		<item>
		<title>Building a Serverless PHP Application with Bref, Symfony, and DynamoDB Session Management</title>
		<link>https://rafael.bernard-araujo.com/building-a-serverless-php-application-with-bref-symfony-and-dynamodb-session-management.php</link>
					<comments>https://rafael.bernard-araujo.com/building-a-serverless-php-application-with-bref-symfony-and-dynamodb-session-management.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Tue, 30 Dec 2025 07:48:35 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2283</guid>

					<description><![CDATA[Introduction Serverless apps are fantastic for automatic scaling, but there’s a catch: they expect you to be stateless. Most web applications, however, rely on sessions to remember users and persist state. Traditional PHP session handlers store data on the filesystem, which doesn’t play nicely with ephemeral AWS Lambda instances. Your sessions vanish as soon as [&#8230;]]]></description>
										<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Serverless apps are fantastic for automatic scaling, but there’s a catch: they expect you to be stateless. Most web applications, however, rely on sessions to remember users and persist state. Traditional PHP session handlers store data on the filesystem, which doesn’t play nicely with ephemeral AWS Lambda instances. Your sessions vanish as soon as the instance disappears.</p>
<p>The usual fix? Fire up a Redis cluster. Works, but suddenly you’ve added infrastructure, ongoing maintenance, and extra costs. Your “serverless” app feels a lot less serverless.</p>
<p>What if we could manage sessions <strong>without touching Redis or any other server</strong>?</p>
<p>In this post, we’ll show you how to build a <strong>truly serverless PHP app</strong> using <strong>Bref</strong>, <strong>Symfony</strong>, and <strong>DynamoDB</strong> for session management. Along the way, you’ll see:</p>
<ul>
<li>A <strong>custom DynamoDB-backed session handler</strong> that replaces filesystem sessions</li>
<li>How to deploy your app via <strong>Lambda Function URLs</strong> using AWS CDK</li>
<li>Storing <strong>CSRF tokens in DynamoDB</strong> for fully stateless operation</li>
<li><strong>Single-table design patterns</strong> for efficient multi-entity storage</li>
</ul>
<p>By the end, you’ll know not just <em>how</em> to implement this architecture, but also <em>when</em> it makes sense and what trade-offs you’re accepting.</p>
<h2>The Challenge: Sessions in Serverless</h2>
<p>Before we dive into the solution, let’s understand why traditional PHP sessions fail in serverless environments.</p>
<ol>
<li><strong>Ephemeral Storage</strong>: Lambda instances can vanish at any time. Writing sessions to <code>/tmp</code> is like storing them in sand. They disappear when the instance is recycled.</li>
<li><strong>No Shared Filesystem</strong>: Each Lambda invocation runs on its own instance. User A’s session written by instance 1 is invisible to instance 2. That’s a problem if your user expects to stay logged in.</li>
<li><strong>Horizontal Scaling Woes</strong>: Lambda scales horizontally automatically. Without centralized session storage, each instance is isolated. Consistent session management? Forget it.</li>
</ol>
<h3>The Traditional Solution: Redis/ElastiCache</h3>
<p>Most serverless PHP guides suggest Redis. While it works, it comes with headaches:</p>
<ul>
<li><strong>Infrastructure complexity</strong>: VPCs, subnets, and security groups</li>
<li><strong>Maintenance burden</strong>: Patching, monitoring, capacity planning</li>
<li><strong>Cold start penalty</strong>: VPC-connected Lambdas can take 1–2 extra seconds</li>
</ul>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Better idea</strong>: DynamoDB. It’s fully managed, serverless, and scales automatically. No Redis cluster, no maintenance, just pay for what you use.</p>
<h2>Books and Authors App (Serverless Style)</h2>
<p>Imagine you’re building a multi-tenant SaaS app, like an internal tool for managing books and authors. Each user needs a session, and each organization manages its own data. DynamoDB’s single-table design can elegantly handle all this. Serverless scaling takes care of traffic spikes automatically.</p>
<p>Here’s what this example demonstrates:</p>
<ul>
<li><strong>Multi-entity relationships</strong>: Books belong to authors</li>
<li><strong>CRUD operations</strong>: Create, read, update, and delete across related entities</li>
<li><strong>Session-dependent workflows</strong>: Adding/editing books requires authentication</li>
<li><strong>Real-world complexity</strong>: More than a simple counter, less than a full e-commerce platform</li>
</ul>
<h3>Connecting to Real Use Cases</h3>
<p>This architecture shines in scenarios like:</p>
<ul>
<li><strong>Unpredictable traffic</strong>: Seasonal spikes when authors release new books</li>
<li><strong>Session management</strong>: Authors need persistent sessions to edit content</li>
<li><strong>Cost efficiency</strong>: During quiet periods, you pay pennies; during spikes, DynamoDB scales automatically</li>
<li><strong>Zero maintenance</strong>: No Redis clusters to monitor, no database servers to patch</li>
</ul>
<p>The book management example proves that this approach isn’t just theoretical: it’s production-ready.</p>
<h2>Architecture Overview</h2>
<p>To build a serverless PHP application that supports sessions, CSRF protection, and persistent data, we follow a <strong>stateful/stateless separation</strong> pattern. This makes the architecture scalable, cost-efficient, and easy to maintain.</p>
<h3>1. Stateful Layer: Persistent Data</h3>
<p>This layer is responsible for storing all data that needs to survive beyond a single Lambda invocation.</p>
<ul>
<li>
<p><strong>DynamoDB Table</strong></p>
<ul>
<li>Uses a <strong>single-table design</strong> to store sessions, CSRF tokens, users, books, and authors.</li>
<li><strong>TTL enabled</strong> for automatic session expiration.</li>
<li><strong>On-demand billing</strong> ensures automatic scaling with traffic.</li>
<li>Built-in <strong>multi-AZ replication</strong> provides high availability.</li>
</ul>
</li>
<li>
<p><strong>Benefits</strong></p>
<ul>
<li>No infrastructure to manage or patch.</li>
<li>Automatically scales with unpredictable traffic.</li>
<li>Centralized storage simplifies queries and operations.</li>
</ul>
</li>
</ul>
<h3>2. Stateless Layer: Application Logic</h3>
<p>This layer runs the application code and handles requests without storing any persistent state locally.</p>
<ul>
<li>
<p><strong>Lambda Function</strong></p>
<ul>
<li>Runs <strong>PHP-FPM</strong> via Bref.</li>
<li>Handles HTTP requests directly using a <strong>Lambda Function URL</strong> (HTTPS endpoint).</li>
<li>No VPC required to access DynamoDB, reducing cold start latency.</li>
</ul>
</li>
<li>
<p><strong>Static Assets</strong></p>
<ul>
<li>Stored in <strong>S3</strong> (optionally served via CloudFront) to keep Lambda stateless.</li>
</ul>
</li>
<li>
<p><strong>Benefits</strong></p>
<ul>
<li>Scales automatically with traffic.</li>
<li>Cost-efficient: pay only for actual requests.</li>
<li>Stateless logic simplifies deployment and updates.</li>
</ul>
</li>
</ul>
<p>This design ensures a <strong>truly serverless PHP application</strong> that handles session state, persistent data, and scalable workloads without the operational overhead of managing Redis or other caching layers.</p>
<h2>DynamoDB Session Handler and CSRF Implementation</h2>
<h3>Session Handler Implementation</h3>
<p>In a serverless PHP application, traditional session storage (files or local memory) doesn’t work because Lambda functions are <strong>ephemeral</strong>. Each invocation may run on a different container, so we need a centralized, persistent session store.</p>
<p>The core of our solution is a custom session handler that implements PHP's <code>SessionHandlerInterface</code>.</p>
<h4>How It Works</h4>
<ul>
<li><strong>Sessions are stored in DynamoDB</strong> instead of the filesystem.</li>
<li>Each session has a unique <code>session_id</code>, which becomes the partition key (<code>PK</code>) in DynamoDB.</li>
<li>Sessions include the serialized PHP session data and an <strong>expiration timestamp</strong> (TTL).</li>
<li>The handler automatically reads/writes session data on <code>session_start()</code> and <code>session_write_close()</code>.</li>
</ul>
<h4>Key Features</h4>
<ol>
<li><strong>Automatic Expiration</strong>
<ul>
<li>DynamoDB TTL ensures sessions are removed automatically after expiration.</li>
</ul>
</li>
<li><strong>Atomic Operations</strong>
<ul>
<li><code>PutItem</code> and <code>UpdateItem</code> guarantee consistent writes, even with concurrent requests.</li>
</ul>
</li>
<li><strong>Scalable</strong>
<ul>
<li>Can handle thousands of concurrent sessions without extra infrastructure.</li>
</ul>
</li>
<li><strong>Serverless-friendly</strong>
<ul>
<li>No local storage, no Redis, fully compatible with Lambda statelessness.</li>
</ul>
</li>
</ol>
<h4>Implementation</h4>
<pre><code class="language-php">&lt;?php

namespace App\Session;

use AsyncAws\DynamoDb\DynamoDbClient;
use AsyncAws\DynamoDb\Input\DeleteItemInput;
use AsyncAws\DynamoDb\Input\GetItemInput;
use AsyncAws\DynamoDb\Input\PutItemInput;
use AsyncAws\DynamoDb\ValueObject\AttributeValue;

/**
 * A minimal DynamoDB-backed PHP session handler using AsyncAws.
 *
 * Table design (single-table compatible):
 *  - PK: &quot;SESSION&quot;
 *  - SK: &quot;SID#&lt;session_id&gt;&quot;
 *  - data: base64-encoded session payload (string)
 *  - expiresAt: unix epoch seconds (number), enable DynamoDB TTL on this attribute
 *
 * Garbage collection is handled by DynamoDB&#039;s TTL, so gc() is a no-op.
 */
class DynamoDbSessionHandler implements \SessionHandlerInterface
{
    private const string PK_VALUE = &#039;SESSION&#039;;
    private const string SK_PREFIX = &#039;SID#&#039;;

    public function __construct(
        private readonly DynamoDbClient $dynamoDb,
        private readonly string $tableName,
        private readonly int $ttlSeconds = 3600,
    ) {}

    public function open(string $path, string $name): bool
    {
        // Nothing to do
        return true;
    }

    public function close(): bool
    {
        // Nothing to do
        return true;
    }

    public function read(string $id): string
    {
        $result = $this-&gt;dynamoDb-&gt;getItem(new GetItemInput([
            &#039;TableName&#039; =&gt; $this-&gt;tableName,
            &#039;Key&#039; =&gt; [
                &#039;PK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::PK_VALUE]),
                &#039;SK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::SK_PREFIX . $id]),
            ],
            // Strongly consistent read to reduce stale sessions
            &#039;ConsistentRead&#039; =&gt; true,
        ]));

        $item = $result-&gt;getItem();
        if (!$item || !isset($item[&#039;data&#039;])) {
            return &#039;&#039;;
        }

        $encoded = $item[&#039;data&#039;]-&gt;getS();
        if ($encoded === null) {
            return &#039;&#039;;
        }

        $payload = base64_decode($encoded, true);
        return $payload === false ? &#039;&#039; : $payload;
    }

    public function write(string $id, string $data): bool
    {
        $expiresAt = time() + $this-&gt;ttlSeconds;

        $this-&gt;dynamoDb-&gt;putItem(new PutItemInput([
            &#039;TableName&#039; =&gt; $this-&gt;tableName,
            &#039;Item&#039; =&gt; [
                &#039;PK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::PK_VALUE]),
                &#039;SK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::SK_PREFIX . $id]),
                &#039;data&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; base64_encode($data)]),
                &#039;expiresAt&#039; =&gt; new AttributeValue([&#039;N&#039; =&gt; (string) $expiresAt]),
            ],
        ]));

        return true;
    }

    public function destroy(string $id): bool
    {
        $this-&gt;dynamoDb-&gt;deleteItem(new DeleteItemInput([
            &#039;TableName&#039; =&gt; $this-&gt;tableName,
            &#039;Key&#039; =&gt; [
                &#039;PK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::PK_VALUE]),
                &#039;SK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::SK_PREFIX . $id]),
            ],
        ]));

        return true;
    }

    public function gc(int $max_lifetime): int|false
    {
        // Rely on DynamoDB TTL to expire items; nothing to scan/delete here.
        return 0;
    }
}</code></pre>
<h4>Why it matters?</h4>
<p>This approach:</p>
<ul>
<li>Keeps your PHP sessions serverless-compatible.</li>
<li>Avoids cold-start pitfalls associated with local or in-memory session storage.</li>
<li>Provides a reliable, scalable, and fully managed solution for stateful data in a stateless environment.</li>
</ul>
<h3>CSRF Token Storage</h3>
<p>In a serverless environment, CSRF tokens must be handled carefully. Because Lambda executions are stateless, tokens cannot be stored in memory or on the filesystem. Instead, CSRF tokens are persisted in DynamoDB alongside session data.</p>
<p>This approach ensures tokens remain valid and verifiable across multiple Lambda invocations.</p>
<h4>How CSRF Tokens Are Stored</h4>
<p>Each CSRF token is stored as a dedicated item in the DynamoDB table:</p>
<ul>
<li>Tokens are associated with a specific action</li>
<li>Each token has a unique identifier</li>
<li>An expiration timestamp is stored for automatic cleanup</li>
</ul>
<p>This makes CSRF token storage consistent, durable, and serverless-compatible.</p>
<h4>Data Model</h4>
<p>CSRF tokens follow the same single-table design pattern used elsewhere in the application.</p>
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>PK</td>
<td><code>CSRF</code></td>
</tr>
<tr>
<td>SK</td>
<td><code>TOKEN#&lt;token_id&gt;</code></td>
</tr>
<tr>
<td>session</td>
<td><code>&lt;session_id&gt;</code></td>
</tr>
<tr>
<td>expiresAt</td>
<td><code>&lt;timestamp&gt;</code></td>
</tr>
</tbody>
</table>
<p>Using a distinct partition key avoids contention and allows tokens to scale independently from session traffic.</p>
<h4>Lifecycle</h4>
<ol>
<li>A CSRF token is generated when a form is rendered.</li>
<li>The token is persisted in DynamoDB.</li>
<li>On form submission, the token is retrieved and validated.</li>
<li>After validation or expiration, the token is deleted or allowed to expire via TTL.</li>
</ol>
<p>This lifecycle mirrors traditional CSRF handling while remaining compatible with Lambda’s</p>
<h4>Implementation</h4>
<pre><code class="language-php">class DynamoDbCsrfTokenStorage implements CsrfTokenStorageInterface
{
    private const string PK_VALUE = &#039;CSRF&#039;;
    private const string SK_PREFIX = &#039;TOKEN#&#039;;

    public function getToken(string $tokenId): string
    {
        $result = $this-&gt;dynamoDb-&gt;getItem(new GetItemInput([
            &#039;TableName&#039; =&gt; $this-&gt;tableName,
            &#039;Key&#039; =&gt; [
                &#039;PK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::PK_VALUE]),
                &#039;SK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::SK_PREFIX . $tokenId]),
            ],
        ]));

        $item = $result-&gt;getItem();
        return $item[&#039;value&#039;]-&gt;getS() ?? &#039;&#039;;
    }

    public function setToken(string $tokenId, string $token): void
    {
        $expiresAt = time() + $this-&gt;ttlSeconds;

        $this-&gt;dynamoDb-&gt;putItem(new PutItemInput([
            &#039;TableName&#039; =&gt; $this-&gt;tableName,
            &#039;Item&#039; =&gt; [
                &#039;PK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::PK_VALUE]),
                &#039;SK&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; self::SK_PREFIX . $tokenId]),
                &#039;value&#039; =&gt; new AttributeValue([&#039;S&#039; =&gt; $token]),
                &#039;expiresAt&#039; =&gt; new AttributeValue([&#039;N&#039; =&gt; (string) $expiresAt]),
            ],
        ]));
    }
}</code></pre>
<p>This ensures CSRF protection works seamlessly across multiple Lambda invocations.</p>
<h2>Symfony Configuration</h2>
<p>Configuring Symfony correctly is key for serverless PHP apps to work reliably with Lambda, DynamoDB, and Bref. Here’s how we set it up.</p>
<h3>1. Session Storage</h3>
<p>We replace the default PHP session handler with our <strong>DynamoDBSessionHandler</strong>:</p>
<pre><code class="language-yaml"># config/packages/framework.yaml
framework:
    session:
        handler_id: App\Session\DynamoDBSessionHandler
        cookie_secure: auto
        cookie_samesite: lax
        cookie_lifetime: 3600  # 1 hour</code></pre>
<p>Notes:</p>
<ul>
<li><code>handler_id</code> points to our custom service.</li>
<li><code>cookie_secure: auto</code> ensures HTTPS enforcement on Lambda URLs or custom domains.</li>
<li><code>cookie_lifetime</code> aligns with DynamoDB TTL for consistency.</li>
</ul>
<h3>2. Service definition</h3>
<p>Register the DynamoDB session handler as a Symfony service:</p>
<pre><code class="language-yaml"># config/services.yaml
services:
  App\Session\DynamoDbSessionHandler:
    arguments:
      $tableName: &#039;%book_table_name%&#039;
      $ttlSeconds: &#039;%env(default:session_ttl_seconds:int:SESSION_TTL)%&#039;</code></pre>
<ul>
<li><code>$tableName</code> comes from environment variables to support multiple environments.</li>
<li><code>$ttl</code> matches the session lifetime for automatic garbage collection.<br />
This configuration tells Symfony to use our custom handler for all session operations. The handler is automatically injected with the DynamoDB client through Symfony's autowiring.</li>
</ul>
<h3>3. RequestContextListener</h3>
<p>To handle dynamic Lambda Function URLs, we register a listener:</p>
<pre><code class="language-yaml"># config/services.yaml
services:
    App\EventListener\RequestContextListener:
        tags:
            - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }</code></pre>
<p>Purpose:</p>
<ul>
<li>Ensures Symfony’s URL generator produces correct URLs.</li>
<li>Sets proper scheme and host for redirects, forms, and CSRF validation.</li>
<li>Essential for Lambda Function URLs where host/scheme changes per invocation.</li>
</ul>
<h4>Why It’s Needed</h4>
<p>Lambda Function URLs:</p>
<ul>
<li>Provide a direct HTTPS endpoint (e.g., <code>https://xyz.lambda-url.us-east-1.on.aws/</code>)</li>
<li>Are <strong>dynamic</strong> and unknown at build time</li>
<li>Require Symfony to know the <strong>scheme and host</strong> at runtime to generate correct URLs</li>
</ul>
<p>Without a listener:</p>
<ul>
<li>Redirects may point to HTTP instead of HTTPS</li>
<li>CSRF tokens may fail</li>
<li>Session cookies might be rejected</li>
<li>OAuth or SSO integrations could break</li>
</ul>
<h4>Implementation</h4>
<pre><code class="language-php">&lt;?php

namespace App\EventListener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RequestContext;

#[AsEventListener(event: KernelEvents::REQUEST, priority: 1024)]
class RequestContextListener
{
    public function __construct(private RequestContext $requestContext) {}

    public function onKernelRequest(RequestEvent $event): void
    {
        if (!$event-&gt;isMainRequest()) {
            return;
        }

        $request = $event-&gt;getRequest();

        // Force HTTPS for Lambda URLs and set proper context
        if ($request-&gt;headers-&gt;get(&#039;host&#039;) &amp;&amp; str_contains($request-&gt;headers-&gt;get(&#039;host&#039;), &#039;lambda-url&#039;)) {
            $this-&gt;requestContext-&gt;setScheme(&#039;https&#039;);
            $this-&gt;requestContext-&gt;setHost($request-&gt;headers-&gt;get(&#039;host&#039;));
            $this-&gt;requestContext-&gt;setHttpPort(80);
            $this-&gt;requestContext-&gt;setHttpsPort(443);

            $request-&gt;server-&gt;set(&#039;HTTPS&#039;, &#039;on&#039;);
            $request-&gt;server-&gt;set(&#039;SERVER_PORT&#039;, 443);
            $request-&gt;server-&gt;set(&#039;REQUEST_SCHEME&#039;, &#039;https&#039;);
        }
    }
}</code></pre>
<p><strong>The CDK Output Dilemma:</strong></p>
<pre><code class="language-typescript">// CDK can output the Lambda URL after deployment
new CfnOutput(this, &#039;LambdaURL&#039;, { 
    value: statelessStack.monolithLambdaFunctionUrl.url 
});
// But this value is only known AFTER deployment completes
// You can&#039;t use it as an environment variable in the SAME deployment</code></pre>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Tip: This listener is only necessary if you use the Lambda Function URL as the production endpoint. If you use a custom domain, this can be simplified or skipped.</p>
<p>With this configuration, Symfony becomes serverless-ready, maintaining sessions, CSRF protection, and routing behavior seamlessly while leveraging DynamoDB and Lambda.</p>
<h2>Single-Table Design Pattern</h2>
<p>All entities (sessions, CSRF tokens, users, books, authors) live in a <strong>single DynamoDB table</strong>. This simplifies the architecture and enables atomic operations across different entities.</p>
<table>
<thead>
<tr>
<th>Entity</th>
<th>PK</th>
<th>SK</th>
</tr>
</thead>
<tbody>
<tr>
<td>Session</td>
<td><code>SESSION</code></td>
<td><code>SID#&lt;session_id&gt;</code></td>
</tr>
<tr>
<td>CSRF Token</td>
<td><code>CSRF</code></td>
<td><code>TOKEN#&lt;token_id&gt;</code></td>
</tr>
<tr>
<td>Book</td>
<td><code>BOOK-METADATA</code></td>
<td><code>AUTHOR#&lt;author_id&gt;#BOOK#&lt;book_id&gt;</code></td>
</tr>
<tr>
<td>Author</td>
<td><code>AUTHOR-METADATA</code></td>
<td><code>AUTHOR#&lt;author_id&gt;</code></td>
</tr>
<tr>
<td>User</td>
<td><code>USER</code></td>
<td><code>EMAIL#&lt;email&gt;</code></td>
</tr>
</tbody>
</table>
<ul>
<li><strong>Why single-table?</strong>
<ul>
<li>Reduces infrastructure complexity.</li>
<li>Simplifies monitoring and backup.</li>
<li>Supports atomic transactions across multiple entity types.</li>
<li>Aligns with AWS best practices for DynamoDB.</li>
</ul>
</li>
</ul>
<h2>AWS CDK Infrastructure with Bref</h2>
<p>Deploying a serverless Symfony app requires some AWS setup. Using <strong>AWS CDK</strong> with <strong>Bref</strong> makes this smooth, maintainable, and repeatable.</p>
<h3>Why CDK?</h3>
<ul>
<li><strong>Infrastructure as code</strong>: Everything is versioned and reproducible.</li>
<li><strong>Integration with Symfony</strong>: Easy to link environment variables, DynamoDB, and Lambda functions.</li>
<li><strong>Bref-friendly</strong>: Deploy PHP Lambda layers without manually configuring Lambda functions.</li>
</ul>
<h3>Stateful Stack: DynamoDB Table</h3>
<pre><code class="language-ts">import { NestedStack } from &quot;aws-cdk-lib&quot;;
import * as ddb from &quot;aws-cdk-lib/aws-dynamodb&quot;;

export class BlogAppStatefulStack extends NestedStack {
  public readonly ddb: ddb.Table;

  constructor(scope: Construct, id: string, props: MyNestedStackProps) {
    super(scope, id, props);

    this.ddb = new ddb.Table(this, &#039;ddb&#039;, {
      tableName: `${id}-table`,
      partitionKey: { name: &#039;PK&#039;, type: ddb.AttributeType.STRING },
      sortKey: { name: &#039;SK&#039;, type: ddb.AttributeType.STRING },
      billingMode: ddb.BillingMode.PAY_PER_REQUEST,
      deletionProtection: props.shared.environment === &#039;prod&#039;,
      timeToLiveAttribute: &#039;expiresAt&#039;,
    });
  }
}</code></pre>
<p>Key features:</p>
<ul>
<li><strong>Generic Key Schema</strong>: <code>PK</code> and <code>SK</code> enable single-table design</li>
<li><strong>TTL Enabled</strong>: <code>expiresAt</code> attribute automatically removes expired items</li>
<li><strong>Production Protection</strong>: Deletion protection enabled for production environments</li>
</ul>
<h3>Stateless Stack: Lambda Function with Bref</h3>
<pre><code class="language-ts">import { packagePhpCode, PhpFpmFunction } from &quot;@bref.sh/constructs&quot;;
import * as lambda from &quot;aws-cdk-lib/aws-lambda&quot;;
import { FunctionUrl } from &quot;aws-cdk-lib/aws-lambda&quot;;

export class BlogAppStatelessStack extends NestedStack {
  public readonly monolithLambda: PhpFpmFunction;
  public monolithLambdaFunctionUrl: FunctionUrl;

  private createLambda(props: MyNestedStackProps, staticAssetsBucket: Bucket, ddb: ddb.Table) {
    const lambdaEnvironment = {
      APP_ENV: props.shared.environment,
      APP_SECRET: appSecret,
      ASSET_URL: `https://${staticAssetsBucket.bucketDomainName}/`,
      AWS_LAMBDA_LOG_FORMAT: &#039;text&#039;,
      BOOK_TABLE_NAME: `${props.shared.stackPrefix}-StatefulStack-table`,
    };

    const monolithLambda = new PhpFpmFunction(this, &#039;App&#039;, {
      handler: &#039;public/index.php&#039;,
      phpVersion: &#039;8.4&#039;,
      code: packagePhpCode(&#039;php&#039;, {
        exclude: [&#039;.env.local&#039;, &#039;bin/&#039;],
      }),
      functionName: `${props.shared.stackPrefix}-App`,
      timeout: Duration.seconds(28),
      memorySize: Size.gibibytes(2).toMebibytes(),
      environment: lambdaEnvironment,
    });

    // Create Function URL with no authentication
    const monolithLambdaFunctionUrl = monolithLambda.addFunctionUrl({ 
      authType: lambda.FunctionUrlAuthType.NONE 
    });

    // Grant DynamoDB permissions
    ddb.grantReadWriteData(monolithLambda);

    return { monolithLambda, monolithLambdaFunctionUrl };
  }
}</code></pre>
<h3>Lambda Function URL Configuration</h3>
<p>Lambda Function URLs provide a simple HTTPS endpoint without needing API Gateway:</p>
<pre><code class="language-ts">const monolithLambdaFunctionUrl = monolithLambda.addFunctionUrl({ 
  authType: lambda.FunctionUrlAuthType.NONE 
});</code></pre>
<p><strong>Benefits of Lambda URLs:</strong></p>
<ul>
<li><strong>Simplicity</strong>: Direct HTTPS endpoint without API Gateway complexity</li>
<li><strong>Cost</strong>: No API Gateway charges</li>
<li><strong>Performance</strong>: One less hop in the request path</li>
<li><strong>Built-in HTTPS</strong>: Automatic TLS certificate management</li>
</ul>
<p><strong>Configuration Options:</strong></p>
<ul>
<li><code>authType: NONE</code>: Public access (suitable for web applications)</li>
<li><code>authType: AWS_IAM</code>: Requires AWS signature (for service-to-service communication)</li>
</ul>
<h3>Main Stack: Orchestration</h3>
<pre><code class="language-ts">export class BlogApp extends Stack {
  constructor(scope: Construct, id: string, props: MyStackProps) {
    super(scope, id, props);

    const stackPrefix = props.shared.envStackPrefix;

    const statefulStack = new BlogAppStatefulStack(
      this, `${stackPrefix}-StatefulStack`, props
    );

    const statelessStack = new BlogAppStatelessStack(
      this, `${stackPrefix}-StatelessStack`, props, statefulStack
    );

    // Output important values
    new CfnOutput(this, &#039;Lambda&#039;, { 
      value: statelessStack.monolithLambda.functionName 
    });
    new CfnOutput(this, &#039;LambdaURL&#039;, { 
      value: statelessStack.monolithLambdaFunctionUrl.url 
    });
    new CfnOutput(this, &#039;DynamoDb&#039;, { 
      value: statefulStack.ddb.tableName 
    });
  }
}</code></pre>
<h3>Deployment with CDK</h3>
<p>With the infrastructure defined, deploying the application becomes a repeatable and predictable process. This section focuses on <strong>how the application is built, deployed, and updated</strong> using AWS CDK.</p>
<h4>Local Development Environment</h4>
<p>Local development mirrors the production setup as closely as possible while remaining lightweight.</p>
<ul>
<li>Docker is used to provide a consistent PHP environment.</li>
<li>A Makefile abstracts common commands to reduce cognitive load.</li>
<li>Symfony runs locally with the same session and configuration logic used in Lambda.</li>
</ul>
<p>You can run:</p>
<pre><code class="language-bash"># Pre-requisite - source your aws profile
make up</code></pre>
<p>You can check logs via <code>make logs</code>. And get into the container with <code>make bash</code>. The application will be available at <code>http://localhost:8000</code>, but it might fail to load as there is no existent DynamoDB to connect with. You can check local <code>.env</code> file for environment variables.</p>
<h4>Deploying</h4>
<p>Deploy the application using standard CDK commands (inside the container):</p>
<pre><code class="language-bash"># Pre-requisite - Bootstrap CDK if this is your first deployment - npx cdk bootstrap aws://&lt;ACCOUNT_ID&gt;/&lt;REGION&gt;
# Install dependencies
npm run deploy</code></pre>
<p>Alternatively, you can use the <code>Makefile</code> command outsite the container:</p>
<pre><code class="language-bash">make deploy</code></pre>
<h4>What Gets Created</h4>
<p>The deployment creates:</p>
<ol>
<li>DynamoDB table with TTL enabled</li>
<li>Lambda function with PHP 8.4 runtime (via Bref)</li>
<li>Lambda Function URL for HTTPS access</li>
<li>S3 bucket for static assets</li>
<li>IAM roles and permissions</li>
</ol>
<p>The output should be similar to:</p>
<pre><code class="language-bash">BlogApp (sandbox-blog-app): deploying... [1/1]
sandbox-blog-app: creating CloudFormation changeset...

 &#x2705;  BlogApp (sandbox-blog-app)

&#x2728;  Deployment time: 148.76s

Outputs:
BlogApp.AssetsBucket = sandbox-blog-app-sandboxbloga-assetsbucket5cb76180-5lu45xsqvuym
BlogApp.DynamoDb = sandbox-BlogApp-StatefulStack-table
BlogApp.Lambda = sandbox-BlogApp-App
BlogApp.LambdaURL = https://kiv7utcwku6gihqgs4bfkeuzma0oaylo.lambda-url.us-east-1.on.aws/
Stack ARN:
arn:aws:cloudformation:us-east-1:973974862728:stack/sandbox-blog-app/9ba92580-e50b-11f0-a602-0afffb8dc1a9

&#x2728;  Total time: 163.03s</code></pre>
<p>In this case, <code>https://kiv7utcwku6gihqgs4bfkeuzma0oaylo.lambda-url.us-east-1.on.aws/</code> is the Lambda public URL.</p>
<p>When you access the URL, you will see a log-in form. You can use the &quot;Register&quot; link to create a login. Use it and you will be able to manage Authors and Books. Try to log out and access the pages directly.</p>
<p><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/rafael.bernard-araujo.com/wp-content/uploads/2025/12/301225-1.png?w=580&#038;ssl=1" alt="Login" /></p>
<p><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/rafael.bernard-araujo.com/wp-content/uploads/2025/12/301225-2.png?w=580&#038;ssl=1" alt="Register" /></p>
<p><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/rafael.bernard-araujo.com/wp-content/uploads/2025/12/301225-3.png?w=580&#038;ssl=1" alt="Main" /></p>
<p>Internally it will execute a series of commands:</p>
<pre><code class="language-bash"># clean
npm run clean &amp;&amp; \
# execute php packaging including composer install and npm build for symfony
npm run package:sandbox &amp;&amp; \ 
# deploy as a sandbox not requiring approval
NODE_ENV=sandbox cdk deploy --require-approval never</code></pre>
<p>There is a prod version executing <code>make deploy:prod</code>.</p>
<h3>Testing the Session Implementation</h3>
<p>The application includes a test endpoint to verify session persistence:</p>
<pre><code class="language-php">#[Route(&#039;/session-test&#039;, name: &#039;session_test&#039;)]
public function test(Request $request): JsonResponse
{
    $session = $request-&gt;getSession();
    $counter = $session-&gt;get(&#039;counter&#039;, 0);
    $session-&gt;set(&#039;counter&#039;, $counter + 1);

    return new JsonResponse([
        &#039;message&#039; =&gt; &#039;Session test&#039;,
        &#039;session_id&#039; =&gt; $session-&gt;getId(),
        &#039;counter&#039; =&gt; $session-&gt;get(&#039;counter&#039;),
        &#039;handler&#039; =&gt; get_class($session-&gt;getMetadataBag()-&gt;getMetadata(&#039;handler&#039;)),
    ]);
}</code></pre>
<p>Test with curl:</p>
<pre><code class="language-bash"># First request creates session
curl -i -c cookie.txt https://your-lambda-url/session-test

# Subsequent requests increment counter
curl -i -b cookie.txt https://your-lambda-url/session-test
curl -i -b cookie.txt https://your-lambda-url/session-test

# outputs
➜ curl -i -c cookie.txt https://your-lambda-url/session-test
{&quot;message&quot;:&quot;Session incremented&quot;,&quot;session_id&quot;:&quot;02b0c08e1ccd5f3ea015a06c69e29d11&quot;,&quot;counter&quot;:1,&quot;handler&quot;:&quot;App\\Session\\DynamoDbSessionHandler&quot;}%

➜ curl -i -b cookie.txt https://your-lambda-url/session-test
{&quot;message&quot;:&quot;Session incremented&quot;,&quot;session_id&quot;:&quot;02b0c08e1ccd5f3ea015a06c69e29d11&quot;,&quot;counter&quot;:2,&quot;handler&quot;:&quot;App\\Session\\DynamoDbSessionHandler&quot;}%

➜ curl -i -b cookie.txt https://your-lambda-url/session-test
{&quot;message&quot;:&quot;Session incremented&quot;,&quot;session_id&quot;:&quot;02b0c08e1ccd5f3ea015a06c69e29d11&quot;,&quot;counter&quot;:3,&quot;handler&quot;:&quot;App\\Session\\DynamoDbSessionHandler&quot;}%</code></pre>
<h2>Performance Considerations</h2>
<h3>Cold Start Optimization</h3>
<ol>
<li><strong>Memory Allocation</strong>: Using 2GB memory reduces cold start times</li>
<li><strong>Composer Optimization</strong>: <code>--no-dev --optimize-autoloader</code> reduces code size</li>
<li><strong>PHP 8.4</strong>: Latest PHP version with JIT compiler support</li>
</ol>
<h3>DynamoDB Performance</h3>
<ol>
<li><strong>Consistent Reads</strong>: Ensures session consistency at the cost of slightly higher latency</li>
<li><strong>On-Demand Billing</strong>: No capacity planning, automatic scaling</li>
<li><strong>TTL</strong>: Automatic cleanup without scan operations</li>
</ol>
<p>The serverless model's primary advantage is alignment of costs with actual usage, particularly beneficial for applications with variable or unpredictable traffic patterns. However, actual costs vary significantly based on traffic patterns, request complexity, and specific use cases. It's recommended to use AWS cost estimation tools and monitor actual usage to understand the financial impact for your specific application.</p>
<h2>Security Best Practices</h2>
<h3>Session Security</h3>
<ol>
<li><strong>Secure Flag</strong>: Ensures cookies only sent over HTTPS</li>
<li><strong>SameSite</strong>: Protects against CSRF attacks</li>
<li><strong>Regenerate ID</strong>: After authentication to prevent session fixation</li>
</ol>
<pre><code class="language-yaml">framework:
    session:
        cookie_httponly: true
        cookie_secure: auto
        cookie_samesite: lax</code></pre>
<h3>DynamoDB Permissions</h3>
<p>The Lambda function requires minimal permissions:</p>
<pre><code class="language-typescript">ddb.grantReadWriteData(monolithLambda);</code></pre>
<p>This grants only:</p>
<ul>
<li><code>dynamodb:GetItem</code></li>
<li><code>dynamodb:PutItem</code></li>
<li><code>dynamodb:DeleteItem</code></li>
<li><code>dynamodb:Query</code></li>
<li><code>dynamodb:Scan</code></li>
</ul>
<p>No administrative permissions are granted to the Lambda function.</p>
<h2>Limitations</h2>
<p>While serverless PHP with DynamoDB sessions offers compelling advantages, it's important to understand the limitations and trade-offs. Here's an honest assessment of where this architecture may not be the best fit:</p>
<h3>1. Cold Start Latency</h3>
<p><strong>The Reality</strong>: Lambda cold starts can add <strong>1-3 seconds</strong> to the first request after a function has been idle. In practice this occurs for less than 1% of the calls.</p>
<p><strong>Mitigation Strategies</strong>:</p>
<ul>
<li><strong>Provisioned Concurrency</strong>: Pre-warm Lambda instances to eliminate cold starts (adds ~$15/month per instance)</li>
<li><strong>Keep-Warm Pings</strong>: Use CloudWatch Events to invoke functions every 5-10 minutes (adds minimal cost but doesn't help with scaling)</li>
<li><strong>Larger Memory Allocation</strong>: We use 2GB memory which provides faster CPUs, reducing cold start duration</li>
<li><strong>Optimize Code</strong>: Minimize dependencies, use PHP preloading, optimize autoloader</li>
</ul>
<p><strong>When it's acceptable</strong>: Background jobs, internal tools, APIs with relaxed SLAs<br />
<strong>When it's problematic</strong>: User-facing e-commerce, real-time chat, gaming applications</p>
<h3>2. Request Timeout Constraints</h3>
<p><strong>The Reality</strong>: Our configuration uses <strong>28 seconds timeout</strong> (API Gateway compatible), though Lambda supports up to <strong>15 minutes</strong>, which Lambda URLs supports.</p>
<p><strong>Not Suitable For</strong>:</p>
<ul>
<li><strong>Long-running batch jobs</strong>: Data exports, report generation, video processing</li>
<li><strong>Large file uploads</strong>: Direct file uploads over 10MB become unreliable</li>
<li><strong>Complex data migrations</strong>: Multi-step transformations requiring minutes to complete</li>
<li><strong>WebSocket connections</strong>: Not supported by Lambda Function URLs (use API Gateway WebSocket instead)</li>
</ul>
<p><strong>Recommended Alternatives</strong>:</p>
<ul>
<li><strong>Keep Lambda URL</strong>: If API Gateway specific features are not needed, we can use custom domain with Lambda URLs and process up to 15 minutes</li>
<li><strong>AWS Step Functions</strong>: Orchestrate long-running workflows across multiple Lambda invocations</li>
<li><strong>ECS/Fargate</strong>: For truly long-running processes (hours), use containers instead</li>
<li><strong>Presigned S3 URLs</strong>: For large file uploads, let clients upload directly to S3</li>
<li><strong>SQS + Background Workers</strong>: Offload heavy processing to asynchronous queues</li>
</ul>
<h3>3. Session Consistency Edge Cases</h3>
<p><strong>The Reality</strong>: DynamoDB is eventually consistent by default, but we use <code>ConsistentRead: true</code> to mitigate this.</p>
<p><strong>Why We Use ConsistentRead</strong>:</p>
<pre><code class="language-php">&#039;ConsistentRead&#039; =&gt; true,  // Ensures we always get the latest session data</code></pre>
<p><strong>Rare Race Conditions</strong>:<br />
Even with consistent reads, race conditions can occur when:</p>
<ul>
<li><strong>Simultaneous Writes</strong>: User opens multiple tabs, both modify session simultaneously—last write wins</li>
<li><strong>Write-then-Read Timing</strong>: Session written in one Lambda, immediately read by another—minimal delay possible</li>
<li><strong>Cross-Region Scenarios</strong>: If using Global Tables, replication lag can cause stale reads in remote regions</li>
</ul>
<p><strong>Practical Impact</strong>: In 99.9% of cases, consistent reads solve the problem. Edge cases typically affect power users opening many tabs or distributed teams across continents.</p>
<p><strong>Mitigation</strong>: For critical operations (e.g., payment processing), use DynamoDB conditional expressions to ensure atomic updates and detect conflicts.</p>
<h3>4. DynamoDB Costs at Scale</h3>
<p>DynamoDB's pay-per-request pricing is cost-effective at low-to-moderate traffic but pricing characteristics change at high scale.</p>
<h4>Assumptions</h4>
<p><strong>DynamoDB</strong>:</p>
<ul>
<li>On-demand billing: $1.25 per million reads/writes</li>
<li>1KB session item size</li>
<li>1 read + 1 write per request</li>
</ul>
<p><strong>Redis (ElastiCache)</strong>:</p>
<ul>
<li>t4g.medium: $0.037/hr (~$27/month)</li>
<li>1 node sufficient for low-medium traffic</li>
<li>High traffic may require bigger node(s)</li>
</ul>
<h4>Cost Table</h4>
<table>
<thead>
<tr>
<th>Traffic</th>
<th>Requests / Month</th>
<th>DynamoDB Cost</th>
<th>Redis Cost</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Low</td>
<td>1M</td>
<td>$2.50</td>
<td>$27</td>
<td>DynamoDB far cheaper at low traffic</td>
</tr>
<tr>
<td>Medium</td>
<td>10M</td>
<td>$25</td>
<td>$27</td>
<td>Costs roughly similar; DynamoDB slightly lower ops</td>
</tr>
<tr>
<td>High</td>
<td>50M</td>
<td>$125</td>
<td>$108 (cache.m5.large 3 nodes)</td>
<td>Redis may become cheaper with large, sustained traffic, but ops complexity rises</td>
</tr>
</tbody>
</table>
<p><strong>When Fixed Infrastructure (like Redis/ElastiCache) May Become More Cost-Effective</strong>:</p>
<ul>
<li>Sustained high traffic volumes where fixed costs are fully utilized</li>
<li>Long-lived sessions with more reads than writes</li>
<li>Advanced caching features needed beyond simple session storage</li>
</ul>
<p><strong>Hidden DynamoDB Cost Factors</strong>:</p>
<ul>
<li>Consistent reads cost more than eventually consistent reads</li>
<li>Session writes on every request (even if session data unchanged)</li>
<li>AWS free tier limitations after 12 months</li>
</ul>
<p>Start with DynamoDB for simplicity and operational efficiency. Monitor costs monthly as traffic grows. If costs become a concern at high scale, evaluate whether fixed infrastructure or caching optimizations make sense for your specific use case.</p>
<hr />
<p>These limitations are not dealbreakers but they're <strong>trade-offs</strong>. For the right use cases (bursty traffic, cost-sensitive, minimal ops), the benefits far outweigh the drawbacks.</p>
<h2>Conclusion</h2>
<p>Building serverless PHP applications doesn't require sacrificing familiar frameworks or patterns. By implementing a custom DynamoDB session handler, we achieve:</p>
<ul>
<li><strong>Truly serverless architecture</strong>: No Redis, no EFS, pure AWS managed services</li>
<li><strong>Production-ready session management</strong>: Consistent, scalable, and secure</li>
<li><strong>Cost-effective</strong>: Pay only for actual usage</li>
<li><strong>Developer-friendly</strong>: Standard Symfony application with minimal modifications</li>
<li><strong>Type-safe infrastructure</strong>: AWS CDK with TypeScript</li>
<li><strong>Modern PHP</strong>: PHP 8.4 with all latest features</li>
<li><strong>Local development</strong>: Docker-compose for local testing</li>
</ul>
<p>The combination of Bref for Lambda PHP support, Symfony for application framework, and DynamoDB for stateful storage creates a robust, scalable, and maintainable serverless application architecture.</p>
<h3>When Should You Use This Architecture?</h3>
<p><strong>Choose this approach when:</strong></p>
<ul>
<li>Traffic is unpredictable or bursty (blogs, seasonal apps, internal tools)</li>
<li>Cost optimization matters more than absolute performance</li>
<li>Zero operational overhead is a priority</li>
<li>You need automatic scaling without capacity planning</li>
</ul>
<p>Common use cases are:</p>
<ul>
<li>CMS - Blogs, documentation sites, and knowledge bases with infrequent or sporadic traffic, when sudden spikes are scaled automatically and quite periods costs pennies</li>
<li>Admin Panels and Internal Tools - Dashboard interfaces, internal reporting tools, and back-office applications with sporadic usage patterns. DynamoDB maintains session state without requiring Redis or similar infrastructure.</li>
<li>Multi-Tenant SaaS Applications - B2B platforms where each tenant has independent traffic patterns. DynamoDB's single-table design efficiently manages sessions across all tenants without cross-tenant interference.</li>
<li>API Services with Session Requirements - REST APIs that need stateful operations like OAuth flows, multi-step workflows, or temporary data caching. No Redis clusters to maintain, no session cleanup cron jobs to manage. DynamoDB TTL handles everything automatically.</li>
<li>Seasonal Applications - Event registration systems, holiday campaign sites, tax filing applications, and other time-bound services.</li>
<li>Microservices Requiring Session State - Distributed systems where individual services need temporary state management across invocations.</li>
</ul>
<p><strong>Consider alternatives when:</strong></p>
<ul>
<li>You require consistent sub-100ms response times</li>
<li>Traffic is predictable and sustained at high levels (&gt;10M requests/month)</li>
<li>Long-running processes or WebSocket connections are needed</li>
</ul>
<h3>The Bigger Picture</h3>
<p>This implementation demonstrates that <strong>serverless and stateful aren't mutually exclusive</strong>. While serverless advocates often emphasize &quot;stateless functions,&quot; real-world applications need state management. The key is choosing the right state storage mechanism, and DynamoDB proves that managed, serverless databases can handle session management as effectively as traditional infrastructure, with far less operational burden.</p>
<p>Whether you're building a content management system, an internal admin panel, or a multi-tenant SaaS application, this architecture provides a production-ready foundation. Start simple, monitor costs and performance, and scale confidently knowing your infrastructure will grow with your application without requiring a dedicated ops team.</p>
<h2>Resources</h2>
<ul>
<li><a href="https://bref.sh/">Bref Documentation</a></li>
<li><a href="https://github.com/brefphp/constructs">Bref CDK Constructs</a></li>
<li><a href="https://async-aws.com/clients/dynamodb.html">AsyncAws DynamoDB Client</a></li>
<li><a href="https://symfony.com/doc/current/session.html">Symfony Session Documentation</a></li>
<li><a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html">Lambda Function URLs</a></li>
<li><a href="https://www.alexdebrie.com/posts/dynamodb-single-table/">DynamoDB Single-Table Design</a></li>
</ul>
<h2>Source Code</h2>
<p>The complete source code for this application is available at: <a href="https://github.com/rafaelbernard/serverless-php-with-bref-symfony-and-dynamodb-session-management/">rafaelbernard/serverless-php-with-bref-symfony-and-dynamodb-session-management/</a></p>
<p>For detailed technical implementation notes, test coverage reports, and deployment validation, see <a href="https://github.com/rafaelbernard/serverless-php-with-bref-symfony-and-dynamodb-session-management/blob/master/IMPLEMENTATION_SUMMARY.md"><code>IMPLEMENTATION_SUMMARY.md</code></a> in the repository. This document covers:</p>
<ul>
<li>Complete test suite (112 tests across PHP and CDK)</li>
<li>Infrastructure validation details</li>
<li>Code quality metrics</li>
<li>Deployment procedures and best practices</li>
</ul>
<h3><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f4a1.png" alt="💡" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Bonus: Guide to Custom Domain Configuration with Route53</h3>
<p>Lambda Function URLs provide a quick way to expose your Lambda function over HTTPS, but the auto-generated URL (e.g., <code>https://abc123xyz.lambda-url.us-east-1.on.aws/</code>) isn't branded or memorable. But you can add a Simple CNAME Mapping: Direct Route53 CNAME to Lambda Function URL (easiest, limited SSL control).</p>
<p>This is the <strong>quickest and easiest</strong> method. Just create a CNAME record pointing to your Lambda Function URL. Best for internal tools, prototypes, and non-production environments.</p>
<h4>Prerequisites</h4>
<p>Before configuring custom domains, ensure you have:</p>
<ol>
<li><strong>Domain registered in Route53</strong> (or another registrar with ability to update nameservers)</li>
<li><strong>Hosted Zone created in Route53</strong> for your domain</li>
</ol>
<h4>Implementation with CDK</h4>
<p>Here's how to add a custom domain CNAME record pointing to your Lambda Function URL using AWS CDK:</p>
<pre><code class="language-typescript">import * as route53 from &#039;aws-cdk-lib/aws-route53&#039;;
import * as route53Targets from &#039;aws-cdk-lib/aws-route53-targets&#039;;
import * as lambda from &#039;aws-cdk-lib/aws-lambda&#039;;
import { Construct } from &#039;constructs&#039;;

// Assuming you have a Lambda function with Function URL enabled
const myFunction = new lambda.Function(this, &#039;MyFunction&#039;, {
  // ... function configuration
  functionUrlOptions: {
    authType: lambda.FunctionUrlAuthType.NONE, // or AWS_IAM
  },
});

// Get the hosted zone for your domain
const hostedZone = route53.HostedZone.fromLookup(this, &#039;HostedZone&#039;, {
  domainName: &#039;yourdomain.com&#039;,
});

// Create CNAME record pointing to Lambda URL
new route53.CnameRecord(this, &#039;LambdaUrlCname&#039;, {
  zone: hostedZone,
  recordName: &#039;api&#039;, // Creates api.yourdomain.com
  domainName: cdk.Fn.parseDomainName(myFunction.functionUrl), // Extracts hostname from URL
  ttl: cdk.Duration.minutes(5),
  comment: &#039;CNAME to Lambda Function URL&#039;,
});

// Output the custom domain
new cdk.CfnOutput(this, &#039;CustomDomainUrl&#039;, {
  value: `https://api.yourdomain.com`,
  description: &#039;Custom domain URL for Lambda function&#039;,
});</code></pre>
<h4>Testing Your CNAME Setup</h4>
<p>After creating the CNAME record, verify it works:</p>
<pre><code class="language-bash"># Check DNS propagation
dig api.yourdomain.com

# Test the endpoint
curl -i https://api.yourdomain.com/

# Verify SSL certificate
openssl s_client -connect api.yourdomain.com:443 -servername api.yourdomain.com | grep subject</code></pre>
<p><strong>Expected Results</strong>:</p>
<ul>
<li>DNS query returns Lambda Function URL hostname as CNAME target</li>
<li>HTTP request succeeds with same response as Lambda URL</li>
<li>SSL certificate shows AWS-managed certificate (not your custom domain)</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/building-a-serverless-php-application-with-bref-symfony-and-dynamodb-session-management.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2283</post-id>	</item>
		<item>
		<title>Principles in Refactoring &#8211; Slowing Down New Features?</title>
		<link>https://rafael.bernard-araujo.com/principles-in-refactoring-slowing-down-new-features.php</link>
					<comments>https://rafael.bernard-araujo.com/principles-in-refactoring-slowing-down-new-features.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Wed, 22 Oct 2025 02:59:39 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[software engineering]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2252</guid>

					<description><![CDATA[The whole purpose of refactoring is to make us program faster, producing more value with less effort. and But I think the most dangerous way that people get trapped is when they try to justify refactoring in terms of &#34;clean code&#34;, &#34;good engineering practice&#34;, or similar moral reasons. The point of refactoring isn't to show [&#8230;]]]></description>
										<content:encoded><![CDATA[<blockquote>
<p>The whole purpose of refactoring is to make us program faster, producing more value with less effort.</p>
</blockquote>
<p>and</p>
<blockquote>
<p>But I think the most dangerous way that people get trapped is when they try to justify refactoring in terms of &quot;clean code&quot;, &quot;good engineering practice&quot;, or similar moral reasons. The point of refactoring isn't to show how sparkly a code base is -- it is purely economic. We refactor because it makes us faster -- fastor add features, faster to fix bugs.</p>
</blockquote>
<p>-- From <em>Refactoring: Improving the Design of Existing Code</em> (Martin Fowler and Kent Beck), page 56</p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/principles-in-refactoring-slowing-down-new-features.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2252</post-id>	</item>
		<item>
		<title>The Rule of Three</title>
		<link>https://rafael.bernard-araujo.com/the-rule-of-three.php</link>
					<comments>https://rafael.bernard-araujo.com/the-rule-of-three.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Sun, 19 Oct 2025 19:56:20 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[software engineering]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2243</guid>

					<description><![CDATA[The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor. -- Don Roberts]]></description>
										<content:encoded><![CDATA[<blockquote>
<p>The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.</p>
</blockquote>
<p>-- Don Roberts</p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/the-rule-of-three.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2243</post-id>	</item>
		<item>
		<title>Domain-Driven Design &#8211; DDD</title>
		<link>https://rafael.bernard-araujo.com/domain-driven-design-ddd.php</link>
					<comments>https://rafael.bernard-araujo.com/domain-driven-design-ddd.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Fri, 18 Oct 2024 08:09:26 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ddd]]></category>
		<category><![CDATA[domain-driven design]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2107</guid>

					<description><![CDATA[Domain-driven development (DDD) is an approach to software design that focuses on the core domain and the logic that drives a business. The idea is to model the software based on real-world business concepts, ensuring that the code closely reflects the domain it is meant to serve. Key aspects of DDD include: Domain Model: A [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Domain-driven development (DDD) is an approach to software design that focuses on the core domain and the logic that drives a business. The idea is to model the software based on real-world business concepts, ensuring that the code closely reflects the domain it is meant to serve.</p>
<p>Key aspects of DDD include:</p>
<ol>
<li>
<p><strong>Domain Model</strong>: A shared understanding of the business logic, defined in terms meaningful to domain experts and developers.</p>
</li>
<li>
<p><strong>Ubiquitous Language</strong>: A common language shared by technical and non-technical stakeholders to describe the domain, ensuring clarity and reducing miscommunication.</p>
</li>
<li>
<p><strong>Bounded Contexts</strong>: Distinct areas within a larger system where a specific domain model applies. Each context can evolve independently while being integrated with others.</p>
</li>
<li>
<p><strong>Entities and Value Objects</strong>: Entities have unique identities, while value objects are immutable and are defined only by their properties.</p>
</li>
<li>
<p><strong>Aggregates</strong>: Clusters of related objects treated as a unit, ensuring consistency in business operations.</p>
</li>
<li>
<p><strong>Repositories and Services</strong>: Repositories handle data access, while services implement business operations that don’t belong to a single entity.</p>
</li>
</ol>
<p>DDD emphasizes collaboration between developers and domain experts to ensure software design mirrors business processes and terminology.</p>
<blockquote>
<p>A particularly important part of DDD is the notion of Strategic Design - how to organize large domains into a network of Bounded Contexts. [1]</p>
</blockquote>
<p><strong>Why is this important for your business?</strong></p>
<p>The design it proposes puts our focus on the core domain and the business logic, which makes our product relevant and where it differentiates from competitors. The DDD design boosts the understanding of <strong>what our application does</strong> instead of <em>which technology (framework, dependencies) it uses</em>.</p>
<blockquote>
<p>Domain-Driven Design is an approach to software development that centers the development on programming a domain model that has a rich understanding of the processes and rules of a domain. The name comes from a 2003 book by Eric Evans that describes the approach through a catalog of patterns. Since then a community of practitioners have further developed the ideas, spawning various other books and training courses. The approach is particularly suited to complex domains, where a lot of often-messy logic needs to be organized. [1]</p>
</blockquote>
<p>We will see more about how this translates to our code as we understand the key aspects to be expanded in future posts.</p>
<p>Related:<br />
[1] <a href="https://martinfowler.com/bliki/DomainDrivenDesign.html">Domain-Driven Design by Martin Fowler</a><br />
[2] <a href="https://en.wikipedia.org/wiki/Domain-driven_design">Domain-Driven Design on Wikipedia</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/domain-driven-design-ddd.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2107</post-id>	</item>
		<item>
		<title>Lambda extension to cache SSM and Secrets Values for PHP Lambda on CDK</title>
		<link>https://rafael.bernard-araujo.com/lambda-extension-to-cache-ssm-and-secrets-values-for-php-lambda-on-cdk.php</link>
					<comments>https://rafael.bernard-araujo.com/lambda-extension-to-cache-ssm-and-secrets-values-for-php-lambda-on-cdk.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Thu, 27 Jun 2024 08:58:29 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[aws-lambda]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[serverless]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=2038</guid>

					<description><![CDATA[Introduction Managing secrets securely in AWS Lambda functions is crucial for maintaining the integrity and confidentiality of your applications. AWS provides services like AWS Secrets Manager and AWS Systems Manager Parameter Store to manage secrets. However, frequent retrieval of secrets can introduce latency and additional costs. To optimize this, we can cache secrets using a [&#8230;]]]></description>
										<content:encoded><![CDATA[<h1>Introduction</h1>
<p>Managing secrets securely in AWS Lambda functions is crucial for maintaining the integrity and confidentiality of your applications. AWS provides services like AWS Secrets Manager and AWS Systems Manager Parameter Store to manage secrets. However, frequent retrieval of secrets can introduce latency and additional costs. To optimize this, we can cache secrets using a Lambda Extension.</p>
<p>In this article, we will demonstrate how to use a pre-existing Lambda Extension to cache secrets for a PHP Lambda function using the Bref layer and AWS CDK for deployment.</p>
<p>On a high-level, these are the components involved:</p>
<p><a href="https://i0.wp.com/d2908q01vomqb2.cloudfront.net/1b6453892473a467d07372d45eb05abc2031647a/2022/11/17/secrets1.png?ssl=1" title="Components"><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/d2908q01vomqb2.cloudfront.net/1b6453892473a467d07372d45eb05abc2031647a/2022/11/17/secrets1.png?w=580&#038;ssl=1" alt="Lambda Execution Components" title="Components" /></a></p>
<blockquote>
<p><a href="https://aws.amazon.com/blogs/compute/using-the-aws-parameter-and-secrets-lambda-extension-to-cache-parameters-and-secrets/?ref=serverlessland">Using the AWS Parameter and Secrets Lambda extension to cache parameters and secrets</a></p>
<p>The new AWS Parameters and Secrets Lambda extension provides a managed parameters and secrets cache for Lambda functions. The extension is distributed as a Lambda layer that provides an in-memory cache for parameters and secrets. It allows functions to persist values through the <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#runtimes-lifecycle">Lambda execution lifecycle</a>, and provides a configurable time-to-live (TTL) setting.</p>
<p>When you request a parameter or secret in your Lambda function code, the extension retrieves the data from the local in-memory cache, if it is available. If the data is not in the cache or it is stale, the extension fetches the requested parameter or secret from the respective service. This helps to reduce external API calls, which can improve application performance and reduce cost.</p>
</blockquote>
<h1>Prerequisites</h1>
<ul>
<li>AWS Account</li>
<li>AWS CLI configured</li>
<li>AWS CDK installed</li>
<li>PHP installed</li>
<li>Composer installed</li>
</ul>
<p>If you have <a href="https://docs.docker.com/engine/install/">Docker</a>, all requirements are being installed by it.</p>
<h1>Repository Overview</h1>
<p>The code for this project is available in the following GitHub repository: <a href="https://github.com/rafaelbernard/serverless-patterns/tree/rafaelbernard-feature-lambda-extension-ssm-secrets-cdk-php">rafaelbernard/serverless-patterns</a>. The relevant files are located in the <code>lambda-extension-ssm-secrets-cdk-php</code> folder.</p>
<h1>Step-by-Step Guide</h1>
<h2>1. Cloning the Repository</h2>
<p>First, clone the repository and navigate to the relevant directory:</p>
<pre><code class="language-bash">git clone --branch rafaelbernard-feature-lambda-extension-ssm-secrets-cdk-php https://github.com/rafaelbernard/serverless-patterns.git
cd serverless-patterns/lambda-extension-ssm-secrets-cdk-php</code></pre>
<h2>2. Project Structure</h2>
<p>The project structure is as follows:</p>
<pre><code>.
├── assets
│   └── lambda
│       └── lambda.php
├── bin
│   └── cdk.ts
├── cdk
│   └── cdk-stack.ts
├── cdk.json
├── docker-compose.yml
├── Dockerfile
├── example-pattern.json
├── Makefile
├── package.json
├── package-lock.json
├── php
│   ├── composer.json
│   ├── composer.lock
│   └── handlers
│       └── lambda.php
├── README.md
├── run-docker.sh
└── tsconfig.json</code></pre>
<h2>3. Setting Up the Lambda Function</h2>
<p>The main logic for fetching and caching secrets is in <code>php/handlers/lambda.php</code>:</p>
<pre><code class="language-php">&lt;?php

use Bref\Context\Context;
use Bref\Event\Http\HttpResponse;
use GuzzleHttp\Client;
use Symfony\Component\HttpFoundation\JsonResponse;

// Responsibilities are simplified into one file for demonstration purposes
// We would have have those methods in a Service class

function getParam(string $parameterPath): string
{
    // Set `withDecryption=true if you also want to retrieve SecureString SSMs
    $url = &quot;http://localhost:2773/systemsmanager/parameters/get?name={$parameterPath}&amp;withDecryption=true&quot;;

    try {
        $client = new Client();

        $response = $client-&gt;get($url, [
            &#039;headers&#039; =&gt; [
                &#039;X-Aws-Parameters-Secrets-Token&#039; =&gt; getenv(&#039;AWS_SESSION_TOKEN&#039;),
            ]
        ]);

        $data = json_decode($response-&gt;getBody());
        return $data-&gt;Parameter-&gt;Value;
    } catch (\Exception $e) {
        error_log(&#039;Error getting parameter =&gt; &#039; . print_r($e, true));
    }
}

function getSecret(string $secretName): stdClass
{
    $url = &quot;http://localhost:2773/secretsmanager/get?secretId={$secretName}&quot;;

    try {
        $client = new Client();

        $response = $client-&gt;get($url, [
            &#039;headers&#039; =&gt; [
                &#039;X-Aws-Parameters-Secrets-Token&#039; =&gt; getenv(&#039;AWS_SESSION_TOKEN&#039;),
            ]
        ]);

        $data = json_decode($response-&gt;getBody());
        return json_decode($data-&gt;SecretString);
    } catch (\Exception $e) {
        error_log(&#039;Error getting secretsmanager =&gt; &#039; . print_r($e, true));
    }
}

return function ($request, Context $context) {
    $secret = getSecret(getenv(&#039;THE_SECRET_NAME&#039;));
    $response = new JsonResponse([
        &#039;status&#039; =&gt; &#039;OK&#039;,
        getenv(&#039;THE_SSM_PARAM_PATH&#039;) =&gt; getParam(getenv(&#039;THE_SSM_PARAM_PATH&#039;)),
        getenv(&#039;THE_SECRET_NAME&#039;) =&gt; [
            &#039;password&#039; =&gt; $secret-&gt;password,
            &#039;username&#039; =&gt; $secret-&gt;username,
        ],
    ]);

    return (new HttpResponse($response-&gt;getContent(), $response-&gt;headers-&gt;all()))-&gt;toApiGatewayFormatV2();
};</code></pre>
<h2>4. Setting Up AWS CDK Stack</h2>
<p>The AWS CDK stack is defined in <code>cdk/cdk-stack.ts</code>:</p>
<pre><code class="language-typescript">import { CfnOutput, CfnParameter, Stack, StackProps } from &#039;aws-cdk-lib&#039;;
import { Construct } from &#039;constructs&#039;;
import { join } from &quot;path&quot;;
import { packagePhpCode, PhpFunction } from &quot;@bref.sh/constructs&quot;;
import { FunctionUrlAuthType, LayerVersion, Runtime } from &quot;aws-cdk-lib/aws-lambda&quot;;
import { StringParameter } from &quot;aws-cdk-lib/aws-ssm&quot;;
import { Policy, PolicyStatement } from &#039;aws-cdk-lib/aws-iam&#039;;
import { Secret } from &#039;aws-cdk-lib/aws-secretsmanager&#039;;

export class CdkStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    const stackPrefix = id;

    // May be set as parameter new CfnParameter(this, &#039;parameterStoreExtensionArn&#039;, { type: &#039;String&#039; });
    const parameterStoreExtensionArn = &#039;arn:aws:lambda:us-east-1:177933569100:layer:AWS-Parameters-and-Secrets-Lambda-Extension:11&#039;;
    const parameterStoreExtension = new CfnParameter(this, &#039;parameterStoreExtensionArn&#039;, { type: &#039;String&#039;, default: parameterStoreExtensionArn });

    const paramTheSsmParam = new StringParameter(this, `${stackPrefix}-TheSsmParam`, {
      parameterName: `/${stackPrefix.toLowerCase()}/ssm/param`,
      stringValue: &#039;the-value-here&#039;,
    });

    // CDK cannot create SecureString
    // You would create the SecureString out of CDK and use the param name here
    // const paramAnSsmSecureStringParam = StringParameter.fromSecureStringParameterAttributes(this, `${stackPrefix}-AnSsmSecureStringParam`, {
    //   parameterName: `/${stackPrefix.toLowerCase()}/ssm/secure-string/params`,
    // });

    const templatedSecret = new Secret(this, &#039;TemplatedSecret&#039;, {
      generateSecretString: {
        secretStringTemplate: JSON.stringify({ username: &#039;postgres&#039; }),
        generateStringKey: &#039;password&#039;,
        excludeCharacters: &#039;/@&quot;&#039;,
      },
    });

    // The param path that will be used to retrieve value by the lambda
    const lambdaEnvironment = {
      THE_SSM_PARAM_PATH: paramTheSsmParam.parameterName,
      THE_SECRET_NAME: templatedSecret.secretName,
      // If you create the SecureString
      // THE_SECURE_SSMPARAM_PATH: paramAnSsmSecureStringParam.parameterName,
    };

    const functionName = `${id}-lambda`;
    const theLambda = new PhpFunction(this, `${stackPrefix}${functionName}`, {
      handler: &#039;lambda.php&#039;,
      phpVersion: &#039;8.3&#039;,
      runtime: Runtime.PROVIDED_AL2,
      code: packagePhpCode(join(__dirname, `../assets/lambda`)),
      functionName,
      environment: lambdaEnvironment,
    });

    // Add extension layer
    theLambda.addLayers(
      LayerVersion.fromLayerVersionArn(this, &#039;ParameterStoreExtension&#039;, parameterStoreExtension.valueAsString)
    );

    // Set additional permissions for parameter store
    theLambda.role?.attachInlinePolicy(
      new Policy(this, &#039;additionalPermissionsForParameterStore&#039;, {
        statements: [
          new PolicyStatement({
            actions: [&#039;ssm:GetParameter&#039;],
            resources: [
              paramTheSsmParam.parameterArn,
              // If you create the SecureString
              // paramAnSsmSecureStringParam.parameterArn,
            ],
          }),
        ],
      }),
    )

    templatedSecret.grantRead(theLambda);

    const fnUrl = theLambda.addFunctionUrl({ authType: FunctionUrlAuthType.NONE });

    new CfnOutput(this, &#039;LambdaUrl&#039;, { value: fnUrl.url });
  }
}</code></pre>
<h2>5. Deploying with AWS CDK</h2>
<p>Make sure you have already AWS variables set and run below command to install required dependancies:</p>
<pre><code class="language-shell"># Using docker -- check run-docker.sh
make up</code></pre>
<p>or</p>
<pre><code class="language-shell"># Using local
npm ci
cd php &amp;&amp; composer install --no-scripts &amp;&amp; cd -</code></pre>
<p>After that, you will have all dependencies installed. Deploy it executing:</p>
<pre><code class="language-shell"># Using docker
make deploy</code></pre>
<p>or</p>
<pre><code class="language-shell"># Using local
npm run deploy</code></pre>
<h2>6. Testing the Lambda Function</h2>
<p>The CDK output will have the Lambda function URL, which you can use to test and retrieve the values:</p>
<pre><code class="language-shell">Outputs:
LambdaExtensionSsmSecretsCdkPhpStack.LambdaUrl = https://keamdws766oqzr6dbiindaix3a0fdojb.lambda-url.us-east-1.on.aws/</code></pre>
<p>You should see the secret value and parameter value returned by the Lambda function. Subsequent invocations should retrieve the values from the cache, reducing latency and cost.</p>
<pre><code class="language-json">{
  &quot;status&quot;: &quot;OK&quot;,
  &quot;/lambdaextensionssmsecretscdkphpstack/ssm/param&quot;: &quot;the-value-here&quot;,
  &quot;TemplatedSecret3D98B577-4jOWSbUMCHmF&quot;: {
    &quot;password&quot;: &quot;!o9GpBzpa&gt;dYdo.Gx3J2!&lt;zd(s-Fg;ev&quot;,
    &quot;username&quot;: &quot;postgres&quot;
  }
}</code></pre>
<h3>Performance benefits</h3>
<p>A similar <a href="https://aws.amazon.com/blogs/compute/using-the-aws-parameter-and-secrets-lambda-extension-to-cache-parameters-and-secrets/">example application written in Python</a> performed three tests, <strong>reducing API calls ~98%</strong>. I am quoting their findings, as the benefits are the same for this PHP Lambda:</p>
<blockquote>
<p>To evaluate the performance benefits of the Lambda extension cache, three tests were run using the open source tool Artillery to load test the Lambda function. </p>
<pre><code class="language-yaml">config:
 target: "https://lambda.us-east-1.amazonaws.com"
phases:
  -
duration: 60
arrivalRate: 10
rampTo: 40</code></pre>
<pre><code>Test 1: The extension cache is disabled by setting the TTL environment variable to 0. This results in 1650 GetParameter API calls to Parameter Store over 60 seconds.</code></pre>
<p>Test 2: The extension cache is enabled with a TTL of 1 second. This results in 106 GetParameter API calls over 60 seconds.<br />
Test 3: The extension is enabled with a TTL value of 300 seconds. This results in only 18 GetParameter API calls over 60 seconds.</p>
<p>In test 3, the TTL value is longer than the test duration. The 18 GetParameter calls correspond to the number of Lambda execution environments created by Lambda to run requests in parallel. Each execution environment has its own in-memory cache and so each one needs to make the GetParameter API call.</p>
<p>In this test, using the extension has <strong>reduced API calls by ~98%</strong>. Reduced API calls results in reduced function execution time, and therefore reduced cost.</p>
</blockquote>
<h2>7. Clean up</h2>
<p>To delete the stack, run:</p>
<pre><code class="language-shell">make bash
npm run destroy</code></pre>
<h1>Conclusion</h1>
<p>In this article, we demonstrated how to use a pre-existing Lambda Extension to cache secrets for a PHP Lambda function using the Bref layer and AWS CDK for deployment. By caching secrets, we can improve the performance and reduce the cost of our serverless applications. The approach detailed here can be adapted to various use cases, enhancing the efficiency of your AWS Lambda functions.</p>
<p>For more information on the Parameter Store, Secrets Manager, and Lambda extensions, refer to:</p>
<ul>
<li><a href="https://docs.aws.amazon.com/systems-manager/latest/userguide/ps-integration-lambda-extensions.html">Using Parameter Store parameters in AWS Lambda functions</a></li>
<li><a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_lambda.html">Use AWS Secrets Manager secrets in AWS Lambda functions</a></li>
<li><a href="https://aws.amazon.com/blogs/compute/introducing-aws-lambda-extensions-in-preview/">Introducing AWS Lambda Extensions</a></li>
<li><a href="https://aws.amazon.com/blogs/compute/caching-data-and-configuration-settings-with-aws-lambda-extensions/">Caching data and configuration settings with AWS Lambda extensions</a></li>
<li><a href="https://aws.amazon.com/blogs/compute/using-the-aws-parameter-and-secrets-lambda-extension-to-cache-parameters-and-secrets/?ref=serverlessland">AWS blog on using Lambda Extensions to cache secrets</a></li>
</ul>
<p>For more serverless learning resources, visit <a href="https://serverlessland.com/">Serverless Land</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/lambda-extension-to-cache-ssm-and-secrets-values-for-php-lambda-on-cdk.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2038</post-id>	</item>
		<item>
		<title>A bref AWS PHP story – Part 3</title>
		<link>https://rafael.bernard-araujo.com/a-bref-aws-php-story-part-3.php</link>
					<comments>https://rafael.bernard-araujo.com/a-bref-aws-php-story-part-3.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Tue, 27 Feb 2024 07:44:50 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[aws-cdk]]></category>
		<category><![CDATA[bref]]></category>
		<category><![CDATA[bref-php-aws-story]]></category>
		<category><![CDATA[cdk]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[serverless]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=1980</guid>

					<description><![CDATA[We are starting Part 3 of the Series &#34;A bref AWS PHP history&#34;. You can check Part 1, where I presented the PHP language as a reliable and good alternative for Serverless applications and Part 2 where we see the usage of CDK features in favour of a faithful CI/CD. Part 3 is to show [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>We are starting Part 3 of the Series <a href="https://rafael.bernard-araujo.com/tag/bref-php-aws-story">&quot;A bref AWS PHP history&quot;</a>. You can check <a href="https://dev.to/rafaelbernard/a-bref-aws-php-history-part-1-2agn">Part 1</a>, where I presented the PHP language as a reliable and good alternative for Serverless applications and <a href="https://dev.to/rafaelbernard/a-bref-aws-php-story-part-2-1dhe">Part 2</a> where we see the usage of CDK features in favour of a faithful CI/CD.</p>
<p>Part 3 is to show the upgrade path to Bref 2 and to achieve more coverage of the AWS resources. We will use DynamoDB, a powerful database for serverless architectures.</p>
<p>Some of those topics seem straightforward to some people, but I would like to avoid guessing that this is known to the audience since I have experienced some PHP developers struggling to put all these together for the first time due to the paradigm change. It should be fun.</p>
<p>Table of contents:</p>
<ol>
<li>What else are we doing?</li>
<li>Describing more AWS services - Adding a DynamoDB table</li>
<li>Bref upgrade</li>
<li>Testing CDK</li>
<li>PHP and AWS Services</li>
<li>Wrap-up</li>
</ol>
<h2>What else are we doing?</h2>
<p>In this section, we'll explore additional functionalities and enhancements to our serverless application. Building upon the foundation laid in Part 2, we'll introduce new features and integrations to further extend the capabilities of our AWS PHP application.</p>
<p>The <a href="https://dev.to/rafaelbernard/a-bref-aws-php-story-part-2-1dhe">Part 2</a> uses the result of the Fibonacci of a provided integer or a random integer from 400 to 1000 (to get a good image and not to overflow <code>integer</code>). This integer is the number of pixels of an image from the bucket and an arbitrary request metadata we are creating. If the image does not exist, the lambda will fetch a random image from the web with that number of pixels, save it and generate the metadata.</p>
<p>The computing complexity is irrelevant because it could be very complex logic or very simple, and the topics we are discussing in this part of the series will use the same design.</p>
<p>The lambda will now search the metadata in a DynamoDB table, saving the metadata when it does not exist. DynamoDB is largely used in Lambda code.</p>
<p><a href="https://github.com/rafaelbernard/bref-initial-php-aws-story/tree/part-3">Get the part-3 source-code on GitHub</a> and <a href="https://github.com/rafaelbernard/bref-initial-php-aws-story/compare/tag-part-2...tag-part-3">the diff from part-2</a>.</p>
<h2>Describing more AWS services - Adding a DynamoDB Table</h2>
<p>DynamoDB plays a crucial role in serverless architectures, offering scalable and high-performance NoSQL database capabilities. In this section, we'll delve into the process of integrating DynamoDB into our AWS CDK stack, expanding our application's data storage and retrieval capabilities.</p>
<p><a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">DynamoDB</a> is a fully managed NoSQL database service provided by AWS, offering seamless integration with other AWS services, automatic scaling, and built-in security features. Its scalability, low latency, and flexible data model make it well-suited for serverless architectures and applications with varying throughput requirements.</p>
<pre><code class="language-ts">    const table = new Table(this, TableName, {
      partitionKey: { name: &#039;PK&#039;, type: AttributeType.STRING },
      sortKey: { name: &#039;SK&#039;, type: AttributeType.STRING },
      removalPolicy: RemovalPolicy.DESTROY,
      tableName: TableName,
    });</code></pre>
<p>Following the same principles for creating other AWS resources, we utilize the AWS CDK to define a DynamoDB table within our stack. Let's dive into the key parameters of the Table constructor:</p>
<ul>
<li><code>partitionKey</code>: This parameter defines the primary key attribute for the DynamoDB table, used to distribute items across partitions for scalability. In our example, <code>{ name: &#039;PK&#039;, type: AttributeType.STRING }</code> specifies a partition key named 'PK' with a string type. The naming convention ('PK') is arbitrary and can be tailored to suit your application's needs.</li>
<li><code>sortKey</code>: For tables requiring a composite primary key (partition key and sort key), the sortKey parameter comes into play. Here, <code>{ name: &#039;SK&#039;, type: AttributeType.STRING }</code> defines a sort key named 'SK' with a string type. Like the partition key, the name and type of the sort key can be customized based on your data model.</li>
<li><code>removalPolicy</code>: This parameter determines the behaviour of the DynamoDB table when the CloudFormation stack is deleted. By setting <code>RemovalPolicy.DESTROY</code>, we specify that the table should be deleted (destroyed) along with the stack. Alternatively, you can opt for <code>RemovalPolicy.RETAIN</code> to preserve the table post-stack deletion, which may be useful for retaining data.</li>
</ul>
<p>By decoupling configuration from implementation, we adhere to SOLID principles, ensuring cleaner and more robust code. This approach fosters flexibility, allowing our code to seamlessly adapt to changes, such as modifications to the table name while maintaining its functionality.</p>
<p>The implementation code is aware that the name will come from an environment variable and will work with that (yes, if you think that test will be easy to write, you are right):</p>
<pre><code class="language-ts">    const lambdaEnvironment = {
      TableName,
      TableArn: table.tableArn,
      BucketName: brefBucket.bucketName,
    };</code></pre>
<h2>Bref Upgrade</h2>
<p><a href="https://bref.sh">Bref</a>, the PHP runtime for AWS Lambda, continually evolves to provide developers with the latest features and optimizations. In this section, we'll discuss the upgrade to Bref 2.0 and explore how it enhances the deployment process and performance of our serverless PHP applications.</p>
<p>In this section, we're upgrading our usage of <a href="https://bref.sh">Bref</a>, a PHP runtime for AWS Lambda, to version 2.0. Bref simplifies the deployment of PHP applications to AWS Lambda, enabling us to run PHP code serverlessly.</p>
<p>The upgrade involves modifying our AWS CDK code to utilize the new features and improvements introduced in Bref 2.0. One notable improvement is the automatic selection of the latest layer of the PHP version, which simplifies the deployment process and ensures that our Lambda functions run on the most up-to-date PHP environment available.</p>
<pre><code class="language-ts">  const getLambda = new PhpFunction(this, <code>${stackPrefix}${functionName}</code>, {
    handler: 'get.php',
    phpVersion: '8.3',
    runtime: Runtime.PROVIDED_AL2,
    code: packagePhpCode(join(__dirname, <code>../assets/get</code>), {
      exclude: ['test', 'tests'],
    }),
    functionName,
    environment: lambdaEnvironment,
  });</code></pre>
<ul>
<li><strong>`PhpFunction` Constructor</strong>: We&#039;re using the `PhpFunction` constructor provided by Bref to define our Lambda function. This constructor allows us to specify parameters such as the handler file, PHP version, runtime, code location, function name, and environment variables.</li>
<li>`handler`: Specifies the entry point file for our Lambda function, where the execution starts.</li>
<li>`phpVersion`: Defines the PHP version to be used by the Lambda function. In this case, we&#039;re using PHP version 8.3.</li>
<li>`runtime`: Indicates the Lambda runtime environment. Here, `Runtime.PROVIDED_AL2` signifies the use of the Amazon Linux 2 operating system.</li>
<li>`code`: Specifies the location of the PHP code to be deployed to Lambda.</li>
<li>`functionName`: Sets the name of the Lambda function.</li>
<li>`environment`: Allows us to define environment variables required by the Lambda function, such as database connection strings or configuration settings.</li>
</ul>
<p>By upgrading to Bref 2.0 and configuring our Lambda function accordingly, we ensure compatibility with the latest enhancements and optimizations provided by Bref, thereby improving the performance and reliability of our serverless PHP applications on AWS Lambda.</p>
<h2>Testing CDK</h2>
<p>Ensuring the correctness and reliability of our AWS CDK infrastructure is crucial for maintaining a robust serverless architecture. In this section, we&#039;ll delve into testing our CDK resources, focusing on the DynamoDB table we added in the previous section.</p>
<p>As described earlier, we utilized the AWS CDK to provision a DynamoDB table within our serverless stack. Now, let&#039;s ensure that the table is configured correctly and behaves as expected by writing tests using the CDK&#039;s testing framework.</p>
<p>First, let&#039;s revisit how we added the DynamoDB table:</p>
<pre><code class="language-ts">const table = new Table(this, TableName, {
  partitionKey: { name: 'PK', type: AttributeType.STRING },
  sortKey: { name: 'SK', type: AttributeType.STRING },
  removalPolicy: RemovalPolicy.DESTROY,
  tableName: TableName,
});</code></pre>
<p>In this code snippet, we define a DynamoDB table with specified attributes such as partition key, sort key, removal policy, and table name. Now, to ensure that this table is created with the correct configuration, we&#039;ll write tests using CDK&#039;s testing constructs.</p>
<p>Check the following thest:</p>
<pre><code class="language-ts">test('Should have DynamoDB', () => {
  expectCDK(stack).to(
    haveResource(
      'AWS::DynamoDB::Table',
      {
        "DeletionPolicy": "Delete",
        "Properties": {
          "AttributeDefinitions": [
            {
              "AttributeName": "PK",
              "AttributeType": "S",
            },
            {
              "AttributeName": "SK",
              "AttributeType": "S",
            },
          ],
          "KeySchema": [
            {
              "AttributeName": "PK",
              "KeyType": "HASH",
            },
            {
              "AttributeName": "SK",
              "KeyType": "RANGE",
            },
          ],
          "ProvisionedThroughput": {
            "ReadCapacityUnits": 5,
            "WriteCapacityUnits": 5,
          },
          "TableName": "BrefStory-table",
        },
        "Type": "AWS::DynamoDB::Table",
        "UpdateReplacePolicy": "Delete",
      },
      ResourcePart.CompleteDefinition,
    )
  );
});</code></pre>
<p>This test ensures that the DynamoDB table is created with the correct attribute definitions, key schema, provisioned throughput, table name, and other properties specified during its creation. By writing such tests, we validate that our CDK infrastructure is provisioned accurately and functions as intended.</p>
<h2>PHP and AWS Services</h2>
<p>Leveraging PHP in a serverless environment opens up new possibilities for interacting with AWS services. In this section, we&#039;ll examine how PHP code seamlessly integrates with various AWS services, following best practices for maintaining clean and modular code architecture.</p>
<p>This is the part where we have fewer serverless needs impacting the code, as the PHP code will follow the same logic we might be using to communicate with AWS services on any other platform overall (there are always some specific use cases).</p>
<p>The reuse of the same existing logic is excellent. It leverages the decision to keep using PHP when moving that workload to Serverless, as the bulk of the knowledge and already proven code would remain as-is. We may escape the trap of classifying that PHP code as legacy as if it should be avoided, terminated or halted.</p>
<p>As a side note, a few external layers of our software architecture are touched if a good software architecture was applied before. Therefore, during the implementation of this architectural change, it should be quick to realise how beneficial and time-saving it is to have a well-architectured application with a balanced decision for patterns, principles, and designs to be applied, ultimately giving flexibility to the application and its features.</p>
<p>The handler is simplified now and should accommodate everything to a class in the direction of following SRP, a principle that we are bringing to the code during the code bites:</p>
<h3>Applications, domains, infrastructure, etc</h3>
<p>Our `PicsumPhotoService` is still orchestrating the business logic. The Single Responsibility Principle and Inversion of Control are applied. We are injecting the specialized services in the constructor:</p>
<pre><code class="language-php">// readonly class PicsumPhotoService
    public function __construct(
        private HttpClientInterface $httpClient,
        private ImageStorageService $storageService,
        private ImageRepository $repository,
    )
    {
    }</code></pre>
<p>Each specialized service has all its dependencies injected in the constructor as well. We can see the factory instantiation:</p>
<pre><code class="language-php">    public static function createPicsumPhotoService(): PicsumPhotoService
    {
        return new PicsumPhotoService(
            HttpClient::create(),
            new S3ImageService(
                new S3Client(),
                getenv('BucketName'),
            ),
            new DynamoDbImageRepository(
                new DynamoDbClient(),
                getenv('TableName'),
            ),
        );
    }</code></pre>
<p>The `ImageStorageService` will handle all image operations, connecting to the AWS Service when appropriate and observing business logic details. This is a slim interface:</p>
<pre><code class="language-php">interface ImageStorageService
{
    public function getImageFromBucket(int $imagePixels): ?array;

    public function saveImage(int $imagePixels, mixed $fetchedImage): void;

    public function createAndPutMetadata(int $imagePixels, array $metadata): PutObjectOutput;
}</code></pre>
<p>Instead of `: PutObjectOutput`, usually we would return a domain object, to not couple the interface with implementation details of using S3 Services, but for simplicity, I did not create a domain object here. It would be preferable though.</p>
<p>The `ImageRepository` will handle all metadata operations. It will save into a repository and observe logic details as well. Following the same principles, this is a slim interface:</p>
<pre><code class="language-php">interface ImageRepository
{
    public function findImage(int $imagePixels): ImageMetadataItem;

    public function addImageMetadata(ImageMetadataItem $imageMetadataItem): PutItemOutput;
}</code></pre>
<p>The `ImageMetadataItem` is a representation of one of the domain objects we have in our codebase.</p>
<pre><code class="language-php">readonly class ImageMetadataItem
{
    public function __construct(public int $imagePixels, public array $metadata)
    {
    }

    public function toDynamoDbItem(): array
    {
        return [
            'PK' => new AttributeValue(['S' => 'IMAGE']),
            'SK' => new AttributeValue(['S' => "PIXELS#{$this->imagePixels}"]),
            'pixels' => new AttributeValue(['N' => "{$this->imagePixels}"]),
            'metadata' => new AttributeValue(['S' => json_encode($this->metadata)]),
            ...ConvertToDynamoDb::item($this->metadata),
        ];
    }

    /**
     * @param array<string, AttributeValue> $item
     */
    public static function fromDynamoDb(array $item): static
    {
        return new static(
            (int) $item['pixels']->getN(),
            (array) json_decode($item['metadata']->getS()),
        );
    }
}</code></pre>
<p>If you check the implementation details, it operates transparently with all the services, business logic and AWS Services without any high couple with them. There are two utility functions:</p>
<ul>
<li><code>toDynamoDbItem</code>: to transform the object into a valid DynamoDb Item to be added</li>
<li><code>fromDynamoDb</code>: to perform the opposite operation, transforming a DynamoDb Item into a domain object</li>
</ul>
<p>The scope of the operation is very clear and does not bring the domain into dependency on those services, as the domain object can be used independently. It does not block any other way of dealing with it, giving the usage with other types of services, such as different databases or APIs. This is very important to the maintainability of the application without sacrificing the ease of readiness as it keeps the context of the utilities in the right place.</p>
<p>If you check all PHP code carefully, Bref is such a great abstraction layer that, removing the code from the handler file, any other line of code can be used as a lambda or a web application interchangeably without changing any line of code. This is very powerful, as you can imagine how you can leverage and migrate some of the existing code to lambda by just creating a handler that will trigger your existing code, if the code is well structured.</p>
<h2>Wrap-up</h2>
<p>It would be simple like that. Check more details in the source code, install it and try it yourself. This project is ready to:</p>
<ul>
<li>Extend lambda function using Bref</li>
<li>Upgrade to use Bref 2.0</li>
<li>Create a DynamoDB table</li>
<li>Test the stack Cloudformation code</li>
<li>Separate the PHP logic</li>
<li>Have PHP communicating with AWS Services</li>
</ul>
<p>Links:</p>
<ul>
<li><a href="https://rafael.bernard-araujo.com/tag/bref-php-aws-story">https://rafael.bernard-araujo.com/tag/bref-php-aws-story</a></li>
<li><a href="https://bref.sh">https://bref.sh</a></li>
<li><a href="https://dev.to/rafaelbernard/a-bref-aws-php-history-part-1-2agn">https://dev.to/rafaelbernard/a-bref-aws-php-history-part-1-2agn</a></li>
<li><a href="https://dev.to/rafaelbernard/a-bref-aws-php-story-part-2-1dhe">https://dev.to/rafaelbernard/a-bref-aws-php-story-part-2-1dhe</a></li>
<li><a href="https://github.com/rafaelbernard/bref-initial-php-aws-story/tree/part-3">https://github.com/rafaelbernard/bref-initial-php-aws-story/tree/part-3</a></li>
<li><a href="https://github.com/rafaelbernard/bref-initial-php-aws-story/compare/tag-part-2...tag-part-3">https://github.com/rafaelbernard/bref-initial-php-aws-story/compare/tag-part-2...tag-part-3</a></li>
<li><a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html">https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Introduction.html</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/a-bref-aws-php-story-part-3.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1980</post-id>	</item>
		<item>
		<title>GitHub Actions workflow for deploying a scheduled task using AWS ECS and EventBridge Scheduler</title>
		<link>https://rafael.bernard-araujo.com/github-actions-workflow-for-deploying-a-scheduled-task-using-aws-ecs-and-eventbridge-scheduler.php</link>
					<comments>https://rafael.bernard-araujo.com/github-actions-workflow-for-deploying-a-scheduled-task-using-aws-ecs-and-eventbridge-scheduler.php#respond</comments>
		
		<dc:creator><![CDATA[rafael]]></dc:creator>
		<pubDate>Mon, 22 Jan 2024 03:58:57 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[aws]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[ecs]]></category>
		<category><![CDATA[EventBridge]]></category>
		<category><![CDATA[eventbridge scheduler]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[github actions]]></category>
		<category><![CDATA[terraform]]></category>
		<guid isPermaLink="false">https://rafael.bernard-araujo.com/?p=1951</guid>

					<description><![CDATA[Cron jobs are repetitive tasks scheduled to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration. Some workloads that are still running on non-containerized platforms (VMs, bare metal, etc.) are suitable to be moved to Serveless with multiple alternatives, depending on the context of each task. Considering AWS services, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Cron jobs are repetitive tasks scheduled to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration. Some workloads that are still running on non-containerized platforms (VMs, bare metal, etc.) are suitable to be moved to Serveless with multiple alternatives, depending on the context of each task.</p>
<p>Considering AWS services, for most of the options EventBridge Scheduler will be used to manage tasks as it is capable of invoking lots of AWS services. One of them is <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/tasks-scheduled-eventbridge-scheduler.html">invoking a containerized application, or ECS task</a>.</p>
<blockquote><p>
Amazon EventBridge Scheduler is a serverless scheduler that allows you to create, run, and manage tasks from one central, managed service. Highly scalable, EventBridge Scheduler allows you to schedule millions of tasks that can invoke more than 270 AWS services and over 6,000 API operations. Without the need to provision and manage infrastructure, or integrate with multiple services, EventBridge Scheduler provides you with the ability to deliver schedules at scale and reduce maintenance costs.<br />
-- <a href="https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html">https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html</a><br />
(EventBridge Scheduler is recommended to be used instead of CloudWatch Scheduler with EventBridge rules)
</p></blockquote>
<p>I worked on an application running on EC2 to ECS that is still using its cron jobs. Cron jobs were migrated to EventBridge Scheduler. Our CI/CD uses GitHub Actions and Terraform. AWS provides actions that can create and deploy a <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html">ECS task definition (the container blueprint)</a> to an ECS service, but there is no action to deploy an ECS task to the EventBridge Scheduler, as the cron task is not executed under a service.</p>
<p>To deploy the new code we have to write some code to the GitHub Action and I think it might benefit others in a similar context. We use Terraform as Infrastructure as a Code, so keep this in mind if you need to adapt to your IaaS solution.</p>
<p>There will be the full Yaml file here but I will comment parts of it separately afterwards.</p>
<pre><code class="language-yaml">name: Deploy Scheduled task XYZ

on:
  workflow_dispatch:
    inputs:
      imageHash:
        description: &#039;Image hash to deploy&#039;
        required: true
        type: string
      environment:
        description: &#039;Environment to run tests against&#039;
        type: environment
        required: true
        default: test

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  APP_NAME: application-name-here
  AWS_REGION: &quot;ap-southeast-2&quot;
  ECR_NAME: ecr-name-here
  ECS_CLUSTER: cluster-name-here
  IMAGE_NAME: application-image-name-here
  TASK_NAME: cron-task-name-here

permissions:
  id-token: write
  contents: read    # This is required for actions/checkout

jobs:
  deploy:
    name: Deploy to ${{ inputs.environment }}
    runs-on: ubuntu-latest
    environment: ${{ inputs.environment }}
    env:
      JOB_ENV: ${{ inputs.environment }}
    steps:
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets[format(&#039;iam_role_to_assume_{0}&#039;, inputs.environment)] }}
          role-session-name: github-ecr-push-workflow-${{ inputs.environment }}
          aws-region: ${{ env.AWS_REGION }}

      - name: Verify image
        run: aws ecr describe-images --repository-name ${{ inputs.environment }}-${{ env.ECR_NAME }}-${{ env.APP_NAME }} --image-ids imageTag=${{ inputs.imageHash }}

      - name: Login to Amazon ECR
        id: login-ecr
        uses: aws-actions/amazon-ecr-login@v2

      - name: Download the task definition ${{ env.TASK_NAME }}
        run: aws ecs describe-task-definition --task-definition ${{ env.TASK_NAME }} --query taskDefinition &gt; task-definition.json

      - name: Fill in the new image ID in the Amazon ECS task definition ${{ env.TASK_NAME }}
        id: task-def-cron
        uses: aws-actions/amazon-ecs-render-task-definition@v1
        env:
          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        with:
          task-definition: task-definition.json
          container-name: ${{ env.TASK_NAME }}
          image: ${{ env.ECR_REGISTRY }}/${{ env.JOB_ENV }}-${{ env.ECR_NAME }}-${{ env.APP_NAME }}:${{ inputs.imageHash }}

      - name: Deploy Amazon ECS task definition ${{ env.TASK_NAME }}
        id: deploy-cron
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def-cron.outputs.task-definition }}
          cluster: ${{ inputs.environment }}-${{ env.ECS_CLUSTER }}

      - name: Checkout infrastructure
        uses: actions/checkout@v4
        with:
          repository: orgnamehere/iaas-repo-here
          ref: main
          path: &#039;./working-path&#039;
          token: ${{ secrets.PAT_TOKEN }}

      - name: Update schedule ${{ env.TASK_NAME }}
        working-directory: &#039;./working-path&#039;
        env:
          GH_TOKEN: ${{ secrets.PAT_TOKEN }}
          INFRASTRUCTURE_FILE: &#039;path/to/your/module/terraform-file-here.tf&#039;
          UNESCAPED_ARN: ${{ steps.deploy-cron.outputs.task-definition-arn }}
        run: |
          # Escape regexp non-safe characters from the ARN to prevent sed to fail
          export ESCAPED_ARN=${UNESCAPED_ARN//:/\\:}
          export ESCAPED_ARN=${ESCAPED_ARN//\//\\/}
          echo &quot;Escaped ARN: $ARN&quot;
          # Retrieve <code>&lt;appl-name&gt;:&lt;version&gt;</code> part of the ARN to use in PR 
          export ARRAY_ARN_PARTS=(${UNESCAPED_ARN//\// })
          export VERSION_PART=${ARRAY_ARN_PARTS[1]}
          export COMMIT_MESSAGE=&quot;DEPLOY: Deployment on ${{ inputs.environment }} - $VERSION_PART&quot;
          # Use task definition version for branch name
          export BRANCH_NAME=&quot;deploy-${VERSION_PART//:/-}&quot;
          git config user.email &quot;deployment-pipeline@example.com&quot;
          git config user.name &quot;Github Actions Pipeline&quot;
          git checkout -b ${BRANCH_NAME}
          sed -i &#039;/task_definition_arn /s/&quot;.*/&#039;&quot;\&quot;${ESCAPED_ARN}&quot;\&quot;&#039;/&#039; $INFRASTRUCTURE_FILE
          git add ${{ env.INFRASTRUCTURE_FILE }}
          git commit -m &quot;$COMMIT_MESSAGE&quot;
          git push --set-upstream origin ${BRANCH_NAME}
          gh pr create --fill --body &quot;- [x] $COMMIT_MESSAGE&quot;</code></pre>
<p>This pipeline will create the scheduled task definition, check out the IaaS repository, change the Scheduler task definition ARN and create a PR in the IaaS repository. We are using that also as a way to have approval to deploy to Production, but it can be automated if needed.</p>
<p>Let's comment on some parts:</p>
<pre><code class="language-yaml">on:
  workflow_dispatch:
    inputs:
      imageHash:
        description: &#039;Image hash to deploy&#039;</code></pre>
<p>It is good to separate the image creation from the deployment. This input is required assuming the image was created and published to the registry. This promotes reusability and flexibility.</p>
<pre><code class="language-yaml">      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          role-to-assume: ${{ secrets[format(&#039;iam_role_to_assume_{0}&#039;, inputs.environment)] }}
          role-session-name: github-ecr-push-workflow-${{ inputs.environment }}
          aws-region: ${{ env.AWS_REGION }}</code></pre>
<p>Environment is a required input and this pipeline can be executed against any environment you defined in your repository.</p>
<pre><code class="language-yaml">      - name: Deploy Amazon ECS task definition ${{ env.TASK_NAME }}
        id: deploy-cron
        uses: aws-actions/amazon-ecs-deploy-task-definition@v1
        with:
          task-definition: ${{ steps.task-def-cron.outputs.task-definition }}
          cluster: ${{ inputs.environment }}-${{ env.ECS_CLUSTER }}</code></pre>
<p>Although the action name is &quot;deploy task definition&quot; it will create the task, but not deploy, as this is only possible when you provide a service input (by the time this article is being written). But we are not deploying to a service though, so the action will only create the task definition, but the EventBridge Scheduler remains calling the same task definition it was invoking before the creation of this task definition.</p>
<pre><code class="language-yaml">      - name: Checkout infrastructure
        uses: actions/checkout@v4
        with:
          repository: orgnamehere/iaas-repo-here
          ref: main
          path: &#039;./working-path&#039;
          token: ${{ secrets.PAT_TOKEN }}</code></pre>
<p>Using <a href="https://docs.aws.amazon.com/cli/latest/reference/scheduler/update-schedule.html">AWS CLI</a> was an alternative we considered, but changing the <code>target</code> of the EventBridge scheduler becomes a little bit confusing and brings some cognitive complexity in case we need to change something. We decided then to fetch the IaaS repository and control the task definition version to be the target of the scheduler in Terraform code, so we could also be sure any dependency that the target change could have would be managed by Terraform, instead of another CLI change in this pipeline. We checkout the IaaS repo and save the path as <code>./working-path</code> to keep the workspace clean. The name is your choice.</p>
<pre><code class="language-yaml">      - name: Update schedule ${{ env.TASK_NAME }}
        working-directory: &#039;./working-path&#039;
        env:
          GH_TOKEN: ${{ secrets.PAT_TOKEN }}
          INFRASTRUCTURE_FILE: &#039;path/to/your/module/terraform-file-here.tf&#039;
          UNESCAPED_ARN: ${{ steps.deploy-cron.outputs.task-definition-arn }}
        run: |
          # Escape regexp non-safe characters from the ARN to prevent sed to fail
          export ESCAPED_ARN=${UNESCAPED_ARN//:/\\:}
          export ESCAPED_ARN=${ESCAPED_ARN//\//\\/}
          echo &quot;Escaped ARN: $ARN&quot;
          # Retrieve <code>&lt;appl-name&gt;:&lt;version&gt;</code> part of the ARN to use in PR 
          export ARRAY_ARN_PARTS=(${UNESCAPED_ARN//\// })
          export VERSION_PART=${ARRAY_ARN_PARTS[1]}
          export COMMIT_MESSAGE=&quot;DEPLOY: Deployment on ${{ inputs.environment }} - $VERSION_PART&quot;
          # Use task definition version for branch name
          export BRANCH_NAME=&quot;deploy-${VERSION_PART//:/-}&quot;
          git config user.email &quot;deployment-pipeline@example.com&quot;
          git config user.name &quot;Github Actions Pipeline&quot;
          git checkout -b ${BRANCH_NAME}
          sed -i &#039;/task_definition_arn /s/&quot;.*/&#039;&quot;\&quot;${ESCAPED_ARN}&quot;\&quot;&#039;/&#039; $INFRASTRUCTURE_FILE
          git add ${{ env.INFRASTRUCTURE_FILE }}
          git commit -m &quot;$COMMIT_MESSAGE&quot;
          git push --set-upstream origin ${BRANCH_NAME}
          gh pr create --fill --body &quot;- [x] $COMMIT_MESSAGE&quot;</code></pre>
<p>This is where we use <code>sed</code> to search and replace the ARN in the terraform code. We scape the ARN before applying sed to not mess with the search regexp.</p>
<p>The terraform code expected to be changed will be something like this:</p>
<pre><code class="language-diff"># main.tf
-        task_definition_arn     = &quot;arn:aws:ecs:ap-southeast-2:123456789012:task-definition/task-definition-cron-name:57&quot;
+       task_definition_arn     = &quot;arn:aws:ecs:ap-southeast-2:123456789012:task-definition/task-definition-cron-name:58&quot;</code></pre>
<p>Links:</p>
<ul>
<li><a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/tasks-scheduled-eventbridge-scheduler.html">https://docs.aws.amazon.com/AmazonECS/latest/userguide/scheduled_tasks-eventbridge-scheduler.html</a></li>
<li><a href="https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html">https://docs.aws.amazon.com/scheduler/latest/UserGuide/what-is-scheduler.html</a></li>
<li><a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html">https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definitions.html</a></li>
<li><a href="https://docs.aws.amazon.com/cli/latest/reference/scheduler/update-schedule.html">https://docs.aws.amazon.com/cli/latest/reference/scheduler/update-schedule.html</a></li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://rafael.bernard-araujo.com/github-actions-workflow-for-deploying-a-scheduled-task-using-aws-ecs-and-eventbridge-scheduler.php/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1951</post-id>	</item>
	</channel>
</rss>
