Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

aws lambda - AWSLambdaInternal Uploaded file must be a non-empty zip InvalidParameterValueException

Using node.js on WSL2 with Debian, I'm doing a sample project as detailed here, and I'm currently at this stage.

OLD [solved; scroll to NEW]

When I try to deploy, it gives me

An error occurred: ApiGatewayResourceNotesId - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException;

It seems to be coming from my YAML line here (I say seems because this is the only code in all my project files which mentions "(R|r)esource")
(Error #1)

  environment:
    tableName: notes-sample

  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Scan
        - dynamodb:Query
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:DescribeTable
      Resource: "arn:aws:dynamodb:us-west-2:*:*" # <-- here ---

Yet the example shown on the website uses the same format as above, and I assume it works for them.

Edit 1: Also using Resource: '*' gives the same error.
Edit 2: Also using Amazon's docs and formatting like Resource: "arn:aws:dynamodb:us-west-2::" gives the same error.
Edit 4: Also tried

      Resource:
        Fn::Join:
          - ''
          - - "arn:aws:dynamodb:us-west-2:<redacted>:table/notes-sample"

I've also tried formatting it this way
(Error #2)

  environment:
    tableName: { "Ref": "notes-sample" } # <-- here: change 1 ---
  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Scan
        - dynamodb:Query
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:DescribeTable
      Resource: { "Fn::GetAtt": ["sample-note", "Arn"] } # <-- here: change 2 ---

however, when Serverless validates the template, it gives me the error

Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [notes-sample] in the Resources block of the template
      at /home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/lib/plugins/aws/deploy/lib/validateTemplate.js:20:13

For one, I can't find that folder on my PC, and don't know where else it might be, and two the problem still seems to be this one line of code, and I don't know what I'm missing, here.

I've looked at examples one, two, and tried to go through this Japanese site, but I don't seem to be seeing what's wrong with this line.

I appreciate anyone's help.

Edit 3: Also using Resource: { arn:aws:dynamodb:us-west-2:: } or Resource: { arn:aws:dynamodb:us-west-2:*:* } gives this error

Error: The CloudFormation template is invalid: [/Resources/IamRoleLambdaExecution/Type/Policies/0/PolicyDocument/Statement/2/Resource/arn:aws:dynamodb:us-west-2:*:*] 'null' values are not allowed in templates

I found out I was focusing on the wrong area and for two of my YAML files function definitions I had used [brackets] in place of {braces} from

  update:
    handler: update.main
    events:
      - http:
          path: notes/[id] # <-- here --
          method: put
  delete:
    handler: delete.main
    events:
      - http:
          path: notes/[id] # <-- here --
          method: delete

to

  update:
    handler: update.main
    events:
      - http:
          path: notes/{id} # <-- here --
          method: put
  delete:
    handler: delete.main
    events:
      - http:
          path: notes/{id} # <-- here --
          method: delete

Edit 5: I also fixed all the functions using path: notes to path: notes-sample so they match my [environment][tableName] declaration.

NEW

However, now the error I'm getting is

An error occurred: DeleteLambdaFunction - Uploaded file must be a non-empty zip (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException

I checked out AWS Lambda deployment guide and this Python thread, neither of which helped directly solve this.

INFORMATION

serverless.yml

# NOTE: update this with your service name
service: notes-api

# Create an optimized package for our functions 
package:
  individually: true

plugins:
  - serverless-bundle # Package our functions with Webpack
  - serverless-offline
  - serverless-dotenv-plugin # Load .env as environment variables

provider:
  name: aws
  runtime: nodejs12.x
  stage: prod
  region: us-west-2
  # To load environment variables externally
  # rename env.example to .env and uncomment
  # the following line. Also, make sure to not
  # commit your .env.
  environment:
    tableName: notes-sample
  #  SAMPLE_ENV_VAR: ${env:SAMPLE_ENV_VAR}

  # 'iamRoleStatements' defines the permission policy for the Lambda function.
  # In this case Lambda functions are granted with permissions to access DynamoDB.
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Scan
        - dynamodb:Querynpm i -D
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
        - dynamodb:DescribeTable
      Resource: "arn:aws:dynamodb:us-west-2:082581148431:table/notes-sample"

functions:
  create:
    handler: create.main
    events:
      - http:
          path: notes-sample
          method: post
  get:
    # Defines an HTTP API endpoint that calls the main function in get.js
    # - path: url path is /notes-sample/{id}
    # - method: GET request
    handler: get.main
    events:
      - http:
          path: notes-sample/{id}
          method: get
  list:
    handler: list.main
    events:
      - http:
          path: notes-sample
          method: get
  update:
    handler: update.main
    events:
      - http:
          path: notes-sample/{id}
          method: put
  delete:
    handler: delete.main
    events:
      - http:
          path: notes-sample/{id}
          method: delete

delete.js

import handler from "./libs/handler-lib";
import dynamoDb from "./libs/dynamodb-lib";

export const main = handler(async (event, context) => {
    const params = {
        TableName: process.env.tableName,
        Key: {
            userId: "123",
            noteId: event.pathParameters.id,
        },
    };
    await dynamoDb.delete(params);
    return { status: true };
});

dynamo-lib.js

import AWS from "aws-sdk";

/**
 * If DynamoDB table is in a different region,
 * make sure to set it by calling
 * AWS.config.update({ region: "my-region" });
 * before initializing the DynamoDB client
 */
const client = new AWS.DynamoDB.DocumentClient();

export default {
  get: (params) => client.get(params).promise(),
  put: (params) => client.put(params).promise(),
  query: (params) => client.query(params).promise(),
  update: (params) => client.update(params).promise(),
  delete: (params) => client.delete(params).promise(),
};

Old error #1

An error occurred: ApiGatewayResourceNotesId - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end. (Service: AmazonApiGateway; Status Code: 400; Error Code: BadRequestException; Request ID: f*******-4ae8-4**0-a822-7***********; Proxy: null).

Old error #2

Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [notes-sample] in the Resources block of the template
      at /home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/lib/plugins/aws/deploy/lib/validateTemplate.js:20:13
      at tryCatcher (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:725:18)
      at _drainQueueStep (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate.Async.drainQueues [as _onImmediate] (/home/USER/.nvm/versions/node/v15.6.0/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:15:14)
      at processImmediate (node:internal/timers:463:21)
question from:https://stackoverflow.com/questions/65856003/awslambdainternal-uploaded-file-must-be-a-non-empty-zip-invalidparametervalueexc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...