Sam Hulick
08/19/2021, 4:57 PMSomething went wrong installing the "sharp" module
Cannot find module '../build/Release/sharp-darwin-x64.node'
Require stack:
- /Volumes/SuperData/Sites/reelcrafter/v2-microservices/.build/src/services/media-processor/functions/get-metadata.js
- /Volumes/SuperData/Sites/reelcrafter/v2-microservices/node_modules/@serverless-stack/cli/scripts/util/bootstrap.js
my theory is that the Lambda code is running locally on the Darwin architecture, but it’s using the Lambda layer which is Amazon Linux arch. how can I solve this?thdxr
08/19/2021, 4:58 PMthdxr
08/19/2021, 4:58 PMSam Hulick
08/19/2021, 4:59 PMbuild/Release
folder along w/ the Amazon one, then upload that to the layerSam Hulick
08/19/2021, 4:59 PMthdxr
08/19/2021, 5:09 PMSam Hulick
08/19/2021, 5:10 PMSHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux sharp
Sam Hulick
08/19/2021, 5:10 PMplatform=linux,darwin
thdxr
08/19/2021, 5:11 PMSam Hulick
08/19/2021, 5:33 PMSam Hulick
08/19/2021, 5:35 PMsharp-mac
. and then in my Lambda func:
const { default: sharp } = await import(process.env.IS_LOCAL ? 'sharp-mac' : 'sharp')
Sam Hulick
08/19/2021, 5:36 PMSam Hulick
08/19/2021, 5:39 PM...(process.env.IS_LOCAL ? {} : { layers: [sharpLayer] }),
Sam Hulick
08/19/2021, 5:44 PMSam Hulick
08/19/2021, 6:03 PMyarn start
and a Lambda func imports a dependency.. that’s looking for the dependency where, exactly? is it using my node_modules folder? because I don’t see the bundled code in .build/cdk.out
thdxr
08/19/2021, 6:05 PMnode_modules
Sam Hulick
08/19/2021, 6:10 PMCannot find module '../build/Release/sharp-darwin-x64.node'
Sam Hulick
08/19/2021, 6:10 PMthdxr
08/19/2021, 6:14 PMSam Hulick
08/19/2021, 6:14 PMthdxr
08/19/2021, 6:14 PMSam Hulick
08/19/2021, 6:16 PMlambdaFunction: new sst.Function(stack, 'GetMetadataFunc', {
functionName: `${stack.stackName}-audioProcGetMetadata`,
handler: `${srcPath}/get-metadata.main`,
...(process.env.IS_LOCAL ? {} : { layers: [sharpLayer] }),
permissions: [[props!.uploadBucket.s3Bucket, 'grantRead']],
}),
and just for good measure, in the global SST app:
bundle: {
// sharp is a binary, and we have it in a Lambda layer, so exclude it
externalModules: [...(process.env.IS_LOCAL ? [] : ['sharp']), 'knex'],
loader: {
'.node': 'binary',
},
},
thdxr
08/19/2021, 6:20 PMSam Hulick
08/19/2021, 6:21 PMthdxr
08/19/2021, 6:24 PMthdxr
08/19/2021, 6:24 PMthdxr
08/19/2021, 6:26 PMnode_modules/sharp/build/Release
is sharp-linux-x64.nodethdxr
08/19/2021, 6:27 PMsrcPath
set to something?Sam Hulick
08/19/2021, 6:28 PMSam Hulick
08/19/2021, 6:30 PMsrcPath
in tsconfig.json?thdxr
08/19/2021, 6:32 PMthdxr
08/19/2021, 6:32 PMSam Hulick
08/19/2021, 6:32 PMSam Hulick
08/19/2021, 6:33 PMSam Hulick
08/19/2021, 6:33 PMSam Hulick
08/19/2021, 6:34 PMSam Hulick
08/19/2021, 6:34 PMthdxr
08/19/2021, 6:35 PMchild/node_modules
thdxr
08/19/2021, 6:37 PMsrc/core
and src/services
in my mono repo. In app.setDefaultFunctionProps
I set srcPath: "src/services"
and then define my handler
relative to that. That will run all the esbuild stuff inside the srcPath which is what you wantSam Hulick
08/19/2021, 6:43 PMSam Hulick
08/19/2021, 6:57 PMpackage.json
in src/services
? cuz there’s nothing there but foldersthdxr
08/19/2021, 6:58 PMSam Hulick
08/19/2021, 6:58 PMSam Hulick
08/19/2021, 6:59 PM<project root>/package.json
, <project root>/node_modules
. and the package.json at the root defines services/*
as workspacesSam Hulick
08/19/2021, 6:59 PMservices/media-processor/package.json
thdxr
08/19/2021, 6:59 PMservices/media-processor
- I only have one subpackage for all my services not one per serviceSam Hulick
08/19/2021, 7:00 PMsrcPath
defined at the stack level, per function, right? just for the media processor Lambdasthdxr
08/19/2021, 7:01 PMSam Hulick
08/19/2021, 7:08 PMrest-api
service instead, with just a dummy endpoint I can easily call to test.
'GET /sentry-test': {
function: {
handler: 'functions/sentry-test.main',
srcPath: 'src/services/rest-api',
},
authorizationType: sst.ApiAuthorizationType.NONE,
},
Sam Hulick
08/19/2021, 7:08 PMservices/rest-api
Sam Hulick
08/19/2021, 7:10 PMthdxr
08/19/2021, 7:23 PMSam Hulick
08/19/2021, 7:23 PMyarn start
is rather slow, and it bombed a few times with errorsSam Hulick
08/19/2021, 7:23 PMSam Hulick
08/19/2021, 7:25 PMsrcPath
on a stack levelthdxr
08/19/2021, 7:26 PMapp.setDefaultFunctionProps
as long as you do it in each stackthdxr
08/19/2021, 7:26 PMSam Hulick
08/19/2021, 7:29 PMSam Hulick
08/19/2021, 7:30 PMSam Hulick
08/19/2021, 7:31 PMSam Hulick
08/19/2021, 7:32 PMNote that for TypeScript functions, if theI have my tsconfig at project root onlyis not the project root, SST expects thesrcPath
to be in this directory.tsconfig.json
Sam Hulick
08/19/2021, 7:32 PMSam Hulick
08/19/2021, 7:32 PMOnly applicable if theoption is set tobundle
.false
Sam Hulick
08/19/2021, 7:33 PMSam Hulick
08/19/2021, 7:33 PMsrcPath
was crucial for meSam Hulick
08/19/2021, 7:37 PMsrc/services/*
, you should always have a srcPath
for each func set to the proper services subfolder, right?thdxr
08/19/2021, 7:43 PMSam Hulick
08/19/2021, 7:43 PMapp.setDefaultFunctionProps({
srcPath: 'src/services/media-processor',
});
but oddly enough, I get an error:
Error: Cannot find a handler file for "src/services/graphql/functions/transcode-audio.main".
thdxr
08/19/2021, 7:44 PMthdxr
08/19/2021, 7:44 PMSam Hulick
08/19/2021, 7:44 PMSam Hulick
08/19/2021, 7:44 PMSam Hulick
08/19/2021, 7:47 PMsrc/services/auth
.. in each service folder. ughSam Hulick
08/19/2021, 7:47 PMthdxr
08/19/2021, 7:48 PMcore
vs service
and not so much service1
vs service2
Sam Hulick
08/19/2021, 7:49 PMthdxr
08/19/2021, 7:49 PMcore
vs service
thdxr
08/19/2021, 7:49 PMSam Hulick
08/19/2021, 7:49 PMthdxr
08/19/2021, 7:50 PMsrc/core
that contains all my domains and functions inside them and then src/services
which contains lambdas that import from src/core
thdxr
08/19/2021, 7:51 PMsrc/core
and having services
simply call that functionality in lambdasSam Hulick
08/19/2021, 7:51 PMsrc/core
is your supporting functions for the CoreStack in your infra, right?Sam Hulick
08/19/2021, 7:51 PMsrc/core
thdxr
08/19/2021, 7:51 PMsrc/services
Sam Hulick
08/19/2021, 7:52 PMsrc/core
then? sorry, I’m running on fumes the past 24 hrsthdxr
08/19/2021, 7:54 PMsrc/core
I have billing, bus, user
modules. user
contains functions like from_id
from_email
update_profile(...)
which all just talks to cognito. billing
contains functions like create
which creates a stripe customer. Then in src/services
I have lambdas that react to a new Cognito user and publish an event bus.publish
and another lambda subscribed to that event that calls billing.create
to create a new stripe customer whenever a new cognito user is createdthdxr
08/19/2021, 7:55 PMsrc/core
and mostly glue in src/services
Sam Hulick
08/19/2021, 7:55 PMsrc/services
is just Lambda handlers that import & run code from coreSam Hulick
08/19/2021, 7:56 PMsrc/core
and src/services
, each with their own package.json/tsconfig/etcthdxr
08/19/2021, 7:56 PMSam Hulick
08/19/2021, 7:57 PMSam Hulick
08/19/2021, 7:57 PMSam Hulick
08/19/2021, 7:58 PMthdxr
08/19/2021, 7:59 PMthdxr
08/19/2021, 8:00 PMSam Hulick
08/19/2021, 8:00 PMSam Hulick
08/19/2021, 8:01 PMsrc/services
.. and do you have that broken out by stack/service? src/services/A
, src/services/B
? I mean just folders, not as subpackagesthdxr
08/19/2021, 8:01 PMthdxr
08/19/2021, 8:01 PMSam Hulick
08/19/2021, 8:03 PMSam Hulick
08/19/2021, 8:03 PMSam Hulick
08/19/2021, 8:05 PMcore
can have its own packages all related to biz logic, project root packages can be 100% about SST/CDK, and then services
would just be Lambda related stuff like TS types for handlers or whateverSam Hulick
08/19/2021, 8:06 PMSam Hulick
08/19/2021, 8:07 PMthdxr
08/19/2021, 8:08 PMTed Mader
09/19/2021, 12:04 AMdeploy
script:
"deploy": "SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm_config_arch=x64 npm_config_platform=linux sst deploy",
2. Set my bundle
config:
defaultFunctionProps: {
// Include `sharp-linux-x64.node` binary needed in Lambda
bundle: {
nodeModules: ['sharp'],
loader: {
'.node': 'binary',
},
},
Ted Mader
09/19/2021, 12:04 AMTed Mader
09/19/2021, 12:06 AMSam Hulick
09/19/2021, 1:07 AM