Trustwave's 2024 Retail Report Series Highlights Alarming E-Commerce Threats and Growing Fraud Against Retailers. Learn More

Trustwave's 2024 Retail Report Series Highlights Alarming E-Commerce Threats and Growing Fraud Against Retailers. Learn More

Services
Managed Detection & Response

Eliminate active threats with 24/7 threat detection, investigation, and response.

Co-Managed SOC (SIEM)

Maximize your SIEM investment, stop alert fatigue, and enhance your team with hybrid security operations support.

Advisory & Diagnostics

Advance your cybersecurity program and get expert guidance where you need it most.

Penetration Testing

Test your physical locations and IT infrastructure to shore up weaknesses before exploitation.

Database Security

Prevent unauthorized access and exceed compliance requirements.

Email Security

Stop email threats others miss and secure your organization against the #1 ransomware attack vector.

Digital Forensics & Incident Response

Prepare for the inevitable with 24/7 global breach response in-region and available on-site.

Firewall & Technology Management

Mitigate risk of a cyberattack with 24/7 incident and health monitoring and the latest threat intelligence.

Solutions
BY TOPIC
Microsoft Security
Unlock the full power of Microsoft Security
Offensive Security
Solutions to maximize your security ROI
Rapidly Secure New Environments
Security for rapid response situations
Securing the Cloud
Safely navigate and stay protected
Securing the IoT Landscape
Test, monitor and secure network objects
Why Trustwave
About Us
Awards and Accolades
Trustwave SpiderLabs Team
Trustwave Fusion Security Operations Platform
Trustwave Security Colony
Partners
Technology Alliance Partners
Key alliances who align and support our ecosystem of security offerings
Trustwave PartnerOne Program
Join forces with Trustwave to protect against the most advance cybersecurity threats

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

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'

Other type of secret

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

follows: follows

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

automatic rotation

 

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.

ABOUT TRUSTWAVE

Trustwave is a globally recognized cybersecurity leader that reduces cyber risk and fortifies organizations against disruptive and damaging cyber threats. Our comprehensive offensive and defensive cybersecurity portfolio detects what others cannot, responds with greater speed and effectiveness, optimizes client investment, and improves security resilience. Learn more about us.

Latest Intelligence

Discover how our specialists can tailor a security program to fit the needs of
your organization.

Request a Demo