2016-03-21 169 views
0

這是一個新手問題。請多多包涵。如何使用AWS SDK啓動ec2實例並通過AWS CLI連接到它

我試圖使用適用於JAVA的AWS開發工具包創建實例,請遵循官方教程。當涉及到的密鑰對部分我感到困惑:

CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest(); 
    createKeyPairRequest.withKeyName("azzouz_key"); 
    CreateKeyPairResult createKeyPairResult = Client.createKeyPair(createKeyPairRequest); 
    KeyPair keyPair = new KeyPair(); 
    keyPair = createKeyPairResult.getKeyPair(); 
    String privateKey = keyPair.getKeyMaterial(); 

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest(); 

    runInstancesRequest.withImageId("ami-4b814f22") 
      .withInstanceType("m1.small") 
      .withMinCount(1) 
      .withMaxCount(1) 
      .withKeyName("azzouz_key") 
      .withSecurityGroups("Azzouz_group"); 

通過做這樣,我該如何使用AWS CLI連接到我使用SDK創建的實例,我的意思是如何定位.pem文件(如果它確實存在)。我的邏輯在使用代碼創建ec2實例方面是否正確並通過aws cli進行管理,或者它們都應該採用相同的方式?

回答

2

PEM密鑰位於您的privateKey字符串變量的內容中。您需要將其寫入文件中,因爲這是您唯一能夠通過API訪問的文件。

要連接到服務器,請不要使用AWS CLI。您可以使用ssh命令。我會閱讀這個問題的答案,以獲得更多關於使用SSH連接到EC2服務器的信息:how to login to ec2 machine?

+0

謝謝。我知道我必須使用ssh命令,這就是我的意思。 所以解決方案是將privateKey寫入文件。要試試這個,讓你知道它是否有幫助。 –

+0

您在問題中指定了「AWS CLI」,這是AWS提供的特定工具的名稱。 https://aws.amazon.com/cli/ –