'Getting password from AWS Secret Manager using IAM with PHP?

I am quite new to the AWS infrastructure. I would like to get the MySQL password from the AWS Secret Manager. I have read that it is recommended to use IAM instead of storing the credentials on the webserver. Unfortunately I did not find a code example how I can access it. What are the actions I have to do?

My current approach without the IAM connection is this:

require 'vendor/autoload.php';

use Aws\SecretsManager\SecretsManagerClient; 
use Aws\Exception\AwsException;

$client = new SecretsManagerClient([
    'version' => 'latest',
    'region' => 'eu-central-1',
]);

$secretName = 'WebApp/App/MySQL';

try {
    $result = $client->getSecretValue([
        'SecretId' => $secretName,
    ]);

How can I add the IAM connection to the code? And which role do I have to create to get the SQL connection data from Secrets Manager?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source