coding-to-music / coding-to-music.github.io

https://pandemic-overview.readthedocs.io/en/latest/index.html
MIT License
2 stars 8 forks source link

AWS Summary - Chalice #167

Open coding-to-music opened 3 years ago

coding-to-music commented 3 years ago

AWS Summary - Chalice

Create a virtualenv and install Chalice

To start using Chalice, you will need a new virtualenv with Chalice installed.

Instructions

Make sure you have Python 3 installed. See the env-setup page for instructions on how to install Python.

Create a new virtualenv called chalice-env by running the following command:

python3.7 -m venv /tmp/chalice-env

Activate your newly created virtualenv:

source /tmp/chalice-env/bin/activate

Install chalice using pip:

pip install chalice

Verification

To check that chalice was installed, run:

chalice --version

chalice 1.6.0, python 3.7.3, darwin 15.6.0

mine:
chalice 1.15.1, python 3.8.10, linux 5.4.0-52-generic

The version of chalice must be version 1.6.0 or higher and the version of Python should be 3.7.

using environment variable set in .bash_aliases

export MEDIA_BUCKET_NAME='media-query-mediabucket-9rbalpyrst24'

(chalice-env) root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/workshop-intro# history | grep detect
 1289  aws rekognition detect-labels     --image-bytes fileb://chalice-workshop/code/media-query/final/assets/sample.jpg
 1291  aws rekognition detect-labels     --image-bytes fileb://code/media-query/final/assets/sample.jpg
 1294  aws rekognition detect-labels     --image-bytes fileb://code/media-query/final/assets/sample.jpg
 1296  aws rekognition detect-labels     --image-bytes fileb://code/media-query/final/assets/sample.jpg
 1306  JOB_ID=$(aws rekognition start-label-detection --video S3Object="{Bucket=$MEDIA_BUCKET_NAME,Name=sample.mp4}" --query JobId --output text)
 1308  JOB_ID=$(aws rekognition start-label-detection --video S3Object="{Bucket=$MEDIA_BUCKET_NAME,Name=sample.mp4}" --query JobId --output text)
 1310  JOB_ID=$(aws rekognition start-label-detection --video S3Object="{Bucket=$MEDIA_BUCKET_NAME,Name=sample.mp4}" --query JobId --output text)
 1316  JOB_ID=$(aws rekognition start-label-detection --video S3Object="{Bucket=$MEDIA_BUCKET_NAME,Name=sample.mp4}" --query JobId --output text)
 1325  JOB_ID=$(aws rekognition start-label-detection --video S3Object="{Bucket=$MEDIA_BUCKET_NAME,Name=sample.mp4}" --query JobId --output text)
 1326  aws rekognition get-label-detection --job-id $JOB_ID
 1483  history | grep detect

set MEDIA_BUCKET_NAME=

 1307  MEDIA_BUCKET_NAME=media-query-mediabucket-9rbalpyrst24
printenv | grep MEDIA_BUCKET_NAME
(chalice-env) root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/workshop-intro# printenv | grep MEDIA_BUCKET_NAME
MEDIA_BUCKET_NAME=media-query-mediabucket-9rbalpyrst24

aws s3 ls $MEDIA_BUCKET_NAME

(chalice-env) root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/workshop-intro# aws s3 ls $MEDIA_BUCKET_NAME
2021-07-12 23:03:39     111370 architecture.jpg
2021-07-12 23:03:49     111347 othersample.jpg
2021-07-12 23:03:18     111232 sample.jpg
2021-07-13 06:03:46    5178235 sample.mp4

Create a S3 bucket

Create a S3 bucket for uploading images and use with the Chalice application.

Instructions

1. Use the AWS CLI and the resources.json CloudFormation template to deploy a CloudFormation stack media-query that contains a S3 bucket

aws cloudformation deploy --template-file resources.json --stack-name media-query

Verification

1. Retrieve and store the name of the S3 bucket using the AWS CLI

