Skip to content

Latest commit

 

History

History
340 lines (225 loc) · 13.1 KB

File metadata and controls

340 lines (225 loc) · 13.1 KB

LAB 01 - Take a look around

In this hands-on lab, you'll configure the AWS Cloud9 service to look around legacy application. You can run application 'CloudAlbum' in the AWS Cloud9 EC2 instance.

GoGreen company provides a hosted photo image album services (CloudAlbum) to it's end user. Customer access the web application to :

  • To watch their personal photos with internet access.
  • Upload photos to the CloudAlbum service for photo management.
  • Track geolocation of uploaded photos

CloudAlbum has a general three-tier architecture.

The application has following software stack.

Legacy application is not Restful and application backend is tightly coupled with Jinja2 Template Engine.

It has following several features.

Prerequisites

The following prerequistied are required for the workshop:

  • AWS Console Access
  • AWS CLI installed and configured on your EC2 or PC. (AdministratorAccess recommended)
  • An EC2 SSH Key-Pair (Optional)

TASK 1. Create AWS Cloud9 environment and explore the environment.

In this section, you will create an AWS Cloud9 environment and explore the environment.

  1. In the AWS Console, click Services, then click Cloud9 to open the Cloud9 dashboard.

  2. You can choose Singapore region.

  3. Click Create environment at the top-right corner.

  4. For Name, type workshop-<INITIAL> (keep in mind this, it will be reused.)

  5. Click Next step.

  6. On the Configure settings page, leave the default selection in the Environment settings section.(If you want, you can choose a high-performance instance.)

  7. Click Next step.

  8. Review the details and click Create environment. This should launch your AWS Cloud9 environment in a few minutes.

  9. Upon environment creation, notice the terminal window on the bottom pane. The terminal provides a remote login to the instance on which the AWS Cloud9 environment is hosted, just as you used SSH for remote login. A pre-authenticated AWS CLI is installed in your terminal.

  10. Explore the terminal by typing this command:

aws ec2 describe-instances
  • Is it works well? Cool. Go to next step.
sudo pip-3.6 install boto3
  1. At the terminal, type python3 and press ENTER.

  2. For the confirmination, try the Python Boto 3 APIs by executing these commands:

import boto3
client = boto3.client('ec2')
client.describe_instances()
  1. Press CTRL+D to exit the Python interpreter.

NOTE : You can also use virtualenv for your project. Please refer following links. In this LAB doesn't use virtualenv for the convinience.

TASK 2. Look around legacy application and try run it.

Check out the workshop repository from the Github.

cd ~/environment
git clone https://github.com/liks79/aws-chalice-migration-workshop.git
  1. Install the requirements for the project by executing the command below in your AWS Cloud9 terminal.
sudo pip-3.6 install -r ~/environment/aws-chalice-migration-workshop/LAB01/CloudAlbum/requirements.txt
  1. Check the config.py Open this file in Cloud9 terminal window or Cloud9 IDE editor.
vi ~/environment/aws-chalice-migration-workshop/LAB01/CloudAlbum/cloudalbum/config.py
import os

