Skip to content

Latest commit

 

History

History
78 lines (61 loc) · 1.76 KB

File metadata and controls

78 lines (61 loc) · 1.76 KB

GSP199 - Service Accounts and Roles: Fundamentals

Task 1. Creating and managing service accounts

  1. Buat service account

    gcloud iam service-accounts create my-sa-123 --display-name "my service account"
  2. Granting roles

    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. Understanding roles

Task 3. Use the client libraries to access BigQuery from a service account

  1. Buat service account Navigation menu > IAM & Admin, Service accounts, + Create Service Account

  2. Buat VM instances

  3. Connect ssh

    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
  4. Buat code python

    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='YOUR_PROJECT_ID',
        credentials=credentials)
    print(client.query(query).to_dataframe())
    " > query.py
  5. Replace project id

    sed -i -e "s/YOUR_PROJECT_ID/$(gcloud config get-value project)/g" query.py
  6. Replace service account

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

    python3 query.py