I'm getting started with nodejs and the serverless framwork.
My handler.js contains:
'use strict';
var index = require('./index.js');
module.exports.hello = async event => {
var res = await index.main();
console.log('hello');
console.log(res);
console.log('IN HANDLER');
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'main function executed!',
input: event,
},
null,
2
),
};
};
My serverless.yml contains:
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
provider:
name: aws
runtime: nodejs12.x
region: us-east-1
# here we put the layers we want to use
layers:
# Google Chrome for AWS Lambda as a layer
# Make sure you use the latest version depending on the region
# https://github.com/shelfio/chrome-aws-lambda-layer
# - arn:us-east-1:arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:10
- arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:10
# function parameters
# you can add packaging information here
#package:
# include:
# - include-me.js
# - include-me-dir/**
# exclude:
# - exclude-me.js
# - exclude-me-dir/**
functions:
hello:
handler: handler.hello
# main:
# handler: handler.main
# The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details
events:
- http:
path: hello/get
method: get
my index.js:
async function main(event, context, callback) {
const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer');
const os = require('os');
const CREDS = require('./.creds');
// exports.handler = async (event, context, callback) => {
let result = null;
let browser = null;
try {
browser = await chromium.puppeteer.launch({
args: chromium.args,
defaultViewport: chromium.defaultViewport,
executablePath: await chromium.executablePath,
headless: chromium.headless,
ignoreHTTPSErrors: true,
})
}
catch {
console.log('browser failed')
};
var page = await browser.newPage();
........
// })().catch(e => { console.error(e) });
};
main().catch(e => { console.error(e) });
module.exports.main = main;
When I run :
$ sls invoke -f hello
Serverless Error ---------------------------------------
Function not found: arn:aws:lambda:us-east-1:155754363046:function:sellthelandnow-dev-hello
The error is in the title. What am I doing wrong here?
Let me explain here. Serverless framework can invoke(run) lambda in two ways(locally and in cloud-AWS). It seems you are trying to invoke lambda in AWS. (arn:aws:lambda:us-east-1:155754363046:function:sellthelandnow-dev-hello) Basically this arn does not exist in your AWS-155754363046 account. you need to use
serverless deploy
to deploy lamdba to aws env.If you just want to test locally, the command is
serverless invoke local --function functionName
So I will suggest in case you want to invoke lambda in the cloud.You need to first deploy it Or you use invoke local.
Thanks,
Ashish