Start Django development server on AWS EC2 Amazon Linux - django

This tutorial here shows how to start the development server on AWS EC2:
ALLOWED_HOSTS=['EC2_DNS_NAME']
...
python manage.py runserver 0.0.0.0:8000
In a browser, navigate to public DNS of the instance, make sure to append port “8000” at the end and if everything was properly configured, it will show the index page of the application.
For example: www.ec2dnsname.com:8000
My public IPv4 DNS is of this form:
ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com
So, in my settings.py
ALLOWED_HOSTS=['ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com']
And run this in the EC2 terminal:
python manage.py runserver 0.0.0.0:8000
However, when I try to reach this address:
www.ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com:8000
nothing is showing up (The page took too long to respond).
Is this the complete and correct way to just try run a development server on AWS EC2?
Update after #Iam_batman response: Below is the Security Group setting attached to my EC2 instance:
All TCP TCP 0 - 65535 0.0.0.0/0 –
All TCP TCP 0 - 65535 ::/0 –
The Port Range seems to cover everything. I still can't access the index page from the development server.
Also, my AWS account is a workplace account, not a personal account. If I follow #iam_batman setup below, using HTTP and SSH protocol, I get denied the access to port 80 and 22 respectively.
Update: To further test, I run a curl command as following :
curl http://ec2-xx-xxx-xx-xxx.region.compute.amazonaws.com:8000
I get the entire HTML code for the standard Django index page (the one with the spaceship). This implies that the website is already available at that address. But for some reason, it does not render on the browser?

There's two things you should check out.
1. Check if security groups are configured.
Search for "Security Groups" in aws dashboard. Find the 'Create Security Group' Button on the top right and create a security group with inbound and outbound rules that allow http traffic on the port you want to communicate with.
Here's an example where i am using the standard 80 port for http. But, you can use 8000 if you want.
Now, you just need to assign the the security group you created to your ec2 instance. Go to EC2 dashboard and select your instance. Then Actions > Security > Change Security Groups. Select the security group you just created and you are good to go.
2. Maybe you need an application server
I have never actually tried deploying with the django development server ( runserver command ) because its not meant for production. For, deploying python based frameworks like django/flask/fast-api you need to use an appserver like: uwsgi, gunicorn etc. So, do give it a look. You need an appserver atleast. For, more production use cases you might need to have an actual webserver like nginx/apache on top of the app server.
These are my two notes on your issue.

Related

Unable to open Public IPv4 DNS in AWS EC2 - Linux instance

I have a Spring boot project which I want to host on an AWS-EC2 instance. I was able to create its image using Git-hub, Jenkin and docker. I was also able to successfully pull and run this image in the Linux console of my AWS-EC2 instance.
According the tutorial I was following I should have been able to open the project now using the public IPv4 DNS but the response I got was that it refuse to connect.
I know that this usually has to do with Inbound rules so I added a rule to allow all traffic but it didn't help.
For anyone who wants to know:
Git-hub repository: https://github.com/SalahuddinShayan/telecom
Docker-Hub repository: https://hub.docker.com/repository/docker/salahuddinshayan/telecom
Command I used to run the image in AWS:
docker run -p8081:8081 --name final-app --link docker-mysql:mysql salahuddinshayan/telecom
Security Groups:
Networking Details:
Here is the Error:
I am completely stumped by it. Does anyone an idea on what to do to fix this?
Please check if your client is calling the right protocol, e.g. http vs https.
You are transmitting on port 8081. http://3.110.29.193:8081/ works fine from the EC2 side. 404 status is raised, so this is a client side error, not a server side error.
It means that no firewall is blocking traffic and a process (your app) was found that listens on IP:Port that you require. The problem is that the process it encountered (your app) is sending only a WhiteLabel Error Page, which is a generic Spring Boot error page that is displayed when no custom error page is present. So the issue is with the Spring app itself and not with EC2 or with connection. In other words: the traffic can reach your Spring app, but your Spring app has nothing to say in response.
As a side note, after deploying your app I would advise to refine the inbound traffic rules to allow only the traffic you want. There is no need of allowing all traffic on all ports.

