Trustwave Rapid Response: CrowdStrike Falcon Outage Update. Learn More

Trustwave Rapid Response: CrowdStrike Falcon Outage Update. Learn More

Services
Capture
Managed Detection & Response

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

twi-managed-portal-color
Co-Managed SOC (SIEM)

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

twi-briefcase-color-svg
Advisory & Diagnostics

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

tw-laptop-data
Penetration Testing

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

twi-database-color-svg
Database Security

Prevent unauthorized access and exceed compliance requirements.

twi-email-color-svg
Email Security

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

tw-officer
Digital Forensics & Incident Response

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

tw-network
Firewall & Technology Management

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

Solutions
BY TOPIC
Offensive Security
Solutions to maximize your security ROI
Microsoft Exchange Server Attacks
Stay protected against emerging threats
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
SpiderLabs Blog

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.

Latest SpiderLabs Blogs

Trustwave Rapid Response: CrowdStrike Falcon Outage Update

Trustwave is proactively assessing and monitoring our clients who may have been impacted by CrowdStrike’s recently rolled-out update for its Windows users. The critical issue identified with...

Read More

Facebook Malvertising Epidemic – Unraveling a Persistent Threat: SYS01

The Trustwave SpiderLabs Threat Intelligence team's ongoing study into how threat actors use Facebook for malicious activity has uncovered a new version of the SYS01 stealer. This stealer is designed...

Read More

Tips for Optimizing Your Security Operations Framework

Building an effective Security Operations framework that provides the right balance of people, processes, and technologies can take years.

Read More