A Dynamic IP Allowlist for an AWS Lightsail Server
AWS keeps running promotions. New accounts get $200 in credit ($100 fixed plus $100 for completing tasks). I did not need an overseas VPS much before, but lately a small box has started to look useful. Lightsail at $5 or $7 is hard to beat: 512 MB or 1 GB of RAM, 1 TB or 2 TB of transfer per month, and 20 GB or 40 GB of disk.
The annoying part of running your own server is the ports. Open them and the whole internet scans you. Keep them closed and you cannot use the box yourself. Since this machine is only for me, the cleaner fix is an IP allowlist at the firewall.
1. Turn on a firewall allowlist
Add one more layer: only the current public IPs of home and the phone may reach TCP 443 and UDP 443. Everything else is dropped at the firewall.
What that buys:
- Connections are limited to networks I control.
- Knowing the address and the port is not enough to connect or scan from an arbitrary public IP.
- nftables owns the allowlist. TCP and UDP share the same IPv4 and IPv6 sets.
- Home and the phone report separately, so they can be maintained separately.
An allowlist is not magic. Home broadband and mobile networks both change public IPs. If you only edit the rules by hand, a change can lock you out. The hard part is not adding the allowlist. It is keeping the allowlist in step with the IP.
2. Uploading IPs: Cloudflare Worker + R2
Home and the phone are stored as two objects:
home.jsonmobile.json
API host:
1https://xxxxxxxxx
Worker routes:
| Method | Path | Purpose |
|---|---|---|
| PUT | /home | Report the home IP |
| PUT | /mobile | Report the phone IP |
| GET | /home | Read the home IP |
| GET | /mobile | Read the phone IP |
Reporters authenticate with HOME_TOKEN and MOBILE_TOKEN. The server reads with AWS_TOKEN.
The Worker writes what it receives into Cloudflare R2. Each object carries IPv4, IPv6, updated_at, and similar fields.
The split is straightforward:
- When home sees an IP change, it updates the home object.
- When the phone sees an IP change, it updates the mobile object.
- The Worker checks the request and the IP format, then writes R2.
- Lightsail never has to accept a connection from the home network or the phone. It only needs to read the Worker API.
Who reports and how the firewall is updated are no longer the same problem.
3. How the server learns about an update
The first version was blunt: a script on the server, every 10 minutes.
It pulled home and mobile from the Worker API and compared them with the nftables sets. New addresses were added. If nothing was new, it stopped.
That works, and it is simple. It is also polling:
- There is lag. Right after an IP changes, the server may not know for almost 10 minutes.
- Most checks do nothing. When the IP is stable, the server still hits the API, downloads the JSON, and compares it.
- Interval and responsiveness fight each other. Shorter intervals cut lag and raise request volume. Longer intervals save work and slow the update.
- The server cannot tell whether anything changed. It can only keep asking, on a timer, whether there is new data.
Once something already reports IP changes, having the server ask again every 10 minutes is duplicate work.
So this became event-driven. After a successful upload, the reporter side tells the server to pull. The server no longer guesses on a schedule.
4. The Worker notifies SQS directly
This is the part worth writing down. After the IP object is updated, the Cloudflare Worker sends a message straight to AWS SQS. The server then pulls from the queue itself.
1. Do not let the Worker call Lightsail
After the R2 write, the Worker could POST to an HTTP endpoint on Lightsail and run the update script immediately. That means opening another inbound port, or hanging the callback off a port that is already proxied.
I did not want a public management entry just for an IP callback:
- Another port, or extra HTTP on a port that is already open.
- Auth, signatures, replay protection, and request checks on a public endpoint.
- A new surface for scans and attacks.
- The server has to accept inbound management traffic, which makes the edge and the service harder to reason about.
2. The Worker calls SQS SendMessage
The Worker signs the request with AWS Signature Version 4 (SigV4). The credentials live in Worker secrets:
AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY
The IAM identity only has sqs:SendMessage on the target queue. The Worker does not get broader SQS or account admin rights.
The queue is in Singapore (ap-southeast-1), named:
1ip-whitelist-update
After a successful R2 write, the message body stays small:
1{
2 "event": "ip_updated",
3 "timestamp": 1730000000
4}
timestamp is the current Unix time. When the consumer sees ip_updated, it runs the allowlist update script on the server.
3. The path
1Home device / phone
2 │
3 │ report IP
4 ▼
5Cloudflare Worker
6 │
7 ├── write Cloudflare R2
8 │
9 └── AWS SigV4
10 │
11 │ SQS SendMessage
12 ▼
13 AWS SQS
14 │
15 │ Lightsail long-polls
16 ▼
17 Lightsail consumer
18 │
19 ▼
20 update-whitelist.sh
21 │
22 ▼
23 nftables
Direction matters. The Worker opens HTTPS to SQS. Lightsail long-polls SQS on its own. Nothing on the public internet has to connect inbound to Lightsail, and the server does not grow an HTTP callback. That cuts the wasted work of a timer, and the path from upload to an updated ruleset stays under about 20 seconds.
5. Updating the allowlist on the server
Lightsail runs a long-lived Python consumer:
1/usr/local/sbin/sqs-ip-consumer.py
systemd unit:
1/etc/systemd/system/sqs-ip-consumer.service
It uses boto3 against the Singapore queue, long-polls for 20 seconds, and takes at most one message at a time.
On ip_updated it does not touch the firewall itself. It runs the script that already existed:
1/usr/local/sbin/update-whitelist.sh
Steps:
- The consumer receives the SQS event.
- It runs the IP update script.
- The script GETs
/homeand/mobileon the Worker. - It checks both JSON documents and the IP/CIDR values.
- It reads the current nftables IPv4 and IPv6 sets.
- It checks whether each address is already present.
- New addresses are appended. Existing ones are skipped.
- The consumer deletes the SQS message only after the script exits successfully.
If the script fails, the message stays. SQS delivers it again after the visibility timeout. While the script is running, the consumer extends visibility so another consumer cannot take the same message mid-run.
The unit uses Restart=always, so a crash comes back, and the service starts on boot.
6. How old addresses are treated
This part is deliberate.
Suppose the phone is on IPv6 address A today, and the carrier later hands out B. If every report replaced the whole allowlist, A would be removed.
Real networks are messier than that:
- An address can flip away and come back.
- More than one address can be valid at the same time.
- Reports are not perfectly synchronized.
- One report does not list every source that should still be allowed.
So the sets only grow, and TCP and UDP share them:
- New addresses are added.
- Addresses that are already there are skipped.
- An old address is not removed just because one report omitted it.
- Cleanup is a manual check.
The script is idempotent. Running the same event again skips addresses that are already present. After about half a year I can merge the set by hand. The addresses assigned to me are still from residential and mobile pools, which is the exposure I am willing to keep.
7. Commands I actually use
Consumer status
1sudo systemctl is-active sqs-ip-consumer.service
2sudo systemctl is-enabled sqs-ip-consumer.service
Consumer logs
1sudo journalctl -u sqs-ip-consumer.service -n 100 --no-pager
2sudo journalctl -u sqs-ip-consumer.service -f
Run the update script by hand
1sudo bash /usr/local/sbin/update-whitelist.sh
Show the sets
1sudo nft list set inet whitelist ipv4
2sudo nft list set inet whitelist ipv6
Show the TCP / UDP rules
1sudo nft list ruleset
The same IPv4 and IPv6 sets apply to TCP 443 and UDP 443. The persisted rules live in /etc/whitelist.nft, and the running ruleset stays in sync with that file.
Check the app
1sudo systemctl is-active
2sudo ss -lntp | grep ':443'
3sudo run -test -config /usr/local/etc/config.json
Memory
1free -h
2ps aux --sort=-%mem | head -12
3swapon --show
On the last check the machine had about 412 MiB of RAM and about 208 MiB available. The SQS consumer was using about 16 MiB, with a peak around 38 MiB. On this t3.nano that is not a meaningful load.
8. What the design ended up as
Looking back, it grew in steps:
- Move off a shared host onto a box I control.
- Stick to one protocol family instead of multiplying listeners.
- Once the ports were up, put an nftables allowlist in front of public scans.
- Home and the phone report separately. The Worker stores the objects in R2.
- A 10-minute poll on the server worked, with lag and empty requests.
- Then events: after the R2 write, the Worker calls SQS
SendMessagewith SigV4, with no Lambda in the middle. - Lightsail long-polls SQS. No extra public callback port.
- On an event, the server pulls both IP documents and only appends new addresses. Old ones stay.
The shape is small: the edge is an allowlist, the data lives in Worker/R2, the Worker sends the event straight to SQS, and the server pulls and updates nftables.
The useful part is not how many AWS or Cloudflare products are in the diagram. Each layer has one job. The public internet does not call the server. The server does not poll for nothing. And an IP change does not mean logging in to edit the firewall by hand.
