Dan Van Brunt
03/16/2022, 6:10 PMsst.Function
internally? Each time I’ve tried this I run into issues with where to store construct assets and how to reference them into sst.Function
Dan Van Brunt
03/16/2022, 6:24 PMDerek Kershner
03/16/2022, 7:44 PMDerek Kershner
03/16/2022, 7:45 PMIS_LOCAL
and otherwise use cdk.lambda_nodejs
Dan Van Brunt
03/16/2022, 7:47 PMyarn link
Derek Kershner
03/16/2022, 7:48 PMlink
uses the built package, so it would work fine (wouldn’t be IS_LOCAL
).
The setup has some different props, but relatively similar (other than handler
, which is a key, valuable difference).Derek Kershner
03/16/2022, 7:50 PMDerek Kershner
03/16/2022, 7:50 PMDerek Kershner
03/16/2022, 7:51 PMnodejs_lambda
and lambda
allow you to have a relative reference, though. you could always ask SST to do the same.Frank
handler
? If you have a sample repo for a npm module or a screenshot of the error, I want to put it on my todo to give it a try. And I can prioritize this.Dan Van Brunt
03/17/2022, 1:04 PMcdk.Function
for the core /assets
code and then borrow similar copyFiles functionality from sst.Function
in order to bake in the files supplied by the construct user.
Otherwise, not sure how I might be able to just use sst.Function
for BOTH of the above.Dan Van Brunt
03/17/2022, 1:41 PMError: Cannot find a handler file for "/Users/me/Projects/klick-packages/packages/cdk-constructs/assets/social-cards"
Dan Van Brunt
03/17/2022, 2:03 PMconst api = new sst.Api(this, 'Api', {
routes: {
'GET /{template}/{file}': {
function: {
handler: `node_modules/@klickmarketing/cdk-constructs/assets/social-cards/index.handler`,
Derek Kershner
03/17/2022, 2:27 PMhardcoding this path works….So long as the package is installed at the root. 🙂
Dan Van Brunt
03/17/2022, 2:27 PMDerek Kershner
03/17/2022, 2:28 PMDerek Kershner
03/17/2022, 2:31 PMlambda
works (I have packages that work this way):
code: lambda.Code.fromAsset(path.join(__dirname, 'my-lambda-handler')),
handler: 'index.main',
Dan Van Brunt
03/17/2022, 2:33 PMsst.Function
demands that its urls are relative of the srcPathDan Van Brunt
03/17/2022, 2:35 PMsrcPath
Derek Kershner
03/17/2022, 2:36 PMsst.Function
uses cdk.Lambda
, it shouldn’t be a crazy lift to support the other pattern. an object overload to handler
, maybe.Derek Kershner
03/17/2022, 2:36 PMsst.Function
is likely to be the easiest path.Derek Kershner
03/17/2022, 2:36 PMDan Van Brunt
03/17/2022, 2:37 PMglobal.__basedir
or somethingDerek Kershner
03/17/2022, 2:38 PMDan Van Brunt
03/17/2022, 2:39 PMDerek Kershner
03/17/2022, 2:40 PMDan Van Brunt
03/17/2022, 2:45 PMnode_modules
explicitly since later versions of yarn3 do not have a node_modules folder. But thats pretty much out of scope for my brain while trying to also figure this mess out. 😄Derek Kershner
03/17/2022, 2:46 PMIt’s really hard, and I got it to function, so long as monorepos werent in play, but I dont recommend the pattern and have since abandoned the construct as the SST console does what it did.
Derek Kershner
03/17/2022, 2:47 PMnode_modules
just seems like a no-no in general, as even old timey commonjs
stopped you from doing so whenever possible.Dan Van Brunt
03/17/2022, 3:09 PMrootPath
is easy…. it seems the issue is more on how to get the TRUE _dirname of the current module. At least in yarn link
since it keeps resolving to the direct path on my pc vs the one that goes through the node__modules folder.
const root = scope.node.root as App
const rootPath = root.appPath
Dan Van Brunt
03/17/2022, 3:11 PMconst assets = path.join(__dirname, '../assets/social-cards')
const rootPath = root.appPath
const relative = path.relative(rootPath, assets)
const handler = path.join(relative, 'index.handler')
but these are the paths…
assets: '/Users/me/Projects/klick-packages/packages/cdk-constructs/assets/social-cards',
rootPath: '/Users/me/Projects/idx',
relative: '../klick-packages/packages/cdk-constructs/assets/social-cards',
handler: '../klick-packages/packages/cdk-constructs/assets/social-cards/index.handler'
Derek Kershner
03/17/2022, 3:12 PMDan Van Brunt
03/17/2022, 3:14 PMDerek Kershner
03/17/2022, 3:15 PMDan Van Brunt
03/17/2022, 3:16 PMDan Van Brunt
03/17/2022, 3:17 PMDan Van Brunt
03/17/2022, 3:48 PMDan Van Brunt
03/17/2022, 3:48 PMDan Van Brunt
03/17/2022, 4:24 PM2022-03-17T12:19:27.838-04:00
Copy
2022-03-17T16:19:27.835Z undefined ERROR Uncaught Exception
{
"errorType": "Runtime.MalformedHandlerName",
"errorMessage": "'../klick-packages/packages/cdk-constructs/assets/social-cards/index.handler' is not a valid handler name. Use absolute paths when specifying root directories in handler names.",
"stack": [
"Runtime.MalformedHandlerName: '../klick-packages/packages/cdk-constructs/assets/social-cards/index.handler' is not a valid handler name. Use absolute paths when specifying root directories in handler names.",
" at _throwIfInvalidHandler (/var/runtime/UserFunction.js:211:11)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:237:3)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1085:14)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
" at Module.load (internal/modules/cjs/loader.js:950:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
" at internal/main/run_main_module.js:17:47"
]
}
Dan Van Brunt
03/17/2022, 4:40 PMyarn start
and I get this in sst console…
npm ERR! code EACCES
16:32:35.401
npm ERR! syscall mkdir
npm ERR! path /var/task
npm ERR! errno EACCES
16:32:35.402
npm ERR! FetchError: Invalid response body while trying to fetch <https://registry.yarnpkg.com/aws-lambda-ric>: EACCES: permission denied, mkdir '/var/task'
npm ERR! at /Users/dvanbrunt/.nvm/versions/node/v16.13.1/lib/node_modules/npm/node_modules/minipass-fetch/lib/body.js:162:15
npm ERR! at async /Users/dvanbrunt/.nvm/versions/node/v16.13.1/lib/node_modules/npm/node_modules/libnpmexec/lib/index.js:106:12
npm ERR! at async Promise.all (index 0)
npm ERR! at async exec (/Users/dvanbrunt/.nvm/versions/node/v16.13.1/lib/node_modules/npm/node_modules/libnpmexec/lib/index.js:94:17)
npm ERR! FetchError: Invalid response body while trying to fetch <https://registry.yarnpkg.com/aws-lambda-ric>: EACCES: permission denied, mkdir '/var/task'
npm ERR! at /Users/dvanbrunt/.nvm/versions/node/v16.13.1/lib/node_modules/npm/node_modules/minipass-fetch/lib/body.js:162:15
npm ERR! at async /Users/dvanbrunt/.nvm/versions/node/v16.13.1/lib/node_modules/npm/node_modules/libnpmexec/lib/index.js:106:12
npm ERR! at async Promise.all (index 0)
npm ERR! at async exec (/Users/dvanbrunt/.nvm/versions/node/v16.13.1/lib/node_modules/npm/node_modules/libnpmexec/lib/index.js:94:17) {
npm ERR! code: 'EACCES',
npm ERR! errno: 'EACCES',
npm ERR! syscall: 'mkdir',
npm ERR! path: '/var/task',
npm ERR! type: 'system'
npm ERR! }
npm
16:32:35.403
ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.
Dan Van Brunt
03/17/2022, 4:42 PMDan Van Brunt
03/17/2022, 4:48 PMyarn deploy
same error… but I looked in the .build
folder and I see this… it appears as though there is no function code being added…. just the copyFiles
part with templates
Dan Van Brunt
03/17/2022, 4:55 PMDan Van Brunt
03/17/2022, 4:56 PMnode_modules/@klickmarketing/cdk-constructs/assets/social-cards/index.handler
• DOES bundle properly
• DOES set proper lambda handler
• DOES successfully run the lambda handler no issues
So it just seems the issue is figuring out how to change how things can be passed into sst.Function.handler or find a way to safely derive the relative path to where the module folder is.Dan Van Brunt
03/17/2022, 4:58 PMDan Van Brunt
03/18/2022, 2:57 AMyarn link
its a bit icky
const root = this.node.root as App
const { appPath } = root
const srcPath = 'node_modules/@klickmarketing/cdk-constructs/dist'
const dirname = path.relative(appPath, __dirname)
const relativeDirname = this.isLinking ? srcPath : dirname
const assets = path.join(relativeDirname, '../assets/social-cards')
const handler = path.join(assets, 'index.handler')
The above works for “non-linked” (isLinking defaults to false)
If you ARE linking…. you have to pass isLinking: true to the construct and it will use the hardcoded location of the assets. If you REALLY didn’t have a better option you could even allow for that hardcoded value to be passed in as well.
IT WORKS… just not purty for linkingFrank
RDS
construct. Function code lives in node_modules
and assets lives in user’s app.Frank
Function
to make this easier.Frank