conf = {
    # Mandatory variable
    'GMAPS_KEY': os.getenv('GMAPS_KEY', None),
    'DB_URL': os.getenv('DB_URL', None),

    # Default config values
    'APP_HOST': os.getenv('APP_HOST', '0.0.0.0'),
    'APP_PORT': os.getenv('APP_PORT', 8080),
    'FLASK_SECRET': os.getenv('FLASK_SECRET', os.urandom(24)),
    'SESSION_TIMEOUT': os.getenv('SESSION_TIMEOUT', 30),
    'SQLALCHEMY_TRACK_MODIFICATIONS': os.getenv('SQLALCHEMY_TRACK_MODIFICATIONS', False),
    'DB_ECHO_FLAG': os.getenv('DB_ECHO_FLAG', True),
    'DB_POOL_SIZE': os.getenv('DB_POOL_SIZE', 10),
    'DB_MAX_OVERFLOW': os.getenv('DB_MAX_OVERFLOW', 20),
    'DB_SQLALCHEMY_POOL_TIMEOUT': os.getenv('DB_SQLALCHEMY_POOL_TIMEOUT', 15),
    'DB_SQLALCHEMY_POOL_RECYCLE': os.getenv('DB_SQLALCHEMY_POOL_RECYCLE', 7200),
    'LOG_FILE_PATH': os.getenv('LOG_FILE_PATH', os.path.join(os.getcwd(), 'logs')),
    'LOG_FILE_NAME': os.getenv('LOG_FILE_NAME', 'cloudalbum.log'),
    'ALLOWED_EXTENSIONS': ['jpg', 'jpeg'],
    'UPLOAD_FOLDER': os.getenv('UPLOAD_FOLDER', os.path.join(os.getcwd(), 'upload')),
    'LOGGING_FORMAT': os.getenv('LOGGING_FORMAT', '%(asctime)s %(levelname)s: %(message)s in [%(filename)s:%(lineno)d]'),
    'LOGGING_MAX_BYTES': os.getenv('LOGGING_MAX_BYTES', 100000),
    'LOGGING_BACKUP_COUNT': os.getenv('LOGGING_BACKUP_COUNT', 1000),
    'LOGGING_LEVEL': os.getenv('LOGGING_LEVEL', 'debug'),
    'PER_PAGE': os.getenv('PER_PAGE', 6),
    'THUMBNAIL_WIDTH': os.getenv('THUMBNAIL_WIDTH', 300),
    'THUMBNAIL_HEIGHT': os.getenv('THUMBNAIL_HEIGHT', 200),
}
  • The second parameter of os.getenv function is the default value to use when the first parameter does not exist.
  1. Check the following variables in config.py.
    • GMAPS_KEY and DB_URL variables are mandatory for this application.
    • You will set up these values in step 23
    # Mandatory variable
    'GMAPS_KEY': os.getenv('GMAPS_KEY', None),
    'DB_URL': os.getenv('DB_URL', None),
  1. Check the ALLOWED_EXTENSIONS : Only JPG files are allowed. (in config.py)

  2. Setup these variables before run this application.

  3. Select aws-chalice-migration-workshop/LAB01/CloudAlbum/run.py in the tree view.

  4. First, open the run.py(double click), and on the top menu bar, click Run → Run With → Python 3. Ensure that you are using the Python 3 runner, as shown in the screenshot below.

  1. You may fail due to following reason.
DB_URL or GMAPS_KEY are not configured!
Check your environment variables!
  1. To successfully run the code, you will need to set environment variables. To do so, follow the instructions below.

  2. On the right side of the Run Configuration pane at the bottom, click ENV, as in the screenshot below.

  1. First, In the Name column, type DB_URL. In the Value column, type the SQLITE file DB URL (for the test purpose only) like this. DB table will be created automatically via SQLAlchemy.
sqlite:////home/ec2-user/environment/aws-chalice-migration-workshop/LAB01/sqlite.db
  • ALTERNATIVE WAY: You can set the DB_URL value directly in config.py.
'DB_URL': os.getenv('DB_URL', 'sqlite:////home/ec2-user/environment/aws-chalice-migration-workshop/LAB01/sqlite.db'),
  1. Second, in the Name column, type GMAPS_KEY. In the Value column, type the Google MAP API KEY (Temporary API Key will be provided to the workshop attendees by presenter).
  • ALTERNATIVE WAY: You can set the GMAPS_KEY value directly in config.py.
'GMAPS_KEY': os.getenv('GMAPS_KEY', 'REAL_GMAPS_KEY_PROVIDED_BY_INSTRUCTOR'),

NOTE: You can issue your own Google Maps API Key, refer to following links (Credit card is required): https://console.developers.google.com/apis/dashboard

  1. Run application again. You may succeed.

TASK 3: Connect to your application (Cloud9)

To test the application, you can configure your envirionment. There are two way to configure your environment. Choose your option.

  1. For Cloud9 preview
  • Cloud9 preview feature is easy to use.
  • Click the Preview menu and choose Preview Runnig Application.
  • That's all

  • For the convenience, you can run pop-up browser window.

TASK 3 [OPTIONAL-TASK]: Connect to your application (SSH Tunneling)

Alternative way, you can use to establish an SSH tunnel to your AWS Cloud9 instance.

  1. [OPTIONAL-TASK] : SSH tunnelFor MAC/Linux users:

  • In your MAC/Linux terminal, type the command below to get the public portion from your existing any key pair .pem file. Make sure to replace YOUR_KEY with the name of the key pair .pem file
