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!).
Related
Hello I coded this website that generates math problems (Here is the code: Here)
It is coded on flask and it is locally being hosted on this link that is not accessible to other people http://127.0.0.1:5000/ .I have a google domain and I want to have a website. What things / services do I need to use. I have been wait to see if I need to use AWS but I think I might need to. I have tried things like transferring it off of flask but I can't. If this is a repost sorry please post there answer thanks -Ben
I am assuming what you're asking is to host your flask web site so others can view it. The address you mention in your post is the local host address for your computer and is only accessible from your own computer. If you only want someone on your same network (WiFi) to access it, you would need to replace "127.0.0.1" with the IP address of your computer. You would also likely have to open up a firewall on your computer to allow the port 5000.
However, if you want anyone on the internet to access your site, there are a ton of ways to do this but since you mentioned AWS, you can do this easily by running a small EC2 instance (virtual server). If you have a new AWS account and have not already run any EC2 in that account, you can actually run a small EC2 instance for free for a whole year. Great for small projects. If you're just getting started with EC2, you may want to go here https://aws.amazon.com/ec2/getting-started/
Basic steps:
Spin up an EC2 instance. Choose the default Amazon Linxu 2 OS type, make sure to create/assign a key pair so you can later ssh into it, make sure the Allow SSH from anywhere setting is checked/selected and the Allow HTTP checkbox is checked (not HTTPS).
Wait for the instance to launch.
Log into your instance by clicking on your ec2 instance in the list of ec2 instnaces and click the Connect button, click the Connect button again (Instance connect tab). If that doesn't work, follow the steps on the SSH client tab.
Install flask
pip3 install flask
Clone your git repo
git clone https://github.com/some0ne14/Math-Ibex.git
Change to your repos' folder
cd Math-Ibex/Math-Practice-Website-master
Edit your main.py so that the app.run line looks like the following (you can do this on GitHub before you run git clone actually or use the nano command to edit the file easily). This allows the system to run on the standard web port 80.
app.run(host='0.0.0.0', port=80, debug=True)
Run the following to start the application. If you want to run it as a service so you can walk away or close the terminal and it will still stay running, just search on here how to run flask as a service.
python3 main.py
You can now connect to your server with any web browser using your EC2 instance's public IP address or generated AWS DNS name (available on the EC2 instnace property page).
Make sure to stop your instance when not using it to save those free runtime minutes.
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.
I'm creating a python flask api on remote desktop and running it on localhost of remote desktop.
Is there anyway I can access this api from my local machine?
We are working in a team and I want to share this with my team members, but this is confidential and not to be deployed on open server.
We want to post and get the result with every member's local machine from api runnnig on remote desktop.
Both of our local machines and remote desktop are windows10.
Sorry for being abstract but I'm searching for any way out. Thanks.
Well, you should open your way to this API. You'll have to set up a VPN or IP address filter in the server so you can access the server from your network while still have it secured on the Internet. You can also setup a simpler proxy if you prefer it. I'll not cover the details on how to setup a VPN or proxy since it can get pretty extensive, but a Google search will help you out find the best alternative for you.
AFAIK, the Remote Desktop Protocol does not allow for any kind of VPN. However, if you can switch to TeamViewer, it does have an easy to setup VPN system that will allow you to get into the network with few configuration. Once a VPN is configured, it will work like if you were in the same network as the server, so from there you can access your API from your host machine by just going to the IP address of the server.
Do notice the security policies of whoever owns the server, since you can get into trouble if you don't have permission to enable some access from the outside. Security goes always in front of comfort.
Short term solution:
Firstly download ngrok for your operating system.
For debugging and testing purposes you can expose a secure tunnel connection to your API by running this command in your command prompt / terminal.
ngrok http <PORT_NUMBER>-host-header="localhost:<PORT_NUMBER>"
Where PORT_NUMBER is the port number in which your flask application is running.
Example if your flask application is running at port 5000 then simply execute this command:
ngrok http 5000 -host-header="localhost:5000"
Running this will give you two hostnames one with HTTP and other a secure HTTPS connected by a tunnel like this for a duration of 8 hours after which the command needs to again re-run.
Which you can call remotely
Long term solution:
Deploy flask application using FastCGI
or
To a cloud infrastructure provider like Microsoft Azure which gives readymade templates for flask applications.
I am trying to run flower on a remote ubuntu server. However, I am unsure on what address/port to run it on so that other people can login (I have the basic auth set up) and check their celery workers. The ubuntu server is actually an EC2 instance, so am I supposed to use its private or public ip address? Do I just open any standard port? In their docs, they use their example setup with http://localhost/5555 but I do not think that will work if flower will be running on a remote server. Any advice?
Flower runs on 5555 by default- which port are you running it on? The private IP is only available if the requests are coming from INSIDE your amazon network, so probably public.
So, if my guesses are right, you want to create an AWS security rule allowing traffic from "anywhere" to port 5555 and apply that to your instance, and then access that instance using its public ip like
http://50.31.10.99:5555
I have recently started using pyrax, the python binding for the Rackspace API. I have a test account on Rackspace and a server running there. Using my username and api_key I can authenticate and list all the servers found in that region.
How do I connect to a particular server given its name, server id, IPv4 address, IPv6 address, flavor etc.?
Pyrax is specifically used to interact with the Rackspace Cloud API in provisioning the resources.
A different library would be required if you wanted to connect to the server via SSH. Check out something like http://www.lag.net/paramiko/