I'm testing a simple Pulumi concept: packaging the infrastructure. I'm using a very simple example for this, I'm trying to deploy a S3 bucket by importing a npm package.
Here's my npm bucket
npm package:
index.ts
import * as aws from "@pulumi/aws";
import { ComponentResource, ComponentResourceOptions, Input, Output } from "@pulumi/pulumi";
export interface S3Args {
description: string;
baseTags: aws.Tags;
}
export class S3 extends ComponentResource {
s3: aws.s3.Bucket;
private readonly name: string;
private readonly description: string;
private readonly baseTags: { [k: string]: Input<string> };
constructor(name: string, args: S3Args, opts?: ComponentResourceOptions) {
super("oli:s3-test", name, {}, opts);
// Make base info available to other methods.
this.name = name;
this.description = args.description;
this.baseTags = args.baseTags;
// VPC
this.s3 = new aws.s3.Bucket(`${name}-test`, {
tags: this.resourceTags({
Name: `${args.description} test`,
}),
}, {parent: this});
}
private resourceTags(additionalTags: { [k: string]: Input<string> }) {
return Object.assign(additionalTags, this.baseTags);
}
}
tsconfig.json
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"skipLibCheck": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.ts"
]
}
package.json
{
"name": "@obutterbach/bucket",
"version": "1.0.0",
"description": "Pulumi package for S3",
"main": "index.ts",
"type": "module",
"scripts": {
"build": "tsc",
"lint": "tslint --project ."
},
"repository": {
"type": "git",
"url": "git https://github.com/obutterbach/pulumi-infra-package.git"
},
"publishConfig": {
"name": "@obutterbach/bucket",
"registry": "https://npm.pkg.github.com"
},
"devDependencies": {
"@types/node": "14.6.2",
"ts-node": "^8.0.2",
"typescript": "^2.9.1",
"tslint": "^5.10.0"
},
"dependencies": {
"@pulumi/aws": "^4.0.0",
"@pulumi/pulumi": "^3.0.0"
},
"author": "Oli",
"license": "ISC"
}
So far so good, I'm able to perform the following command:
npm i
, npm run build
, npm run lint
and npm publish
Now I got a different repo for deploying Pulumi and I'm using:
Install npm package:
npm install @obutterbach/bucket
index.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import { S3 } from "@obutterbach/bucket";
const website = new S3 ("testing", {
description: "blabla",
baseTags: {"bla": "bla"}
});
Using Pulumi preview
keeps sending me this error and I cannot find the reason behind:
$ pulumi preview
Please choose a stack, or create a new one: deploy-dev
Previewing update (deploy-dev):
Type Name Plan Info
pulumi:pulumi:Stack pulumi-deploy-deploy-dev create 1 error
Diagnostics:
pulumi:pulumi:Stack (pulumi-deploy-deploy-dev):
error: Running program '/Users/oli/Documents/pulumi-deploy' failed with an unhandled exception:
/Users/oli/Documents/pulumi-deploy/node_modules/@obutterbach/bucket/index.ts:1
import * as aws from "@pulumi/aws";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Object.require.extensions.<computed> [as .ts] (/Users/oli/Documents/pulumi-deploy/node_modules/ts-node/src/index.ts:431:14)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:93:18)
at Object.<anonymous> (/Users/oli/Documents/pulumi-deploy/index.ts:4:1)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
Thank you again for your help!
CodePudding user response:
It looks like your @obutterbach/bucket
package is running the wrong file index.ts
when imported. Node.js is unable to run Typescript files.
I believe you should change the main
in package.json
pointing to your index.js
in your @obutterbach/bucket
package.
{
"name": "@obutterbach/bucket",
"version": "1.0.0",
"description": "Pulumi package for S3",
"main": "./bin/index.js",
"types": "./bin/index.d.ts",
...
}
Source: https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html