Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahcoad/2cc1b251a7adf4240ae26a534f12b9b1 to your computer and use it in GitHub Desktop.
Save noahcoad/2cc1b251a7adf4240ae26a534f12b9b1 to your computer and use it in GitHub Desktop.
Event 2019-05-24: AWS w Python and AWS Chalice Intro at Dallas Makerspace

AWS Intro w Python

Taught by Noah Coad, Senior IoT Architect at AWS
at the Dallas Makerspace on 2019-05-24 and 2019-06-27

Overview

Amazon Web Services (AWS) provides essential building blocks to creating your own applications. Like code in the cloud (AWS Lambda), image recognition detection (AWS Rekognize), database (AWS DynamoDB), file storage (S3), etc. Through this course you'll get started on combining 6 of these AWS services together to build a cloud app that you can submit an image to, store it, detect elements in the image, store those in a database, and manage it all through a REST API. We'll be using a Python framework for AWS called AWS Chalice, which makes coding, connecting, testing, and deploying these components real easy.

Class Resources

  1. Intro Video that walks through these steps
  2. Prezi Presentation
  3. Class info and register at: Dallas Makerspace Event
  4. Walking through this workshop:
    AWS Chalice Media Query Application
  5. AWS Free Tier account: https://aws.amazon.com/free
  6. Use an AWS Cloud9 Development Environment at https://aws.amazon.com/cloud9/
    Note: Cloud9 uses temporary credentials by default, which AWS Chalice can not deploy with. So an IAM Role with an AdministativeAccess policy must be attached to the Cloud9 EC2 instance. See the "Getting Started Walkthrough" section below for details on setting this up.
  7. Please provide feedback on the course at: Event Feedback
  8. If you'd like to be notified of future classes, please add your info here: Signup for Event Notices
  9. Help me get to 1000 Twitter followers =) https://twitter.com/noahcoad
  10. Realted Local Meetup Groups: AWS DFW Official, DFW Pythoneers, AWS User Group, AWS On Tap, AWS Cloud, AWS Deep Dives, Python for Everyone, DFW Pythoneers, PyMavs, dot_py, Coding Temple Dallas, Learn to Code, Coding Dojo
  11. Dallas Makerspace Info: Homepage, Other Events, and Open House Tours are Thursdays at 7:00pm


Getting Started Walkthrough

This will walk you through each step to get started from never having used AWS before to your first AWS Lambda code running in the cloud. Watch intro video walks through these steps.

Setup a new free AWS account

Create a new AWS account if you don't have one already.

  1. Create a free AWS account at https://aws.amazon.com/free
    A credit card is required. But all the services used here remain under the free tier level.
  2. Log into your new free account here: AWS Console Login
  3. Switch region to "Oregon" if it's not already ...
    Top right of console click "N. Virginia" if you see it, and select "US West (Oregon)"

Create a Cloud9 development environment

Use a free Cloud9 development environment in the cloud on any computer (Windows, Mac, Linux, Chromebook, etc) for a consistent and easy way to start coding.

  1. Create a Cloud9 environment by clicking the orange 'Create environment' button at https://us-west-2.console.aws.amazon.com/cloud9/home/product?region=us-west-2#
    1. Name it "Chalice Walkthrough"
    2. Accept all other defaults on the following pages
  2. Open a new tab and create a new IAM Role at https://console.aws.amazon.com/iam/home#/roles
    1. Click blue "Create Role"
    2. Choose "EC2" from list, then click blue "Next: Permissions"
    3. Check the "AdministativeAccess" policy checkbox, then click the blue "Next: Tags" button, then "Next: Review"
    4. Give it the name "cloud9_admin" and click "Create role"
  3. Attach new role to the Cloud9 EC2 virtual machine at https://us-west-2.console.aws.amazon.com/ec2/v2/home?region=us-west-2#Instances:search=cloud9;sort=tag:Name
    1. Right-click the item in the list with "cloud9" in it's name
    2. Select 'Instance Settings' > 'Attach/Replace IAM Role'
    3. From the drop-down list, select the "cloud9_admin" role from earlier, then the blue "Apply" button
  4. Go into your new "Chalice Walkthrough" Cloud9 environment at https://us-west-2.console.aws.amazon.com/cloud9/home?region=us-west-2 ... and click the "Open IDE" button
  5. Turn off temporary credentials by selecting the "AWS Cloud9" bold menu item at the top > "Preferences" > "AWS Settings" in the list > "Credentials" > and turn off the "AWS managed temporary credentials" switch

Start using AWS, Python, and AWS Chalice

This replaces the Part 0: Introduction to AWS Lambda and Chalice walkthrough using the Cloud9 development environment for consistency.

  1. Make a large terminal window by clicking the "Window" menu > "New Terminal" and enter these commands
# creates a new ~/.aws/config file needed by AWS Chalice
echo [default] > ~/.aws/config
echo region=us-west-2 >> ~/.aws/config

# create a new python app environment
virtualenv chalice-env

# intializes the new python app environment
source chalice-env/bin/activate

# installs the AWS Chalice python framework
pip install chalice

# creates a new chalice project called 'workshop-intro'
chalice new-project workshop-intro

# change into the folder and list files
cd workshop-intro
ls

Click app.py to open it in the editor and replace ALL the contents with...

from chalice import Chalice

app = Chalice(app_name='workshop-intro')

@app.lambda_function()
def hello_world(event, context):
    return {'hello': 'world'}

Save the file ("File" menu > "Save"), then back in the terminal...

# deploys the new code to the AWS Lambda cloud
chalice deploy

# runs the AWS Lambda in the cloud
chalice invoke -n hello_world

View your new lambda at https://us-west-2.console.aws.amazon.com/lambda/home?region=us-west-2#/functions?f0=a3c%3D%3Ad29ya3Nob3AtaW50cm8%3D

Now add another method that uses a parameter by adding these lines to the end of app.py...

@app.lambda_function()
def hello_name(event, context):
    name = event['name']
    return {'hello': name}

Save file, then redeploy and run with...

# redeploy the latest updates
chalice deploy

# run the new lambda, and pass in a simple JSON object as input
echo '{"name": "Kyle"}' | chalice invoke -n hello_name

Notice you've now created two lambda at https://us-west-2.console.aws.amazon.com/lambda/home?region=us-west-2#/functions?f0=a3c%3D%3Ad29ya3Nob3AtaW50cm8%3D

You can also see these lambdas by running...

# use AWS command line tool to list functions
aws lambda list-functions

# a little fancier, use the jq tool to filter to just the function names
sudo yum install -y jq
aws lambda list-functions | jq '.Functions[].FunctionName'

Finally delete the lambdas so far with...

# deletes the deployed AWS resources
chalice delete

Now continue the walkthrough here: https://chalice-workshop.readthedocs.io/en/latest/media-query/01-intro-rekognition.html

As a bonus, check out the REST APIs w Chalice video.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment