1. Docs
  2. Pulumi IaC
  3. Get started
  4. Get started
  5. Modify program

Pulumi & Google Cloud: Modify program

    Now that your storage bucket is provisioned, let’s add an object to it. First, from within your project directory, create a new index.html file with some content in it.

    cat <<EOT > index.html
    <html>
        <body>
            <h1>Hello, Pulumi!</h1>
        </body>
    </html>
    EOT
    
    cat <<EOT > index.html
    <html>
        <body>
            <h1>Hello, Pulumi!</h1>
        </body>
    </html>
    EOT
    
    @"
    <html>
      <body>
        <h1>Hello, Pulumi!</h1>
      </body>
    </html>
    "@ | Out-File -FilePath index.html
    

    Now that you have an index.html file with some content, open index.js index.ts __main__.py main.go Program.cs Program.fs Program.vb App.java Pulumi.yaml and modify it to add that file to your storage bucket.

    For this, you’ll use Pulumi’s FileAsset class to assign the content of the file to a new BucketObject.

    In index.js, create the BucketObject right after creating the bucket itself:

    const bucketObject = new gcp.storage.BucketObject("index.html", {
        bucket: bucket.name,
        source: new pulumi.asset.FileAsset("index.html")
    });
    

    In index.ts, create the BucketObject right after creating the bucket itself:

    const bucketObject = new gcp.storage.BucketObject("index.html", {
        bucket: bucket.name,
        source: new pulumi.asset.FileAsset("index.html")
    });
    

    In __main__.py, create a new bucket object by adding the following right after creating the bucket itself:

    bucket_object = storage.BucketObject(
        "index.html", bucket=bucket.name, source=pulumi.FileAsset("index.html")
    )
    

    In main.go, create the BucketObject right after creating the bucket itself:

    _, err = storage.NewBucketObject(ctx, "index.html", &storage.BucketObjectArgs{
        Bucket: bucket.Name,
        Source: pulumi.NewFileAsset("index.html"),
    })
    if err != nil {
        return err
    }
    

    In Program.cs, create the BucketObject right after creating the bucket itself:

    var bucketObject = new BucketObject("index.html", new BucketObjectArgs
    {
        Bucket = bucket.Name,
        Source = new FileAsset("./index.html")
    });
    

    In index.js index.ts main.py main.go Program.cs Program.fs Program.vb App.java Pulumi.yaml , import the following additional classes, then create the BucketObject right after creating the bucket itself:

    // ...
    import com.pulumi.asset.FileAsset;
    import com.pulumi.gcp.storage.BucketIAMBinding;
    import com.pulumi.gcp.storage.BucketIAMBindingArgs;
    import com.pulumi.gcp.storage.BucketObject;
    import com.pulumi.gcp.storage.BucketObjectArgs;
    
    public class App {
        public static void main(String[] args) {
            Pulumi.run(ctx -> {
                // ...
    
                // Create a Bucket object
                var bucketObject = new BucketObject("index.html", BucketObjectArgs.builder()
                    .bucket(bucket.name())
                    .source(new FileAsset("index.html"))
                    .build()
                );
    
                // ...
            });
        }
    }
    

    In index.js index.ts main.py main.go Program.cs Program.fs Program.vb App.java Pulumi.yaml , create the BucketObject right below the bucket itself.

    resources:
      # ...
      index-html:
        type: gcp:storage:BucketObject
        properties:
          bucket: ${my-bucket}
          source:
            fn::fileAsset: ./index.html
    

    Notice how you provide the name of the bucket you created earlier as an input for the BucketObject. This tells Pulumi which bucket the object should live in.

    Below the BucketObject, add an IAM binding allowing the contents of the bucket to be viewed anonymously over the Internet:

    const bucketBinding = new gcp.storage.BucketIAMBinding("my-bucket-binding", {
        bucket: bucket.name,
        role: "roles/storage.objectViewer",
        members: ["allUsers"]
    });
    
    const bucketBinding = new gcp.storage.BucketIAMBinding("my-bucket-binding", {
        bucket: bucket.name,
        role: "roles/storage.objectViewer",
        members: ["allUsers"]
    });
    
    bucket_iam_binding = storage.BucketIAMBinding(
        "my-bucket-binding",
        bucket=bucket.name,
        role="roles/storage.objectViewer",
        members=["allUsers"],
    )
    
    _, err = storage.NewBucketIAMBinding(ctx, "my-bucket-binding", &storage.BucketIAMBindingArgs{
        Bucket: bucket.Name,
        Role:   pulumi.String("roles/storage.objectViewer"),
        Members: pulumi.StringArray{
            pulumi.String("allUsers"),
        },
    })
    if err != nil {
        return err
    }
    
    var bucketBinding = new BucketIAMBinding("my-bucket-binding", new BucketIAMBindingArgs
    {
        Bucket = bucket.Name,
        Role = "roles/storage.objectViewer",
        Members = new[]
        {
            "allUsers",
        },
    });
    
    var bucketBinding = new BucketIAMBinding("my-bucket-binding", BucketIAMBindingArgs.builder()
        .bucket(bucket.name())
        .role("roles/storage.objectViewer")
        .members("allUsers")
        .build());
    
    my-bucket-binding:
      type: gcp:storage:BucketIAMBinding
      properties:
        bucket: ${my-bucket.name}
        role: "roles/storage.objectViewer"
        members:
          - allUsers
    

    Next, you’ll deploy your changes.

      PulumiUP 2024. Watch On Demand.