Sunday, May 17, 2020

GCP Study Notes 11 : Example of deploying APP engine step by step

Lab: GCP Fundamentals: Getting Started with App Engine Toggle Lab Panel

Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Google Cloud Shell provides command-line access to your GCP resources.

#Updates are available for some Cloud SDK components
gcloud components update

#You can list the active account name with this command:
gcloud auth list

Output:
Credentialed accounts:
- google1623327_student@qwiklabs.net

#You can list the project ID with this command:
gcloud config list project
Example output:

[core]
project = qwiklabs-gcp-44776a13dea667a6

#list all service-accounts available:
gcloud iam service-accounts list
NAME EMAIL DISABLED
Dataflow service account dataflow-service-account@brcm-edg-dsa-poc-edc.iam.gserviceaccount.com False
Dataproc service account dataproc-service-account@brcm-edg-dsa-poc-edc.iam.gserviceaccount.com False
Service Account used for Kubeflow admin actions. kubeflow-deployment-admin@cbrcm-edg-dsa-poc-edc.iam.gserviceaccount.com False
bigquery-sa bigquery-sa@brcm-edg-dsa-poc-edc.iam.gserviceaccount.com False

#list all users/service-accounts who have been granted any IAM roles on a specified project:
gcloud projects get-iam-policy MY_PROJECT --format=json
sample output:
- members:
- serviceAccount:12345678-compute@developer.gserviceaccount.com
- user:alice@foobar.com
role: roles/editor
- members:
- user:you@yourdomain.com
- user:someoneelse@yourdomain.com
role: roles/owner
etag: ARBITRARY_ETAG_HERE

#To get the organizaton ID
gcloud organizations list

#list all non-service accounts from the entire GCP organization:
gcloud organizations get-iam-policy ORGANIZATION_ID | grep user\: | sort | uniq

Task 1: Install the Cloud SDK for App Engine

Run the following command to install the gcloud component that includes the App Engine extension for Python 3.7:
gcloud components install app-engine-python
#if got some permisssion error, use the following:
sudo apt-get install google-cloud-sdk-app-engine-python

Initialize your App Engine app with your project and choose its region:
gcloud app create --project=$DEVSHELL_PROJECT_ID
When prompted, select the region where you want your App Engine application located.

Clone the source code repository for a sample application in the hello_world directory:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples

Navigate to the source directory:
cd python-docs-samples/appengine/standard_python37/hello_world


Good video for this tutorial is from the courseRA tutorial:
Week 1: Demonstration: Getting Started with App Engine
https://www.youtube.com/watch?v=zsYsWx1higQ

Task 2: Run Hello World application locally

In this task, you run the Hello World application in a local, virtual environment in Cloud Shell. Ensure that you are at the Cloud Shell command prompt.Execute the following command to download and update the packages list.

sudo apt-get update

Set up a virtual environment in which you will run your application.
Python virtual environments are used to isolate package installations from the system.

sudo apt-get install virtualenv

virtualenv -p python3 venv

If prompted [Y/n], press Y and then Enter.
Activate the virtual environment.
source venv/bin/activate

#use "ls -l" instead of "list -a" to list all the files
#use "cat requirements.txt" to view the file first.
Navigate to your project directory and install dependencies.
pip install -r requirements.txt

Run the application:
python main.py

In Cloud Shell, click Web preview (Web Preview) > Preview on port 8080 to preview the application.

Task 3: Deploy and run Hello World on App Engine

To deploy your application to the App Engine Standard environment, navigate to the source directory:
cd ~/python-docs-samples/appengine/standard_python37/hello_world

Deploy your Hello World application.
gcloud app deploy

This app deploy command uses the app.yaml file to identify project configuration.
Launch your browser to view the app at http://YOUR_PROJECT_ID.appspot.com
gcloud app browse

GCP provides Deployment Manager to setup your environment. It's an Infrastructure Management Service that automates the creation and management of your Google Cloud Platform resources for you. To use it, you create a template file using either the YAML markup language or Python that describes what you want the components of your environment to look like. Then, you give the template to Deployment Manager, which figures out and does the actions needed to create the environment your template describes. If you need to change your environment, edit your template and then tell Deployment Manager to update the environment to match the change. Here's a tip: you can store and version control your Deployment Manager templates in Cloud Source repositories.

GCP Cloud Function "= =" AWS Lambda Function

AWS Lambda is a compute service that lets you run code without provisioning or managing servers. AWS Lambda executes your code only when needed and scales automatically, from a few requests per day to thousands per second. You pay only for the compute time you consume - there is no charge when your code is not running. You can use AWS Lambda to run your code in response to events, such as changes to data in an Amazon S3 bucket or an Amazon DynamoDB table; to run your code in response to HTTP requests using Amazon API Gateway

What is the advantage of putting event-driven components of your application into GCP Cloud Functions? Cloud Functions handles scaling these components seamlessly. Your code executes whenever an event triggers it, no matter whether it happens rarely or many times per second. That means you don't have to provision compute resources to handle these operations.



No comments:

Post a Comment

GCP Study notes 13: Architecting with Google Kubernetes Engine: Foundations (courseRA notes)

Architecting with Google Compute Engine Specialization : 4 Courses in this Specialization. 1. Google Cloud Platform Fundamentals: Core In...