「Pulumi Automation API」でPulumi CLIの機能をコード化しよう

・コンテンツ一覧取得
6. HTTPリクエストを投げるターミナル(②)で以下のコマンドを実行すると、コンテンツの一覧を取得できます。現時点では「hello」と「bye」コンテンツが存在することを確認できます。
②
$ curl --header "Content-Type: application/json" --request GET http://localhost:1337/sites
—
{
"ids": [
"kenneth_prv_org/bye",
"kenneth_prv_org/hello"
]
}
・コンテンツURL取得
7. HTTPリクエストを投げるターミナル(②)でcurl先のURLにid(hello)を指定すると、そのidのコンテンツURLを取得できます。
②
$ curl --header "Content-Type: application/json" --request GET http://localhost:1337/sites/hello
—
{
"id": "hello",
"url": "s3-website-bucket-*******.s3-website-ap-northeast-1.amazonaws.com"
}
・コンテンツ内容更新
8. コンテンツの中身を更新します。HTTPリクエストを投げるターミナル(②)にて、「--data」に続くjsonで、更新するコンテンツの「id」と、更新したいコンテンツの内容「content」をそれぞれ指定します。また、curl先のURLにも更新対象のid(hello)を指定します。
②
$ curl --header "Content-Type: application/json" --request PUT --data '{"id":"hello","content":"hello TOKYO¥sn"}' http://localhost:1337/sites/hello
コンテンツ新規作成時と同様に、HTTPサーバー側のターミナルでpulumi upが実行されていることがわかります。ただし、実行内容に「update」が含まれている点はコンテンツ新規作成時と異なります。
①
Updating (*/hello)
View Live: https://app.pulumi.com/*/pulumi_over_http/hello/updates/2
pulumi:pulumi:Stack pulumi_over_http-hello running
@ Updating.....
~ aws:s3:Bucket s3-website-bucket updating (0s) [diff: -serverSideEncryptionConfiguration]
@ Updating......
~ aws:s3:Bucket s3-website-bucket updated (2s) [diff: -serverSideEncryptionConfiguration]
aws:s3:BucketPublicAccessBlock bucket-public-access-block
~ aws:s3:BucketObject index updating (0s) [diff: ~content]
aws:s3:BucketPolicy bucket-policy
@ Updating....
~ aws:s3:BucketObject index updated (0.56s) [diff: ~content]
@ Updating.......
pulumi:pulumi:Stack pulumi_over_http-hello
Outputs:
website_url: "s3-website-bucket-*.s3-website-ap-northeast-1.amazonaws.com"
Resources:
~ 2 updated
3 unchanged
Duration: 7s
127.0.0.1 - - [27/Jul/2023 14:08:28] "PUT /sites/hello HTTP/1.1" 200 -
9. HTTPリクエストを投げるターミナル(②)に戻り、更新したコンテンツのURLにcurlアクセスすると、コンテンツ内容が更新されていることを確認できます。
② $ curl s3-website-bucket-*******.s3-website-ap-northeast-1.amazonaws.com — hello TOKYO
・コンテンツ削除
10. HTTPリクエストを投げるターミナル(②)にて、DELETEメソッドでidがhelloのコンテンツを削除します。
② $ curl --header "Content-Type: application/json" --request DELETE http://localhost:1337/sites/hello
コンテンツ新規作成時や更新時と同様に、HTTPサーバー側のターミナルでpulumi upが実行されていることがわかります。実行内容に「delete」が含まれていることも確認できます。
①
Destroying (*/hello)
View Live: https://app.pulumi.com/*/pulumi_over_http/hello/updates/3
- aws:s3:BucketPublicAccessBlock bucket-public-access-block deleting (0s)
- aws:s3:BucketPolicy bucket-policy deleting (0s)
- aws:s3:BucketObject index deleting (0s)
@ Destroying.....
- aws:s3:BucketObject index deleted (1s)
- aws:s3:BucketPublicAccessBlock bucket-public-access-block deleted (2s)
@ Destroying....
- aws:s3:BucketPolicy bucket-policy deleted (2s)
- aws:s3:Bucket s3-website-bucket deleting (0s)
- aws:s3:Bucket s3-website-bucket deleted (0.61s)
@ Destroying....
- pulumi:pulumi:Stack pulumi_over_http-hello deleting (0s)
- pulumi:pulumi:Stack pulumi_over_http-hello deleted
Outputs:
- website_url: "s3-website-bucket-*******.s3-website-ap-northeast-1.amazonaws.com"
Resources:
- 5 deleted
Duration: 5s
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 kenneth_prv_org/hello`.
無事にコンテンツの削除が完了すると、curlを実行する側のターミナルで「stack 'hello' successfully removed!」のメッセージが表示されます。
②
{
"message": "stack 'hello' successfully removed!"
}
11. HTTPリクエストを投げるターミナル(②)で、byeコンテンツも同様に削除します。
②
$ curl --header "Content-Type: application/json" --request DELETE http://localhost:1337/sites/bye
—
{
"message": "stack 'bye' successfully removed!"
}
12. ここで、前述の「コンテンツ一覧取得」を再度実行すると、無事に2つのコンテンツが削除(一覧に表示されない)されていることが確認できます。
②
$ curl --header "Content-Type: application/json" --request GET http://localhost:1337/sites
—
{
"ids": []
}
ハンズオンは以上です。
おわりに
今回は、Automation APIの概要と、実際にAutomation APIを使ったRESTful APIの静的Webサーバーを構築するハンズオンを解説しました。
Pulumi CLIのようなインフラ作成手順自体をコード化することで、インフラをソフトウェアとして扱えるような感覚がより増すのではないでしょうか。インフラを抽象化してスケールさせる手段(Automation API)と、アプリ開発者に慣れ親しんだPythonやGoなどの開発言語を組み合わせられるのは、Pulumiならではかと思います。次回もお楽しみに!
連載バックナンバー
Think ITメルマガ会員登録受付中
全文検索エンジンによるおすすめ記事
- PulumiでAWSリソースをデプロイしよう
- TerraformからPulumiへの移行
- 既に存在するリソースをPulumiで管理してみよう
- Policy as Codeでインフラのコンプライアンスを自動実現! 「Pulumi CrossGuard」を活用してみよう
- Pulumi Kubernetes Operatorを活用してPulumiのCI/CDを実現しよう
- 「Pulumi Stack」とは ー Pulumiによるマルチステージ環境の構築方法
- SecretもPulumiで使いこなしたい! PulumiのSecurityを試してみよう
- Pulumiの最新機能「Pulumi ESC」を使ってみよう
- Oracle Cloud Hangout Cafe Season7 #2「IaC のベストプラクティス」(2023年7月5日開催)
- CloudサービスとRPAの連携


