<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hemanth Gangula</title>
    <description>The latest articles on DEV Community by Hemanth Gangula (@hemanthgangula).</description>
    <link>https://dev.to/hemanthgangula</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1527654%2Fd63425a1-d6fa-4a9b-9648-a53e42bf6988.png</url>
      <title>DEV Community: Hemanth Gangula</title>
      <link>https://dev.to/hemanthgangula</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hemanthgangula"/>
    <language>en</language>
    <item>
      <title>AWS Region Migration: Moving a Live Production App, Solo</title>
      <dc:creator>Hemanth Gangula</dc:creator>
      <pubDate>Wed, 25 Nov 2026 13:30:00 +0000</pubDate>
      <link>https://dev.to/hemanthgangula/aws-region-migration-moving-a-live-production-app-solo-18ih</link>
      <guid>https://dev.to/hemanthgangula/aws-region-migration-moving-a-live-production-app-solo-18ih</guid>
      <description>&lt;p&gt;I was the only engineer, and the AWS setup was older than my time there. It had been running for years, built in the pre-AI days by people who were no longer around to ask. Then it had to move from us-east-2 to us-west-2, and the AWS region migration was mine to plan and run, solo.&lt;/p&gt;

&lt;p&gt;There was no Terraform and no CloudFormation. There were two EC2 instances (one for the app, one for a WordPress site hosted separately), an RDS MySQL database, a secret in Secrets Manager, and a pile of DNS records where nobody could say which ones were still in use. Here are the plan, the commands, and the one line of code that would have broken the whole thing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Why:&lt;/strong&gt; about 80% of the traffic came from the US west, but the servers were in Ohio.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How:&lt;/strong&gt; copied the EC2 servers across regions as images, rehearsed the database move with a snapshot, then did the real cutover with a fresh mysqldump in a late-night maintenance window.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Result:&lt;/strong&gt; one line of code changed, no data lost, a fast rollback kept ready, and the old region cleaned up so it stopped billing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The surprise:&lt;/strong&gt; a 504 error five days later that my own testing had hidden.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why move regions at all
&lt;/h2&gt;

&lt;p&gt;About 80% of the traffic came from the western US, which is also where the team is. The servers sat in us-east-2, Ohio, for no deeper reason than that someone picked it when the business was starting out. The business grew, most of the users turned out to be in the west, and the servers stayed in Ohio. Every request paid for that distance.&lt;/p&gt;

&lt;p&gt;My first question was whether moving only the servers would be enough. It wouldn't: the app talks to the database on almost every request, so a server in the west with the database still in Ohio just moves the slow hop somewhere else. The app servers and the database had to move together.&lt;/p&gt;

&lt;p&gt;The obvious alternative was a CDN. A CDN caches files close to users, but this app goes to the database on almost every request, so most of each request would still travel to Ohio. A CDN also earns its keep when users are spread across many regions. Ours were concentrated in very few, so moving the servers next to them fixed the problem without adding another service to run and pay for.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup I was moving
&lt;/h2&gt;

&lt;p&gt;Nothing fancy, and that helped:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;An app server on EC2&lt;/strong&gt; running Django behind gunicorn and nginx, a Next.js frontend, Redis and Celery, each as a systemd service.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A second EC2 instance for a WordPress site&lt;/strong&gt;, hosted separately from the app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;One RDS MySQL instance&lt;/strong&gt; holding two databases: one for the app and one for WordPress.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Secrets Manager&lt;/strong&gt; holding the database credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Route 53&lt;/strong&gt; for DNS.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;S3 stayed where it was. Bucket URLs don't care which region the app runs in, and moving a bucket means creating a new one under a new name, because bucket names are globally unique. The cross-region traffic it adds costs very little, so moving it wasn't worth the risk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I didn't rebuild it with Terraform first
&lt;/h2&gt;

&lt;p&gt;I didn't write Terraform for this, and I didn't reinstall anything. The servers had years of manual setup on them, and I couldn't be sure I knew all of it. Rebuilding from scratch meant finding every package, config file and cron job someone had added by hand. That takes a long time, and missing one would only show up in production.&lt;/p&gt;

&lt;p&gt;It also wasn't the right trade for a startup this size. A faster app mattered more to customers than a tidy infrastructure repo, so that's where the effort went.&lt;/p&gt;

&lt;p&gt;So I replicated instead of rebuilding. I copied the server images across regions, which carried every file and service over exactly as it was. Infrastructure as code is still on my list. It just wasn't the safe first move for a stack I hadn't built myself.&lt;/p&gt;

