From a Best Practice Perspective its good not having to pass sensitive information around, and especially not hard coding them.
Best Practice: Security
One good way is to use SSM with KMS to Encrypt/Decrypt them, but since EC2 has a Metadata Service available, we can make use of that to retrieve temporary credentials. One requirement though, is that the instance will require an IAM Role where the code will be executed on. The IAM Role also needs to have sufficient privileges to be able to execute, whatever you need to do.
The 12 Factor Methodology however states to use config in your environment variables, but from the application logic, its easy to save it in our environment.
Scenario: Applications on AWS EC2
When you run applications on Amazon EC2 the nodes has access to the EC2 Metadata Service, so in this case our IAM Role has a Policy that authorizes GetItem on our DynamoDB table, therefore we can define our code with no sensitive information, as the code will do all the work to get the credentials and use the credentials to access DynamoDB.
Use Temporary Credentials to Read from DynamoDB
In this example we will get the temporary credentials from the metadata service, then define the temporary credentials in our session to authorize our request against dynamodb to read from our table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Also, when you are logged onto the EC2 instance, you can use curl to see the temporary credentials information:
1 2 3 4 5 6 7 8 9 10 11