Halloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowHalloween DealsAmazon USOctober picks are moving - compare before checkoutAmazon US: current deals, useful picks and tech finds.See Picks×
FREEDOM251

Freedom251 › Blog › API key rotation

Secure LLM API Key Storage and Zero-Downtime Rotation in Node.js with AWS Secrets Manager

By Freedom251 Team · · 11 min read

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Store each LLM provider key as an AWS Secrets Manager secret, grant the Node.js workload only secretsmanager:GetSecretValue for that secret, and retrieve it through the AWS SDK at runtime. For rotation, create a new version under AWSPENDING, activate and test it with the provider, then move AWSCURRENT only after the test succeeds. Secrets Manager automatically keeps the former value as AWSPREVIOUS, giving you a rollback path without putting keys in source code, .env files, images, or logs.

What this design protects

An LLM API key is a bearer credential: anyone who obtains it can usually submit requests until the provider revokes it or a quota is exhausted. Keeping it in a local .env file is convenient for development, but it creates several exposure paths in production: source-control commits, container layers, support bundles, crash dumps, copied environment variables, and verbose request logging.

As an Amazon Associate I earn from qualifying purchases.

A production design separates four responsibilities:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Secrets Manager stores encrypted versions and labels the active version.
  • IAM decides which runtime and rotation identities may read or change the secret.
  • Node.js requests the value when needed and holds it only in memory.
  • A rotation function obtains, activates, tests, and promotes a replacement provider key.

Secrets Manager encrypts secret data at rest. The default AWS managed key for Secrets Manager can be used in many same-account deployments; a customer-managed KMS key gives you additional key-policy and audit control but requires an explicit kms:Decrypt grant for every identity that reads the secret.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

This protects storage and delivery. It does not make a provider key harmless after disclosure. You still need provider-side limits, revocation procedures, and monitoring that never records the key itself.

Choose a secret shape that is easy to rotate

For one credential, a JSON object is usually clearer than storing a bare string. For example:

{
  "apiKey": "provider-key-value",
  "provider": "your-llm-provider",
  "accountId": "production"
}

Keep metadata non-sensitive and stable. Do not put a key in the secret name, tags, description, CloudFormation parameters, or deployment output. A name such as prod/llm/provider-a is sufficient. Store only the fields your application needs; every additional field increases the chance of accidental disclosure.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Create separate secrets for development, staging, and production. A separate secret also makes it possible to grant a staging role no access to production credentials. If you use multiple providers, use separate secrets unless the rotation and access policy are deliberately identical.

Set up the secret and identities

Create the secret

Create a Secrets Manager secret in the same AWS Region as the workload when practical. Put the JSON value in Secrets Manager rather than passing it through a shell command or CI log. If you use a customer-managed KMS key, choose it during secret creation and record which roles will need decrypt permission.

Use the secret ARN or a controlled name as configuration. The application configuration should contain an identifier such as LLM_SECRET_ID, never the provider key itself.

Use separate runtime and rotation roles

The application role needs to read the current value. The rotation role needs additional Secrets Manager operations and the provider operation required to create or activate a replacement. Do not combine these roles merely because it is simpler.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter | USB C Dongle with 90W Max Power Delivery, 3×5Gbps USB A Data Ports, for MacBook, Dell, and More (Charger Not Included)
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

A minimal application policy for one secret looks like this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "secretsmanager:GetSecretValue",
      "Resource": "arn:aws:secretsmanager:REGION:ACCOUNT:secret:prod/llm/provider-a-*"
    }
  ]
}

If that secret uses a customer-managed KMS key, add only the required kms:Decrypt permission for that key:

{
  "Effect": "Allow",
  "Action": "kms:Decrypt",
  "Resource": "arn:aws:kms:REGION:ACCOUNT:key/KEY-ID"
}

The KMS key policy must also permit the role. AWS managed key behavior and cross-account access differ from customer-managed keys, so verify the key policy and account boundaries before deploying.

Use the default credential provider chain

In production, let the AWS SDK obtain credentials from the approved runtime identity: an IAM role for an EC2 instance, ECS task, or Lambda function; workload identity in a compatible container platform; or another organization-approved identity mechanism. Do not create long-lived access keys in the source tree or put AWS credentials beside the LLM key in an environment file.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Retrieve the key in Node.js

Install the modular AWS SDK package:

npm install @aws-sdk/client-secrets-manager

The SDK client below uses the default credential provider chain. It parses a structured secret only in memory and applies a bounded five-minute cache:

import {
  SecretsManagerClient,
  GetSecretValueCommand
} from "@aws-sdk/client-secrets-manager";

const client = new SecretsManagerClient({});
let cached;
let expiresAt = 0;

