问题描述
在 WSL2 和 Debian 上使用 node.js,我正在做一个详细的示例项目 here,我目前在 this stage。
OLD [已解决;滚动到新]
当我尝试部署时,它给了我
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;
它似乎来自我这里的 YAML 行(我说似乎,因为这是我所有项目文件中唯一提到“(R|r)esource”的代码)
(错误 #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 ---
然而 the website 上显示的示例使用与上述相同的格式,我认为它适用于他们。
编辑 1:同样使用 Resource: '*'
会产生相同的错误。
编辑 2: 同样使用 Amazon's docs 和格式如 Resource: "arn:aws:dynamodb:us-west-2::"
会产生相同的错误。
编辑 4: 也尝试过
Resource:
Fn::Join:
- ''
- - "arn:aws:dynamodb:us-west-2:<redacted>:table/notes-sample"
我也试过这样格式化
(错误 #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 ---
然而,当无服务器验证模板时,它给了我错误
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
一方面,我在我的电脑上找不到那个文件夹,也不知道它可能在哪里;另一方面,问题似乎仍然是这一行代码,我不知道我在做什么不见了,在这里。
我查看了示例 one、two,并试图通过 this Japanese site,但我似乎没有看到这条线有什么问题。
感谢任何人的帮助。
编辑 3:同时使用 Resource: { arn:aws:dynamodb:us-west-2:: }
或 Resource: { arn:aws:dynamodb:us-west-2:*:* }
会出现此错误
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
我发现我关注的是错误的区域,对于我的两个 YAML 文件函数定义,我使用了 [brackets] 代替了来自
的 {braces} update:
handler: update.main
events:
- http:
path: notes/[id] # <-- here --
method: put
delete:
handler: delete.main
events:
- http:
path: notes/[id] # <-- here --
method: delete
到
update:
handler: update.main
events:
- http:
path: notes/{id} # <-- here --
method: put
delete:
handler: delete.main
events:
- http:
path: notes/{id} # <-- here --
method: delete
编辑 5:我还使用 path: notes
将所有函数修复为 path: notes-sample
,以便它们匹配我的 [environment][tableName]
声明。
新
但是,现在我得到的错误是
An error occurred: DeleteLambdaFunction - Uploaded file must be a non-empty zip (Service: AWSLambdaInternal; Status Code: 400; Error Code: InvalidParameterValueException
我检查了 AWS Lambda deployment guide 和 this Python thread,它们都没有直接帮助解决这个问题。
信息
无服务器.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
删除.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(),};
旧错误 #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).
旧错误 #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)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)