How to access jenkins dashboard on webbrowser in AWS?

I am very new to AWS and Devops part, I read some official documentation and I installed Jenkins server in my ec2-instance it's successfully installed and it's running also ,when i try to open by using http://my-public-ip:8080 it's not opening. How to open my Jenkins dashboard in my web browser?
This is my EC2 instance inbound rules:
When I am trying to search in my browser it's showing some thing like this:
This site can't be reached13.235.67.157 took too long to respond.
Try:
Checking the connection
Checking the proxy and the firewall
Running Windows Network Diagnostics
ERR_CONNECTION_TIMED_OUT
I would suggest go to your security group of this instance which is launch-wizard-5 Edit it and You have to choose "Custom TCP rule"
Then you will be able to add the port to 8080
Wait for second
and open http://my-public-ip:8080 in your favorite browser
You'll able to access your Jenkins GUI
Jenkins uses a specific port: 8080
To verify this, inside of your jenkins shell try this:
try with netstat -plnt.
and
curl localhost:8080 -v
If you have a valid response showing you that 8080 port is online, your jenkin is ready to use. Just configure the 8080 port in your AWS inbound rules. Finally you will be able to access with:
http://my-public-ip:8080
If you achieve that, your next goal must be to hide the 8080 port. For that you could use the ELB to assign a custom domain if you have someone.
Add security groups
ALLTraffic
0.0.0.0/0
Then refresh the page

Django deployment over AWS

I am new to Amazon cloud. I have deployed a django application on AWS EC2. I started the application but unable to see it from the browser. Which IP address will have the access? I tried both private and public but both are not resolved.
Try to run you app using command python manage.py runserver 0.0.0.0:8000.
Your app will be running publicly. Try to figure out IPv4 Public IP in the Instance section of AWS.
Then move to Security Groups which is listed in left navigation under NETWORK & SECURITY.
Choose the security group which is assigned to your instance. After that click on EDIT button on Inbound rule section and allow port in which the app is running. For example: port 8000.
Type: Custom
Protocol : TCP
Port Range : 8000
Source : Anywhere or My IP
Once added. Click on Save and make sure in Django App settings,allowed host is set to *. So, that you can access your application from anywhere.
ALLOWED_HOSTS = ['*']
The above code is not recommended for production. Good to use for development/testing.

How to Configure Angular2 on AWS

