Kujtim Hoxha
08/13/2021, 10:44 AMapp.setDefaultFunctionProps
but it looks like you can only do that within the context of a stack, I think there should be an easier way to add lambda layers for all functions in all my stacks (I ended up just adding it to one of the stacks that the others are dependent on)Frank
LayerVersion.fromLayerVersionArn(this, "Layer", layerArn);
Or were you creating a new layer?
new lambda.LayerVersion(this, 'Layer', {
code: lambda.Code.fromAsset(...),
});
Kujtim Hoxha
08/13/2021, 3:39 PMKujtim Hoxha
08/13/2021, 3:39 PMFrank
Frank
app.setDefaultFunctionProps
looked like?Kujtim Hoxha
08/13/2021, 3:41 PMconst sentryLayer = lambda.LayerVersion.fromLayerVersionArn(
this,
'SentryLayer',
'arn:aws:lambda:us-east-1:943013980633:layer:SentryNodeServerlessSDK:26'
);
app.setDefaultFunctionProps({
layers: process.env.IS_LOCAL ? [] : [sentryLayer],
});
Frank
this
isn’t a Stack right. Was that the error you were getting?Kujtim Hoxha
08/13/2021, 3:43 PMapp
Kujtim Hoxha
08/13/2021, 3:43 PMKujtim Hoxha
08/13/2021, 3:44 PMImport at 'SentryLayer' should be created in the scope of a Stack, but no Stack found
Frank
const sentryLayer = (stack) => lambda.LayerVersion.fromLayerVersionArn(
stack,
'SentryLayer',
'arn:aws:lambda:us-east-1:943013980633:layer:SentryNodeServerlessSDK:26'
);
app.setDefaultFunctionProps((stack) => ({
layers: process.env.IS_LOCAL ? [] : [sentryLayer(stack)],
}));
Kujtim Hoxha
08/13/2021, 3:45 PMKujtim Hoxha
08/13/2021, 3:45 PMJanessa
09/02/2021, 4:52 PMSentry.init({})
in each lambda handler?Kujtim Hoxha
09/02/2021, 4:57 PMapp.setDefaultFunctionProps((stack) => {
stackLayer[stack.artifactId] =
stackLayer[stack.artifactId] || sentryLayer(stack);
return {
layers: process.env.IS_LOCAL ? [] : [stackLayer[stack.artifactId]],
};
});
And for the handlers
if (process.env.NODE_ENV !== 'development') {
// x-ray tracing for all aws services
// eslint-disable-next-line @typescript-eslint/no-var-requires
captureAWS(require('aws-sdk'));
const Sentry = require('@sentry/serverless');
Sentry.AWSLambda.init({
dsn: process.env.SENTRY_DNS,
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: process.env.SENTRY_TRACES_SAMPLE_RATE,
});
}
Kujtim Hoxha
09/02/2021, 4:58 PMNODE_ENV
inside your functions just add thisJanessa
09/02/2021, 4:58 PMKujtim Hoxha
09/02/2021, 5:00 PMconst sentryLayer = (stack: Stack) => {
return lambda.LayerVersion.fromLayerVersionArn(
stack,
'SentryLayer',
'arn:aws:lambda:us-east-1:943013980633:layer:SentryNodeServerlessSDK:26'
);
};
const stackLayer: { [key: string]: ILayerVersion } = {};
app.setDefaultFunctionProps((stack) => {
stackLayer[stack.artifactId] =
stackLayer[stack.artifactId] || sentryLayer(stack);
return {
layers: process.env.IS_LOCAL ? [] : [stackLayer[stack.artifactId]],
};
});
^ here is the full codeJanessa
09/02/2021, 5:33 PMexport const lambdaHandler: Handler = (async (event: Event, context: Context) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const Sentry = require('@sentry/serverless');
Sentry.AWSLambda.init({
dsn: process.env.SENTRY_DNS,
// We recommend adjusting this value in production, or using tracesSampler
// for finer control
tracesSampleRate: 1.0 //process.env.SENTRY_TRACES_SAMPLE_RATE,
});
try {
randomFunction()
} catch (error) {
console.log("Error!")
Sentry.captureException(error)
}
})
const randomFunction = () => {
throw new Error("Function not implemented.");
}
Kujtim Hoxha
09/02/2021, 5:34 PMSENTRY_DNS
setup ?Janessa
09/02/2021, 5:35 PMKujtim Hoxha
09/02/2021, 5:36 PMSentry.captureException(error)
try doing Sentry.AWSLambda.init
outside of your handler function and wrap the handler using
if (process.env.NODE_ENV !== 'development') {
handler = Sentry.AWSLambda.wrapHandler(handler);
}
Janessa
09/02/2021, 5:37 PMKujtim Hoxha
09/02/2021, 5:38 PMKujtim Hoxha
09/02/2021, 5:38 PMJanessa
09/02/2021, 5:39 PMKujtim Hoxha
09/02/2021, 5:40 PMJanessa
09/02/2021, 5:41 PMKujtim Hoxha
09/02/2021, 5:41 PMKujtim Hoxha
09/02/2021, 5:42 PMmiddy
Kujtim Hoxha
09/02/2021, 5:42 PMexport const wrapper = (handler: LambdaHandler, logger?: Logger): any => {
if (process.env.NODE_ENV !== 'development') {
handler = Sentry.AWSLambda.wrapHandler(handler);
}
return middy(handler)
.use(warmerMiddleware())
.use(httpEventNormalizer())
.use(httpHeaderNormalizer())
.use(httpJsonBodyParser())
.use(httpUrlEncodeBodyParser())
.use(authMiddleware())
.use(httpCors())
.use(errorMiddleware(logger))
.use(httpResponseSerializer());
};
And now all my functions look like
export const handler = wrapper(async (event: APIGatewayProxyEvent) => {
....
})