Posts

Showing posts with the label python

Google App Engine - Hello World in Python

In this post, I am going to try using Google App Engine to run the hello-world app in  https://cloud.google.com/appengine/docs/standard/python3/quickstart  . Cloud Function vs App Engine Unlike AWS equivalents ( reference ), the hello-world samples are very similar for both Cloud Function and App Engine. Here is the decision tree in case you wonder the difference: https://cloud.google.com/blog/products/compute/choosing-the-right-compute-option-in-gcp-a-decision-tree init app % gcloud app create ... Please enter your numeric choice: 2 (asia-northeast1=Tokyo) ... Success! install gcloud component % gcloud components install app-engine-python get sample app % git clone https://github.com/GoogleCloudPlatform/python-docs-samples % cd python-docs-samples/appengine/standard_python3/hello_world % cat main.py run locally % pip install  -r requirements.txt % python main.py % curl http://localhost:8080 deploy % gcloud app deploy % gcloud app browse

Google Cloud Function - Hello World (gcloud)

In this post, I am going to create a test Cloud Function on CLI, based on the tutorial:  https://cloud.google.com/functions/docs/tutorials/http  . For the browser equivalent, see Google Cloud Function - Hello World (Browser) . install gcloud The CLI tool is included in the Google Cloud SDK. % brew install google-cloud-sdk % source /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/path.zsh.inc % gcloud --version Google Cloud SDK 339.0.0 bq 2.0.67 core 2021.04.30 gsutil 4.61 init config & create project % gcloud init You must log in to continue. Would you like to log in (Y/n)?  Y Pick cloud project to use: (Create a new project) Enter a Project ID: foo20210509 Check the status: % gcloud config configurations list % gcloud config list enable billing "This command is currently in ALPHA and may change without notice" ( https://cloud.google.com/sdk/gcloud/reference/alpha/billing ) % gcloud alpha billing accounts list ACCOUNT_ID          ...

Chalice Built-in Authorizers: Try Sample

"Chalice supports multiple mechanisms for authorization ... A Built-in authorizer is used when you’d like to write your custom authorizer in Chalice" ( https://aws.github.io/chalice/topics/authorizers.html ). In this post, I am going to run the sample in the above page. create project % chalice new-project chalice_auth % cd chalice_auth app.py from chalice import Chalice, AuthResponse app = Chalice(app_name= 'chalice_auth' ) @ app.authorizer () def demo_auth (auth_request):     token = auth_request.token     if token == 'allow' :         return AuthResponse(routes=[ '/' ], principal_id= 'user' )     else :         return AuthResponse(routes=[], principal_id= 'user' ) @ app.route ( '/' , authorizer=demo_auth) def index ():     return { 'context' : app.current_request.context} @ app.route ( '/hello' ) def index ():     return { 'hello' : 'world' } The endpoint /hello is added for comparis...

REST API with Chalice + Pynamo

Image
In this post, I am going to deploy a sample REST API on AWS API Gateway, Lambda, and DynamoDB using Chalice and PynamoDB. Chalice: https://web-quickstart.blogspot.com/2021/03/aws-chalice.html Pynamo: https://web-quickstart.blogspot.com/2021/05/crud-dynamodb-from-python-pynamodb.html API spec The following methods will be served for CRUD operations: /users/  POST:   param: id name /users/{id}  GET:   response: id name    PUT:   param: name    DELETE create project % chalice new-project user_api % cd ./user_api prepare files % tree . ├── app.py ├── chalicelib │   ├── __init__.py │   └── user.py ├── docker-compose.yml └── requirements.txt # app.py from chalice import Chalice from chalice import ForbiddenError, NotFoundError from chalicelib.user import User app = Chalice(app_name= 'user_api' ) @ app.route ( '/users' , methods=[ 'POST' ]) def create_user ():     user_as_json = app.current_request.json_body ...

CRUD DynamoDB from Python (PynamoDB)

In this post, I am goint to use DynamoDB from PynamoDB. You may compare the code with  CRUD DynamoDB from Python (boto3) what is PynamoDB? PynamoDB is "a Pythonic interface for Amazon's DynamoDB" https://github.com/pynamodb/PynamoDB install % pip install pynamodb % pip list | grep pynamo pynamodb                           5.0.3 student.py You can define the entity like: from pynamodb.attributes import UnicodeAttribute from pynamodb.models import Model class Student (Model):   class Meta :     table_name = 'student'     host = 'http://localhost:8000' # region = 'us-east-1'     write_capacity_units = 5       read_capacity_units = 5     id = UnicodeAttribute(hash_key= True )   grade = UnicodeAttribute(null= True ) You may remove the host = localhost (and optionally specify the region) to run it on the cloud. test.py from student import Studen...