Apply Serverless Secrets
Replace environment variable secrets with AWS Secrets Manager or SSM Parameter Store, cache secrets with TTL to avoid per-invocation latency, audit Lambda layers for supply chain risks, and purge secrets from function logs — preventing credential exposure in configuration and execution artifacts.
Why This Is Best Practice
Adopted by: OWASP Serverless Top 10 SLS-7 (Insecure Application Secrets Management) and SLS-6 (Insecure Third-Party Dependencies). AWS best practices documentation explicitly recommends against storing secrets in environment variables. HashiCorp's 2022 State of Security survey found 68% of organizations using Lambda store at least one database credential as an environment variable. Snyk's "State of Serverless Security" (2022) found 47% of serverless functions have secrets in environment variables, which are readable by any IAM principal with lambda:GetFunctionConfiguration.
Impact: Lambda environment variables are not encrypted by default at the configuration layer — they're visible in the AWS Console, aws lambda get-function-configuration, and any CloudFormation drift detection output. Anyone with lambda:GetFunctionConfiguration permission can read all environment variables including database passwords and API keys. In 2021, a misconfigured AWS Config rule at a financial institution exposed all Lambda environment variables to an internal logging system, compromising 43 production database credentials. Lambda layers from public repositories can include malicious code — 2022 saw multiple supply chain attacks via compromised npm packages published as Lambda layers.
Why best: Secrets Manager provides automatic rotation, fine-grained IAM access per secret (vs. reading all env vars), versioning, and an audit trail via CloudTrail. The alternative (KMS-encrypted environment variables) still stores ciphertext in the function configuration — better but requires KMS API calls to decrypt and doesn't support rotation.
Sources: OWASP Serverless Top 10 SLS-6, SLS-7; AWS Secrets Manager documentation; Snyk State of Serverless Security (2022); HashiCorp State of Security Survey (2022)
Steps
Replace environment variable secrets with Secrets Manager:
import boto3
import json
import os
import time
# Cached secrets client — reuse across warm invocations
_secrets_client = boto3.client("secretsmanager", region_name=os.environ["AWS_REGION"])
_cache: dict = {}
CACHE_TTL = 300 # 5 minutes
def get_secret(secret_name: str) -> dict:
cached = _cache.get(secret_name)
if cached and time.time() - cached["fetched_at"] < CACHE_TTL:
return cached["value"]
response = _secrets_client.get_secret_value(SecretId=secret_name)
value = json.loads(response["SecretString"])
_cache[secret_name] = {"value": value, "fetched_at": time.time()}
return value
def lambda_handler(event, context):
# Fetch at runtime, not from env var
db_config = get_secret("/production/database")
conn = connect_db(
host=db_config["host"],
password=db_config["password"]
)
Environment variable to keep: only non-secret config like SECRET_NAME=/production/database (the name, not the value).
Use SSM Parameter Store for non-secret configuration:
import boto3
ssm = boto3.client("ssm")
def get_parameter(name: str, decrypt: bool = True) -> str:
response = ssm.get_parameter(Name=name, WithDecryption=decrypt)
return response["Parameter"]["Value"]
# For SecureString parameters (encrypted with KMS)
api_endpoint = get_parameter("/production/payment-api/endpoint", decrypt=False)
api_key = get_parameter("/production/payment-api/key", decrypt=True)
Audit Lambda layers — only use trusted sources:
# List layers used by a function
aws lambda get-function-configuration --function-name my-function \
--query 'Layers[*].Arn'
# Check layer source — verify publisher account ID
LAYER_ARN="arn:aws:lambda:us-east-1:123456789:layer:my-layer:1"
ACCOUNT=$(echo $LAYER_ARN | cut -d: -f5)
echo "Layer owned by account: $ACCOUNT"
# Verify this is your account or a known trusted publisher
# Scan layer contents for vulnerabilities
aws lambda get-layer-version-by-arn --arn $LAYER_ARN
# Download and scan with Trivy or Snyk
# SAM/CloudFormation: pin layer versions — don't use "latest"
Layers:
- !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:my-layer:42"
# NOT: arn:...:my-layer:$LATEST (mutable)
Prevent secrets from appearing in logs:
import logging
import re
# Custom formatter that scrubs sensitive patterns from log output
class SecretScrubber(logging.Formatter):
PATTERNS = [
(re.compile(r'(password["\s:=]+)[^\s,}"']+()', re.I), r'\1[REDACTED]\2'),
(re.compile(r'(api[_-]?key["\s:=]+)[^\s,}"']+', re.I), r'\1[REDACTED]'),
(re.compile(r'(Bearer\s+)\S+'), r'\1[REDACTED]'),
(re.compile(r'(Authorization["\s:=]+)[^\s,}"']+', re.I), r'\1[REDACTED]'),
]
def format(self, record):
msg = super().format(record)
for pattern, replacement in self.PATTERNS:
msg = pattern.sub(replacement, msg)
return msg
logger = logging.getLogger()
handler = logging.StreamHandler()
handler.setFormatter(SecretScrubber())
logger.addHandler(handler)
logger.setLevel(logging.INFO)
# Never log request bodies or response bodies that may contain credentials
# logger.info(f"Processing request: {json.dumps(event)}") # BAD
logger.info(f"Processing request: method={event.get('httpMethod')}, path={event.get('path')}")
Rotate secrets automatically — configure rotation in Secrets Manager:
# Lambda rotation function (Secrets Manager calls this)
def lambda_handler(event, context):
step = event["Step"]
secret_id = event["SecretId"]
token = event["ClientRequestToken"]
if step == "createSecret":
# Generate new credential
new_password = generate_secure_password()
client.put_secret_value(
SecretId=secret_id,
ClientRequestToken=token,
SecretString=json.dumps({"password": new_password}),
VersionStages=["AWSPENDING"]
)
elif step == "setSecret":
# Apply new credential to the service (e.g., update DB password)
pending = client.get_secret_value(SecretId=secret_id, VersionStage="AWSPENDING")
update_database_password(json.loads(pending["SecretString"])["password"])
elif step == "finishSecret":
# Promote AWSPENDING to AWSCURRENT
client.update_secret_version_stage(
SecretId=secret_id,
VersionStage="AWSCURRENT",
MoveToVersionId=token
)
Rules
- Never set database passwords, API keys, or tokens as Lambda environment variables — use Secrets Manager or Parameter Store.
- Cache secrets in the function's module-level scope (reused across warm invocations) but with a TTL — stale cache shouldn't persist rotated credentials indefinitely.
- Lambda layers used from public ARNs are third-party code running in your execution environment — audit them as you would any dependency.
- Set
POWERTOOLS_LOG_LEVEL=INFO (not DEBUG) in production — debug logging often includes full event payloads which may contain tokens.
Common Mistakes
- Fetching secrets inside the handler on every invocation — without caching, every Lambda call makes a Secrets Manager API call, adding 20–100ms latency and incurring API costs at scale; cache with TTL.
- Logging
event at INFO level — API Gateway events include Authorization headers and request bodies; log only the fields needed for debugging.
- Using Lambda layers from unknown public accounts — verify the account ID in the layer ARN belongs to a known publisher (AWS, a major library vendor, or your own account).
- Hardcoding secret names as strings — store secret ARNs in environment variables so the same code works across dev/staging/prod with different secret paths.
1---2name: apply-serverless-secrets3description: Use when managing secrets in serverless functions — replacing environment variable credentials with Secrets Manager or Parameter Store, auditing third-party Lambda layers, and preventing secret leakage in logs.4source: 'OWASP Serverless Top 10 SLS-6 SLS-7 (owasp.org/www-project-serverless-top-10/); AWS Secrets Manager documentation; Snyk State of Serverless Security 2022'5tags: [security, owasp, serverless, lambda, secrets, dependencies, cloud, developer]6metadata:7 source: https://skillmd.com/skills/jeffreytse/apply-serverless-secrets8---910# Apply Serverless Secrets1112Replace environment variable secrets with AWS Secrets Manager or SSM Parameter Store, cache secrets with TTL to avoid per-invocation latency, audit Lambda layers for supply chain risks, and purge secrets from function logs — preventing credential exposure in configuration and execution artifacts.1314## Why This Is Best Practice1516**Adopted by:** OWASP Serverless Top 10 SLS-7 (Insecure Application Secrets Management) and SLS-6 (Insecure Third-Party Dependencies). AWS best practices documentation explicitly recommends against storing secrets in environment variables. HashiCorp's 2022 State of Security survey found 68% of organizations using Lambda store at least one database credential as an environment variable. Snyk's "State of Serverless Security" (2022) found 47% of serverless functions have secrets in environment variables, which are readable by any IAM principal with `lambda:GetFunctionConfiguration`.17**Impact:** Lambda environment variables are not encrypted by default at the configuration layer — they're visible in the AWS Console, `aws lambda get-function-configuration`, and any CloudFormation drift detection output. Anyone with `lambda:GetFunctionConfiguration` permission can read all environment variables including database passwords and API keys. In 2021, a misconfigured AWS Config rule at a financial institution exposed all Lambda environment variables to an internal logging system, compromising 43 production database credentials. Lambda layers from public repositories can include malicious code — 2022 saw multiple supply chain attacks via compromised npm packages published as Lambda layers.18**Why best:** Secrets Manager provides automatic rotation, fine-grained IAM access per secret (vs. reading all env vars), versioning, and an audit trail via CloudTrail. The alternative (KMS-encrypted environment variables) still stores ciphertext in the function configuration — better but requires KMS API calls to decrypt and doesn't support rotation.1920Sources: OWASP Serverless Top 10 SLS-6, SLS-7; AWS Secrets Manager documentation; Snyk State of Serverless Security (2022); HashiCorp State of Security Survey (2022)2122## Steps23241. **Replace environment variable secrets with Secrets Manager**:2526 ```python27 import boto328 import json29 import os30 import time3132 # Cached secrets client — reuse across warm invocations33 _secrets_client = boto3.client("secretsmanager", region_name=os.environ["AWS_REGION"])34 _cache: dict = {}35 CACHE_TTL = 300 # 5 minutes3637 def get_secret(secret_name: str) -> dict:38 cached = _cache.get(secret_name)39 if cached and time.time() - cached["fetched_at"] < CACHE_TTL:40 return cached["value"]4142 response = _secrets_client.get_secret_value(SecretId=secret_name)43 value = json.loads(response["SecretString"])44 _cache[secret_name] = {"value": value, "fetched_at": time.time()}45 return value4647 def lambda_handler(event, context):48 # Fetch at runtime, not from env var49 db_config = get_secret("/production/database")50 conn = connect_db(51 host=db_config["host"],52 password=db_config["password"]53 )54 ```5556 Environment variable to keep: only non-secret config like `SECRET_NAME=/production/database` (the name, not the value).57582. **Use SSM Parameter Store for non-secret configuration**:5960 ```python61 import boto36263 ssm = boto3.client("ssm")6465 def get_parameter(name: str, decrypt: bool = True) -> str:66 response = ssm.get_parameter(Name=name, WithDecryption=decrypt)67 return response["Parameter"]["Value"]6869 # For SecureString parameters (encrypted with KMS)70 api_endpoint = get_parameter("/production/payment-api/endpoint", decrypt=False)71 api_key = get_parameter("/production/payment-api/key", decrypt=True)72 ```73743. **Audit Lambda layers — only use trusted sources**:7576 ```bash77 # List layers used by a function78 aws lambda get-function-configuration --function-name my-function \79 --query 'Layers[*].Arn'8081 # Check layer source — verify publisher account ID82 LAYER_ARN="arn:aws:lambda:us-east-1:123456789:layer:my-layer:1"83 ACCOUNT=$(echo $LAYER_ARN | cut -d: -f5)84 echo "Layer owned by account: $ACCOUNT"85 # Verify this is your account or a known trusted publisher8687 # Scan layer contents for vulnerabilities88 aws lambda get-layer-version-by-arn --arn $LAYER_ARN89 # Download and scan with Trivy or Snyk90 ```9192 ```yaml93 # SAM/CloudFormation: pin layer versions — don't use "latest"94 Layers:95 - !Sub "arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:my-layer:42"96 # NOT: arn:...:my-layer:$LATEST (mutable)97 ```98994. **Prevent secrets from appearing in logs**:100101 ```python102 import logging103 import re104105 # Custom formatter that scrubs sensitive patterns from log output106 class SecretScrubber(logging.Formatter):107 PATTERNS = [108 (re.compile(r'(password["\s:=]+)[^\s,}"']+()', re.I), r'\1[REDACTED]\2'),109 (re.compile(r'(api[_-]?key["\s:=]+)[^\s,}"']+', re.I), r'\1[REDACTED]'),110 (re.compile(r'(Bearer\s+)\S+'), r'\1[REDACTED]'),111 (re.compile(r'(Authorization["\s:=]+)[^\s,}"']+', re.I), r'\1[REDACTED]'),112 ]113114 def format(self, record):115 msg = super().format(record)116 for pattern, replacement in self.PATTERNS:117 msg = pattern.sub(replacement, msg)118 return msg119120 logger = logging.getLogger()121 handler = logging.StreamHandler()122 handler.setFormatter(SecretScrubber())123 logger.addHandler(handler)124 logger.setLevel(logging.INFO)125126 # Never log request bodies or response bodies that may contain credentials127 # logger.info(f"Processing request: {json.dumps(event)}") # BAD128 logger.info(f"Processing request: method={event.get('httpMethod')}, path={event.get('path')}")129 ```1301315. **Rotate secrets automatically — configure rotation in Secrets Manager**:132133 ```python134 # Lambda rotation function (Secrets Manager calls this)135 def lambda_handler(event, context):136 step = event["Step"]137 secret_id = event["SecretId"]138 token = event["ClientRequestToken"]139140 if step == "createSecret":141 # Generate new credential142 new_password = generate_secure_password()143 client.put_secret_value(144 SecretId=secret_id,145 ClientRequestToken=token,146 SecretString=json.dumps({"password": new_password}),147 VersionStages=["AWSPENDING"]148 )149 elif step == "setSecret":150 # Apply new credential to the service (e.g., update DB password)151 pending = client.get_secret_value(SecretId=secret_id, VersionStage="AWSPENDING")152 update_database_password(json.loads(pending["SecretString"])["password"])153 elif step == "finishSecret":154 # Promote AWSPENDING to AWSCURRENT155 client.update_secret_version_stage(156 SecretId=secret_id,157 VersionStage="AWSCURRENT",158 MoveToVersionId=token159 )160 ```161162## Rules163164- Never set database passwords, API keys, or tokens as Lambda environment variables — use Secrets Manager or Parameter Store.165- Cache secrets in the function's module-level scope (reused across warm invocations) but with a TTL — stale cache shouldn't persist rotated credentials indefinitely.166- Lambda layers used from public ARNs are third-party code running in your execution environment — audit them as you would any dependency.167- Set `POWERTOOLS_LOG_LEVEL=INFO` (not DEBUG) in production — debug logging often includes full event payloads which may contain tokens.168169## Common Mistakes170171- **Fetching secrets inside the handler on every invocation** — without caching, every Lambda call makes a Secrets Manager API call, adding 20–100ms latency and incurring API costs at scale; cache with TTL.172- **Logging `event` at INFO level** — API Gateway events include `Authorization` headers and request bodies; log only the fields needed for debugging.173- **Using Lambda layers from unknown public accounts** — verify the account ID in the layer ARN belongs to a known publisher (AWS, a major library vendor, or your own account).174- **Hardcoding secret names as strings** — store secret ARNs in environment variables so the same code works across dev/staging/prod with different secret paths.