简体   繁体   English

使用python的boto3库登录到AWS实例

[英]Logging into a AWS instance using boto3 library of python

I am attempting to write a python code that would few of my manual steps in logging into the AWS platform. 我正在尝试编写一个Python代码,这将是我登录AWS平台所需的几个手动步骤。

In Ubuntu terminal , I used to write the command 在Ubuntu终端中,我曾经编写命令

ssh -A ec2-user@<ip-address>

and then again log into another instance using 然后再次使用登录到另一个实例

ssh ec2-user@<ip.address>

Now I am looking at python code that would be able to automate this logging in process. 现在,我正在查看能够自动执行此登录过程的python代码。 I have written the following code till now. 到目前为止,我已经编写了以下代码。

import boto3
ec2 = boto3.resource('ec2')

There are 2 ways mostly to configure the boto3 library. 主要有2种方法来配置boto3库。

  1. You need to configure it first on your system and use the same configuration everywhere. 您需要先在系统上对其进行配置,然后在各处使用相同的配置。 You can use AWS CLI for this by running aws configure on your terminal. 您可以通过在终端上运行aws configure来使用AWS CLI

  2. Set the environment variables and call the boto3 configuration via process.env.ENV_KEY and then use it like : 设置环境变量,并通过process.env.ENV_KEY调用boto3配置,然后像这样使用它:

client = boto3.client( 'ec2', aws_access_key_id=process.env.ACCESS_KEY, aws_secret_access_key=process.env.SECRET_KEY, aws_session_token=process.env.SESSION_TOKEN, )

If you want to perform actions on a running instance, boto3 is not what you're looking for. 如果要在正在运行的实例上执行操作,则不需要boto3。 What you're asking about is more in the realm of what's called configuration management . 您要问的更多是在所谓的配置管理领域

While you could write something yourself using an SSH library like Paramiko , you may want to look at a more purpose-built software package like Fabric . 虽然您可以使用诸如Paramiko之类的SSH库自己编写一些东西,但您可能希望查看更加专用的软件包,如Fabric It's built on-top of the aforementioned Paramiko, with added functionality tailored to running commands on remote servers. 它建立在上述Paramiko之上,并具有专门为在远程服务器上运行命令而定制的附加功能。 For a more full-featured, open source configuration management solution, I recommend looking into Ansible . 对于功能更全的开源配置管理解决方案,我建议调查Ansible

AWS also has a native service for configuring EC2 instances called EC2 Run Command . AWS还具有称为EC2 Run Command的用于配置EC2实例的本机服务。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM