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.
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)
In this section, you will create an AWS Cloud9 environment and explore the environment.
-
In the AWS Console, click Services, then click Cloud9 to open the Cloud9 dashboard.
-
You can choose Singapore region.
-
Click Create environment at the top-right corner.
-
For Name, type workshop-<INITIAL> (keep in mind this, it will be reused.)
-
Click Next step.
-
On the Configure settings page, leave the default selection in the Environment settings section.(If you want, you can choose a high-performance instance.)
-
Click Next step.
-
Review the details and click Create environment. This should launch your AWS Cloud9 environment in a few minutes.
-
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.
-
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
-
At the terminal, type python3 and press ENTER.
-
For the confirmination, try the Python Boto 3 APIs by executing these commands:
import boto3
client = boto3.client('ec2')
client.describe_instances()
- 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.
-
https://docs.aws.amazon.com/ko_kr/cloud9/latest/user-guide/sample-python.html#sample-python-install
-
https://docs.aws.amazon.com/ko_kr/cloud9/latest/user-guide/sample-python.html#sample-python-run
Check out the workshop repository from the Github.
cd ~/environment
git clone https://github.com/liks79/aws-chalice-migration-workshop.git
- 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
- 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.
- 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),
-
Check the ALLOWED_EXTENSIONS : Only JPG files are allowed. (in config.py)
-
Setup these variables before run this application.
-
Select aws-chalice-migration-workshop/LAB01/CloudAlbum/run.py in the tree view.
-
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.
- You may fail due to following reason.
DB_URL or GMAPS_KEY are not configured!
Check your environment variables!
-
To successfully run the code, you will need to set environment variables. To do so, follow the instructions below.
-
On the right side of the Run Configuration pane at the bottom, click ENV, as in the screenshot below.
- 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'),
- 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
- Google Maps API changes billing rules from July 16. Users must have their own API KEY for the Google Map access.
- Run application again. You may succeed.
To test the application, you can configure your envirionment. There are two way to configure your environment. Choose your option.
- You can use Cloud9 preview feature.
- Previewing Running Applications in the AWS Cloud9 Integrated Development Environment (IDE)
- For this, we should run application with 8080, 8081, 8082 port for 127.0.0.1.
- https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html
- Previewing Running Applications in the AWS Cloud9 Integrated Development Environment (IDE)
- For Cloud9 preview
- Cloud9 preview feature is easy to use.
- Click the
Preview
menu and choosePreview Runnig Application
. - That's all
- For the convenience, you can run pop-up browser window.
Alternative way, you can use to establish an SSH tunnel to your AWS Cloud9 instance.
- [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>
- Connect your legacy application
Check the application.
- Connect to your application using Cloud9 preview or to http://localhost:8080 in your browser.
- Take a look around and perform test.
- Sign in / up
- Upload Sample Photos
- Download sample images here (EXIF data included)
- Look your Album
- Find photos with Search tool
- Check the Photo Map
- Click the
stop icon
to stop your application. - Close your terminal after application stop.
- Close all your opened file tab.
- Go to : LAB 02 - Move to serverless