How do I run python script on aws EC2 instance(ubuntu)?

+1 vote

We have an ec2 instance"abc1"(Ubuntu) and I created an image from abc1. Then, we launch a new instance "abc2" from abc1 image. We want abc2 to run the script when it will be launched.
 So,we write the text in userdata:

#!/bin/bash
cd ~/env/xxx/bin/
source activate
python3.6 ~/env/xxx/source/test.py


We manually use these commands, it's valid.
However, it's not working when using user data automatically.
Does anyone know how to get it fixed, I know it's very minor but please help out, Thanks in Advance :)

Apr 17, 2018 in AWS by Flying geek
• 3,150 points
1,640 views

2 answers to this question.

+1 vote
I believe that you are using the SSH into the ec2 instance as a different user than the instance which is actually running it, so the HOME directory (~) is not the same when you run manually vs run with cloudinit.

Try writing the script using the absolute paths, not the relative paths then you'll be able to integrate with it and then would be able to run the python precisely.
answered Apr 17, 2018 by Cloud gunner
• 4,260 points
0 votes

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.

answered Aug 16, 2018 by Priyaj
• 56,160 points

Related Questions In AWS

0 votes
1 answer

AWS EC2 Free Tier- how many instances can i run?

Amazon has a handy page that details ...READ MORE

answered Jul 6, 2018 in AWS by Priyaj
• 56,160 points
240 views
+1 vote
11 answers

Python AWS Boto3: How do i read files from S3 Bucket?

All of the answers are kind of ...READ MORE

answered Mar 30 in AWS by awsdbaexpert
• 200 points
22,343 views
0 votes
1 answer

How do I enable SSH for my AWS-EB instance?

Using the EB CLI to do eb ...READ MORE

answered Oct 9, 2018 in AWS by Archana
• 4,090 points
35 views
+13 votes
2 answers
0 votes
1 answer
+1 vote
2 answers

How do I get my AWS Glue client in JAVA?

Hey, you've been using a correct code ...READ MORE

answered Apr 17, 2018 in AWS by Cloud gunner
• 4,260 points
774 views