Flask Docker Lab – Lesson 2

Lesson 2 – Setting up the dev environment

Lesson Overview

In this lesson, we will set up a Linux machine in AWS EC2 that will work like a developer’s desktop. On that machine, we will download, modify, and test code locally using docker containers.

As a best practice, do not use the AWS account root user for any task where it’s not required. Instead, create a new IAM user for each person that requires administrator access. Then make those users administrators by placing the users into an “Administrators” group to which you attach the AdministratorAccess managed policy.

AWS

Lesson

  1. If you don’t already have a non-root, administrators AWS IAM user,
  2. Create EC2 Admin Role to be used by our EC2 developer desktop
    • IAM > Roles > Create Role
    • Click Link – Select the service that will use this role – “EC2”
    • Click Link – Select your use case – “EC2” (top choice)
    • Push Button – Next: Permissions
    • Check Box – “AdministratorAccess”
    • Push Button – Next Review
    • Set – Role name – “EC2Admin”
    • Push Button – Create Role
  3. Deploy AWS Linux AMI to EC2
    • AWS Console > EC2 > Instances
    • Select Region > **Oregon** (the lab is built for **us-west-2**)
    • Push Button – Launch Instances
    • Select Tab – Community AMIs
    • Search – **ami-50e19028**
    • Push Button – Select (next to image called: flask_docker_lab/developer_desktop-1525870042)
    • Check Box – **t2.large** (it costs about $0.09/hour to have a 8GB RAM desktop)
    • Push Button – Next: Configure Instance Details
    • IAM Role – Select – “EC2Admin” role we just created
    • Push Button – Next: Add Add storage
    • Push Button – Next: Add Tags
    • Push Button – Next: Configure Security Group
    • Update SSH rule – Add Rule – Type: “ssh”, Port “22”, Source: **”My IP”**
    • Push Button – Add Rule – Type: “Custom TCP Rule”, Port “5901”, Source: “My IP”, Description: “VNC”
    • Push Button – Review and Launch
    • Push Button – Launch
    • Keypair – create without keypair (we wont need it for this desktop)
    • Check Box – I acknowledge
    • Push Button – Launch Instance
    • Push Button – View Instance
    • Copy IPv4 Public IP address to clipboard
  4. Wait a few minutes, then connect to AWS EC2 instance with VNC
    • OPTION A – Install Chrome VNC viewer extension
      • To install: go to chrome vnc viewer > Install
      • To launch: Chrome > Apps > VNC Viewer
      • Connect to : EC2 public_ip port: 5901 (ex 54.245.171.175:5901)
      • password is `dockerlab`
    • *OPTION B* – Via VNC Client (faster)
      • Download & Install the VNC viewer
      • Connect to EC2 public_ip port: 5901 (ex 54.245.171.175:5901)
      • password will be `dockerlab`
      • To improve the display –
      • right click on title bar > Properties
      • Set – Options Tab > Picture Quality – Medium
      • Click OK
  5. Reset the linux user password and test AWS cli connectivity.
    • Open a terminal window Start > System Tools > LxTerminal
    • Make sure your aws cli is authorized to access your AWS account
    • aws s3 ls
    • This should list out any S3 buckets you have without error.
    • If your AWS IAM role is configured incorrectly, you will get an error
    • EXAMPLE OF ERROR:
      $ aws s3 ls 
      Unable to locate credentials. 
      You can configure credentials by running "aws configure".
  6. Open Firefox in the VNC window and open this blog
    • Start > Internet > Firefox Web Browser
  7. Go to Github and Fork the flask_docker_lab repository
    • Go to: https://github.com/cloudshiftstrategies/flask_docker_lab
    • Signin (or Signup to create a new user) – remember your username!
    • Click “Fork” on the top right of the page
    • This will create a copy of the flask_docker_lab code in your personal
    • github account
  8. Link our travis-ci.org account to our github account.
    • login to [travis-ci.org](https://travis-ci.org/) with your github credentials
    • Authorize Travis-ci to communicate with github
    • Click on Your Name in top right > Accounts
    • Flick the switch next to the forked flask_docker_lab to enable travis builds
  9. Clone your forked copy of the code
    • From your personal copy of the flask_docker_lab repository
    • click Clone or download
    • copy the repository url (https://github.com/${USER}/flask_docker_lab.git)
    • From a terminal in the developer desktop
      • (substitute user info below with your github username):   git clone https://github.com/YOUR_GITHUB_USERNAME/flask_docker_lab.git
        $ git clone http://github.com/peterb154/flask_docker_lab.git
        Cloning into 'flask_docker_lab'...
        remote: Counting objects: 239, done.
        remote: Compressing objects: 100% (191/191), done.
        remote: Total 239 (delta 66), reused 208 (delta 35), pack-reused 0
        Receiving objects: 100% (239/239), 409.02 KiB | 0 bytes/s, done.
        Resolving deltas: 100% (66/66), done.
        Checking connectivity... done.
  10. Launch the code with docker-compose
    cd flask_docker_lab
    docker-compose build
    docker-compose up -d
  11. Check out the app
    • From a browser inside of EC2 instance, go to
    • http://localhost:5000
  12. Tweak the code using Atom
    1. Open atom ide by clicking Start > Programing > Atom
    2. Note: Be patient, it will take 30+ seconds to open Atom for the first time
    3. Navigate to flask_docker_lab > app > config.py – edit SITE_TITLE
    4. Hit CTRL-S to save changes
    5. Validate the the changes are in effect on our local docker image

Lesson 2 Summary

In this lesson, we completed the following tasks:

  1. Created an admin user in AWS console (never work as root)
  2. Create an IAM role for an EC2 instance
  3. Provisioned a developer’s Linux desktop into EC2
  4. Connected to that desktop via VNC
  5. Validated aws CLI & API connectivity using the role
  6. Forked out project code from peterb154/flask_docker_lab
  7. Connected travis-ci to our github flask_docker_lab project
  8. Cloned our project code to the developers workstation
  9. Launched a local docker container to run the code
  10. Validated that the application runs on the local machine
  11. Made the site our own, and validated the local changes

In Lesson 3 we’ll configure AWS infrastructure to host the application and configure travis-ci to be our continuous integration server.