Codementor Events

PyCharm setup for AWS automatic deployment

Published Aug 08, 2018
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 then EC2 under Compute
    services_Compute_ec2_.png

  • Then click on Launch Instance
    launch_instance.png

  • select the Ubuntu Image for the Instance.
    select_ubuntu_image.png

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 the pycharm_aws_tut.pem file won't be available later to download.
    create_new_key_pair.png

  • Click Launch Instances and then view instances.
    Soon the instance would be launch and update from pending status to running status and you may provide a name (anything under 255 characters) like pycharm_aws

pending_to_running.png

running.png

Assign security group policies for accessing instance via command line or PyCharm.

  • Click on Security Groups under Network & Security then Create Security Group
    create_security group.png
  • Fill in the form for security group and from Actions click on Edit inbound rules to give SSH and HTTP.
    N.B: Port is the value on which you wish to access your web app.
    Edit_inbound_rules.png

SSH to the EC2 instance using below command.

  • Make note of the Public DNS of that instance and the user_name, by default the username is ubuntu for ubuntu instances.

public_dns.png

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.
connect_aws_instance_via_command_line.png
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)
    basic_flask_app.png

  • 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.
    Add_remote_interpreter.png

  • Click on + to add a new python interpreter and then click on SSH interpreter
    ssh_interpreter.png

  • Provide Public DNS in HOST and ubuntu as username
    provide_dns.png

  • Click Next and add the pycharm_aws_tut.pem file.
    provide_pem.png

  • 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
    which_python.png

  • Sync Folders
    Sync_folders.png

  • Click Ok and again Ok.

Automatically deploy flask app on remote.

no_upload_yet.png

  • Now click on Tools -> Deployment -> Configuration
    fill in the form as per the screenshot and then click Test SFTP Connection
    fill_sftp_form.png

Mapping between local directory and remote directory.

  • check Mapping tab and make local path and remote path are the ones you wish to Sync & 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.
    background.png

files_uploaded.png

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 executing pip3 freeze if flask is installed.
    Then run the flask application.
  • sudo python3 my_aws_script.py
    flaskrunninh.png

Access the app via browser.

  • Copy and paste the Public DNS of the instance in the browser.
    hello.png

  • Access /index
    index.png

References:

  1. Dev Endpoint Tutorial Pycharm
  2. Connect AWS EC2 instance with Pycharm
Discover and read more posts from Abhishake Gupta
get started
post commentsBe the first to share your opinion
Siddhesh Chavan
6 years ago

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!

Abhishake Gupta
5 years ago

Thank you Siddhesh.

For windows the username is Administrator
and Redhat the username is ec2-user.

Show more replies