PyCharm setup for AWS automatic deployment
In this post we will learn steps to perform PyCharm setup for AWS automatic deployment.
N.B: This is only possible with PyCharm Professional Edition
Download link
Launch an AWS (ubuntu) instance
-
Go to
services
thenEC2
underCompute
-
Then click on
Launch Instance
-
select the
Ubuntu Image
for the Instance.
Generate key pairs for authentication (instance)
-
Create a new key pair and provide a name for it.
Should click on download and save it to a accessible location else thepycharm_aws_tut.pem
file won't be available later to download.
-
Click
Launch Instances
and thenview instances
.
Soon the instance would be launch and update frompending
status torunning
status and you may provide aname
(anything under 255 characters) likepycharm_aws
Assign security group policies for accessing instance via command line or PyCharm.
- Click on
Security Groups
underNetwork & Security
thenCreate Security Group
- Fill in the form for security group and from
Actions
click onEdit inbound rules
to giveSSH
andHTTP
.
N.B: Port is the value on which you wish to access your web app.
SSH to the EC2 instance using below command.
- Make note of the
Public DNS
of that instance and theuser_name
, by default the username isubuntu
forubuntu instances
.
sample: ssh -i /path_to_key/my_key.pem user_name@public_dns_name
Update file permissions: chmod 400 pycharm_aws_tut.pem
.
Then execute: ssh -i pycharm_aws_tut.pem ubuntu@ec2-18-218-197-98.us-east-2.compute.amazonaws.com
N.B: Make sure your current directory has pycharm_aws_tut.pem
file.
Congratulations you are connected your AWS EC2 Ubuntu Instance
via Command line
.
Create Flask app on my local system.
-
Create a file named as
my_aws_script.py
(this is your flask script)
-
Add below lines of code.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello World 1"
@app.route('/index')
def index():
return "Hey This is an index page"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=80)
PyCharm settings to use remote python interpreter.
-
Go to
File
->Settings
->Project Interpreter
and add a new interpreter.
-
Click on
+
to add a new python interpreter and then click onSSH interpreter
-
Provide
Public DNS
in HOST andubuntu
asusername
-
Click
Next
and add thepycharm_aws_tut.pem
file.
-
Update python interpreter by passing the path of python on your ubuntu instance machine.
-
You may get this by running
which python3
on command line of instance
-
Sync Folders
-
Click
Ok
and againOk
.
Automatically deploy flask app on remote.
- Now click on
Tools
->Deployment
->Configuration
fill in the form as per the screenshot and then clickTest SFTP Connection
Mapping between local directory and remote directory.
- check
Mapping
tab and makelocal path
andremote path
are the ones you wish toSync
& click ok. - Now, go to
Tools
->Deployment
->Automatic
and click to enable Automatic Deployment. - Soon background task would start running and sync the two locations.
Run the app on remote.
Execute below commands on the remote machine.
sudo apt-get udpate
on remote machine.sudo apt-get install python3-pip
pip3 install flask
Check by executingpip3 freeze
if flask is installed.
Then run the flask application.sudo python3 my_aws_script.py
Access the app via browser.
-
Copy and paste the Public DNS of the instance in the browser.
-
Access
/index
Hey AB Abhi,
Thank you very much for the post. This post is really helpful to deploy AWS in pyCharm on Ubuntu.
However, in my organization we’re prompted to work on Windows OS. I see, username is Ubuntu for new server configuration in SSH interpreter.But, in our case what would be the username for same thing, since we work on Windows OS.
The other thing is, in my EC2, Public DNS(IPV4) field is blank wheres Private DNS has some value. How can go ahead in such situation to deploy AWS in pyCharm.
Thanks!
Thank you Siddhesh.
For windows the username is Administrator
and Redhat the username is ec2-user.