Last active
August 8, 2021 08:28
-
-
Save yassermog/ca5ed7849c875d26ab3a73d43fe5f6cc to your computer and use it in GitHub Desktop.
getAWSSecretValue.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function getAWSSecretValue($secretName) | |
| { | |
| $client = new SecretsManagerClient([ | |
| 'profile' => 'default', | |
| 'region' => 'ap-southeast-1', | |
| 'version' => '2017-10-17', | |
| ]); | |
| try { | |
| $result = $client->getSecretValue([ | |
| 'SecretId' => $secretName, | |
| ]); | |
| } catch (AwsException $e) { | |
| $error = $e->getAwsErrorCode(); | |
| if ($error == 'DecryptionFailureException') { | |
| // Secrets Manager can't decrypt the protected secret text using the provided AWS KMS key. | |
| // Handle the exception here, and/or rethrow as needed. | |
| throw $e; | |
| } | |
| if ($error == 'InternalServiceErrorException') { | |
| // An error occurred on the server side. | |
| // Handle the exception here, and/or rethrow as needed. | |
| throw $e; | |
| } | |
| if ($error == 'InvalidParameterException') { | |
| // You provided an invalid value for a parameter. | |
| // Handle the exception here, and/or rethrow as needed. | |
| throw $e; | |
| } | |
| if ($error == 'InvalidRequestException') { | |
| // You provided a parameter value that is not valid for the current state of the resource. | |
| // Handle the exception here, and/or rethrow as needed. | |
| throw $e; | |
| } | |
| if ($error == 'ResourceNotFoundException') { | |
| // We can't find the resource that you asked for. | |
| // Handle the exception here, and/or rethrow as needed. | |
| throw $e; | |
| } | |
| } | |
| if (isset($result['SecretString'])) { | |
| $secret = $result['SecretString']; | |
| } else { | |
| $secret = base64_decode($result['SecretBinary']); | |
| } | |
| return $secret; | |
| } | |
| ?> |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
getAWSSecretValue in PHP form AWS Secrets Manager