生成AWS一键登录链接


生成一键登录AWS控制台的链接

第一步:保存登录密钥

echo '
[default]
aws_access_key_id = id
aws_secret_access_key = key
' > ~/.aws/credentials

第二步:编写代码

use Aws\Sts\StsClient;
use Aws\Exception\AwsException;

try {
    // https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.phar
    require 'aws.phar';

    $client = new StsClient(['version' => 'latest', 'region' => 'ap-northeast-1', 'profile' => 'default']);
    $policy = '{"Version": "2012-10-17", "Statement": [{"Effect": "Allow", "Action": "*", "Resource": "*"}]}';
    $token = $client->getFederationToken(['Name' => 'admin', 'Policy' => $policy]);
    $credentials = $token['Credentials'];
    $session = ['sessionId' => $credentials['AccessKeyId'], 'sessionKey' => $credentials['SecretAccessKey'], 'sessionToken' => $credentials['SessionToken']];
    $sign = json_decode(file_get_contents('https://signin.aws.amazon.com/federation?Action=getSigninToken&SessionDuration=1800&Session=' . urlencode(json_encode($session))), true);
    echo $loginUrl = 'https://signin.aws.amazon.com/federation?Action=login&Issuer=admin&Destination=https%3A%2F%2Fconsole.aws.amazon.com%2F&SigninToken=' . $sign['SigninToken'];
} catch (AwsException $e) {
    error_log($e->getMessage());
}