Skip to content

GCP完全ガイド

GCPの実践的な実装方法を、実務で使える実装例とベストプラクティスとともに詳しく解説します。

Terminal window
# gcloud CLIでのインスタンス作成
gcloud compute instances create my-instance \
--zone=asia-northeast1-a \
--machine-type=n1-standard-1 \
--image-family=ubuntu-2004-lts \
--image-project=ubuntu-os-cloud
Terminal window
# インスタンスの起動
gcloud compute instances start my-instance --zone=asia-northeast1-a
# インスタンスの停止
gcloud compute instances stop my-instance --zone=asia-northeast1-a
# インスタンスの削除
gcloud compute instances delete my-instance --zone=asia-northeast1-a
Terminal window
# バケットの作成
gsutil mb -p my-project -c STANDARD -l asia-northeast1 gs://my-bucket
# ファイルのアップロード
gsutil cp file.txt gs://my-bucket/
# ファイルのダウンロード
gsutil cp gs://my-bucket/file.txt ./
{
"lifecycle": {
"rule": [
{
"action": {"type": "SetStorageClass", "storageClass": "NEARLINE"},
"condition": {"age": 30}
},
{
"action": {"type": "Delete"},
"condition": {"age": 365}
}
]
}
}
// Cloud Functionsの実装
import { CloudFunction } from '@google-cloud/functions-framework';
export const helloWorld: CloudFunction = (req, res) => {
res.send('Hello World!');
};
// デプロイ
// gcloud functions deploy helloWorld --runtime nodejs18 --trigger http
// Cloud Storageイベント
import { Storage } from '@google-cloud/storage';
export const processFile = async (file: Storage.File) => {
console.log(`Processing file: ${file.name}`);
// ファイル処理
};
// BigQueryクエリ
import { BigQuery } from '@google-cloud/bigquery';
const bigquery = new BigQuery();
const query = `
SELECT name, COUNT(*) as count
FROM \`my-project.my_dataset.my_table\`
GROUP BY name
ORDER BY count DESC
LIMIT 10
`;
const [rows] = await bigquery.query(query);
console.log('Results:', rows);
Terminal window
# Cloud SQLインスタンスの作成
gcloud sql instances create my-instance \
--database-version=POSTGRES_14 \
--tier=db-f1-micro \
--region=asia-northeast1

GCP完全ガイドのポイント:

  • Compute Engine: 仮想マシンの管理
  • Cloud Storage: オブジェクトストレージ
  • Cloud Functions: サーバーレス関数
  • BigQuery: データ分析
  • Cloud SQL: マネージドデータベース

適切なGCPの使用により、スケーラブルで効率的なシステムを構築できます。