-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathCreateKeyPair.php
46 lines (37 loc) · 1.28 KB
/
CreateKeyPair.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/*
* ABOUT THIS PHP SAMPLE: This sample is part of the SDK for PHP Developer Guide topic at
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/ec2-examples-working-with-key-pairs.html
*
*
*
*/
// snippet-start:[ec2.php.create_key_pair.complete]
// snippet-start:[ec2.php.create_key_pair.import]
require 'vendor/autoload.php';
// snippet-end:[ec2.php.create_key_pair.import]
/**
* Create a Key Pair
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// snippet-start:[ec2.php.create_key_pair.main]
$ec2Client = new Aws\Ec2\Ec2Client([
'region' => 'us-west-2',
'version' => '2016-11-15',
'profile' => 'default'
]);
$keyPairName = 'my-keypair';
$result = $ec2Client->createKeyPair(array(
'KeyName' => $keyPairName
));
// Save the private key
$saveKeyLocation = getenv('HOME') . "/.ssh/{$keyPairName}.pem";
file_put_contents($saveKeyLocation, $result['keyMaterial']);
// Update the key's permissions so it can be used with SSH
chmod($saveKeyLocation, 0600);
// snippet-end:[ec2.php.create_key_pair.main]
// snippet-end:[ec2.php.create_key_pair.complete]