Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Serverless functions, says doesn't exist

I am trying to deploy my first serveless project and want to have it update a simple item in my dynamodb. So I started making a simple service in serverless.yml:

service: serverless
    provider:
      name: aws
      runtime: nodejs4.3
      iamRoleStatements:
        - Effect: "Allow"
          Resource: "*"
          Action:
            - "dynamodb:*"
    functions:
      createMovie:
        handler: handler.createMovie
        events:
          - http:
              path: movies/create
              method: post
              integration: lambda
              cors: true

Then I found some more code online to link up the dynamodb portion of my app and added it into serverless.yml at the bottom: resources:

  Resources:
    DynamoDbTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: moviesTwo
        AttributeDefinitions:
          - AttributeName: movieTitle
            AttributeType: S
        KeySchema:
          - AttributeName: movieTitle
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
    DynamoDBIamPolicy:
      Type: AWS::IAM::Policy
      DependsOn: DynamoDbTable
      Properties:
        PolicyName: lambda-dynamodb
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Action:
                - dynamodb:GetItem
                - dynamodb:PutItem
              Resource: arn:aws:dynamodb:*:*:table/moviesTwo
        Roles:
          - Ref: IamRoleLambdaExecution

My function in handler.js is:

module.exports.createMovie = (event, context, cb) => {  
  const params = {
    TableName: 'movies',
    Item: {
        "movieTitle": "Star Wars"
    }
  };

  return dynamo.put(params, cb);
};

I ran serverless deploy and it worked on many levels. Created my lambda for me, gave me an endpoint and functions:

endpoints: POST - https://tbwg38fvc0.execute-api.us-east-1.amazonaws.com/dev/movies/create functions: serverless-dev-hello serverless-dev-createMovie

But I run it in postman and I get an internal server error, and I tried calling: serverless invoke -f serverless-dev-createMovie -l and it says:

Serverless Error ---------------------------------------

 Function "serverless-dev-createMovie" doesn't exist
 in this Service

So I am confused what I am doing wrong. Is there another setting in AWS I am missing that needs to be set for invoke to work?

like image 820
Todd Coulson Avatar asked Feb 17 '17 16:02

Todd Coulson


1 Answers

I think there are two issues here.

  1. The function name is 'createMovie' so you need to say: serverless invoke -f createMovie -l

  2. Check Cloudwatch log for the internal server error. I am guessing it's the dynamodb table name. You've created 'moviesTwo', but you have

TableName: 'movies'

Hope this helps.

like image 152
user1736525 Avatar answered Sep 24 '22 22:09

user1736525