MEDIA_BUCKET_NAME=$(aws cloudformation describe-stacks --stack-name media-query --query "Stacks[0].Outputs[?OutputKey=='MediaBucketName'].OutputValue" --output text)

2. Ensure you can access the S3 bucket by listing its contents

aws s3 ls $MEDIA_BUCKET_NAME

Note that the bucket should be empty.

coding-to-music commented 3 years ago

Media Query Application - combine multiple event handlers in Chalice to create an image processing pipeline. It takes as input any image or video and it will identify objects, people, text, scenes, and activities. This results of this analysis can then be queried with a REST API

https://aws.github.io/chalice/samples/media-query/index.html

https://github.com/aws/chalice/tree/master/docs/source/samples/media-query/code/

Once the application is deployed, use the AWS CLI to

fetch the name of the bucket that is storing the media files:

aws cloudformation describe-stacks --stack-name media-query \
    --query "Stacks[0].Outputs[?OutputKey=='MediaBucketName'].OutputValue" \
    --output text

media-query-mediabucket-9rbalpyrst24

find the URL

chalice url

https://n4dkhj8yqg.execute-api.us-east-1.amazonaws.com/api/

List out all if the media files using the application’s API with HTTPie:

http https://n4dkhj8yqg.execute-api.us-east-1.amazonaws.com/api/

commands for the aws cli

usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]

To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help

aws: error: argument subcommand: Invalid choice, valid choices are:

ls                                       | website                                 
cp                                       | mv                                      
rm                                       | sync                                    
mb                                       | rb                                      
presign                                 

aws s3 ls

aws s3 ls media-query-mediabucket-9rbalpyrst24

2021-07-12 23:03:39     111370 architecture.jpg
2021-07-12 23:03:49     111347 othersample.jpg
2021-07-12 23:03:18     111232 sample.jpg
2021-07-12 23:04:05     111288 sample.mp4
coding-to-music commented 3 years ago

https://github.com/coding-to-music/chalice-workshop-intro

https://chalice-workshop.readthedocs.io/en/latest/media-query/00-intro-chalice.html

Part 0: Introduction to AWS Lambda and Chalice¶

This section will provide an introduction on how to use AWS Chalice and provide instructions on how to go about building your very first Chalice application running on AWS Lambda. Steps include:

Create a virtualenv and install Chalice¶

To start using Chalice, you will need a new virtualenv with Chalice installed.

Instructions

Make sure you have Python 3 installed. See the env-setup page for instructions on how to install Python.

Create a new virtualenv called chalice-env by running the following command:

python3 -m venv /tmp/chalice-env

Activate your newly created virtualenv:

source /tmp/chalice-env/bin/activate

Install chalice using pip:

pip install chalice

Verification

To check that chalice was installed, run:

chalice --version

chalice 1.6.0, python 3.7.3, darwin 15.6.0

mine:
chalice 1.15.1, python 3.8.10, linux 5.4.0-52-generic

The version of chalice must be version 1.6.0 or higher and the version of Python should be 3.7.

Create a new Chalice application

With chalice now installed, it is time to create your first Chalice application.

Instructions

Run the chalice new-project command to create a project called workshop-intro:

chalice new-project workshop-intro

Verification

A new workshop-intro directory should have been created on your behalf. Inside of the workshop-intro directory, you should have two files: an app.py file and a requirements.txt file:

ls workshop-intro

app.py           requirements.txt

mine
drwxr-xr-x  3 root root 4096 Jul 13 20:36 ./
drwxr-xr-x 22 root root 4096 Jul 13 20:36 ../
drwxr-xr-x  2 root root 4096 Jul 13 20:36 .chalice/
-rw-r--r--  1 root root   37 Jul 13 20:36 .gitignore
-rw-r--r--  1 root root  739 Jul 13 20:36 app.py
-rw-r--r--  1 root root    0 Jul 13 20:36 requirements.txt

Hello world Lambda function

Let’s create our first Lambda function and deploy it using Chalice.

