简体   繁体   English

如何使用AWS开发工具包启动ec2实例并通过AWS CLI连接到它

[英]how to launch ec2 instance using AWS SDK and connect to it via AWS CLI

this is a newbie question. 这是一个新手问题。 Please, bear with me. 请多多包涵。

I tried to create an instance using AWS SDK for JAVA by following the official tutorial. 我尝试按照官方教程使用适用于JAVA的AWS开发工具包创建实例。 When It comes to the key pair part I get confused : 当谈到密钥对部分时,我会感到困惑:

 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");

By doing like this, how can I use aws cli to connect to the instance I created using the SDK, I mean how to locate the .pem file ( if it does exist ). 这样,如何使用aws cli连接到使用SDK创建的实例,就意味着如何找到.pem文件(如果存在)。 Is my logic correct in term of creating ec2 instance using the code and manage it via aws cli, or it should all be in the same way?! 就使用代码创建ec2实例并通过aws cli进行管理而言,我的逻辑是正确的,还是应该以相同的方式进行?

The PEM key is in the contents of your privateKey String variable. PEM密钥位于privateKey String变量的内容中。 You need to write that out to a file, as this is the only time you will be able to get to it via the API. 您需要将其写到文件中,因为这是您唯一一次可以通过API进行访问的文件。

To connect to the server, you don't use the AWS CLI. 要连接到服务器,请不要使用AWS CLI。 You would use the ssh command. 您将使用ssh命令。 I would read the answers to this question for more information about using SSH to connect to an EC2 server: how to login to ec2 machine? 我将阅读此问题的答案,以获取有关使用SSH连接到EC2服务器的更多信息: 如何登录ec2机器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 aws sdk 在 EC2 实例上启动和安装应用程序 - Launch and install applications on EC2 instance using aws sdk 如何使用 AWS Javascript SDK 从 EC2 实例连接其他 AWS 云服务? - How to connect other AWS cloud service from an EC2 instance using AWS Javascript SDK? 如何使用json文件和python启动AWS EC2实例? - How to launch aws ec2 instance using json file with python? 如何使用 AWS CLI 查找 EC2 实例的操作系统 - How to find OS of an EC2 instance using AWS CLI 通过适用于PHP的AWS开发工具包启动EC2实例时无法添加标签(无CLI) - Unable to add tags when launching EC2 instance via AWS SDK for PHP (no CLI) 无法通过AWS EC2实例使用PHP连接到AWS MYSQL RDS实例 - Unable to connect to AWS MYSQL RDS instance using PHP via AWS EC2 Instance 如何使用带Terraform的AWS启动模板启动AWS EC2实例? - How do I launch an AWS EC2 instance using an AWS launch template with Terraform? 如何使用 AWS CLI 使用 EC2 标签或名称删除 EC2 实例 - How to delete EC2 instance using AWS CLI using EC2 tag or name 如何使用AWS Cli启动具有自定义根卷ebs大小(大于8GB)的ec2实例 - How to launch ec2 instance with custom root volume ebs size (more than 8GB) using AWS Cli 如何使用PHP通过https启动AWS Ec2实例? - How to start an AWS Ec2 instance via https using PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM