Description
Use the Lambda execution role’s temporary credentials for AWS calls. Long-term keys you put in environment.variables can remain in Terraform state, deployment configuration, and logs that print the environment.
Potential impact
A valid access key ID and its secret can be reused with the key’s permissions. Functions sharing that key will need new credentials when it is revoked.
Remediation
Remove any credentials you supplied and limit the execution role to the function’s needs. Authenticate deployments separately. Retrieve external-service secrets from Secrets Manager or Systems Manager Parameter Store SecureString with restricted read permissions. If a real key was exposed, deactivate it promptly and switch its consumers. Review AWS activity and copies in source, state, and logs.
Examples
Prepare the referenced execution role and deployment package. Choose a supported lambda_runtime compatible with the package and handler.
Before
resource "aws_lambda_function" "lambda_function" {
filename = "lambda_function_payload.zip"
function_name = "lambda_function_name"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.test"
runtime = var.lambda_runtime
environment {
variables = {
foo = "AKIAIOSFODNN7EXAMAAA"
}
}
}
foo contains an illustrative ID-like string. No corresponding secret is shown.
After
resource "aws_lambda_function" "lambda_function" {
filename = "lambda_function_payload.zip"
function_name = "lambda_function_name"
role = aws_iam_role.iam_for_lambda.arn
handler = "exports.test"
runtime = var.lambda_runtime
environment {
variables = {
foo = "test"
}
}
}
foo becomes test. Verify that the function can perform its required operations using the existing execution role.