Pulumiでチュートリアルを実施する。(APIGateway×Lambda)

### はじめに
AWSのプロビジョニングツールとして有名なPulumi。最近話題になってきているので、チュートリアルを実施します！！

### Pulumiとは
AWSやGCP、Azure等をデプロイするIaCツール。TerafformやCloudFormationが有名ですが、IaCツールとしてPulumiも有名です！！
![スクリーンショット 2022-08-28 21.00.37.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/2481e794-92be-f06e-b8a8-79ed00119463.png)

### 前提条件
AWS profileを事前に設定しておいてください。

### プロジェクトのセットアップ

```shell:ディレクトリ構成
~/develop/purlumi/pulumi-lambda$ tree -I node_modules 
.
├── Pulumi.dev.yaml
├── Pulumi.yaml
├── index.js
├── package-lock.json
├── package.json
└── www
    ├── favicon.png
    └── index.html

1 directory, 7 files
```

```shell:プロジェクトのセットアップ
~/develop/purlumi$ mkdir pulumi-lambda && cd pulumi-lambda

~/develop/purlumi/pulumi-lambda$ pulumi new hello-aws-javascript --name pulumi-lambda 
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 description: (A simple AWS serverless JavaScript Pulumi program) 
Created project 'pulumi-lambda'

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...


added 124 packages, and audited 125 packages in 16s

44 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Finished installing dependencies

Your new project is ready to go! ✨

To perform an initial deployment, run 'pulumi up'

```

### ソースコード

```shell:index.js
const pulumi = require("@pulumi/pulumi");
const aws = require("@pulumi/aws");
const awsx = require("@pulumi/awsx");

// Create a public HTTP endpoint (using AWS APIGateway)
const endpoint = new awsx.apigateway.API("hello", {
  routes: [
    // Serve static files from the `www` folder (using AWS S3)
    {
      path: "/",
      localPath: "www",
    },

    // Serve a simple REST API on `GET /name` (using AWS Lambda)
    {
      path: "/source",
      method: "GET",
      eventHandler: (req, ctx, cb) => {
        cb(undefined, {
          statusCode: 200,
          body: Buffer.from(JSON.stringify({ name: "AWS" }), "utf8").toString(
            "base64"
          ),
          isBase64Encoded: true,
          headers: { "content-type": "application/json" },
        });
      },
    },
  ],
});

// Export the public URL for the HTTP service
exports.url = endpoint.url;

```

### プロビジョニング
```
~/develop/purlumi/pulumi-lambda$ pulumi up           
Previewing update (dev)

View Live: https://app.pulumi.com/XXXXXXX

     Type                                Name                       Plan       
 +   pulumi:pulumi:Stack                 pulumi-lambda-dev          create     
 +   └─ aws:apigateway:x:API             hello                      create     
 +      ├─ aws:iam:Role                  hello4c238266              create     
 +      ├─ aws:s3:Bucket                 hello                      create     
 +      ├─ aws:iam:Role                  hello66382ec5              create     
 +      ├─ aws:lambda:Function           hello66382ec5              create     
 +      ├─ aws:iam:RolePolicyAttachment  hello4c238266              create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-4aaabb8e     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-1b4caae3     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-a1de8170     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-019020e7     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-b5aeb6b6     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-e1a3786d     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-6c156834     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-7cd09230     create     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-74d12784     create     
 +      ├─ aws:s3:BucketObject           hello4c238266/index.html   create     
 +      ├─ aws:s3:BucketObject           hello4c238266/favicon.png  create     
 +      ├─ aws:apigateway:RestApi        hello                      create     
 +      ├─ aws:apigateway:Deployment     hello                      create     
 +      ├─ aws:lambda:Permission         hello-f61e7551             create     
 +      └─ aws:apigateway:Stage          hello                      create     
 
Outputs:
    url: output<string>

Resources:
    + 22 to create

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

View Live: https://app.pulumi.com/XXXXXXX

     Type                                Name                       Status       
 +   pulumi:pulumi:Stack                 pulumi-lambda-dev          created     
 +   └─ aws:apigateway:x:API             hello                      created     
 +      ├─ aws:s3:Bucket                 hello                      created     
 +      ├─ aws:iam:Role                  hello66382ec5              created     
 +      ├─ aws:iam:Role                  hello4c238266              created     
 +      ├─ aws:s3:BucketObject           hello4c238266/index.html   created     
 +      ├─ aws:s3:BucketObject           hello4c238266/favicon.png  created     
 +      ├─ aws:iam:RolePolicyAttachment  hello4c238266              created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-019020e7     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-e1a3786d     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-4aaabb8e     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-7cd09230     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-74d12784     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-1b4caae3     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-6c156834     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-b5aeb6b6     created     
 +      ├─ aws:iam:RolePolicyAttachment  hello66382ec5-a1de8170     created     
 +      ├─ aws:lambda:Function           hello66382ec5              created     
 +      ├─ aws:apigateway:RestApi        hello                      created     
 +      ├─ aws:apigateway:Deployment     hello                      created     
 +      ├─ aws:lambda:Permission         hello-f61e7551             created     
 +      └─ aws:apigateway:Stage          hello                      created     
 
Outputs:
    url: "https://app.pulumi.com/XXXXXXX"

Resources:
    + 22 created

Duration: 29s

```

![スクリーンショット 2022-08-28 21.12.51.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/931f1997-34e8-76f2-8ad9-05e0ea2cde49.png)

![スクリーンショット 2022-08-28 21.13.53.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/599049/998ac251-1b97-e3b6-e987-89a38462ce6a.png)
→これが出ていればOK！！！