Skip to content

How to reflect Secrets Manager value changes in containers on Fargate

0

I am running a container that runs on Fargate, and there are parts in the application code that use connection information to RDS. I would like to get that connection information from Secrets Manager. Since Secrets Manager values ​​are rotated, I would like existing containers to be updated when changes occur. Is there a way to achieve this?

I also considered referencing Secrets Manager in the ECS task definition, but I understand that this method does not update containers that are already running, and that the task must be restarted. Please let me know if there is a way to update existing containers without restarting the task.

3 Answers
0

Hello.

I think the following documents will be helpful.
Change the application to obtain authentication information and cache it.
After that, if the cache expires or is rotated and an authentication error occurs, you can create an application to retrieve authentication information from Secrets Manager again.
https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/rotate-database-credentials-without-restarting-containers.html

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

Referencing Secrets Manager in the ECS task definition sets environment variables which do not refresh unless the container is restarted and is not a secure method to provide credentials

Riku's answer is the best way to go

EXPERT

answered 2 years ago

0

Thank you for your reply. I have checked the contents of your website.

I am using a Java application, so I understand that the following can be used.

https://github.com/aws/aws-secretsmanager-jdbc

I tried the following to test the connection.

  • Add the following to your Java application
public class DatabaseConnection {
    public static void main(String[] args) {
        try {
            /// Load the JDBC driver
            Class.forName("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver").newInstance();

            // Get connection information using secret ID
            String secretId = "{my_secret_id(ARN)}";
            Properties info = new Properties();
            info.put("user", secretId);

            // Establish database connection
            String url = "jdbc-secretsmanager:postgresql://{my_db_host}:{my_db_port}/{my_db_name}";
            Connection conn = DriverManager.getConnection(url, info);

            // Connection success message
            System.out.println("Connection established successfully!");

            // Close the connection
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • Download "aws-secretsmanager-jdbc-2.0.2.jar" and place it in any directory.

https://mvnrepository.com/artifact/com.amazonaws.secretsmanager/aws-secretsmanager-jdbc/2.0.2

  • Compile Java program

javac -cp .:{path}/aws-secretsmanager-jdbc-2.0.2.jar DatabaseConnection.java

  • Run

java -cp .:{path}/aws-secretsmanager-jdbc-2.0.2.jar DatabaseConnection

As a result, the following error will appear.

Exception in thread "main" java.lang.NoClassDefFoundError: software/amazon/awssdk/services/secretsmanager/SecretsManagerClient at com.amazonaws.secretsmanager.util.JDBCSecretCacheBuilderProvider.build(JDBCSecretCacheBuilderProvider.java:58) at com.amazonaws.secretsmanager.sql.AWSSecretsManagerDriver.<init>(AWSSecretsManagerDriver.java:126) at com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver.<init>(AWSSecretsManagerPostgreSQLDriver.java:63) at com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver.<clinit>(AWSSecretsManagerPostgreSQLDriver.java:55) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:421) at java.base/java.lang.Class.forName(Class.java:412) at DatabaseConnection.main(DatabaseConnection.java:9) Caused by: java.lang.ClassNotFoundException: software.amazon.awssdk.services.secretsmanager.SecretsManagerClient at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526) ... 8 more

Can you tell me how to deal with this?

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.