&lt;h2&gt;
  
  
  The hardcoded region that would have broken everything
&lt;/h2&gt;

&lt;p&gt;During the dry run I found this in &lt;code&gt;settings.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;boto3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"secretsmanager"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region_name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"us-east-2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The app loaded its database credentials from Secrets Manager, and the region was hardcoded. In the new region, the app would have kept reading the old region's secret, which pointed at the old database in Ohio. Best case, the new servers can't reach that database and the app fails on cutover night. Worst case, they can, the app quietly keeps writing to the old database, and every one of those writes disappears the day the old region is cleaned up.&lt;/p&gt;

&lt;p&gt;That one line was the only code change the whole migration needed. If you're planning a move, grep for your old region name before anything else, in the repo and on the servers themselves, because env files, &lt;code&gt;~/.aws/config&lt;/code&gt; and systemd units travel inside the server image too:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;grep -rn &lt;span class="s2"&gt;"us-east-2"&lt;/span&gt; .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 1: Copy EC2 instances to another region with AMIs
&lt;/h2&gt;

&lt;p&gt;For each EC2 instance, I created an image (AMI) without rebooting it, waited for it to finish, then copied it to us-west-2:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ec2 create-image \
  --region us-east-2 \
  --instance-id i-0123456789abcdef0 \
  --name &lt;span class="s2"&gt;"app-server-migration"&lt;/span&gt; \
  --no-reboot

aws ec2 &lt;span class="nb"&gt;wait&lt;/span&gt; image-available --region us-east-2 --image-ids ami-0123456789abcdef0

aws ec2 copy-image \
  --region us-west-2 \
  --source-region us-east-2 \
  --source-image-id ami-0123456789abcdef0 \
  --name &lt;span class="s2"&gt;"app-server"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I launched new instances from the copied images, gave each an Elastic IP, and checked that every service came up. On the app server that meant gunicorn, the frontend, nginx, Redis and Celery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo systemctl status gunicorn nginx redis-server celery --no-pager
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A copied server brings its cron jobs and scheduled tasks with it, so if you do this, keep them switched off on the new servers until cutover, or they run in both regions.&lt;/p&gt;

&lt;p&gt;I copied the WordPress server first. It was the less critical of the two, so it was the safest place to prove the image-copy approach worked before touching the app server.&lt;/p&gt;

&lt;p&gt;To test the new servers without touching real DNS, I pointed the domain at their Elastic IPs in &lt;code&gt;/etc/hosts&lt;/code&gt; on my own machine:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# /etc/hosts on my machine only&lt;/span&gt;
203.0.113.10   example.com
203.0.113.10   www.example.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That sends only my own browser to the new region, while every user still reaches the old one. The real DNS didn't change until cutover night. This trick comes back to bite me later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Security groups and IAM roles
&lt;/h2&gt;

&lt;p&gt;I created two security groups in the new region. The web server's group allows HTTP and HTTPS. The database's group accepts MySQL connections only from the web server's group, so the database is never reachable from the internet.&lt;/p&gt;

&lt;p&gt;IAM is global, so roles don't need moving between regions. What the new servers did need was a role attached to them, because an AMI doesn't carry one. I used the move to switch the servers from stored access keys to IAM roles: I created the roles by hand with the AWS CLI and attached them to the new instances, so the servers reach other AWS services through a role instead of a key on the box. Once the move was done, I removed the old IAM users.&lt;/p&gt;

&lt;p&gt;One more thing to check: any IAM policy that names a secret by its ARN. The ARN includes the region, so the policy has to point at the new secret.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Recreate the secrets in the new region
&lt;/h2&gt;

&lt;p&gt;Secrets Manager can replicate a secret to another region. I created a separate one in us-west-2 instead, with the same keys, and left the old one untouched as part of the rollback:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws secretsmanager create-secret \
  --region us-west-2 \
  --name my-app/prod/database \
  --secret-string file://secret.json

rm secret.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database host inside the secret was updated later, once the new RDS instance existed.&lt;/p&gt;

&lt;p&gt;After the move, I also rotated the database password and the keys for every third-party integration as routine security maintenance, so no credential in the new region was one that had lived in the old one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Move RDS MySQL to another region, as a dry run first
&lt;/h2&gt;

&lt;p&gt;Before touching production, I wanted a real copy of the database running in the new region. So I took a manual snapshot, copied it across, and restored it into the new VPC with the new security group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws rds create-db-snapshot \
  --region us-east-2 \
  --db-instance-identifier my-prod-db \
  --db-snapshot-identifier premigration

