Obtaining temporary AWS credentials in PHP, Python

Introduction

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.

Used in Python

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

Use with PHP

Used with Laravel

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

Use PHP SDK directly

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

Obtaining temporary AWS credentials in PHP, Python
[Python] Scraping in AWS Lambda
Write AWS Lambda function in Python
Avoid nested loops in PHP and Python
(For myself) Flask_AWS_1 (Install PHP, MySQL, phpMyAdmin, Python in AWS virtual environment)
Quadtree in Python --2
Python in optimization
CURL in python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
POST JSON in Python and receive it in PHP
Disassemble in Python
Getting started with AWS IoT easily in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Grouping combination in Python / Ruby / PHP / Golang (Go)
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
[AWS IoT] Register things in AWS IoT using the AWS IoT Python SDK
Execute python3 system with PHP exec () on AWS EC2
Operators ++,-cannot be used in python (difference from php)
[Python] Retry process (Exponential Backoff) memo in AWS Lambda
Handle prime numbers in Python / Ruby / PHP / Golang (Go)
AWS SDK for Python (Boto3) development in Visual Studio 2017
How to handle JSON in Ruby, Python, JavaScript, PHP
Sorted list in Python
Daily AtCoder # 36 in Python
Clustering text in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python