I've had success building the Angular2 Quickstart app on my local machine, but I'd like to jump forward to setting it up on an AWS instance. The issue is that I ssh into the instance and so the npm start command doesn't load the app into my local browser. Even the "External" addresses provided don't work, even with an elastic IP assigned and the corresponding port opened. I've also looked through the node_modules/lite-server files to update any necessary configuration changes, but I haven't found how to alter it properly.
Can anyone point me to some resources that can help me get started wit angular2 on AWS?
So, first, there are some differences between your localhost and the aws EC2 instance.
Your computer is a graphical computer with a browser, the ec2 vm is just a command line based linux server and has no browser installed on it.
npm start, just like "grunt someTask" has extra stuff built into the steps that wouldn't make sense on a linux server... i.e. automatically opening your browser window and doing live-reload for active development, etc.
the EC2 vm is a place where you want to treat it more like beta server or production server, meaning that you dont start and stop your app with npm start, instead you want to start it with node yourAppFile.js, and you are merely running it to see if it works. You don't develop here.
Also, the ec2 vm itself is guarded / protected by a security group, and that allows very few ports to be accessed from the outside world by default, so you've got to go edit the "inbound rules" of the security group and add a rule that essentially allows: "custom" --> "the node port" --> "your IP". Doing so will make it possible to connect to it using the vm-public-ip:port-of-node-app which will look something like this in your browser: 56.128.17.42:3000. And because you put your own IP in that rule, no other IP addresses would be able to load it in their browsers... just you. If you were to say "0.0.0.0/0" in the ip field, that allows all users anywhere to connect.
I would test with curl on your local machine to make sure it's up and running. You can do that with "curl http://localhost:3000/ (change 3000 to whatever port you are running on).
If that is working okay, you may have an issue with security groups. Go to the security group topic on the EC2 page and select the security group for your server and make sure that you have the port opened from your IP address (you can google "what is my ip" if you don't know it). You can just open it the whole world if you're not worried about security.

Flask web app not responding to external requests on EC2

I've got a very simple Flask application that I'm hosting on an Amazon EC2 node and for whatever reason I can't see it externally. The flask app is here
from flask import Flask
app = Flask(__name__)
app.config['DEBUG'] = False
#app.route('/')
def hello_world():
return 'Hello World!'
#app.route('/p1')
def p1():
return "P1!!!"
if __name__ == '__main__':
app.run(host='0.0.0.0')
When I run the script it looks like the server is running fine, so in my browser (on a different computer) I put the following :5000 (the IP address I pull off of AWS). What's interesting is that it just seems to hang, and eventually produces an error. My guess is that I'm missing some configuration in AWS but I don't know what it is. Any help would be greatly appreciated
EDIT I tried deploying the app on my local machine. And when I try to access it from the browser using localhost:5000, it works. When I replace localhost with my IP address, it fails
Found this question while searching for a solution to the same issue.
edit run.py to enable flask to respond to requests from other than localhost.
this example enables responding to requests from anywhere. good security policies would use something more restrictive.
app.run(host='0.0.0.0')
in the AWS control panel go to EC2: select instance.
the browser should be pointed to the address from 'Public DNS (IPv4)'
(the ip# from IPv4 Public IP might also be useful)
look for 'Security groups': right-click to open the security group on a new page.
check inbound rules.
by default, flask binds to port 5000. add rule permitting incoming TCP traffic on port 5000.
while good security protocol should limit the number of ports left open and the range of IP's permitted to connect, it might be easier to permit 'anywhere' to connect over 'all tcp'.
NB: check if the default port has been changed in the flask config file run.py
ie: line below changes port from the default 5000 to 3000.
app.run(debug=True, port=3000)
Can also check if the flask instance is working locally by ssh'ing to the server and using a local instance of the lynx text browser to verify the port is responding. ie
lynx localhost:5000
Was able to ultimately answer my own question, both really
The problem I was having on AWS was that my inbound for that EC2 was not allowing access through the ports that I would need.
When I tried running it on my local machine at work, firewall settings change the address of localhost (and my IP) so that's why I couldn't access it outside of using localhost:5000
In your EC2 instance, the security group is what restricting your entry to the website.
Go to AWS portal, select your instance
Locate the security group and click the name
in the inbound rule window, select add rule
Not a recommended security practice but to get it running, select All TCP
add '0.0.0.0' in the source
your website will be running
I faced a similar issue in which the Flask app on EC2 instance was not responding. Turns out that I had to modify the inbound rules because:
The default security groups and newly created security groups include default rules that do not enable you to access your instance from the internet.
In order to modify the inbound rules, go to:
Instances dashboard > Security > Security Groups (go to your security group) > Edit Inbound Rules > Add Rule.
Fill the values:
Type -> Custom TCP
Protocol -> TCP
Port range -> 5000 (If your flask app is running on default port)
Source -> Your IP or 0.0.0.0/0 if you want to allow all traffic. You can also select My IP from source dropdown. This will automatically get your IP.
Save and you are good to go!
As you are already using host="0.0.0.0" in your app it should be accessible from anywhere. The only think blocking it is your aws security group inbound rules. Add a All TCP inbound rule for port 0-65535 (your app port should be in this range) with source 0.0.0.0/0 and it should work.