aws rds &lt;span class="nb"&gt;wait&lt;/span&gt; db-snapshot-available --region us-east-2 --db-snapshot-identifier premigration

aws rds copy-db-snapshot \
  --region us-west-2 \
  --source-region us-east-2 \
  --source-db-snapshot-identifier arn:aws:rds:us-east-2:123456789012:snapshot:premigration \
  --target-db-snapshot-identifier premigration-west

aws rds restore-db-instance-from-db-snapshot \
  --region us-west-2 \
  --db-instance-identifier my-prod-db \
  --db-snapshot-identifier premigration-west \
  --db-subnet-group-name my-db-subnets \
  --vpc-security-group-ids sg-0fedcba9876543210 \
  --no-publicly-accessible
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your snapshot is encrypted, the copy also needs &lt;code&gt;--kms-key-id&lt;/code&gt; with a key in the target region. If you use custom parameter or option groups, create them in the new region and pass them to the restore, or you get the defaults.&lt;/p&gt;

&lt;p&gt;I ran some of these steps from AWS CloudShell and others in the console, so I could see exactly what each one changed.&lt;/p&gt;

&lt;p&gt;This gave me a full working stack in us-west-2, app servers and database, that nobody was using yet. I ran through the app end to end on it. This was the dry run, and it's where the hardcoded region showed up.&lt;/p&gt;

&lt;p&gt;It showed one more thing. For part of the testing I put the new stack on a temporary subdomain, and logging in kept throwing me back out. The login flow's redirect URLs lived in the secret and in the auth provider's allowed list, and they all pointed at the production domain. Redirect URLs are per-environment config, just like the region.&lt;/p&gt;

&lt;p&gt;Once testing was done, I removed the temporary subdomain, its DNS record and its certificate, so nothing extra was left pointing at the new servers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Keep shipping while you migrate
&lt;/h2&gt;

&lt;p&gt;Releases didn't stop during the migration. The CI/CD pipeline had a branch per region, east and west, and every release went to both, so it landed on the old servers and the new ones. By cutover night, the new region was running the same code as production, and the business never had to pause a release for the move.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Cutover night with mysqldump from RDS
&lt;/h2&gt;

&lt;p&gt;The day before, I lowered the TTL on the three A records that pointed at the servers from 300 seconds to 60. That has to happen well before cutover, so the old 300-second answers expire everywhere. A short TTL makes both the switch and a rollback take effect in about a minute.&lt;/p&gt;

&lt;p&gt;On the night, at our lowest-traffic hour, I worked from a checklist, step by step.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Stop every writer
&lt;/h3&gt;

&lt;p&gt;I stopped gunicorn and Celery on the old app server and stopped the WordPress site on its own server, since both write to the same RDS instance. I waited two minutes for in-flight requests, then checked that nothing was still connected to the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo systemctl stop gunicorn celery
sudo ss -tn &lt;span class="o"&gt;|&lt;/span&gt; grep 3306
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ss&lt;/code&gt; only shows connections from the server you run it on. Running &lt;code&gt;SHOW PROCESSLIST;&lt;/code&gt; on the database itself covers every server at once.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Dump and restore
&lt;/h3&gt;

&lt;p&gt;The snapshot from the dry run was already out of date, so the final copy was a fresh dump of both databases. The command in my runbook failed straight away:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mysqldump: Couldn't execute 'FLUSH TABLES WITH READ LOCK': Access denied for user 'admin'@'%'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;RDS doesn't give the master user the privilege for a global read lock. Newer versions of mysqldump ask for that lock with &lt;code&gt;--single-transaction&lt;/code&gt; when GTIDs are on, to record a consistent GTID position. &lt;code&gt;--set-gtid-purged=OFF&lt;/code&gt; removes the need for it, and since every writer was already stopped, nothing needed the lock anyway:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mysqldump -h &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;old-rds-endpoint&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -u admin -p \
  --single-transaction --no-tablespaces \
  --set-gtid-purged=OFF --skip-lock-tables \
  --routines --triggers \
  --databases app_db wordpress &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; cutover.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you dump from RDS, test the exact command before cutover night. Mine had only ever run against a database where I had full privileges.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Move the dump server to server
&lt;/h3&gt;

&lt;p&gt;The 1.4 GB file goes straight from the old app server to the new one, EC2 to EC2, and never leaves AWS. That needs two permissions, both set up the day before and tested with a small file, because a copy that fails during the maintenance window eats into it.&lt;/p&gt;

