0

I am running a basic hello world application on my ec2 instance. The infrastructure is all setup and I am able to ssh into it.

The code for creating my instance looks like below. I have a simple user_data variable for now, but I know I need to utilize this. I also have a simple html file that says "hello world" as well as a helloworld.py file which just serves the html file.

How would I utilize the user_data (I am having trouble with the syntax) to push the html and helloworld.py file to my ec2 instance then run the script so that the file is served? I am a noobie to nginx and stuff so looking for some direction here...

user_data = '''#!/bin/bash
echo 'test' > /tmp/hello'''

create_instance = resourceEC2.create_instances(
        ImageId = data['ImageId_value'],
        MinCount = 1,
        MaxCount = 1,
        InstanceType = data['InstanceType_value'],
        SubnetId=data['SubnetId-1a'],
        UserData=user_data,
        SecurityGroupIds=[
            security_group_TG['GroupId'],
        ],
      )
shan
  • 125
  • 3
  • 16
  • Example that should get you started: https://stackoverflow.com/questions/57784287/how-to-install-nginx-on-aws-ec2-linux-2 – jarmod Jul 22 '20 at 15:40
  • @jarmod saw this part, but I need one more step. If I run 'python helloworld.py' my static html gets served on localhost:8080. So how do I go about doing that after installing nginx and serving it? – shan Jul 22 '20 at 15:44
  • You don't run web servers using `python xyz.py`. You use NGINX (or equivalent) as a reverse proxy via WSGI. Look at [gunicorn](https://www.digitalocean.com/community/tutorials/how-to-deploy-python-wsgi-apps-using-gunicorn-http-server-behind-nginx), for example. – jarmod Jul 22 '20 at 15:51
  • @jarmod is there any way we can get on a chat? I am having trouble with this still. I found tutorial to set up wsgi. But I can't view anything in my browser https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04 – shan Jul 22 '20 at 16:16
  • Sorry, Stack Overflow doesn't work that way. I'd encourage you to follow a tutorial with their sample app (not yours) initially to get it working. That will teach you a lot and probably help explain the problem you're currently facing. Note that apps listening on localhost or 127.0.0.1 are not going to be accessible outside of that host. Your web server needs to listen on 0.0.0.0, which means all network interfaces. – jarmod Jul 22 '20 at 16:32
  • am I doing the user_data part correctly at least? When I have something simple like below, after instance creates I ssh into it and do nginx --version, nothing comes up user_data = '''#!/bin/bash sudo apt-get update sudo apt-get install nginx -y mkdir ~/myapp/''' – shan Jul 22 '20 at 16:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218370/discussion-between-jarmod-and-shan). – jarmod Jul 22 '20 at 16:37

0 Answers0