Conditional import/export when using different environment

Viewed 298

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
0 Answers
Related