Instructions

  1. Change directories to your newly created workshop-intro directory:
cd workshop-intro
  1. Open the app.py file and delete all lines of code underneath the line: app = Chalice(app_name='workshop-intro'). Your app.py file should only consist of the following lines:
from chalice import Chalice

app = Chalice(app_name='workshop-intro')
  1. Add a new function hello_world decorated by app.lambda_function() that returns {"hello": "world"}. Your app.py file should now consist of the following lines:
from chalice import Chalice

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

@app.lambda_function()
def hello_world(event, context):
    return {'hello': 'world'}
  1. Run chalice deploy to deploy your Chalice application to AWS Lambda:
chalice deploy

Creating deployment package.
Creating IAM role: workshop-intro-dev
Creating lambda function: workshop-intro-dev-hello_world
Resources deployed:
  - Lambda ARN: arn:aws:lambda:us-east-1:708090526287:function:workshop-intro-dev-hello_world

Verification

Run the chalice invoke command to invoke your newly deployed hello_world Lambda function:

$ chalice invoke -n hello_world
{"hello": "world"}

Lambda function using event parameter

Lambda functions accept two parameters: an event and a context parameter. The event parameter is used to provide data to the Lambda function. It is typically a dictionary, but may be a list, string, integer, float, or None. The context parameter provides information about the runtime to the Lambda function. This step will create a Lambda function that will use data from event passed to it to affect its return value.

Instructions

  1. Create an additional Lambda function hello_name using the app.lambda_function() decorator. The function should retrieve the value of the name key in the event parameter and return {'hello': name}:
@app.lambda_function()
def hello_name(event, context):
    name = event['name']
    return {'hello': name}

Your app.py file should now consist of the following lines:

from chalice import Chalice

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

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

@app.lambda_function()
def hello_name(event, context):
    name = event['name']
    return {'hello': name}
  1. Run chalice deploy to deploy your Chalice application with the new Lambda function:
chalice deploy

Creating deployment package.
Updating policy for IAM role: workshop-intro-dev
Updating lambda function: workshop-intro-dev-hello_world
Creating lambda function: workshop-intro-dev-hello_name
Resources deployed:
  - Lambda ARN: arn:aws:lambda:us-east-1:708090526287:function:workshop-intro-dev-hello_world
  - Lambda ARN: arn:aws:lambda:us-east-1:708090526287:function:workshop-intro-dev-hello_name

Verification

  1. Run chalice invoke to invoke the hello_name Lambda function with {"name": "Kyle"} as the event payload:
$ echo '{"name": "Kyle"}' | chalice invoke -n hello_name
{"hello": "Kyle"}
  1. It is also possible for your Lambda function to encounter runtime errors. Passing in an empty event payload when invoking the hello_name will result in the Lambda Function returning a Traceback:
chalice invoke -n hello_name
Traceback (most recent call last):
  File "/var/task/chalice/app.py", line 901, in __call__
    return self.func(event, context)
  File "/var/task/app.py", line 12, in hello_name
    name = event['name']
KeyError: 'name'
Error: Unhandled exception in Lambda function, details above.

Delete the Chalice application

Now with an understanding of the basics of AWS Lambda and Chalice, let’s clean up this introduction application by deleting it remotely.

Instructions

  1. Run chalice delete to delete the deployed Lambda functions running this application:
$ chalice delete
Deleting function: arn:aws:lambda:us-east-1:708090526287:function:workshop-intro-dev-hello_name
Deleting function: arn:aws:lambda:us-east-1:708090526287:function:workshop-intro-dev-hello_world
Deleting IAM role: workshop-intro-dev```

### Validation
Try running chalice invoke on the previously deployed Lambda functions:

```python
$ chalice invoke -n hello_world
Could not find invokable resource with name: hello_world

$ chalice invoke -n hello_name
Could not find invokable resource with name: hello_name

You should no longer be able to invoke both Lambda functions as they have been deleted.