&lt;p&gt;First, the new server's security group has to accept SSH from the old server's public IP, and only from it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ec2 authorize-security-group-ingress \
  --region us-west-2 \
  --group-id sg-0123456789abcdef0 \
  --protocol tcp --port 22 \
  --cidr &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;old-app-server-ip&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/32
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, the old server needs a key the new server trusts. Use a dedicated key for the migration, never a personal key copied onto a server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# on the old app server&lt;/span&gt;
ssh-keygen -t ed25519 -f ~/.ssh/migration -N &lt;span class="s2"&gt;""&lt;/span&gt;
cat ~/.ssh/migration.pub
&lt;span class="c"&gt;# add that line to ~/.ssh/authorized_keys on the new app server&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the copy is one command, and the restore runs on the new app server, because the new database only accepts connections from that server's security group:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# on the old app server&lt;/span&gt;
scp -i ~/.ssh/migration cutover.sql ubuntu@&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;new-app-server&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;:~/

&lt;span class="c"&gt;# on the new app server&lt;/span&gt;
chmod 600 ~/cutover.sql
mysql -h &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;new-rds-endpoint&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; -u admin -p &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; ~/cutover.sql
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When it's done, remove the security group rule, the key from &lt;code&gt;authorized_keys&lt;/code&gt;, and the dump files on both servers. A production dump shouldn't sit around anywhere.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Compare before you flip
&lt;/h3&gt;

&lt;p&gt;I didn't touch DNS until the row counts matched on both sides, for the main tables and the total table count. If they didn't match, the plan was to stop and investigate, with the old region still serving. Use exact &lt;code&gt;SELECT COUNT(*)&lt;/code&gt; queries for this, because the row counts in &lt;code&gt;information_schema&lt;/code&gt; are only estimates.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Restart and flip DNS
&lt;/h3&gt;

&lt;p&gt;I restarted gunicorn and Celery on the new server, then updated the three A records in Route 53 to the new Elastic IPs and checked them with &lt;code&gt;dig&lt;/code&gt; and &lt;code&gt;curl&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rollback I didn't need
&lt;/h2&gt;

&lt;p&gt;The old region stayed running for 48 hours after the switch. If anything went wrong, rollback meant starting the old services again and one DNS change back to the old IPs, live within a minute thanks to the 60-second TTL.&lt;/p&gt;

&lt;p&gt;That rollback is only clean in the first hours. Once users write to the new database, going back means copying those writes back too, or losing them. So I watched the gunicorn, nginx and Celery logs closely through those first hours. I never had to roll back.&lt;/p&gt;

&lt;p&gt;No data was lost. The only thing users could have noticed was the planned maintenance window.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug that waited five days
&lt;/h2&gt;

&lt;p&gt;Five days later the WordPress site started returning 504 Gateway Time-out. The app server's nginx forwards &lt;code&gt;/blog&lt;/code&gt; to the WordPress server, and its upstream said:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight nginx"&gt;&lt;code&gt;&lt;span class="n"&gt;upstream&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt; &lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;443&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The old WordPress server had a certificate and listened on 443. The new one only listened on 80. Before cutover this worked, because the hostname still resolved to the old server. Once nginx picked up the new IP for that hostname, it was knocking on port 443 of a server that only listened on 80, and every request timed out. nginx resolves hostnames in an upstream block when it starts or reloads, not on every request, which is one reason a bug like this can show up days after the DNS change.&lt;/p&gt;

&lt;p&gt;This is where the &lt;code&gt;/etc/hosts&lt;/code&gt; trick bit me. It changed what my own machine resolved, not what the servers resolved when they talked to each other, so my testing hid the problem completely. The fix was one line, pointing the upstream at port 80. If you do the same, point it at the server's private IP inside the VPC, so the plain HTTP hop never leaves the private network.&lt;/p&gt;

&lt;p&gt;After a DNS flip, test the traffic between your servers too, because your browser only shows you half of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd do differently
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Put the region in config from day one.&lt;/strong&gt; The hardcoded &lt;code&gt;region_name&lt;/code&gt; was the riskiest line in the whole project. Next time it goes in an environment variable from the start, so a region change is a config change, not a code change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Write the new region as code.&lt;/strong&gt; I replicated by hand because I didn't fully know the old setup. After this migration I do, and the next region should be Terraform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Clean up the old region on a deadline.&lt;/strong&gt; About a week after cutover I cleaned out the old region: the instances, the database and its manual and automated snapshots, the two migration images, the Elastic IPs, and a handful of S3 buckets left over from old experiments that nobody used. Old resources in a region you no longer use keep billing until someone goes looking, so put the cleanup on the calendar before you start.&lt;/p&gt;

&lt;h2&gt;
  
  
  AWS region migration checklist
&lt;/h2&gt;

&lt;p&gt;If you're about to do this yourself, here's the short version:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Grep the code, config and servers for the old region name.&lt;/li&gt;
&lt;li&gt;Move the app servers and the database together, or the slow hop just moves.&lt;/li&gt;
&lt;li&gt;Copy EC2 instances as AMIs instead of rebuilding a setup you didn't build, and keep copied cron jobs off until cutover.&lt;/li&gt;
&lt;li&gt;Attach IAM roles to the new instances and update any policy that names a regional ARN.&lt;/li&gt;
&lt;li&gt;Recreate or replicate secrets in the new region.&lt;/li&gt;
&lt;li&gt;Rehearse the database move with a snapshot, restored into the right subnet group and security group, and run the whole app on it.&lt;/li&gt;
&lt;li&gt;Check redirect URLs and other per-environment config.&lt;/li&gt;
&lt;li&gt;Remember that new Elastic IPs break any third-party allowlist that has your old IPs.&lt;/li&gt;
&lt;li&gt;Lower the DNS TTL to 60 seconds the day before.&lt;/li&gt;
&lt;li&gt;Test your exact mysqldump command against RDS before cutover night.&lt;/li&gt;
&lt;li&gt;Stop every writer, compare exact row counts, then flip DNS, with the old region kept as a rollback.&lt;/li&gt;
&lt;li&gt;After the flip, test server-to-server traffic, not just your browser.&lt;/li&gt;
&lt;li&gt;Put the old region's cleanup on the calendar.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Moving someone else's stack piece by piece is how I finally learned how it was wired. That's also why the next region will be Terraform.&lt;/p&gt;

&lt;p&gt;Before moving your own stack to another region, what would you grep for first?&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>mysql</category>
      <category>cloud</category>
    </item>
    <item>
      <title>How I Ship systemd Logs to CloudWatch for $0 (Django + Celery on EC2)</title>
      <dc:creator>Hemanth Gangula</dc:creator>
      <pubDate>Tue, 21 Apr 2026 18:32:46 +0000</pubDate>
      <link>https://dev.to/hemanthgangula/how-i-ship-systemd-logs-to-cloudwatch-for-0-django-celery-on-ec2-1844</link>
      <guid>https://dev.to/hemanthgangula/how-i-ship-systemd-logs-to-cloudwatch-for-0-django-celery-on-ec2-1844</guid>
      <description>&lt;p&gt;Running Django and Celery as systemd services on EC2 and tired of SSH-ing in to debug? Here's the exact setup I used to ship logs to CloudWatch Logs for free, without touching a single production service. Real commands, real configs, real gotchas included.&lt;/p&gt;

&lt;p&gt;I was SSH-ing into production to debug. Every. Single. Time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;journalctl -u gunicorn.service -f&lt;/code&gt; was my monitoring stack. It worked until I needed to see what happened three hours ago on a Celery task that silently failed. SSH in, scroll up, hope journald hadn't rotated it yet.&lt;/p&gt;

&lt;p&gt;I needed logs off the server, queryable, and retained without paying $40/month for a logging SaaS I'd barely use. So I set up CloudWatch Logs with the Amazon CloudWatch Agent. Total monthly cost: $0.&lt;/p&gt;

&lt;p&gt;This is exactly how I did it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the CloudWatch Agent Actually Does
&lt;/h2&gt;

&lt;p&gt;Before installing anything, understand the mechanism.&lt;/p&gt;

&lt;p&gt;The agent is not a real-time forwarder. It wakes up every 5 seconds, reads new bytes from your log file since the last read using a stored file offset, compresses them, and ships one HTTPS batch to CloudWatch.&lt;/p&gt;

&lt;p&gt;Your application has zero awareness the agent exists. It reads from outside your process, like &lt;code&gt;tail -f&lt;/code&gt; does. Django doesn't know. Daphne doesn't know. Celery doesn't know.&lt;/p&gt;

&lt;p&gt;Resource profile on a production EC2: roughly 0.1% CPU at steady state with a tiny spike every 5 seconds on flush, 35-60 MB RAM fixed regardless of log volume, and a few KB of compressed network per flush. Not worth worrying about.&lt;/p&gt;

