PulumiのGet Startedをマルチクラウドで試してみた

2022.04.28

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは!DA(データアナリティクス)事業本部 サービスソリューション部の大高です。

先日、マルチクラウドに対応している Infrastructure as Code のツール「Pulumi」のGet StartedをAWSとGoogle Cloudの両方で試してみました。

今回は、この2つのエントリで実施したデプロイを同一プロジェクトで試してみたいと思います。つまり、マルチクラウドへのデプロイを試してみます。

やりたいこと

以下の公式ドキュメントに記載されている「Get Started with Pulumi」の「Get Started with AWS」と「Get Started with Google Cloud」を1つのプロジェクトで実施します。

前提条件

実施する環境として、OSはMacOSを利用し、Nodeも事前に導入済みです。

% node -v
v16.14.2

AWS関連の設定

AWSアカウントで s3:CreateBucket ポリシーが付与されている「Pulumi用のIAMユーザ」を作成しており、アクセスキーを発行済みの状態です。

AWS CLIも導入しており、上記のIAMユーザのアクセスキー、シークレットキーを設定したprofileとしてpulumiを作成済みです。

Google Cloud関連の設定

Google Cloudのアカウントは作成済みで、以下に従って設定済みです。

Get Started with Pulumi

では、実際に前述のドキュメントを参考に進めていきます。

Before You Begin

このあたりは以前に試した、以下のそれぞれのエントリと同様ですので詳細はスキップします。

Create a New Project

プロジェクトは、AWSをベースに作成してみました。

% mkdir pulumi-get-started-with-multi-cloud && cd pulumi-get-started-with-multi-cloud
% pulumi new aws-typescript --force
This command will walk you through creating a new Pulumi project.

Enter a value or leave blank to accept the (default), and press <ENTER>.
Press ^C at any time to quit.

project name: (pulumi-get-started-with-multi-cloud) 
project description: (A minimal AWS TypeScript Pulumi program) 
Created project 'pulumi-get-started-with-multi-cloud'

Please enter your desired stack name.
To create a stack in an organization, use the format <org-name>/<stack-name> (e.g. `acmecorp/dev`).
stack name: (dev) 
Created stack 'dev'

aws:region: The AWS region to deploy into: (us-east-1) ap-northeast-1
Saved config

Installing dependencies...

(...snip...)

Finished installing dependencies

Your new project is ready to go! ✨

To perform an initial deployment, run 'pulumi up'

完了しました。

一方で、Google Cloud用のパッケージが入っていないので、追加でインストールします。

% yarn add @pulumi/gcp

これで一旦インストールは完了です。

Review the New Project

以下の3つのファイルが作成され、yamlファイルは想定どおりの設定になっていることを確認します。

  • Pulumi.yaml
  • Pulumi.dev.yaml
  • index.ts

設定ファイルは以下のようになっていました。

Pulumi.yaml

name: pulumi-get-started-with-multi-cloud
runtime: nodejs
description: A minimal AWS TypeScript Pulumi program

Pulumi.dev.yaml

config:
  aws:region: ap-northeast-1

また、リソース構築用のスクリプトは以下のようになっています。

index.ts

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

// Create an AWS resource (S3 Bucket)
const bucket = new aws.s3.Bucket("my-bucket");

// Export the name of the bucket
export const bucketName = bucket.id;

設定ファイルの修正

まずは設定ファイルを以下のように修正します。

Pulumi.yaml

name: pulumi-get-started-with-multi-cloud
runtime: nodejs
description: A minimal AWS and Google Cloud TypeScript Pulumi program

description を少し修正しました。

Pulumi.dev.yaml

config:
  aws:profile: pulumi
  aws:region: ap-northeast-1
  gcp:project: "foo-bar"

こちらはaws:profileの設定と、gcp:projectの設定を追加しました。

index.tsの修正

リソース構築用のスクリプトを修正します。今回はAWSとGoogle Cloudの両方にバケットを作成するようにします。

index.ts

import * as aws from "@pulumi/aws";
import * as gcp from "@pulumi/gcp";

// Create an AWS resource (S3 Bucket)
const awsBucket = new aws.s3.Bucket("cm-ootaka-get-started-with-pulumi");

// Create a GCP resource (Storage Bucket)
const gcpBucket = new gcp.storage.Bucket("cm-ootaka-get-started-with-pulumi", {
  location: "ASIA",
});

// Export the name of the bucket
export const awsBucketName = awsBucket.id;

// Export the DNS name of the bucket
export const gcpBucketName = gcpBucket.url;

こちらのコードはAWS用とGoogle Cloud用のGet Startedのコードを混ぜた感じですね。

Deploy the Stack

準備ができたのでデプロイしてみましょう。

以下のpulumi upコマンドを実行して、何が作成されるかを確認します。

% pulumi up
Previewing update (dev)