export async function getLlmApiKey() {
  if (cached && Date.now() < expiresAt) {
    return cached;
  }

  const secretId = process.env.LLM_SECRET_ID;
  if (!secretId) {
    throw new Error("LLM_SECRET_ID is not configured");
  }

  const result = await client.send(
    new GetSecretValueCommand({ SecretId: secretId })
  );

  if (!result.SecretString) {
    throw new Error("LLM secret does not contain SecretString");
  }

  const secret = JSON.parse(result.SecretString);
  if (typeof secret.apiKey !== "string" || secret.apiKey.length === 0) {
    throw new Error("LLM secret is missing apiKey");
  }

  cached = secret.apiKey;
  expiresAt = Date.now() + 5 * 60 * 1000;
  return cached;
}

The five-minute value is an implementation example, not an AWS requirement. A shorter time limits propagation delay after rotation but increases Secrets Manager calls and latency. A longer time reduces calls but leaves workers using an old key for longer. Pick a TTL using the provider’s revocation behavior, your rotation interval, request volume, and the number of long-lived workers.

Invalidate and refresh after authentication failure

A cached key can become invalid before its TTL expires if a provider revokes it or rotation changes credentials. On an authentication error, clear the cache, fetch the current value once, and retry the original operation under a bounded policy. Do not let every concurrent request refresh independently.

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
let refreshPromise;

function invalidateLlmApiKey() {
  cached = undefined;
  expiresAt = 0;
}

async function refreshOnce() {
  if (!refreshPromise) {
    refreshPromise = getLlmApiKey().finally(() => {
      refreshPromise = undefined;
    });
  }
  return refreshPromise;
}

export async function callWithKey(callProvider) {
  let key = await getLlmApiKey();
  try {
    return await callProvider(key);
  } catch (error) {
    if (!isAuthenticationFailure(error)) throw error;
    invalidateLlmApiKey();
    key = await refreshOnce();
    return callProvider(key);
  }
}

Implement isAuthenticationFailure for the provider SDK or HTTP status you actually use. Retry once, not indefinitely. A persistent 401 or 403 should page an operator or disable the affected path rather than create a refresh storm.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Design a custom rotation Lambda

Secrets Manager’s custom rotation contract has four stages. The rotation function receives a client request token that identifies the version being created; use it for idempotency so a duplicate invocation does not create conflicting versions.

1. createSecret: create the pending version

Request or generate a replacement key through the LLM provider’s credential API. Store it as a new Secrets Manager version with the request token and the AWSPENDING staging label. If the provider request fails, leave the current version untouched and allow the rotation attempt to fail safely.

Do not write the pending key to logs, exception messages, tracing attributes, or temporary files. If the provider API returns the key in a response object, pass it directly to Secrets Manager and discard other response fields that contain sensitive data.

2. setSecret: activate the pending key

Use the provider’s credential API to activate the pending key or assign it to the intended account and resource. Before changing anything, verify that the current and pending credentials refer to the same provider account or project. This check prevents a confused-deputy failure in which a rotation function accidentally modifies a different tenant.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some providers issue an immediately usable key; others require an explicit enablement call. The exact API, maximum number of active keys, and overlap rules are provider-specific and must be confirmed in that provider’s current documentation.

3. testSecret: make a safe authenticated check

Call a minimal, non-mutating endpoint with the pending key. Prefer an account or model-list operation that has predictable cost. Do not perform an expensive completion, send user content, or invoke an endpoint that changes billing, files, fine-tunes, or other resources merely to test authentication.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray | Data transfer & Fast charging for MacBook, iPhone 17/16/15, iPad, Samsung Galaxy, Apple Watch
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

The safe endpoint is not universal. If the provider has no lightweight verification endpoint, use the smallest documented request and set a strict timeout. A failed test must leave AWSCURRENT on the old value.

4. finishSecret: promote only after success

Move AWSCURRENT to the tested version. Secrets Manager removes AWSPENDING from that version in the same staging-label update and assigns AWSPREVIOUS to the former current version. Do not remove AWSPENDING in a separate cleanup call; promotion is the atomic point at which readers begin receiving the replacement.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Keep the former value available long enough to support rollback and to let already-running requests finish. Deleting old versions immediately can remove your last known-good recovery path.

Implement rotation without downtime

Zero downtime requires an overlap strategy, not just a timer. During rotation, existing workers may still hold the old value in memory while new reads return the promoted value. The provider must therefore accept both credentials for the overlap period, or your deployment must coordinate a faster cutover.

Design choice Operational consequence Best fit
Single active key Revoking the old key before every worker refreshes can cause 401 errors. Providers that enforce one active key, with a coordinated deployment and short cache TTL.
Overlapping active keys Old and pending keys work concurrently; workers can refresh independently. Providers that permit multiple active keys and expose reliable activation and revocation APIs.
Manual emergency rotation An operator performs provider changes and Secrets Manager promotion. Providers without an automation API or teams that cannot safely test automatic rotation.

When overlap is supported, create and activate the pending key, wait for the test to pass, promote it, allow the cache and in-flight requests to drain, then revoke the previous provider key according to your rollback window. When only one key may be active, schedule a maintenance procedure that refreshes all workers, promote the new value, and revoke the old one only after health checks confirm that every instance has reloaded.