&lt;p&gt;Logs show up in CloudWatch within 5-15 seconds of being written. Not real-time, near-real-time. Good enough for debugging a failed task after the fact.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Cost Reality
&lt;/h2&gt;

&lt;p&gt;CloudWatch Logs free tier per account per month: 5 GB ingestion, 5 GB storage.&lt;/p&gt;

&lt;p&gt;A typical Django/Celery setup at &lt;code&gt;INFO&lt;/code&gt; log level generates roughly 80-100 MB/month. With 7-day retention, only about 20 MB sits in storage at any given moment. The free tier covers this comfortably.&lt;/p&gt;

&lt;p&gt;The only thing that'll push you over is leaving Django at &lt;code&gt;DEBUG&lt;/code&gt; level in production. That multiplies log volume 10-50x instantly. Keep Django at &lt;code&gt;INFO&lt;/code&gt;, Celery at &lt;code&gt;ERROR&lt;/code&gt;. You won't see a bill.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Not Point the Agent Directly at journald
&lt;/h2&gt;

&lt;p&gt;Both my services, Daphne and Celery, log through journald. The obvious config would be a &lt;code&gt;journald&lt;/code&gt; collector block in the agent JSON:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"journald"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"collect_list"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"units"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"gunicorn.service"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"log_group_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/prod/daphne"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I tried this first. The agent threw:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;E! Invalid Json input schema.
Under path : /logs/logs_collected | Error : Additional property journald is not allowed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Agent version &lt;code&gt;1.300064&lt;/code&gt; doesn't support the journald collector in its config schema. The docs don't make this obvious upfront.&lt;/p&gt;

&lt;p&gt;So before starting the setup, understand the actual flow you're building:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwkycaglp4tg6029tosm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxwkycaglp4tg6029tosm.jpg" alt="Flowchart showing how systemd service logs flow from journald through a piping service and log file on disk to the CloudWatch Agent and finally into AWS CloudWatch Logs on an EC2 instance&lt;br&gt;
" width="800" height="689"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Two hops instead of one, but completely non-destructive. You don't touch your production services at all during setup. journald still captures everything in parallel, so you keep local history and get CloudWatch shipping simultaneously.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: IAM Role on the EC2
&lt;/h2&gt;

&lt;p&gt;The agent needs AWS credentials to call the CloudWatch API. Attach an IAM role to the EC2 instance directly — not access keys in a config file.&lt;/p&gt;

&lt;p&gt;Create a role in IAM, trusted entity is EC2, attach the &lt;code&gt;CloudWatchAgentServerPolicy&lt;/code&gt; managed policy. Then go to your EC2 instance, Actions, Security, Modify IAM role, attach it.&lt;/p&gt;

