{Pulumi} EC2/GCP

 


https://www.pulumi.com/registry/packages/gcp/api-docs/compute/instance/
https://github.com/pulumi/pulumi-gcp/issues/580


Python: 3.9.13

-- 1. macにPulumiインストール


brew install pulumi/tap/pulumi

 


-- 2. GCPプロジェクト作成

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

 

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

gcloud auth application-default login


-- 3. Create new project


mkdir test
cd test
pulumi new gcp-python


pulumi config set gcp:project project01-9999999
pulumi config set gcp:region asia-northeast1
pulumi config set gcp:zone asia-northeast1-a

pulumi config get gcp:project
pulumi config get gcp:region
pulumi config get gcp:zone


vim __main__.py


import pulumi
import pulumi_gcp as gcp

sa123 = gcp.serviceaccount.Account("sa123",
    account_id="sa123",
    display_name="sa123")


vm01 = gcp.compute.Instance("vm01",
    machine_type="e2-micro",
    name="vm01",

    tags=[
        "tag01",
        "tag02",
    ],
    
    boot_disk=gcp.compute.InstanceBootDiskArgs(
        initialize_params=gcp.compute.InstanceBootDiskInitializeParamsArgs(
            image="debian-cloud/debian-11",
        ),
    ),
    
    network_interfaces=[gcp.compute.InstanceNetworkInterfaceArgs(
        network="default",
        access_configs=[gcp.compute.InstanceNetworkInterfaceAccessConfigArgs()],
    )],


    service_account=gcp.compute.InstanceServiceAccountArgs(
        email=sa123.email,
        scopes=["cloud-platform"],
    ),
    
    scheduling=gcp.compute.InstanceSchedulingArgs(
        preemptible =True,
        automatic_restart=False,
    ) )

 

-- 4. pulumi 実行

 

pulumi up

 


gcloud compute instances list

gcloud compute ssh vm01

 

-- 5. GCPクリーンアップ


pulumi destroy


pulumi stack rm dev --force

 


gcloud projects list

gcloud projects delete project01-9999999 \
--quiet