Secrets Manager can schedule rotation as frequently as every four hours. That is a platform capability, not a recommendation for every provider. The interval must respect the provider’s key-creation quota, active-key limit, revocation delay, and the time your application needs to propagate a new value.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rotation Lambda deployment controls

  • Package the function with pinned, patched dependencies. The function owner is responsible for external libraries and binaries.
  • Give the rotation role only the Secrets Manager actions required for the target secret and the provider API permissions required for credential management.
  • Use a network path that can reach both Secrets Manager and the provider API. A private subnet without the required NAT or interface endpoints will produce timeouts.
  • Set a timeout long enough for provider calls but short enough to prevent overlapping invocations. Make each stage idempotent.
  • Use a dead-letter or alerting path for failed rotations, but scrub secret values from every failure payload.
  • Deploy and test the function in the same Region and account boundaries assumed by the secret’s resource and KMS policies.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Logging, tracing, and error handling

Never log SecretString, the parsed secret object, authorization headers, provider request bodies, or SDK response bodies that may contain credentials. This prohibition includes debug logs enabled temporarily during an incident. Redact environment dumps, HTTP interceptors, exception serializers, and tracing attributes.

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.

Log operational metadata instead: secret identifier without the value, rotation stage, version-staging labels, duration, outcome, provider status class, and a correlation ID. A correlation ID must not be derived from the key. If a library automatically logs requests, disable body and header logging or install a redaction hook before production traffic reaches it.

CloudWatch logs, traces, crash dumps, container layers, CI artifacts, and support bundles all require the same treatment. If a key is exposed, revoke it at the provider immediately, create a replacement, inspect access logs, and treat every copy as compromised.

Test the failure paths before enabling a schedule

  1. Provider creation failure: make the credential API reject a request and verify that AWSCURRENT remains unchanged.
  2. Pending authentication failure: supply an invalid pending key and confirm that testSecret fails without promotion.
  3. Timeout: simulate a slow provider response and verify that the Lambda exits cleanly and can be retried.
  4. Duplicate invocation: replay the same client request token and confirm that the function reuses the existing pending version rather than creating a second replacement.
  5. Rollback: read AWSPREVIOUS, restore it to AWSCURRENT through your documented procedure, and verify that applications recover.
  6. Cache refresh: rotate while a worker has a cached value, then confirm that expiry and one authentication failure lead to a bounded refresh.
  7. Permission denial: remove kms:Decrypt or the secret read permission in a test account and ensure the alert identifies authorization failure without printing the secret.

Troubleshoot common symptoms

Node.js receives AccessDeniedException

Check the runtime role, the secret ARN pattern, the Region configured for the SDK client, and the secret resource policy. If a customer-managed KMS key is used, verify both the IAM policy and the KMS key policy include the role.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Rotation succeeds but requests still use the old key

The worker is probably within its cache TTL or has an in-flight request using the previous value. Confirm which version the next GetSecretValue call returns, shorten the TTL for the required propagation window, and use the single-refresh-on-auth-failure path. Do not restart every worker as the first response unless the provider permits no overlap.

Rotation repeatedly leaves AWSPENDING

Inspect the stage that failed and retry with the same client request token. Check for non-idempotent provider calls, an unreachable provider endpoint, and a test request that is too expensive or invalid. Clean up an abandoned pending version only after confirming that it is not being used by an active rotation.

The provider rejects the replacement key

Verify account or project targeting, provider quotas, maximum active-key count, required scopes, and whether activation is a separate operation. These limits are controlled by the LLM provider, not by Secrets Manager.

Operational checklist

  • Secret values exist only in Secrets Manager and process memory.
  • The application role can read one secret and nothing else it does not need.
  • A customer-managed KMS key, when used, has narrowly scoped kms:Decrypt permissions.
  • Rotation and runtime roles are separate.
  • Every rotation stage is idempotent and uses the client request token.
  • The pending key is tested before promotion, and AWSPREVIOUS remains available for rollback.
  • The provider’s key API, active-key limit, revocation latency, and safe test endpoint are documented for your account.
  • Cache TTL and retry limits are documented and monitored.
  • Logs, traces, errors, dumps, images, and CI artifacts are scrubbed.
  • Alerts cover failed rotation, authentication spikes, permission errors, and unexpected provider spend.

What AWS Secrets Manager cannot decide for you

Secrets Manager defines version staging, IAM integration, and the Lambda rotation lifecycle. It does not define how an individual LLM provider creates keys, how many keys may be active, how quickly revocation takes effect, or which endpoint is safe and inexpensive for testing. Those values must come from the specific provider’s current documentation and your account’s observed behavior.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The durable pattern is therefore provider-neutral but not provider-blind: keep the secret and access policy in AWS, while adapting setSecret, testSecret, overlap timing, and revocation to the provider that actually issues the credential.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

API key rotationAWS LambdaAWS Secrets ManagerIAMLLM API securityNode.js

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.