Terriermon - Digimon

08. Service Accounts and Roles: Fundamentals

2024. 11. 16. 17:34클라우드/GCP

Activate Cloud Shell

 

gcloud auth list

gcloud config list project

gcloud config set compute/region us-west2

 

 

Task 1. Create and manage service accounts

gcloud iam service-accounts create my-sa-123 --display-name "my service account"

 

 

gcloud projects add-iam-policy-binding $DEVSHELL_PROJECT_ID \

    --member serviceAccount:my-sa-123@$DEVSHELL_PROJECT_ID.iam.gserviceaccount.com --role roles/editor

 

 

 

Task 2. Use the client libraries to access BigQuery using a service account

 

 

Put the example code on a Compute Engine instance

sudo apt-get update

 

 

sudo apt-get install -y git python3-pip

 

pip3 install --upgrade pip

 

pip3 install google-cloud-bigquery

 

 

 pip3 install pyarrow

 

pip3 install pandas

 

 

pip3 install db-dtypes

 

echo "

from google.auth import compute_engine

from google.cloud import bigquery

credentials = compute_engine.Credentials(

    service_account_email='YOUR_SERVICE_ACCOUNT')

query = '''

SELECT

  year,

  COUNT(1) as num_babies

FROM

  publicdata.samples.natality

WHERE

  year > 2000

GROUP BY

  year

'''

client = bigquery.Client(

    project='qwiklabs-gcp-00-adc5d8a27bd5',

    credentials=credentials)

print(client.query(query).to_dataframe())

" > query.py

 

 

 

 

sed -i -e "s/qwiklabs-gcp-00-adc5d8a27bd5/$(gcloud config get-value project)/g" query.py

 

cat query.py

 

sed -i -e "s/YOUR_SERVICE_ACCOUNT/bigquery-qwiklab@$(gcloud config get-value project).iam.gserviceaccount.com/g" query.py

cat query.py

 

 

python3 query.py

 

 

참고 : https://www.cloudskillsboost.google/focuses/1038?locale=en&parent=catalog

반응형

'클라우드 > GCP' 카테고리의 다른 글

10. Analyzing Billing Data with BigQuery  (1) 2024.11.18
09. Service Directory  (0) 2024.11.17
07. Cloud Shell and gcloud  (0) 2024.11.15
06. Cloud SQL for MySQL  (1) 2024.11.14
05. Cloud IAM  (0) 2024.11.13