https://pulumi.com logo
Title
e

echoing-zebra-28421

06/27/2021, 11:52 PM
Anyone has any ideas how to solve this error?
Error reading file 'infra/package.json' when computing package dependencies. Error: ENOENT: no such file or directory, open '/infra/package.json'
My dir:
- project
    ...
    - infra
        - Pulumi.yaml
        - tsconfig.json
        ...
    - src
       ...
   - package.json
   - tsconfig.json
I need to have the package.json at that place in my directory. Any help or alternatives to solve the problem will be welcome. I'm using 
@pulumi/pulumi: "^3.4.0"
this error occurs when i run 
pulumi preview --cwd infra
  I run that command in the root of my project. the problem occurs when I try to use  
aws.lambda.CallbackFunction
l

little-cartoon-10569

06/27/2021, 11:54 PM
What's trying to read the file, and how?
e

echoing-zebra-28421

06/28/2021, 12:04 AM
@little-cartoon-10569 My project structure is like this:
- project
    ...
    - infra
        - Pulumi.yaml
        - tsconfig.json
        ...
    - src
       ...
   - package.json
   - tsconfig.json
in my package.json
{
  ...
  "devDependencies": {
    "@pulumi/aws": "^4.0.0",
    "@pulumi/pulumi": "^3.0.0",
     ...
  },
  "main": "infra/index.ts",
  "dependencies": {
    "axios": "^0.21.1"
  }
}
infra/index.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const stack = pulumi.getStack();

const name = `test-${stack}`;
const lambdaRole = new aws.iam.Role(`${name}-role`, {
  assumeRolePolicy: {
    Version: "2012-10-17",
    Statement: [
      {
        Action: "sts:AssumeRole",
        Principal: {
          Service: "<http://lambda.amazonaws.com|lambda.amazonaws.com>",
        },
        Effect: "Allow",
        Sid: "",
      },
    ],
  },
});

const lambdaRoleAttachment = new aws.iam.RolePolicyAttachment(
  `${name}-role-attachment`,
  {
    role: lambdaRole,
    policyArn: aws.iam.ManagedPolicy.AWSLambdaBasicExecutionRole,
  },
);

const lambda = new aws.lambda.CallbackFunction(`${name}-lambda`, {
  callback: (foo, bar) => {
    console.log(foo, bar);
  },
});

export const test = lambda.arn;
when i run:
pulumi preview --cwd infra
My error:
Error reading file 'infra/package.json' when computing package dependencies. Error: ENOENT: no such file or directory, open '/infra/package.json'
but when I comment on that function (aws.lambda.CallbackFunction) or use another kind of simple example. That command works fine for me.
I want to separate my infrastructure from the application code. But I need to use that aws.lambda.CallbackFunction to call the functions it creates. I don't know if there is a better alternative. I am doing an apigatewayV2 but I realized that the problem I have is because of that aws.lambda.CallbackFunction
this is the complete error:
error: Error: Error reading file 'alex-test/infra/package.json' when computing package dependencies. Error: ENOENT: no such file or directory, open 'alex-test/infra/package.json'
        at readFile (alex-test/node_modules/@pulumi/pulumi/runtime/closure/codePaths.js:167:19)
        at computeDependenciesDirectlyFromPackageFile (alex-test/node_modules/@pulumi/pulumi/runtime/closure/codePaths.js:150:22)
        at alex-test/node_modules/@pulumi/pulumi/runtime/closure/codePaths.js:115:36
        at alex-test/node_modules/read-package-tree/rpt.js:162:20
the aws.lambda.CallbackFunction as expected to have the package.json at the same level as Pulumi.yaml and also to have the axios library as a dependency
but I don't want to duplicate the package.json. I think that's wrong. I even tried to create a symbolic link and it didn't work for me. it showed me another error. I don't know what is the best way to solve this problem 😞
l

little-cartoon-10569

06/28/2021, 12:40 AM
Ah, it's an issue getting Pulumi, tsc and npm all seeing the same directory as root? Because there's a ts lambda involved, there are two steps to this. First you can get it working without the lambda (comment out the code); iirc this is straightforward, I think what you've got is good.
Once you've confirmed that, then you need to make the 2nd compile (for the lambda) work too. This means you need to create the symlink to package.json in the project directory and run
npm install
in the project directory
. You can avoid duplicating package.json but afaict you need to duplicate your _node___modules_ directory.
Part of the issue is captured here: https://github.com/pulumi/pulumi/issues/2661 But there's another issue, I think I raised it.. I'll keep hunting.
And another almost identical one: https://github.com/pulumi/pulumi/issues/7168
e

echoing-zebra-28421

06/28/2021, 1:43 AM
My solution is to create the package.json inside infra and run the
npm install
to create the axios dependency. With that duplication of the package.json it works fine for me. my structure would look like this:
- project
    ...
    - infra
        - Pulumi.yaml
        - package.json
        - tsconfig.json
        ...
    - src
       ...
   - package.json
   - tsconfig.json
in
infra/package.json
{
  "dependencies": {
    "axios": "^0.21.1"
  }
}
I hope that at some point that issue is solved. The symbolic link did not work as expected. But hey, if you find a better, more effective solution, I would appreciate it.
maybe the other way would be to create the lambda in another way. But I don't know how to implement the routes with ApigatewayV2 without using the aws.lambda.CallbackFunction
for example: https://github.com/pulumi/examples/blob/master/aws-ts-apigatewayv2-http-api/index.ts in that example they use aws.lambda.Function with aws.lambda.Function no need to duplicate the package.json But I don't know how to create multiple routes. in that example it shows a single route. At that time @curved-pharmacist-41509 helped me with that. and I recommend using Jake to call my lambdas.
l

little-cartoon-10569

06/28/2021, 2:01 AM
🙂 I'm not very knowledgeable about the ins and outs of why this lambda and why not that event rule, and all that sort of stuff. It should be possible though, since iirc CallbackFunction is a wrapper around Function, with some nice mixins..
e

echoing-zebra-28421

06/28/2021, 2:09 AM
hopefully someone can make this work without duplicating the package.json and share a simple example with us 🙏
@*tenwit* thanks for the support
l

little-cartoon-10569

06/28/2021, 2:12 AM
There is a case to be made to have a separate pacakge.json specifically for the lambda. It should need the aws-sdk and not much else...?
e

echoing-zebra-28421

06/28/2021, 2:21 AM
I have a case where you want to separate the infrastructure from the application code. Basically have two directories: Infra and src in infra have the files of pulumi
- Pulumi.yaml
- index.ts
...
where the index.ts use the
aws.lambda.CallbackFunction
in the root you must have the
package.json
with all the dependencies of pulumi. and when this command is run:
pulumi preview --cwd infra
works correctly. If you try to create a project with that structure, it will surely give you that error that I get. But it is when the
aws.lambda.CallbackFunction
is used because with another example that this one in the documentation works well that folder and file structure
l

little-cartoon-10569

06/28/2021, 2:53 AM
If you check the code for CallbackFunction you may find that it hardcodes the compile directory for ts-node or tsc. It's an opinionate implementation. You could implement it yourself and use different directories, to work around that constranit.