Skip to content
DBDeependra Bhatta~/notes
Docker#debugging · #ecs · #aws · #docker · #linux · #node-js

Why My Secrets Looked Missing in ECS

Every env var list in our ECS container was missing the Secrets Manager values. Here is why, and how the Node inspector found them.

· 6 min read
ON THIS PAGE

The issue I faced

Our NestJS API runs on AWS ECS Fargate. Some of its Node.js environment variables come from the ECS task definition, and the rest (database password, third-party API keys) come from AWS Secrets Manager.

One day I needed to confirm which variables the running API had actually loaded, especially the ones from Secrets Manager. So I opened a shell in the container with ECS Exec and started looking.

Every list I checked was missing the secrets. This post is what I tried, what each attempt showed, and why the secrets were "missing" even though the app was working fine.


How our app loads its Node.js environment variables

This is the part I had forgotten. When the API starts, its own code calls Secrets Manager and copies the result into process.env. Simplified:

TSTypeScript
const secret = await secretsManager.send(new GetSecretValueCommand({ SecretId: '/myapp/dev/external-secrets' }));
Object.assign(process.env, JSON.parse(secret.SecretString));

So there are two kinds of variables:

  • Task definition variables. ECS gives these to the container before Node starts.
  • Secrets Manager variables. The app adds these to its own memory after it starts.

Keep that in mind. It explains everything below.


Attempt 1: docker inspect

On a machine running Docker, this is the usual first check:

SHShell
docker inspect <container-name>

The output is a big JSON document. Look for the "Env" list inside "Config". It shows every variable the container was started with.

We run on Fargate, so I could not run docker inspect: AWS does not give you access to the host. The closest thing is the ECS console: Task → Containers → api → Environment variables. It shows the same thing, the variables from the task definition. ECS console container details showing task definition environment variables with no secrets

What I saw: PROJECT_ROOT, PROJECT_NAME, NODE_ENV, LOG_DIR, and similar. No secrets.


Attempt 2: env and /proc inside the container

Next I opened a shell in the container and looked at the processes:

TXTPlain text
/app # ps -ef
PID   USER     TIME  COMMAND
    1 node      0:00 /dev/init -- docker-entrypoint.sh node apps/api/dist/src/main
    8 node      0:17 {MainThread} node apps/api/dist/src/main
   10 root      0:00 /managed-agents/execute-command/amazon-ssm-agent
   38 root      0:00 /managed-agents/execute-command/ssm-agent-worker
  239 root      0:00 /bin/sh
  241 root      0:00 ps -ef

PID 8 is the API. PID 239 is my shell.

env

The quick way is env. It prints the environment of my shell, not the API. I only wanted the names, so I cut off the values:

SHShell
env | cut -d= -f1 | sort

/proc/<pid>/environ

To read the environment of the API process itself, Linux has /proc/<pid>/environ. The entries are separated by a NUL byte, so tr turns them into lines:

Permission denied when reading /proc/8/environ as root Permission denied, even as root. My shell was root, but the API runs as node. Linux treats reading another user's process environment like attaching a debugger, and the container does not have the capability for that (CAP_SYS_PTRACE).

The fix was to switch to the same user as the process:

SHShell
su node -s /bin/sh

Environment variable names from /proc/8/environ after switching to the node user

In our case, env and /proc showed the same list. Our container runs only one app, and my shell ended up with the same environment. In a container with several processes, or where processes start with different environments, the two can differ. Then /proc/<pid>/environ is the one to trust.

The list matched the ECS console, plus a few variables ECS adds itself (AWS_CONTAINER_CREDENTIALS_RELATIVE_URI, ECS_CONTAINER_METADATA_URI_V4 and so on).

Still no secrets. No DATABASE_URL, no API keys.


Why the secrets were missing

A process gets a copy of its environment when it starts. docker inspect, the ECS console and /proc/<pid>/environ all show that starting copy.

Our app adds the Secrets Manager values to process.env later, inside its own memory. Nothing outside the process can see that change. So all three lists were correct. They just answered a different question: "what did the app start with?", not "what does the app have now?".


Attempt 3: asking the running process (Node.js inspector)

To see the live process.env, you have to ask the process itself. Node has a built-in debugger for this.

Only do this on a dev task you can restart. It opens a debug port inside the task.

Turn on the inspector

SHShell
su node -s /bin/sh -c 'kill -USR1 8'

Then check the ports:

TXTPlain text
/app # netstat -ltn
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:20618         0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:9229          0.0.0.0:*               LISTEN
tcp        0      0 :::3001                 :::*                    LISTEN

3001 is the API, still running. 127.0.0.1:9229 is new: the Node inspector, reachable only from inside the task. Here you can see before and after response here.

netstat before and after SIGUSR1, showing the Node inspector listening on port 9229

Connect and list the names

SHShell
node inspect 127.0.0.1:9229

At the debug> prompt:

TXTPlain text
debug> exec Object.keys(process.env).sort()

This time the list was much longer. Along with everything from Attempt 2, it had DATABASE_URL, the database username and password, and all our third-party API keys. These were the Secrets Manager values, loaded fine all along.

For values, I only printed settings I knew were safe:

TXTPlain text
debug> exec ['NODE_ENV', 'AWS_REGION', 'API_PORT'].map(k => k + '=' + process.env[k])

Node debugger printing only safe environment variable values

And for secrets, I only checked that they exist:

TXTPlain text
debug> exec Boolean(process.env.DATABASE_URL)

Node debugger confirming DATABASE_URL exists without showing its value

Leave and clean up

TXTPlain text
debug> .exit

Do not type kill at the debug> prompt. There it means "kill the app".

Port 9229 stays open after you exit, so I restarted the task. The new task's netstat showed no 9229.


Wait, did I just kill my API?

That was my first worry when I typed kill -USR1 8.

kill does not always kill. It sends a signal to a process:

  • kill 8 sends SIGTERM, which shuts the app down
  • kill -9 8 sends SIGKILL, which stops it immediately
  • kill -USR1 8 sends SIGUSR1, and Node's response is to turn on the inspector and keep running

The API kept serving on port 3001 the whole time, and the health check still answered.


What I learned

  1. The environment belongs to a process, and it is copied at start. docker inspect, the ECS console, env and /proc all show the starting copy.
  2. Secrets loaded by code at startup are invisible from outside the process. "Missing" in those lists did not mean missing in the app.
  3. Root in a container cannot read another user's /proc/<pid>/environ. su to the process owner instead.
  4. kill -USR1 turns on Node's debugger. It does not stop the app. Restart the task afterwards to close the port.
  5. Print names, not values. I never needed to see a secret to answer my question.

References