ssh-keygen -f <YOUR_KEY.pem> -y
  • The output looks like the example below. Copy the output of your command.
ssh-rsa
ASDASDASDASDAyc2EAAAADAQABAAABAQDWGRZsPraV6v4UqfZTFKAXK9bhjWVkONEKyAA1CeOkxSN+9WdY7gKgjbPOeUx3LFqRudBvSrP+eKTtthPrl Nx9UBvXniVK252i4h0xnIcrRO1PUpq0EzyqX+n3u2YwytT+on6x98PRjtD4oCKyfFviWBqnRHtWvRre8CWhULuJrmUeo2aPrVTPXo/TwJpZupXv63YyUMPC 2wyDMDsKNZhsqUedkJ8575PGXCg9gEkPg2ulR8NUrzDSfbXIrZLgCcIziwDQ0dA9B28OAQ9saPyXYzrZF1ZmCxKgzSHHiKdBAJ0E/X/s53N5Hg04SIWy4D4lMT 9g+AZG38YPNq68mo4b
  • In your AWS Cloud9 instance, on the left pane, click the Settings icon. Click Show Home in Favorites and Show Hidden Files as shown in the screenshot below.

  • This should display the .ssh folder in the tree view.

  • Expand the .ssh folder and open the authorized_keys file.

  • Paste the public key you copied earlier in the authorized_keys file. Important: Make sure to paste the public key below the message in the authorized_keys file as shown below. Do not delete or overwrite the public key already present in the file. Deleting or overwriting the existing public key will make your Cloud9 instance unusable.

 #
 # Add any additional keys below this line
 #
  • Save the authorized_keys file. By updating this file, you are telling your AWS Cloud9 instance to allow connections that authenticate with the matching private key.

NOTE: You also can paste it using cli command like below. (Paste public key then press CTRL+D for EOF)

cat >> ~/.ssh/authorized_keys
ssh-rsa CXCAAB3Nzaxxyc2EAAAADAQABAAABAQDThHERqJJMcZqitA5DZ35j41UFE0zIO5XxVqElCHNHUXYnmffqFNyTFkfpkHAWsR5zGMnR5I46eZazu4sWNcg3LZx937/STOfN4TCzps/uuooHx/p3whGXIFqsz25Xq1RzI/LsFiSRm3+/I1E482pss3OgCXALR/rF9g7Mud1frt9POq82Zg0R1YHB5hCK6Ldx3U3AnFxdViKHVnDgVijAYO+ua1MFtaSn+FqYoXbMniFiQpOJz2ZTvM/ZhwvfAYJkJPYwQ+7T99pIEb0L/pLecaFkxUcbAiwzW6L79bKAQYwA7vEzI4ndqhyLKwIzadVJnog1hRs0ItiUqDOSLYLN [email protected]
  • Check public-ip of your Cloud9 EC2 instance.
curl http://169.254.169.254/latest/meta-data/public-ipv4 
  • Try to SSH tunneling like this

  • NOTE: Make sure your Cloud9 instance Security group port 22 is opened for SSH tunneling.

ssh -i <YOUR_KEY.pem> -L 8080:localhost:8080 ec2-user@<PUBLIC-IP OF YOUR CLOUD9 EC2 INSTANCE>
  1. Connect your legacy application

TASK 4. Take a look around

Check the application.

  1. Connect to your application using Cloud9 preview or to http://localhost:8080 in your browser.

  1. Take a look around and perform test.

TASK 5. Stop your application

  • Click the stop icon to stop your application.
  • Close your terminal after application stop.
  • Close all your opened file tab.

Congratulation! You completed LAB01.

Optional Challenges

  • Deploy legacy application:

    • Configure EC2, RDS, and ELB.
      • DB_URL of MySQL for SQLAlchemy : 'mysql+pymysql://:@:/cloudalbum?charset=utf8'
  • Deploy legacy application:

    • Configure ElasticBeanstalk
  • Find problems:

    • Perform the same functional tests that you performed in TASK 4.
      • How about user login session?
      • How about photo file storage?
      • How about scalability?

LAB GUIDE LINKS