19
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pulumiのチュートリアルをやってみた

Posted at

pulumiのチュートリアルをやってみました

どんなもの?

aws-cdkのマルチクラウド版といった感じです。
TerraformやCloudformationのようにクラウドのインフラをyamlやjsonで定義するのではなく、プログラムで定義するツールです。

https://pulumi.io/reference/vs/index.html#pulumi-vs-others
Pulumi is a cloud native infrastructure as code project. It lets you provision and manage resources, across many clouds (AWS, Azure, Google Cloud, Kubernetes, OpenStack), using your favorite language

クラウド対応状況

Alibaba Cloudとか無いですが、大体カバーできているようです。

  • Amazon Web Services (AWS)
  • Microsoft Azure
  • Google Cloud Platform
  • Kubernetes for any cloud vendor
  • OpenStack

必要なもの

  • Githubのアカウント

1. インストール

MACの場合です。

$ curl -fsSL https://get.pulumi.com/ | sh
$ pulumi version
v0.16.1

2. プロジェクトの作成

pythonの場合です。
2018-10-29現在python3は未だちゃんと対応できていないようなので、python2の環境で実行します。

$ pulumi new aws-python --dir ahoy-pulumi
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: (ahoy-pulumi)
project description: (A minimal AWS Python Pulumi program)
Created project 'ahoy-pulumi'.
stack name: (ahoy-pulumi-dev)
Created stack 'ahoy-pulumi-dev'
aws:region: The AWS region to deploy into: (us-east-1)
Installing dependencies...
Your new project is configured and ready to go! ✨
Previewing update (ahoy-pulumi-dev):

     Type                 Name                         Plan
 +   pulumi:pulumi:Stack  ahoy-pulumi-ahoy-pulumi-dev  create
 +   └─ aws:s3:Bucket     my-bucket                    create

Resources:
    2 changes
    + 2 to create

Do you want to perform this update? no
error: confirmation declined, not proceeding with the update
$ tree ahoy-pulumi/
ahoy-pulumi/
├── Pulumi.ahoy-pulumi-dev.yaml
├── Pulumi.yaml
├── __main__.py
└── requirements.txt

0 directories, 4 files

AWSにS3バケットを作成するサンプルが出来上がります。

3. デプロイ

__main__.py
bucket = s3.Bucket('pulumi-sample-bucket')
$ pulumi preview
Previewing update (ahoy-pulumi-dev):

     Type                 Name                         Plan
 +   pulumi:pulumi:Stack  ahoy-pulumi-ahoy-pulumi-dev  create
 +   └─ aws:s3:Bucket     pulumi-sample-bucket         create

Resources:
    2 changes
    + 2 to create
$ pulumi update
Previewing update (ahoy-pulumi-dev):

     Type                 Name                         Plan
 +   pulumi:pulumi:Stack  ahoy-pulumi-ahoy-pulumi-dev  create
 +   └─ aws:s3:Bucket     pulumi-sample-bucket         create

Resources:
    2 changes
    + 2 to create

Do you want to perform this update? yes
Updating (ahoy-pulumi-dev):

     Type                 Name                         Status
 +   pulumi:pulumi:Stack  ahoy-pulumi-ahoy-pulumi-dev  created
 +   └─ aws:s3:Bucket     pulumi-sample-bucket         created

Outputs:
    bucket_name: "pulumi-sample-bucket-*******.s3.amazonaws.com"

Resources:
    2 changes
    + 2 created

Duration: 18.103041264s

4. 確認する

pulumiのコンソールからS3バケットができたことを確認します。
Cloudformationのスタック的なものが、pulumi上で確認できるような感じです。

pulumi_1.png pulumi_2.png

5. S3のACLを変更する

__main__.py
bucket = s3.Bucket('pulumi-sample-bucket', acl='public-read')
$ pulumi update
Previewing update (ahoy-pulumi-dev):

     Type                 Name                         Plan       Info
     pulumi:pulumi:Stack  ahoy-pulumi-ahoy-pulumi-dev
 ~   └─ aws:s3:Bucket     pulumi-sample-bucket         update     [diff: ~acl]

Resources:
    1 change
    ~ 1 to update
    1 unchanged

Do you want to perform this update? yes
Updating (ahoy-pulumi-dev):

     Type                 Name                         Status      Info
     pulumi:pulumi:Stack  ahoy-pulumi-ahoy-pulumi-dev
 ~   └─ aws:s3:Bucket     pulumi-sample-bucket         updated     [diff: ~acl]

Outputs:
    bucket_name: "pulumi-sample-bucket-*******.s3.amazonaws.com"

Resources:
    1 change
    ~ 1 updated
    1 unchanged

Duration: 14.821880767s
pulumi3.png

確かにパブリックアクセスが許可になっています。

6. S3のACLを元に戻す

__main__.py
bucket = s3.Bucket('pulumi-sample-bucket', acl='private')
$ pulumi update

7. スタックを削除する

$ pulumi stack rm sirotosiko/ahoy-pulumi-dev
This will permanently remove the 'ahoy-pulumi-dev' stack!
Please confirm that this is what you'd like to do by typing ("ahoy-pulumi-dev"): ahoy-pulumi-dev
Stack 'ahoy-pulumi-dev' has been removed!

これだとpulumi上のスタックは削除されましたが、S3バケットは残ったままになりました。
ちょっと説明と違う気がします。

Delete this Stack
Deleting this stack will remove it from the Pulumi console, along with all of its update history.

If you wish to delete a stack but not the cloud resources associated with it, you may pass --force to the command-line.

リソースだけ消すコマンドがあるので、こちらを先に実行する必要がありそうです。

$ pulumi destroy -s sirotosiko/ahoy-pulumi-dev

雑感

これが安定して動作すれば、クラウドのinfrastructure as codeの決定版になるかな〜。

19
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?