Issuing an access key to AWS EC2 is an anti-pattern. It is best not to issue the key as it can be stolen and used. A good pattern is to issue a temporary access key called an IAM role to EC2.
run.py
import boto3
session = boto3.session.Session(profile_name='prodaccess')
credentials = session.get_credentials()
#Feel free to use this value with boto.
print(credentials.access_key)
print(credentials.secret_key)
print(credentials.token)
Getting temporary AWS credentials using boto3
There is a library called aws-sdk-php-laravel.
aws-sdk-php-laravel Get AWS ElasticSearch Service data in Laravel using IAM Role [Providing temporary credentials to the official AWS SDK for PHP](https://docs.aws.amazon.com/ja_jp/sdk-for-php/v3/developer-guide/aws-sdk-php-v3-developer -guide.pdf)
composer require aws/aws-sdk-php
composer.json
{
"require": {
"aws/aws-sdk-php-laravel": "~3.0"
}
}
As official
createToken is good and what to do.
There is also a sample here [https://gist.github.com/sators/38dbe25f655f1c783cb2c49e9873d58a).
python
$token = $RdsAuthGenerator->createToken($clusterEndpoint . ":" . $clusterPort, $clusterRegion, $dbUsername);
$mysqli = mysqli_init();
mysqli_options($mysqli, MYSQLI_READ_DEFAULT_FILE, "./my.cnf");
$mysqli->real_connect($clusterEndpoint, $dbUsername, $token, $dbDatabase, $clusterPort, NULL, MYSQLI_CLIENT_SSL);
if ($mysqli->connect_errno) {
echo "Error: Failed to make a MySQL connection, here is why: <br />";
echo "Errno: " . $mysqli->connect_errno . "<br />";
echo "Error: " . $mysqli->connect_error . "<br />";
exit;
}
/***** Example code to perform a query and return all tables in the DB *****/
$tableList = array();
$res = mysqli_query($mysqli,"SHOW TABLES");
while($cRow = mysqli_fetch_array($res))
{
$tableList[] = $cRow[0];
}
Recommended Posts