2

I'm currently developing a Lambda function using node14 on AWS, but I have an issue when I want to develop it locally.

In my dev environment I have a corporate proxy needed to connect to AWS. In this case, I use aws-sdk-proxy library. However, I don't want to use this package while in production (as Lambda already has the aws-sdk injected in lambda core).

So, I created this snippet to perform the switch between the 2 environments:

// aws.js
import AWSPROD from 'aws-sdk'
import AWSDEV from 'aws-sdk-proxy'
import config from '../lib/Config.js'
import logger from './logger.js'

let AWS = null

if (config.get('ENVIRONMENT') === 'dev') {
  logger.debug('[DEV] Using aws-sdk-proxy')
  AWS = AWSDEV
} else {
  AWS = AWSPROD
}

export default AWS

Not so clean, but it works. With this code, I can perform this:

import AWS from './aws.js'

Now the problem is that this code implies that I must provide aws-sdk-proxy into the "prod" dependencies of my package.json.

I think it breaks performance of the lambda as the code raised 9Mo (2Mo without), but I wish to keep this way of calling "AWS" SDK [the 2d code block].

I tried to use required or dynamic import but none of these solutions work.

Do you have any advice to improve my code?

PS:

  • The code is transpiled to ES5 using Babel to fit Lambda requirements
  • aws-sdk-proxy library must stay in devDependencies as it's a dev dependency
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Possibly relevant: [Conditional import to switch implementations](https://stackoverflow.com/questions/35919878/conditional-import-to-switch-implementations). This other post suggests you can use `require()` for conditional import in TypeScript. And, dynamic imports using `import()` described [here](https://mitya.uk/articles/javascript-modules-conditional-import). – jfriend00 Jun 25 '21 at 15:34
  • Thank you for your reply. In fact, for your first suggestion, I don't know how I can mix Typescript and ES* code (.js), should I "compile" it to ES6 before using ? For your second suggestion, I already tried using **import()** but I can't get a working code as **import()** is non-blocking and won't fit with **export** (I got `function update() is undefined` [I think the module is not fully loaded]) – Jean-Philippe R. Jun 26 '21 at 08:35
  • In that first link `require()` is allowed right in your TypeScript. I've never done that myself, but that's what the article implies. You can NEVER export something retrieved from a dynamic import with `import()`. ESM modules just don't work that way. Exports have to be static. – jfriend00 Jun 26 '21 at 09:07

0 Answers0