Let's talk about secrets.

Today I’ll tell you about the secrets of one of my customers.

Over the last few weeks I’ve been helping them convert their existing Fargate setup to Lambda, where we’re expecting massive cost savings and performance improvements.

One of the things we need to do is sorting out how to pass secrets to Lambda functions in the least disruptive way.

In their current Fargate setup, they use secret parameters in their task definitions, which contain secretmanager ARNs. Fargate elegantly queries these secrets at runtime and sets the secret values into environment variables visible to the task.

But unfortunately Lambda doesn’t support secret values the same way Fargate does.

(If someone from the Lambda team sees this please try to build this natively into the service :folded_hands:)

We were looking for alternatives that require no changes in the application code, and we couldn’t find any. Unfortunately even the official Lambda extension offered by AWS needs code changes (it runs as an HTTP server so you need to do GET requests to access the secrets).

So we were left with no other choice but to build something ourselves, and today I finally spent some quality time building a small component that attempts to do this in a more user-friendly way.

Here’s how it works:

Secrets are expected as environment variables named with the SECRET_ prefix that each contain secretmanager ARNs.

The tool parses those ARNs to get their region, then fires API calls to secretmanager in that region to resolve each of the secret values.

It collects all the resolved secrets and passes them as environment variables (but without the SECRET_ prefix) to a program expected as command line argument that it executes, much like in the below screenshot.

You’re expected to inject this tool into your Docker images and to prepend it to the Lambda Docker image’s entrypoint or command slice, so you do need some changes to the Docker image, but then you shouldn’t need any application changes to make use of the secret values.

I decided to build this in Rust to make it as efficient as possible, both to reduce the size and startup times.

It’s the first time I build something in Rust, and thanks to Claude Sonnet 3.5, in very short time I had something running.

But then I wanted to implement the region parsing, and that got me into trouble.

I spent more than a couple of hours fiddling with weird Rust compilation errors that neither Claude 3.5 Sonnet nor ChatGPT 4 were able to sort out, even after countless attempts. And since I have no clue about Rust, I couldn’t help fix it.

Eventually I just deleted the broken functions, fired a new Claude chat and from the first attempt it was able to produce working code for the deleted functions.

Once I had it working I decided to open source this, hoping that more experienced Rustaceans will help me further improve this code.

A prebuilt Docker image is also available on the Docker Hub, but you should (and can easily) build your own.

Hope anyone finds this useful.

What is the reason to run Lambdas based on Docker images and not directly as Lambda runtime implementation? The request-response behaviour of Lambdas and something you run in Fargate as a long-running task is different and not exactly a like for like replacement.

I’m happy you found a solution to your problem. Just this is an extremely common pattern, probably executed 100 different ways in many, many companies. For example, at the place I am currently at, our workloads run in kubernetes and the secrets injector is a sidecar container in our pods. This is a basic software paradigm called separation of concerns, where you ensure your application is not overloaded with foreign tasks.

We port the application from Fargate to Lambda, and we want to keep the Docker image as much as possible unchanged.

We plan to use https://github.com/awslabs/aws-lambda-web-adapter/ and that should make the image portable across Lambda and Fargate

interesting setup with the sidecar. if you like to share why didn’t you go with k8s secrets directly or hc vault?

Thanks!

We found three ways to address this and decided to do it with this component, as it was the only one we could think of that would avoid any code changes.

Any other solution would’ve either required code changes to the application or forcing us to make compromises regarding security. The sidecar container is a basic go program that fetches secret data from AWS secretsmanager and injects those into the applications config files (all in memory/streamed). After the application initialized successfully, the config file is deleted. The sidecar checks the timestamp of the app container and blocks re-fetching until the timestamp changes (e.g. container restarted and needs to initialize again).

Probably not perfect, but works well for us and lets us tick all the relevant audit/certifiaction/pentest boxes regarding credentials…

Can’t you inject the secrets to Lambda in the cdk code, or does it have to be during runtime?

we use terraform so we could use a data source for that but any changes would require re-deployent, so it needs to be dynamic.

we did something very similar for ECS Fargate but used the AWS CLI in the sidecar. Also generated a config which we deleted immediately after it was read

I know you can do this using terraform but then they are not secrets anymore just environment variables saved as such against the resource.

You want to get your secrets at run time and then have them not be there once execution has finished.

You can also take a look at the Lambda Powertools for inspiration on ways to query and cache secrets in Lambda.

https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/

Lambda environment variables are encrypted at rest so they remain secret.

If the secrets are sensitive you should not set them as environment variables.

I like to set my secrets at global param outside the lambda handler and check if I’ve already fetched the value. If I haven’t a function call is made to fetch the secret value and set the global.

This will do 2 important things for your lambda. It will reduce the number of calls to secrets manager which will save you some overhead each run. It will also make you slightly more resilient should secrets manager have service level issues.

Anything outside the handler will be able to be reused so long as the lambda remains warm.

I know you want to minimize code changes, but sometimes you just need to bite the bullet.

this is the way. to clarify, op said the secret arn is an environment variable

not sure how your org likes to do this but a developer can see the environment variables in the console. every org is different but some security requirements require that devs are not able to see secrets in any capacity

For us, at least, you need admin rights to see secrets in the console. But I get your broader point.

I would flip it, what’s the reason not to use image based lambdas? Everything is easier about them. There is literally only one draw back - you pay for cold starts.

There is a lambda extension for it https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_lambda.html

that’s fair. ideally the consumer of the secret will retrieve the secret. aws makes this really easy so i question anyone that’s not plucking this low hanging fruit