Possible AWS credential exposure in Lambda environment variables

Keep actual AWS credentials out of Lambda configuration and use an execution role with the required permissions.

Description

Long-term AWS keys in Lambda environment variables can remain in the template, deployment history, and function configuration. Check whether the value is a real secret. For AWS calls, use the temporary credentials supplied by the function’s execution role.

Potential impact

A valid ID and secret together can be used for the actions the key’s policies and other access controls permit.

Remediation

Remove long-term AWS keys from custom environment variables in the template and update the deployed function. Give its execution role only the permissions it needs. Retrieve external-service secrets from a restricted secret store. If a key was exposed, deactivate it promptly, switch its consumers, and verify they work. Review AWS activity and copies in repository and deployment history.

Examples

Supply the S3 code bucket and object key, and the execution role ARN. Check that the function code supports nodejs22.x.

Before

yaml
Parameters:
  FunctionCodeBucket:
    Type: String
  FunctionCodeKey:
    Type: String
  ExecutionRoleArn:
    Type: String
Resources:
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !Ref ExecutionRoleArn
      Environment:
        Variables:
          foo: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
          databaseName: lambdadb
          databaseUser: admin
      Code:
        S3Bucket: !Ref FunctionCodeBucket
        S3Key: !Ref FunctionCodeKey
      Runtime: nodejs22.x

The value in foo is a public example secret from the AWS documentation.

After

yaml
Parameters:
  FunctionCodeBucket:
    Type: String
  FunctionCodeKey:
    Type: String
  ExecutionRoleArn:
    Type: String
Resources:
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.handler
      Role: !Ref ExecutionRoleArn
      Environment:
        Variables:
          foo: test
          databaseName: lambdadb
          databaseUser: admin
      Code:
        S3Bucket: !Ref FunctionCodeBucket
        S3Key: !Ref FunctionCodeKey
      Runtime: nodejs22.x

foo now contains test. Verify that the function uses its existing execution role for AWS calls.

References