Hi Forks
I deployed a lambda function(functionName) to aws, the function saves data I fetched from a API to DynamoDB.
I can invoke the function: sls invoke local -f functionName and the data save to the db with no problem.
however, when I try to call the deployed function. I am getting this error:
Error saving data to DynamoDB: {“message”:“Missing required key ‘TableName’ in params”,“code”:“MissingRequiredParameter”,“time”:“2018-05-14T05:49:57.600Z”}
please note: the TableName I stored in serverless.yml file:
environment:
DYNAMODB_TABLE: myTable
Yml-provider:
provider:
name: aws
runtime: nodejs6.10
region: us-east-1
stage: dev
profile: serverless-admin
environment:
DYNAMODB_TABLE: myTable
iamRoleStatements:
- Effect: Allow
Action:
- “lambda:InvokeFunction”
Resource: “*”
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: “arn:aws:dynamodb:{opt:region, self:provider.region}:*:table/{self:provider.environment.DYNAMODB_TABLE}”
it should deployed to aws
what is missing from deploy? store myTable in serverless.yml isn’t enough when deploy to aws? but, it did run well when invoke the function at local as you see.
thanks
Can you show me the yaml from the Resources
section where you are creating the DynamoDB table? The environment var. for the table name should be used there.
I updated the post, yml provider has been added for ref. thx!
The yaml section is not formatted correctly so it is very hard to read. Here is an example how it should be :
...
provider:
...
environment:
DYNAMODB_TABLE: myTable
...
resources:
Resources:
MyDynamoDBTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
-
AttributeName: id
AttributeType: S
KeySchema:
-
AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: myTable
This configuration assumes that the Serverless Framework will create and manage the DynamoDB table for you. On deploy
it will create the DynamoDB table, and on remove
it will destroy it.
If you are connecting to an existing table you will just need to use the SDK and make dynamo calls from your code. No need to add the DynamoDB config in the Resources
section. You will still need the IAMRoleStatements
to give access to perform operations on the table.
Hope this helps.
thanks for the reply.
in my case: I want to call the deployed function(my-serverless-deployed-function) that saves data to dynamodb.
when I “invoke local” the function, it works well,
however, when I deployed the function to the aws, then call it with the function name, It no longer saves data to db anymore, with no error popup.
here is my code:
const AWS = require(“aws-sdk”);
function deployScraper() {
const lambda = new AWS.Lambda({
region: “us-east-1”
})
const params = {
FunctionName: "my-serverless-deployed-function",
InvocationType: "RequestResponse",
LogType: "Tail"
}
return lambda.invoke(params, function(error, data) {
console.log("lambda.invoked")
console.log(data)
return JSON.stringify(data);
})
}
Here is the output of the call:
lambda.invoked
{ StatusCode: 200,
LogResult: ‘U1RBUlQgUmVxdWVzdElkOiA3ZjAxMTA0Ni01ODNmLTExZTgtODZiNy02YmFjZjIwYWFmYjggVmVyc2lvbjogJExBVEVTVAoyMDE4LTA1LTE1VDEyOjU3OjE5Ljg3M1oJN2YwMTEwNDYtNTgzZi0xMWU4LTg2YjctNmJhY2YyMGFhZmI4CUVycm9yIHNhdmluZyBkYXRhIHRvIER5bmFtb0RCOiB7Im1lc3NhZ2UiOiJNaXNzaW5nIHJlcXVpcmVkIGtleSAnVGFibGVOYW1lJyBpbiBwYXJhbXMiLCJjb2RlIjoiTWlzc2luZ1JlcXVpcmVkUGFyYW1ldGVyIiwidGltZSI6IjIwMTgtMDUtMTVUMTI6NTc6MTkuODczWiJ9CjIwMTgtMDUtMTVUMTI6NTc6MTkuODc1Wgk3ZjAxMTA0Ni01ODNmLTExZTgtODZiNy02YmFjZjIwYWFmYjgJKG5vZGU6MSkgVW5oYW5kbGVkUHJvbWlzZVJlamVjdGlvbldhcm5pbmc6IFVuaGFuZGxlZCBwcm9taXNlIHJlamVjdGlvbiAocmVqZWN0aW9uIGlkOiAxKTogRXJyb3Igc2F3aW5nIGRhdGEgdG8gRHluYW1vREI6IHsibWVzc2FnZSI6Ik1pc3NpbmcgcmVxdWlyZWQga2V5ICdUYWJsZU5hbWUnIGluIHBhcmFtcyIsImNvZGUiOiJNaXNzaW5nUmVxdWlyZWRQYXJhbWV0ZXIiLCJ0aW1lIjoiMjAxOC0wNS0xNVQxMjo1NzoxOS44NzNaIn0KRU5EIFJlcXVlc3RJZDogN2YwMTEwNDYtNTgzZi0xMWU4LTg2YjctNmJhY2YyMGFhZmI4ClJFUE9SVCBSZXF1ZXN0SWQ6IDdmMDExMDQ2LTU4M2YtMTFlOC04NmI3LTZiYWNmMjBhYWZiOAlEdXJhdGlvbjogMTMzOC42NCBtcwlCaWxsZWQgRHVyYXRpb246IDE0MDAgbXMgCU1lbW9yeSBTaXplOiAxMDI0IE1CCU1heCBNZW1vcnkgVXNlZDogNzEgTUIJCg==’,
ExecutedVersion: ‘$LATEST’,
Payload: ‘{“statusCode”:200,“body”:"{\“message\”:\“completed\”}"}’ }
here is yaml serverless.privder
provider:
name: aws
runtime: nodejs6.10
region: us-east-1
stage: dev
profile: serverless-admin
environment:
DYNAMODB_TABLE: myTable
iamRoleStatements:
- Effect: Allow
Action:
- "lambda:InvokeFunction"
Resource: "*"
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
Hello folks,
issue has been fixed myself!
the error code from AWS log isn’t reflect the actual error. I was confused about the error and check many times try to have the missing table name fixed, however, the above script are correct. aws report error, since I have some bugs in my node script. nothing to do with yaml and deplyed handler function.
Thanks!
Good to hear it solved your issue.