Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[support] how to install deps for node.js function? #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Art3mK opened this issue Sep 30, 2020 · 4 comments
Closed

[support] how to install deps for node.js function? #62

Art3mK opened this issue Sep 30, 2020 · 4 comments

Comments

@Art3mK
Copy link

Art3mK commented Sep 30, 2020

Hey,

tried today to deploy lambda using this module, but I can't get that working, I have following config

module "lambda_function" {
  source        = "terraform-aws-modules/lambda/aws"
  function_name = "${random_pet.this.id}-kinesis-reader"
  description   = "Lambda functions to parse kinesis safety data records and store them in S3 and DynamoDB"
  handler       = "index.handler"
  runtime       = "nodejs12.x"
  publish       = true

  source_path = [
    {
      path     = "src"
      commands = [
        "npm install"
      ]
    }]

  store_on_s3 = false
  tags = var.tags
}

resource "random_pet" "this" {
  length = 2
}

lambda function code is located in module directory in src subdir, which contains index.js and package.json files, I'm using terragrunt, running on macOS laptop, terragrunt log:

random_pet.this: Refreshing state... [id=refined-muskox]
module.lambda_function.data.external.archive_prepare[0]: Refreshing state... [id=-]
module.lambda_function.data.aws_iam_policy_document.assume_role[0]: Refreshing state... [id=3693445097]
module.lambda_function.aws_cloudwatch_log_group.lambda[0]: Refreshing state... [id=/aws/lambda/refined-muskox-kinesis-reader]
module.lambda_function.aws_iam_role.lambda[0]: Refreshing state... [id=refined-muskox-kinesis-reader]
module.lambda_function.aws_iam_policy.logs[0]: Refreshing state... [id=arn:aws:iam::12345678910:policy/refined-muskox-kinesis-reader-logs]
module.lambda_function.aws_iam_policy_attachment.logs[0]: Refreshing state... [id=refined-muskox-kinesis-reader-logs]

Error: failed to execute "python3": [13252] prepare: QUERY: {
  "artifacts_dir": "builds",
  "hash_extra": "",
  "hash_extra_paths": "[\".terraform/modules/lambda_function/package.py\"]",
  "paths": "{\"cwd\":\"/Users/artem/projects/example/git/example-infra/live/example-dev/eu-west-1/lambda-kinesis-reader/.terragrunt-cache/I0pgrZY3qsd26sOQh6X_NX7nFzs/kg7RXFBaarwOKyQswQjl7xeLIl8\",\"module\":\".terraform/modules/lambda_function\",\"root\":\".\"}",
  "runtime": "nodejs12.x",
  "source_path": "[{\"commands\":[\"npm install\"],\"path\":\"src\"}]"
}
[13252] prepare: BUILD_PLAN: []
Traceback (most recent call last):
  File ".terraform/modules/lambda_function/package.py", line 1245, in <module>
    main()
  File ".terraform/modules/lambda_function/package.py", line 1241, in main
    exit(args.command(args))
  File ".terraform/modules/lambda_function/package.py", line 1049, in prepare_command
    content_hash = bpm.hash(hash_extra_paths)
  File ".terraform/modules/lambda_function/package.py", line 619, in hash
    raise ValueError('BuildPlanManager.plan() should be called first')
ValueError: BuildPlanManager.plan() should be called first


  on .terraform/modules/lambda_function/package.tf line 7, in data "external" "archive_prepare":
   7: data "external" "archive_prepare" {

Not sure what I'm doing wrong. If I add that src path to source_path list as first item, then build archive contains all files from that folder, but npm install never executes. What's wrong?

Thanks.

@abeluck
Copy link

abeluck commented Oct 7, 2020

I'm having the exact same issue.

Looking at the code that is responsible for parsing the commands, I do not see how it could possibly work:

https://github.com/terraform-aws-modules/terraform-aws-lambda/blob/master/package.py#L654-L695

Walking through that:

  1. we have one command in the commands list "npm install"
  2. it skips the big if c.startswith(':zip'): block and goes straight to the else: batch.append(c)
  3. the loop exits
  4. the function commands_step exits.. and the batch local variable is gone

absolutely nothing is done.

Am I mis-reading this or is this particular feature broken?

@abeluck
Copy link

abeluck commented Oct 7, 2020

Alright.

Parsing the code it seems, this does work, however you must explicitly pass a pseudo command to force zip file creation.

So the correct source_path is:

 source_path = [
    {
      path     = "src"
      commands = [
        "npm install",
        ":zip ."
      ]
    }]

@Art3mK and I were confused, because all the examples that mention npm install, do not have this :zip . psuedo-command.

@antonbabenko
Copy link
Member

Fixed/improved by #62.

Thanks, @chrislwade !

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants