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:

cd ~/env/xxx/bin/
source activate
python3.6 ~/env/xxx/source/

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,280 points

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. You can learn more about AWS EC2 from the AWS Solutions Architect Certification.

answered Apr 17, 2018 by Cloud gunner
• 4,670 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
• 58,090 points

Related Questions In AWS

0 votes
0 answers
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
+2 votes
1 answer
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,670 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP