I'm having trouble getting started with reusable components. I'm trying to use TypeScript to author a simple reusable component resource that I can use to deploy an S3 bucket on AWS.
To demonstrate the issue I'm having, I have created two public GitHub repos:
briancaffey/pulumi-alpha
is the repo that consumes my S3 componentbriancaffey/pulumi-beta
is the repo where I define the reusable S3 component
I created both of these with pulumi new aws-typescript
.
For pulumi-beta
, I am using the following code to define my component resource:
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
export interface S3BucketArgs {
name: string;
}
export class S3Bucket extends pulumi.ComponentResource {
public readonly bucket: aws.s3.Bucket;
constructor(name: string, args: S3BucketArgs, opts?: pulumi.ComponentResourceOptions) {
super("my:modules:S3Bucket", name, {}, opts);
this.bucket = new aws.s3.Bucket(name, {
bucket: args.name,
}, { parent: this });
}
}
In pulumi-alpha
, I installed the pulumi-beta
this package with the following command:
npm i git+https://github.com/briancaffey/pulumi-beta.git
Then I use it like this in index.ts
:
import * as pulumi from "@pulumi/pulumi";
import { S3Bucket } from "pulumi-beta";
const bucket = new S3Bucket("my-bucket", {
name: "my-example-pulumi-bucket",
});
export const bucketName = bucket.bucket.id;
Then I ran tsc
and I can see that it built the application in the bin
directory:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.bucketName = void 0;
const pulumi_beta_1 = require("pulumi-beta");
const bucket = new pulumi_beta_1.S3Bucket("my-bucket", {
name: "my-example-pulumi-bucket",
});
exports.bucketName = bucket.bucket.id;
//# sourceMappingURL=index.js.map
Now I want to run pulumi preview
to see how things will look, but I get this error:
~/git/github/pulumi-alpha$ pulumi preview
Previewing update (dev)
View Live: https://app.pulumi.com/briancaffey/pulumi-alpha/dev/previews/f8469d80-a9d5-4e95-9d9f-8a5830220f5c
Type Name Plan Info
+ pulumi:pulumi:Stack pulumi-alpha-dev create 1 error
Diagnostics:
pulumi:pulumi:Stack (pulumi-alpha-dev):
error: Running program '/Users/brian/git/github/pulumi-alpha' failed with an unhandled exception:
/Users/brian/git/github/pulumi-alpha/node_modules/pulumi-beta/index.ts:1
import * as pulumi from "@pulumi/pulumi";
^^^^^^
SyntaxError: Cannot use import statement outside a module
Back in the pulumi-beta
repo, I added "type": "module",
to package.json
, pushed to GitHub, then reinstalled everything in pulumi-alpha
:
rm -rf bin
rm -rf node_modules
rm package-lock.json
npm i
tsc
Running pulumi preview
again I get the same error as before:
Diagnostics:
pulumi:pulumi:Stack (pulumi-alpha-dev):
error: Running program '/Users/brian/git/github/pulumi-alpha' failed with an unhandled exception:
/Users/brian/git/github/pulumi-alpha/node_modules/pulumi-beta/index.ts:1
import * as pulumi from "@pulumi/pulumi";
^^^^^^
SyntaxError: Cannot use import statement outside a module
Can anyone help guide me on how I can get this basic example to work?