View Live: https://app.pulumi.com/ootaka-daisuke/pulumi-get-started-with-multi-cloud/dev/previews/5483bdb1-97fd-49d0-87e8-ac7605860d9a

     Type                   Name                                     Plan       
 +   pulumi:pulumi:Stack    pulumi-get-started-with-multi-cloud-dev  create     
 +   ├─ aws:s3:Bucket       cm-ootaka-get-started-with-pulumi        create     
 +   └─ gcp:storage:Bucket  cm-ootaka-get-started-with-pulumi        create     
 
Resources:
    + 3 to create

Do you want to perform this update?  [Use arrows to move, enter to select, type to filter]
  yes
> no
  details

問題がなさそうなので、カーソルをyesに合わせて続けます。

Do you want to perform this update? yes
Updating (dev)

View Live: https://app.pulumi.com/ootaka-daisuke/pulumi-get-started-with-multi-cloud/dev/updates/1

     Type                   Name                                     Status      
 +   pulumi:pulumi:Stack    pulumi-get-started-with-multi-cloud-dev  created     
 +   ├─ gcp:storage:Bucket  cm-ootaka-get-started-with-pulumi        created     
 +   └─ aws:s3:Bucket       cm-ootaka-get-started-with-pulumi        created     
 
Outputs:
    awsBucketName: "cm-ootaka-get-started-with-pulumi-2b988e4"
    gcpBucketName: "gs://cm-ootaka-get-started-with-pulumi-e2b1a65"

Resources:
    + 3 created

Duration: 7s

AWSのS3バケットと、GCSバケットが作成されました!

それぞれ以下のコマンドで確認してみましょう。

% aws s3 ls $(pulumi stack output awsBucketName) --profile pulumi

% gsutil ls $(pulumi stack output gcpBucketName)

オブジェクトを配置していないので何も表示されませんが、エラーが発生していないことから各クラウド上にバケットが作成されていることがわかりますね。

後片付け

以下のとおり、destroyコマンドでリソースを削除しておきます。

% pulumi destroy
Previewing destroy (dev)

View Live: https://app.pulumi.com/ootaka-daisuke/pulumi-get-started-with-multi-cloud/dev/previews/5a337cff-5944-4323-9d54-3e69a8d60490

     Type                   Name                                     Plan       
 -   pulumi:pulumi:Stack    pulumi-get-started-with-multi-cloud-dev  delete     
 -   ├─ aws:s3:Bucket       cm-ootaka-get-started-with-pulumi        delete     
 -   └─ gcp:storage:Bucket  cm-ootaka-get-started-with-pulumi        delete     
 
Outputs:
  - awsBucketName: "cm-ootaka-get-started-with-pulumi-2b988e4"
  - gcpBucketName: "gs://cm-ootaka-get-started-with-pulumi-e2b1a65"

Resources:
    - 3 to delete

Do you want to perform this destroy?  [Use arrows to move, enter to select, type to filter]
  yes
> no
  details
Do you want to perform this destroy? yes
Destroying (dev)

View Live: https://app.pulumi.com/ootaka-daisuke/pulumi-get-started-with-multi-cloud/dev/updates/2

     Type                   Name                                     Status      
 -   pulumi:pulumi:Stack    pulumi-get-started-with-multi-cloud-dev  deleted     
 -   ├─ aws:s3:Bucket       cm-ootaka-get-started-with-pulumi        deleted     
 -   └─ gcp:storage:Bucket  cm-ootaka-get-started-with-pulumi        deleted     
 
Outputs:
  - awsBucketName: "cm-ootaka-get-started-with-pulumi-2b988e4"
  - gcpBucketName: "gs://cm-ootaka-get-started-with-pulumi-e2b1a65"

Resources:
    - 3 deleted

Duration: 4s

The resources in the stack have been deleted, but the history and configuration associated with the stack are still maintained. 
If you want to remove the stack completely, run 'pulumi stack rm dev'.

念の為、以下のコマンドで削除されているか確かめておきます。

% aws s3 ls cm-ootaka-get-started-with-pulumi-2b988e4 --profile pulumi

An error occurred (NoSuchBucket) when calling the ListObjectsV2 operation: The specified bucket does not exist

% gsutil ls gs://cm-ootaka-get-started-with-pulumi-e2b1a65
BucketNotFoundException: 404 gs://cm-ootaka-get-started-with-pulumi-e2b1a65 bucket does not exist.

エラーが出ていることから、ちゃんと削除されていることが分かりますね。

まとめ

以上、PulumiのGet Startedをマルチクラウドで試してみました。

今回はAWSとGoogle Cloudを同時にデプロイしてみましたが、同様に他のクラウドやサービスも「パッケージの追加」、「設定の追加」、「リソース作成コードの追加」をしてあげるだけで問題なさそうです。

今回試したところが「バケット作成だけ」というのもありますが、マルチクラウドでも特に難しいポイントはなく、サクッと対応できるのが素敵だと思いました!

どなたかのお役に立てば幸いです。それでは!