Week Notes 2026.06
CDK vs Terraform for AWS lambda function deployment #
CDK is the AWS Cloud Development Toolkit. It allows you to configure your AWS infrastructure in a familiar programming language, often the same one you’re using to implement the Lambda function itself. It provides high-level constructs via libraries, and under the covers it generates CloudFormation templates that are used to provision resources in AWS.
CDK also bundles up your lambda function resources and uploads them to S3 or ECR. It encourages you to develop the lambda function and deployment code alongside each other in the same repository.
I don’t like this approach. Until a year or so ago, I had always used Terraform
for AWS deployments. For lambda functions, I would create an S3 bucket for the
deployment packages (zip files), and the lambda function repository would
contain only the lambda function source code and tests. When building a tag, it
would simply upload the zip file to the S3 bucket under the key
${FUNCTION_NAME}/${VERSION}/function.zip. The terraform manifest defining the
AWS infrastructure would then deploy the desired version from the S3 bucket.
I like this separation of concerns:
-
You have more control over exactly how the lambda function is compiled and packaged.
-
The function build and test can run with different permissions from the deployment pipeline (it only needs permissions to write to the lambda functions bucket, while the deployment will likely need permission to create an IAM role and more).
-
You don’t get the frustrating rate limit error when CDK tries to pull the Docker image used for bundling from ECR.
-
The lambda function source code and the AWS infrastructure are versioned independently: a change to the function permissions or log retention is restricted to the AWS infrastructure repository (where it belongs) and does not impact the function source code repository.
A more fundamental problem with CDK is that, for resources that aren’t directly
(or fully) supported by CloudFormation, you have to define custom
resources.
These are basically lambda functions that get called to manage the resource
lifecycle. These sometimes pop up in unexpected places. For example, imagine you
need to implement a SOC2 policy that requires lambda function logs to be
retained for one year. If you use the log_retention attribute on the CDK
lambda function construct to do this, it will create a custom resource to manage
the CloudWatch log retention. That is, it will create another lambda function
with its own log group and no option to specify its log retention.
Synology NAS data corruption #
I have an aging Synology NAS and it occasionally runs into data corruption
problems. Today it was complaining that SynologyApplicationService could not
start, and journalctl -xe -u pkgctl-SynologyApplicationService.service showed
that the synomibclient_package_action.sh script was failing with an error from
the local PostgreSQL database - it was trying to create an object that already
existed in the schema. I logged in and tried to list objects in the underlying
database:
sudo -u postgres psql
psql (11.11)
Type "help" for help.
postgres=# \c ong
You are now connected to database "ong" as user "postgres".
ong=# \d
ERROR: could not open file "base/36064/2619": No such file or directory
ong=# \q
This calls for drastic action:
sudo -u postgres psql -c 'drop database ong'
I could probably have tried harder to recover the data, but this database is internal to some Synology application and I don’t much care about the data.
Longer-term, I need to ditch this NAS and replace it with something running a file system that’s more robust to sudden power outages (we’ve had a few of those recently).
Progress on the static doc site #
I’m making slow progress with the Caddy/OAuth2 server setup on a CoreOS EC2 instance. I have both Caddy and OAuth2 Proxy up-and-running so I turned my mind to getting the content onto the server.
The content is a static site generated by Hugo. Source for the site is in a Bitbucket repository. One option would be to create an SSH key pair on the server and add the public key as a deploy key in Bitbucket so it can fetch the source from the git repository. It could then run the hugo build step locally in a container.
I decided against this option because there’s no easy way to trigger the build when new content is pushed to Bitbucket. I thought about creating an SQS queue and having Bitbucket publish a message every time a commit is pushed to the publish branch, then having a daemon run on the server listening for messages on the SQS queue and triggering a build. It wouldn’t be a lot of code, but it felt a bit over-engineered.
My next thought was to run the hugo build as a Bitbucket pipeline and push the content to S3. I could then use the Caddy File System module for S3 to serve the content. This is the route I’ve gone down.
The hugo build is up-and-running and the content is in S3.
I created a Dockerfile to build Caddy with the S3 File System module, it looks like this:
FROM docker.io/library/alpine:3.23 AS builder
ENV CADDY_VERSION=v2.10.2
RUN apk add go git
RUN go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
RUN /root/go/bin/xcaddy build --with github.com/sagikazarmark/caddy-fs-s3
FROM docker.io/library/alpine:3.23
COPY --from=builder /caddy /usr/bin/caddy
RUN apk add --no-cache \
ca-certificates \
curl \
libcap \
mailcap
RUN set -eux; \
mkdir -p \
/config/caddy \
/data/caddy \
/etc/caddy \
/usr/share/caddy \
; \
chmod 1777 /config/caddy /data/caddy; \
wget -O /etc/caddy/Caddyfile "https://github.com/caddyserver/dist/raw/33ae08ff08d168572df2956ed14fbc4949880d94/config/Caddyfile"; \
wget -O /usr/share/caddy/index.html "https://github.com/caddyserver/dist/raw/33ae08ff08d168572df2956ed14fbc4949880d94/welcome/index.html"
ENV XDG_CONFIG_HOME=/config
ENV XDG_DATA_HOME=/data
EXPOSE 80
EXPOSE 443
EXPOSE 443/udp
EXPOSE 2019
WORKDIR /srv
CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]
My first thought was to update the ignition file to add the Dockerfile to the
server and add an build
quadlet
to build the image. Before adding this configuration to Butane, I tried doing
these steps manually but unfortunately the t3.micro instance doesn’t have
quite enough vCPU/RAM to compile Caddy.
So instead I set up a Bitbucket pipeline to build and push the image to ECR, and granted the EC2 instance permissions to pull the image.
Now I’ve run into a problem: in order to pull an image from a private ECR
repository, you need to login. This has to happen before podman tries to
pull the image, so my initial attempt at just changing the image URL in the
Caddy systemd quadlet failed miserably.
It should be possible to add an ExecStartPre step to the quadlet to do the
login, but this requires the AWS CLI being installed on the server. This is
available via rpm-ostree but Butane does not (yet) have support for installing
overlay packages. The recommended method is to have Butane create a
systemd unit
to install it and reboot the system.
Before updating the Butane configuration, I tested these steps manually.
I ran sudo rpm-ostree install awscli2
and sudo systemctl reboot then added the following to
/etc/containers/systemd/caddy.container:
[Service]
ExecStartPre = bash -c "aws ecr get-login-password --region eu-west-1 | podman login --username AWS --password-stdin 111111111111.dkr.ecr.eu-west-1.amazonaws.com"
After sudo systemctl daemon-reload and sudo systemctl restart caddy.service
it’s able to pull the container and the service is up-and running!
The final step is to update /etc/caddy/Caddyfile to configure the S3
file system:
{
filesystem tech-docs s3 {
bucket tech-docs-2026020612345677000000001
region eu-west-1
}
}
docs.example.org {
handle /oauth2/* {
reverse_proxy oauth2-proxy:4180 {
header_up X-Real-IP {remote_host}
header_up X-Forwarded-Uri {uri}
}
}
handle {
forward_auth oauth2-proxy:4180 {
uri /oauth2/auth
header_up X-Real-IP {remote_host}
@error status 401
handle_response @error {
redir * /oauth2/sign_in?rd={scheme}://{host}{uri}
}
}
file_server {
fs tech-docs
}
}
}
I still need to update the ignition file to:
- Add the systemd unit to install
awscli2; - Update the
caddy.containerquadlet definition; - Update the
Caddyfile.
Is it worth exploring AWS ECR Docker Credential Helper, which might avoid the login step?
Listening #
- Eyes of the Void by Adrian Tchaikovsky (Audio book)
- Darknet Diaries: MoD (Podcast)
- Open Source Security: The Art of Crisis Management (Podcast)
- Late Night Linux: Episode 371 (Podcast)
- Linux After Dark: Episode 114 (Podcast)
- 2.5 Admins Episode 285: example.com.oops (Podcast)
- Cautionary Tales: Powered by Orgasm (Podcast)