&lt;p&gt;Verify it worked from the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s http://169.254.169.254/latest/meta-data/iam/info &lt;span class="o"&gt;|&lt;/span&gt; python3 -m json.tool
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see your &lt;code&gt;InstanceProfileArn&lt;/code&gt; in the output. That IP &lt;code&gt;169.254.169.254&lt;/code&gt; is the EC2 Instance Metadata Service — a link-local address only reachable from inside the instance itself. The agent hits this on startup to get temporary credentials automatically. No keys, no secrets files sitting on disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Install the Agent
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
sudo dpkg -i amazon-cloudwatch-agent.deb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Create the Log Files
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo mkdir -p /var/log/app
sudo touch /var/log/app/daphne.log /var/log/app/celery.log
sudo chown ubuntu:www-data /var/log/app/daphne.log /var/log/app/celery.log
sudo chmod 644 /var/log/app/daphne.log /var/log/app/celery.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Write the Agent Config
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo tee /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'EOF'&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"agent"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"run_as_user"&lt;/span&gt;: &lt;span class="s2"&gt;"cwagent"&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;,
  &lt;span class="s2"&gt;"logs"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"logs_collected"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"files"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"collect_list"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
          &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"file_path"&lt;/span&gt;: &lt;span class="s2"&gt;"/var/log/app/daphne.log"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_group_name"&lt;/span&gt;: &lt;span class="s2"&gt;"/prod/daphne"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_stream_name"&lt;/span&gt;: &lt;span class="s2"&gt;"{instance_id}"&lt;/span&gt;,
            &lt;span class="s2"&gt;"retention_in_days"&lt;/span&gt;: 7
          &lt;span class="o"&gt;}&lt;/span&gt;,
          &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"file_path"&lt;/span&gt;: &lt;span class="s2"&gt;"/var/log/app/celery.log"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_group_name"&lt;/span&gt;: &lt;span class="s2"&gt;"/prod/celery"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_stream_name"&lt;/span&gt;: &lt;span class="s2"&gt;"{instance_id}"&lt;/span&gt;,
            &lt;span class="s2"&gt;"retention_in_days"&lt;/span&gt;: 7
          &lt;span class="o"&gt;}&lt;/span&gt;,
          &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"file_path"&lt;/span&gt;: &lt;span class="s2"&gt;"/var/log/nginx/access.log"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_group_name"&lt;/span&gt;: &lt;span class="s2"&gt;"/prod/nginx-access"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_stream_name"&lt;/span&gt;: &lt;span class="s2"&gt;"{instance_id}"&lt;/span&gt;,
            &lt;span class="s2"&gt;"retention_in_days"&lt;/span&gt;: 7
          &lt;span class="o"&gt;}&lt;/span&gt;,
          &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"file_path"&lt;/span&gt;: &lt;span class="s2"&gt;"/var/log/nginx/error.log"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_group_name"&lt;/span&gt;: &lt;span class="s2"&gt;"/prod/nginx-error"&lt;/span&gt;,
            &lt;span class="s2"&gt;"log_stream_name"&lt;/span&gt;: &lt;span class="s2"&gt;"{instance_id}"&lt;/span&gt;,
            &lt;span class="s2"&gt;"retention_in_days"&lt;/span&gt;: 7
          &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;]&lt;/span&gt;
      &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;,
    &lt;span class="s2"&gt;"force_flush_interval"&lt;/span&gt;: 5
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;retention_in_days&lt;/code&gt; set here means the agent creates the log group with that retention automatically. No manual console clicks needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Create the Piping Services
&lt;/h2&gt;

&lt;p&gt;One small systemd service per application. Each one runs &lt;code&gt;journalctl -f&lt;/code&gt; for that unit and appends output to the log file the agent reads.&lt;/p&gt;

&lt;p&gt;For Daphne:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo tee /etc/systemd/system/daphne-log-pipe.service &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'EOF'&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;Unit&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nv"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;Pipe gunicorn journald logs to file
&lt;span class="nv"&gt;After&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;gunicorn.service
&lt;span class="nv"&gt;BindsTo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;gunicorn.service

