How to use the AWS Secrets Manager Agent
AWS Secrets Manager is a service that helps you manage, retrieve, and rotate database credentials, application credentials, OAuth tokens, API keys, and other secrets throughout their lifecycles… In this blog post, we introduce a new feature, the Secrets Manager Agent, and walk through how you can…
AWS Secrets Manager is a service that helps you manage, retrieve, and rotate database credentials, application credentials, OAuth tokens, API keys, and other secrets throughout their lifecycles. You can use Secrets Manager to replace hard-coded credentials in application source code with a runtime call to the Secrets Manager service to retrieve credentials dynamically when you need them. Storing the credentials in Secrets Manager helps to avoid unintended access by anyone who inspects your application’s source code, configuration, or components.
In this blog post, we introduce a new feature, the Secrets Manager Agent, and walk through how you can use it to retrieve Secretes Manager secrets.
New approach: Secrets Manager Agent
Previously, if you had an application that used Secrets Manager and needed to retrieve secrets, you had to use the AWS SDK or one of our existing caching libraries. Both these options are specific to a certain coding language and allow only limited scope for customization.
The Secrets Manager Agent is a client-side agent that allows you to standardize consumption of secrets from Secrets Manager across your AWS compute environments. (AWS has published the code for the agent as open source code.) Secrets Manager Agent pulls and caches secrets in your compute environment and allows your applications to consume secrets directly from the in-memory cache. The Secrets Manager Agent opens a localhost port inside your application environment. With this port, you fetch the secret value from the local agent instead of making network calls to the service. This allows you to improve the overall availability of your application while reducing your API calls. Because the Secrets Manager Agent is language agnostic, you can install the binary file of the agent on many types of AWS compute environments.
Although you can use this feature to retrieve and cache secrets in your application’s compute environment, the access controls for Secrets Manager secrets remain unchanged. This means that AWS Identity and Access Management (IAM) principals need the same permissions as if they were to retrieve each of the secrets. You will need to provide GetSecretValue and DescribeSecret permissions to the secrets that you want to consume by using the Secrets Manager Agent.
The Secrets Manager Agent offers protection against server-side request forgery (SSRF). When you install the Secrets Manager Agent, the script generates a random SSRF token on startup and stores it in the file /var/run/awssmatoken. The token is readable by the awssmatokenreader group that the install script creates. The Secrets Manager Agent denies requests that don’t have an SSRF token in the header or that have an invalid SSRF token.
Solution overview
The Secrets Manager Agent provides a language-agnostic way to consume secrets in your application code. It supports various AWS compute services, such as Amazon Elastic Compute Cloud (Amazon EC2), Amazon Elastic Container Service (Amazon ECS), Amazon Elastic Kubernetes Service (Amazon EKS), and AWS Lambda functions. In this solution, we share how you can install the Secrets Manager Agent on an EC2 machine and retrieve secrets in your application code by using CURL commands. See the AWS Secrets Manager Agent documentation to learn how you can use this agent with other types of compute services.
Prerequisites
You need to have the following:
Follow the steps on the Install or update to the latest version of the AWS CLI page to install the AWS CLI and the Configure the AWS CLI page to configure it.
Create the secret
The first step will be to create a secret in Secrets Manager by using the AWS CLI.
To create a secret
- Enter the following command in a terminal to create a secret:
You will see an output like the following:
Record the secret ARN as <SECRET_ARN>, because you will use it in the next section.
Create the IAM role
The Lambda function, the EC2 instance, and the ECS task definition need an IAM role that grants permission to retrieve the secret you just created.
To create the IAM role
- Using an editor, create a file named ec2_iam_policy.json with the following content:
- Type the following command in a terminal to create the IAM role:
- Create a file named iam_permission.json with the following content, replacing <SECRET_ARN> with the secret ARN you noted earlier:
- Type the following command to create a policy:
Record the Arn as <POLICY_ARN>, because you will need that value next.
- Type the following command to add this policy to the IAM role, replacing <POLICY_ARN> with the value you just noted:
- Type the following command to add the AWS Systems Manager policy to the role:
Launch an EC2 instance
Use the steps in this section to launch an EC2 instance.
To create an instance profile
- Type the following command to create an instance profile:
- Type the following command to associate this instance profile with the role you just created:
To create a security group
- Type the following command to create a security group:
Record the group ID as <GROUP_ID>, because you will need this value in the next step.
To launch an EC2 instance
- Run the following command to launch an EC2 instance, replacing <GROUP_ID> with the security group ID:
Record the InstanceId value as <INSTANCE_ID>.
- Check the status of this launch by running the following command:
You will see a response like the following, which shows that the instance is running:
- After the instance is in running state, type the following command to connect to the EC2 instance, replacing <INSTANCE_ID> with the value you noted earlier:
Leave the session open, because you will use it in the next step.
Install the Secrets Manager Agent to the EC2 instance
Use the steps in this section to install the Secrets Manager Agent in the EC2 instance. You will run these commands in the EC2 instance you created earlier.
To download the Secrets Manager Agent code
- Type the following command to install git in the EC2 instance:
- Type the following command to download the Secrets Manager Agent code:
To install the Secrets Manager Agent
- Type the following command to install the Secrets Manager Agent:
To grant permission to read the token file
- Type the following command to copy the token file and grant permission for the current user (ec2-user) to read it:
Retrieve the secret
Now you can use the local web server to retrieve the agent. Processes running in this EC2 instance can retrieve the secret with a REST API call from the web server.
To retrieve a secret
Retrieving a secret is now possible for the process in this EC2 instance, thanks to the local agent.
- Run the following command to retrieve the secret:
You will see the following output:
- Exit from the EC2 instance by typing exit.
Clean up
Follow the steps in this section to clean up the resources created by the solution.
To terminate the EC2 instance and associated resources
- Type the following command to stop the EC2 instance, replacing <INSTANCE_ID> with the EC2 InstanceId received at the time of instance launch:
- Run the following command to delete the security group:
- Run the following command to delete the IAM role from the instance profile:
- Run these commands to delete the instance profile:
To clean up the IAM role
- Run the following command to delete the policy role, replacing <POLICY_ARN> with the value you noted earlier:
- Run the following command to detach the policy from the role:
- Run the following command to delete the IAM role:
To clean up the secret
- Run the following command to delete the secret:
Conclusion
In this post, we introduced the Secrets Manager Agent and showed how to install it in an EC2 instance, allowing the retrieval of secrets from Secrets Manager. An application can call this web server to retrieve secrets without using the AWS SDK. See the AWS Secrets Manager Agent documentation to learn more about how you can use this Secrets Manager Agent in other compute environments.
To learn more about AWS Secrets Manager, see the AWS Secrets Manager documentation.
If you have feedback about this post, submit comments in the Comments section below. If you have questions about this post, contact AWS Support.
Author: Eduardo Patrocinio