SpiderLabs Blog

Using AWS Secrets Manager and Lambda Function to Store, Rotate and Secure Keys

Written by Sally Gebreananeya | Jul 16, 2024 6:11:43 PM

When working with Amazon Web Services (AWS), we often find that various AWS services need to store and manage secrets. AWS Secrets Manager is the go-to solution for this. It's a centralized service designed to help manage sensitive information securely, significantly reduce the risk of credential compromise, and facilitate your credential management process. However, there's more to it than just storing secrets.

 

What are AWS Secrets?

AWS secrets are keys used to authorize different AWS services. These secret keys can be database credentials, application credentials, OAuth tokens, API keys, and access/secret keys. To manage and periodically rotate/update these keys, we can integrate the Lambda function with AWS SecretsManager.

In many cases, AWS services create and manage their secrets. For example, managed secrets often come with built-in rotation. This means the secrets can be updated and rotated automatically without any manual configuration on your part. This automatic rotation is a huge advantage, as it helps maintain security best practices effortlessly. Some of AWS’s services that can create managed secrets are appflow, databrew, datasync, directconnect, ECS, events, marketplace-deployment, opsworks-cm, RDS, redshift, sqlworkbench.

 

The Role of the Managing Service

The service that creates a managed secret also takes on the responsibility of managing it. This can include restrictions on how you interact with these secrets.

For instance, you might find that you cannot update or delete these secrets without going through a recovery period. While this might seem like a limitation, it's a safeguard. An example of a service that manages secrets is RDS. When creating your RDS target, you can choose to store database authentication directly to AWS SecretsManager. By directly storing these credentials, RDS will automatically store and manage the rotation of these credentials.

 

Manually Creating and Managing Secrets

Integrating Lambda functions with AWS SecretsManager simplifies the process of credential rotation for AWS account owners by automating the task.

AWS SecretsManager offers secure storage and rotation of various secrets using Lambda functions. These secrets include:

  • Credentials for Amazon RDS databases
  • Credentials for Amazon DocumentDB databases
  • Credentials for Amazon Redshift data warehouses
  • Credentials for other databases
  • Other types of secrets like API keys, OAuth tokens, and more.

 

Creating and Managing AWS IAM credentials

For storing and rotating IAM credentials (access/secret key pair), we need to create a Lambda function to rotate these secrets. Here are the steps to manage these secrets:

  • Create a Lambda function that will take care of AWS credential rotation
  • Create and configure AWS policies for the Lambda function created
  • Add new secrets to AWS SecretsManager

1. Create a lambda function for AWS credential rotation

Begin by creating a function using any Lambda-supported language. The function will retrieve credentials from AWS SecretsManager, and generate a new key pair for the IAM user every 90 days as set in the console. Discard the old key pairs and update the IAM user details and secrets with the new key pair.

2. Create and configure AWS policies for the Lambda function created

For the Lambda function to access and create IAM credentials, it needs to have an IAM role attached to it with the proper permissions. An identity-based policy creates the role for the Lambda function.

  • Identity-based policy

This policy, when linked to a role and assigned to the Lambda function, authorizes the Lambda function to execute actions on AWS IAM and SecretsManager.

{

   "Version": "2012-10-17",

   "Statement": [

       {

           "Effect": "Allow",

           "Action": [

               "secretsmanager:GetSecretValue",

               "secretsmanager:PutSecretValue",

               "iam:ListAccessKeys",

               "iam:CreateAccessKey",

               "iam:UpdateAccessKey",

               "iam:DeleteAccessKey"

           ],

           "Resource": "*"

       }

   ]

}

  • Resource-based policy

Resource-based policies are attached to AWS services to provide a way to control access to other AWS services. In this case, the policy below enables AWS SecretsManager to trigger the created Lambda function on the rotation period set. The "lambda: InvokeFunction" action in the policy will allow AWS SecretsManager to invoke the selected Lambda function every 90 days in this case.

{

   "Version": "2012-10-17",

   "Statement": [

       {

           "Effect": "Allow",

           "Principal": {

               "Service": "secretsmanager.amazonaws.com"

           },

           "Action": "lambda:InvokeFunction",

           "Resource": "arn:aws:lambda:us-east-2:ACCOUNT_ID:function:KeyRotator"

       }

   ]

}

3. Add Secrets to AWS SecretsManager

Visit https://us-east-2.console.aws.amazon.com/secretsmanager/newsecret?region=us-east-2

Choose secret type 'Other type of secret'

Enter the secrets of the IAM user for whom you want the secret to be stored.

follows: 

Finally, set up an automatic rotation by choosing the Lambda function you created above and setting the rotation period.

 

Conclusion

Storing and managing AWS credentials using AWS Secrets Manager and Lambda functions offers a comprehensive solution that enhances security, simplifies management, ensures compliance, improves operational efficiency, and leverages AWS’s reliable infrastructure. By automating the management of credentials and leveraging secure storage and access controls, you can significantly reduce the risk of credential compromise and streamline your credential management processes.