&lt;span class="o"&gt;[&lt;/span&gt;Service&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nv"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/bin/bash -c &lt;span class="s2"&gt;"journalctl -u gunicorn.service -f --no-pager -o short-iso &amp;gt;&amp;gt; /var/log/app/daphne.log"&lt;/span&gt;
&lt;span class="nv"&gt;Restart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;always
&lt;span class="nv"&gt;RestartSec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3
&lt;span class="nv"&gt;User&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ubuntu

&lt;span class="o"&gt;[&lt;/span&gt;Install&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nv"&gt;WantedBy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;multi-user.target
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Celery:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo tee /etc/systemd/system/celery-log-pipe.service &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; /dev/null &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s1"&gt;'EOF'&lt;/span&gt;
&lt;span class="o"&gt;[&lt;/span&gt;Unit&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nv"&gt;Description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;Pipe celery journald logs to file
&lt;span class="nv"&gt;After&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;celery.service
&lt;span class="nv"&gt;BindsTo&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;celery.service

&lt;span class="o"&gt;[&lt;/span&gt;Service&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nv"&gt;ExecStart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/bin/bash -c &lt;span class="s2"&gt;"journalctl -u celery.service -f --no-pager -o short-iso &amp;gt;&amp;gt; /var/log/app/celery.log"&lt;/span&gt;
&lt;span class="nv"&gt;Restart&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;always
&lt;span class="nv"&gt;RestartSec&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3
&lt;span class="nv"&gt;User&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;ubuntu

&lt;span class="o"&gt;[&lt;/span&gt;Install&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nv"&gt;WantedBy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;multi-user.target
EOF
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;BindsTo=gunicorn.service&lt;/code&gt; means if the main service stops, the piping service stops with it. Clean dependency, no orphaned processes.&lt;/p&gt;

&lt;p&gt;Enable and start:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo systemctl daemon-reload
sudo systemctl &lt;span class="nb"&gt;enable&lt;/span&gt; daphne-log-pipe.service celery-log-pipe.service
sudo systemctl start daphne-log-pipe.service celery-log-pipe.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify lines are flowing into the files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sleep 10 &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; tail -5 /var/log/app/daphne.log &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; tail -5 /var/log/app/celery.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see actual request lines from Daphne and task output from Celery. If the files are empty after 10 seconds, check &lt;code&gt;systemctl status daphne-log-pipe.service&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 6: Start the Agent
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
  -a fetch-config \
  -m ec2 \
  -c file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json \
  -s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check status:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a status
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"status"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"running"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"starttime"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"2026-04-13T05:39:11+00:00"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"configstatus"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"configured"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"1.300064.1b1344"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 7: Verify Logs Are in CloudWatch
&lt;/h2&gt;

&lt;p&gt;From AWS CloudShell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws logs filter-log-events \
  --log-group-name /prod/daphne \
  --region us-west-2 \
  --limit 5 \
  --output table \
  --query &lt;span class="s1"&gt;'events[*].message'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your actual API request lines should appear. If the console shows &lt;code&gt;storedBytes: 0&lt;/code&gt;, wait 2-3 minutes and refresh. The agent flushes every 5 seconds but CloudWatch takes a moment to index.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where Logs Live Now
&lt;/h2&gt;

&lt;p&gt;Three places simultaneously, independently:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Location&lt;/th&gt;
&lt;th&gt;Retention&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;journald on EC2&lt;/td&gt;
&lt;td&gt;capped (set via journald.conf)&lt;/td&gt;
&lt;td&gt;fast local debugging&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;/var/log/app/*.log&lt;/code&gt; on EC2&lt;/td&gt;
&lt;td&gt;logrotate weekly&lt;/td&gt;
&lt;td&gt;agent reads from here&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CloudWatch Logs&lt;/td&gt;
&lt;td&gt;7 days&lt;/td&gt;
&lt;td&gt;off-server, queryable&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The agent is a photocopier. It never deletes, never moves, never touches the original log. Stop the agent tomorrow and your EC2 logs are completely unaffected. If your EC2 dies, CloudWatch still has the last 7 days.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cleaning This Up Later
&lt;/h2&gt;

&lt;p&gt;The piping services are temporary scaffolding. Once you've confirmed everything is stable over a few days, do the cleaner version during a low-traffic window.&lt;/p&gt;

&lt;p&gt;Add these lines directly to your gunicorn and celery service files under &lt;code&gt;[Service]&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ini"&gt;&lt;code&gt;&lt;span class="na"&gt;StandardOutput&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;daphne&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;span class="na"&gt;StandardError&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;daphne&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;log&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reload and restart both services, verify logs still flow, then remove the piping services:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo systemctl stop daphne-log-pipe.service celery-log-pipe.service
sudo systemctl disable daphne-log-pipe.service celery-log-pipe.service
sudo rm /etc/systemd/system/daphne-log-pipe.service
sudo rm /etc/systemd/system/celery-log-pipe.service
sudo systemctl daemon-reload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Direct write, one less moving part. The reason I didn't do this on day one is that it requires a production service restart. Validate the full pipeline first, clean up after.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Can Build on Top
&lt;/h2&gt;

&lt;p&gt;With logs in CloudWatch, the real value starts.&lt;/p&gt;

&lt;p&gt;Metric filters let you turn log patterns into metrics. Every line matching &lt;code&gt;" 500 "&lt;/code&gt; in your daphne logs increments an &lt;code&gt;HTTP5xxErrors&lt;/code&gt; counter. Every &lt;code&gt;ERROR&lt;/code&gt; in Celery increments a &lt;code&gt;CeleryTaskFailures&lt;/code&gt; counter. From there you can build dashboards, set alarms, get SNS notifications when error rates spike.&lt;/p&gt;

&lt;p&gt;Logs Insights is the other useful piece. It's SQL for your log groups:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;fields @timestamp, @message
&lt;span class="o"&gt;|&lt;/span&gt; filter @message like /api\/auth\/login/
&lt;span class="o"&gt;|&lt;/span&gt; filter @message like &lt;span class="s2"&gt;"403"&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; stats count&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; by bin&lt;span class="o"&gt;(&lt;/span&gt;5m&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That query shows login failures per 5-minute window. Took 30 seconds to write, runs in 2 seconds. Try doing that with &lt;code&gt;journalctl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The piping service pattern isn't the cleanest architecture forever, but it got logs off the server without a single production restart on day one. If you're still SSH-ing into EC2 to read logs, this setup takes about 30 minutes and the free tier covers most production workloads. Do it before you actually need it at 2 AM.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>linux</category>
      <category>python</category>
    </item>
  </channel>
</rss>
