I have an API (in python) which has to alter files inside an EC2 instance that is already running. I'm searching on boto3 documentation, but could only find functions to start new EC2 instances, not to connect to an already existing one.
I am currently thinking of replicating the APIs functions to alter the files in a script inside the EC2 instance, and having the API simply start that script on the EC2 instance by accessing it using some sort of SSH library.
Would that be the correct approach, or is there some boto3 function (or in some of the other Amazon/AWS libraries) that allows me to start a script inside existing instances?
An Amazon EC2 instance is just like any computer on the Internet. It is running an operating system (eg Linux or Windows), and it has standard security in-built. The fact that it is an Amazon EC2 instance has no impact.
So, the question really becomes: How do I run a command on a remote computer?
Typical ways of doing this include:
Connecting to the computer (eg via SSH) and running a command
Running a service on the computer that listens on a particular port (eg responding to an API request)
Using remote shell commands to run an operation on another computer
Fortunately, AWS offers an additional option: Use the AWS Systems Manager Run Command:
AWS Systems Manager Run Command lets you remotely and securely manage the configuration of your managed instances. A managed instance is any Amazon EC2 instance or on-premises machine in your hybrid environment that has been configured for Systems Manager. Run Command enables you to automate common administrative tasks and perform ad hoc configuration changes at scale. You can use Run Command from the AWS console, the AWS Command Line Interface, AWS Tools for Windows PowerShell, or the AWS SDKs. Run Command is offered at no additional cost.
Administrators use Run Command to perform the following types of tasks on their managed instances: install or bootstrap applications, build a deployment pipeline, capture log files when an instance is terminated from an Auto Scaling group, and join instances to a Windows domain, to name a few.
Basically, it is an agent installed on the instance (or, for that matter, on any computer on the Internet) and commands can be sent to the computer that are executed by the agent. In fact, the same command can be sent to hundreds of computers if desired.
The AWS Systems Manager Run Command can be triggered by an API call, such as a program using boto3.
Unless you have a specific service running on that machine which allows you to modify mentioned files. I would make an attempt to log onto EC2 instance as to any other machine via network.
You can access EC2 machine via ssh with use of paramiko or pexpect libraries.
If you want to use the execute a script inside of an existing EC2 instance - you could use the reference from the existing answer here : Boto Execute shell command on ec2 instance
IMO, to be able to start a script inside the EC2, the script should be present on the EC2.
Related
I want to run commands remotely from a GCP instance to another one, using a Python script on a Debian machine.
I know that the gcloud ssh command can do that with the subprocess module, but I don't know how to proceed faster as it creates a new key each time I run the command. Is there a way to operate with a service account for example, on which I could setup permissions and keys for each machine on my GCP project?
Create a SSH Key in your "source" machine
ssh-keygen
And then add your public key into the ssh keys tab on the metadata page on Google Cloud Console, under the settings category, from the Compute Engine section.
Then, you should be able to log in to the other instance doing
ssh [user#]other_instance_ip [optional command to execute]
I would like to RDP into an Windows EC2 instance, and run some powershell commands on it. How do I do it in boto3? For example: I need to create Windows user and Password in Windows EC2 Instance using Boto3 or Python Script. Is this possible?
Boto3 SDK is only to manage AWS services, if you are looking for managing windows remotely from python try pywinrm library. WinRM is a windows remote management tool to invoke windows commands remotely. Documentation for pywinrm can be found in the following link.https://pypi.org/project/pywinrm/0.2.2/
You can also consider AWS systems manager:
https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html
I have a python scripts that send mail to users, I want to run this scripts by all instances which is in running state at a particular interval of time(decided by task scheduler of instances).
I have not to use AWS Lambda.
Is there any way by which i can do it? Can i use AWS image?
It appears that you wish to run a script on multiple Amazon EC2 instances at a particular time.
There are basically two ways to do this:
Using cron on each instance
Each Amazon EC2 instance can trigger its own script. On Linux, you would use cron. On Windows you would define a Schedule Task.
Running commands using AWS Systems Manager Run Command
If you wish to externally trigger a command on multiple Amazon EC2 instances, you can use the AWS Systems Manager Run Command. You will first define the commands to be run, and then nominate the instances on which to run the command. The Run Command will manage the process of running the script, gather the results, retry failures and report the results.
The benefit of using the Run Command is that you can centrally manage the process. It is very easy to edit the script and run it when desired. In contrast, if using cron you would need to update the script on every instance.
I have a working RESTful API written in python which works well in my local machine. Now I am having some serious trouble getting started with Amazon EC2.
I have managed to create an account and managed to create an instance , and lauch the instance as well. I have connected to the instance via ssh and passed by credentials.
I have the required file (app.py) on EC2. But I have no idea how to run it. Obviously if I run it from my ssh terminal, it still is a local service.
How to make it a public RESTful API?
Like a firewall, you have to open up the server's ports. You do this via adding rules to the security group while you're configuring the EC2 instance. Add the HTTP rule, and allow all IP addresses (0.0.0.0/0) to access that. See here: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-network-security.html#adding-security-group-rule
You can also set SSH, HTTPS, and other secure ports here (but you probably don't want everyone accessing SSH!).
I understand nearly nothing to the functioning of EC2. I created an Amazon Web Service (AWS) account. Then I launched an EC2 instance.
And now I would like to execute a Python code in this instance, and I don't know how to proceed. Is it necessary to load the code somewhere in the instance? Or in Amazon's S3 and to link it to the instance?
Where is there a guide that explain the usages of instance that are possible? I feel like a man before a flying saucer's dashboard without user's guide.
Here's a very simple procedure to move your Python script from local to EC2 Instance and run it.
> 1. scp -i <filepath to Pem> <filepath to Py File> ec2-user#<Public DNS>.compute-1.amazonaws.com:<filepath in EC2 instance where you want
> your file to be>
> 2. Cd to to the directory in EC2 containing the file. Type Python <Filename.py> There it executed.
Here's a concrete examples for those who likes things shown step-by-step:
In your local directory, create a python script with the following code: print("Hello AWS")
Assuming you already have AWS already set up and you want to run this script in EC2, you need to SCP (Secure Copy Protocol) your file to a directory in EC2. So here's an example:
- My filepath to pem is ~/Desktop/random.pem.
- My filepath to py file is ~/Desktop/hello_aws.py
- My public DNS is ec22-34-12-888
- The ec2 directory where I want my script to be is in /home/ec2-user
- So the full command I run in my local terminal is:
scp -i ~/Desktop/random.pem ~/Desktop/hello_aws.py ec2-user#ec2-34-201-49-170.compute-1.amazonaws.com:/home/ec2-user
Now ssh to your ec2 instance, cd to /home/ec2-user (Or wherever you put your file) and Python hello_aws.py
You have a variety of options. You can browse through a large library of AMIs here.
You can import a vm, instructions are here.
This is a general article about AWS and python.
And in this article, the author takes you through a more advanced system with a combination of datastores in python using the highly recommend django framework.
Launch your instance through Amazon's Management Console -> Instance Actions -> Connect
(More details in the getting started guide)
Launch the Java based SSH CLient
Plugins-> SCFTP File Transfer
Upload your files
run your files in the background (with '&' at the end or use nohup)
Be sure to select an AMI with python included, you can check by typing 'python' in the shell.
If your app require any unorthodox packages you'll have to install them.
Running scripts on Linux ec2 instances
I had to run a script on Amazon ec2 and learned how to do it. Even though the question was asked years back, I thought I would share how easy it is today.
Setting up EC2 and ssh-ing to ec2 host
Sign up and launch an ec2 instance(Do not forget to save the certificate file that will be generated while launching ec2) with default settings.
Once the ec2 is up and running, provide required permissions to the certificate file chmod 400 /path/my-key-pair.pem (or .cer file)
Run the command: ssh -i /path/my-key-pair.pem(.cer) USER#Public DNS(USER data changes based on the operating system you have launched, refer to the below paragraph for more details && Public DNS can be obtained on ec2 instance page)
Use the ssh command to connect to the instance. You specify the private key (.pem) file and user_name#public_dns_name. For Amazon Linux, the user name is ec2-user. For RHEL, the user name is ec2-user or root. For Ubuntu, the user name is ubuntu or root. For Centos, the user name is centos. For Fedora, the user name is ec2-user. For SUSE, the user name is ec2-user or root. Otherwise, if ec2-user and root don't work, check with your AMI provider.
Clone the script to EC2
In order to run the scripts on ec2, I would prefer storing the code on Github as a repo or as a gist(if you need to keep code private) and clone into ec2.
Above mention is very easy and is not error-prone.
Running the python script
I have worked with RHEL Linux instance and python was already installed. So, I could run python script after ssh-ing to host directly. It depends on your operating system you choose. Refer to aws manuals if it's not installed already.
Reference: AWS Doc