serverless-vpc-discovery 5.0.0 → 5.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -185,7 +185,7 @@ To run integration tests, set an environment variable TEST\_VPC\_NAME to the VPC
185
185
  ```
186
186
  export AWS_PROFILE=your_profile
187
187
  export TEST_VPC_NAME=vpc_name
188
- npm build
188
+ npm run build
189
189
  npm run integration-test
190
190
  ```
191
191
 
@@ -18,10 +18,20 @@ const client_ec2_1 = require("@aws-sdk/client-ec2");
18
18
  const globals_1 = __importDefault(require("../globals"));
19
19
  class EC2Wrapper {
20
20
  constructor(credentials) {
21
- this.ec2 = new client_ec2_1.EC2Client([{
22
- credentials,
23
- retryStrategy: globals_1.default.getRetryStrategy()
24
- }]);
21
+ this.ec2 = new client_ec2_1.EC2Client({
22
+ credentials,
23
+ region: globals_1.default.getRegion(),
24
+ retryStrategy: globals_1.default.getRetryStrategy()
25
+ });
26
+ }
27
+ /**
28
+ * Returns the promise that contains the vpc list
29
+ * @returns {Promise.<Vpc[]>}
30
+ */
31
+ getVpcs() {
32
+ return __awaiter(this, void 0, void 0, function* () {
33
+ return yield (0, utils_1.getAWSPagedResults)(this.ec2, "Vpcs", "NextToken", "NextToken", new client_ec2_1.DescribeVpcsCommand({}));
34
+ });
25
35
  }
26
36
  /**
27
37
  * Returns the promise that contains the vpc-id
@@ -1,7 +1,26 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  const util_retry_1 = require("@smithy/util-retry");
13
+ const credential_providers_1 = require("@aws-sdk/credential-providers");
4
14
  class Globals {
15
+ static getRegion() {
16
+ const slsRegion = Globals.options.region || Globals.serverless.service.provider.region;
17
+ return slsRegion || Globals.currentRegion || Globals.defaultRegion;
18
+ }
19
+ static getProfileCreds(profile) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ return yield (0, credential_providers_1.fromIni)({ profile })();
22
+ });
23
+ }
5
24
  static getRetryStrategy(attempts = 3, delay = 3000, backoff = 500) {
6
25
  return new util_retry_1.ConfiguredRetryStrategy(attempts, // max attempts.
7
26
  // This example sets the backoff at 500ms plus 3s per attempt.
@@ -11,4 +30,5 @@ class Globals {
11
30
  }
12
31
  }
13
32
  Globals.pluginName = "Serverless VPC Discovery";
33
+ Globals.defaultRegion = "us-east-1";
14
34
  exports.default = Globals;
package/dist/src/index.js CHANGED
@@ -16,10 +16,13 @@ const globals_1 = __importDefault(require("./globals"));
16
16
  const validation_1 = require("./validation");
17
17
  const schema_1 = require("./schema");
18
18
  const logging_1 = __importDefault(require("./logging"));
19
+ const node_config_provider_1 = require("@smithy/node-config-provider");
20
+ const config_resolver_1 = require("@smithy/config-resolver");
19
21
  class VPCPlugin {
20
22
  constructor(serverless, options, v3Utils) {
21
23
  this.serverless = serverless;
22
24
  globals_1.default.serverless = serverless;
25
+ globals_1.default.options = options;
23
26
  if (v3Utils === null || v3Utils === void 0 ? void 0 : v3Utils.log) {
24
27
  globals_1.default.v3Utils = v3Utils;
25
28
  }
@@ -37,7 +40,7 @@ class VPCPlugin {
37
40
  hookWrapper(lifecycleFunc) {
38
41
  return __awaiter(this, void 0, void 0, function* () {
39
42
  this.validateCustomVPCDiscoveryConfig();
40
- this.initResources();
43
+ yield this.initResources();
41
44
  return yield lifecycleFunc.call(this);
42
45
  });
43
46
  }
@@ -84,10 +87,46 @@ class VPCPlugin {
84
87
  * Setup AWS resources
85
88
  */
86
89
  initResources() {
87
- this.awsCredentials = this.serverless.providers.aws.getCredentials();
88
- this.awsCredentials.region = this.serverless.providers.aws.getRegion();
89
- const baseVPCDiscovery = this.serverless.service.custom ? this.serverless.service.custom.vpcDiscovery : null;
90
- this.lambdaFunction = new lambda_function_1.LambdaFunction(this.awsCredentials, baseVPCDiscovery);
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ // setup AWS resources
92
+ yield this.initSLSCredentials();
93
+ yield this.initAWSRegion();
94
+ const baseVPCDiscovery = this.serverless.service.custom ? this.serverless.service.custom.vpcDiscovery : null;
95
+ this.lambdaFunction = new lambda_function_1.LambdaFunction(globals_1.default.credentials, baseVPCDiscovery);
96
+ // start of the legacy AWS SDK V2 creds support
97
+ // TODO: remove it in case serverless will add V3 support
98
+ try {
99
+ yield this.lambdaFunction.ec2Wrapper.getVpcs();
100
+ }
101
+ catch (error) {
102
+ if (error.message.includes("Could not load credentials from any providers")) {
103
+ globals_1.default.credentials = this.serverless.providers.aws.getCredentials();
104
+ this.lambdaFunction = new lambda_function_1.LambdaFunction(globals_1.default.credentials, baseVPCDiscovery);
105
+ }
106
+ }
107
+ });
108
+ }
109
+ /**
110
+ * Init AWS credentials based on sls `provider.profile`
111
+ */
112
+ initSLSCredentials() {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const slsProfile = globals_1.default.options["aws-profile"] || globals_1.default.serverless.service.provider.profile;
115
+ globals_1.default.credentials = slsProfile ? yield globals_1.default.getProfileCreds(slsProfile) : null;
116
+ });
117
+ }
118
+ /**
119
+ * Init AWS current region based on Node options
120
+ */
121
+ initAWSRegion() {
122
+ return __awaiter(this, void 0, void 0, function* () {
123
+ try {
124
+ globals_1.default.currentRegion = yield (0, node_config_provider_1.loadConfig)(config_resolver_1.NODE_REGION_CONFIG_OPTIONS, config_resolver_1.NODE_REGION_CONFIG_FILE_OPTIONS)();
125
+ }
126
+ catch (err) {
127
+ logging_1.default.logInfo("Node region was not found.");
128
+ }
129
+ });
91
130
  }
92
131
  /**
93
132
  * Updates functions vpc config
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "serverless-vpc-discovery",
3
- "version": "5.0.0",
3
+ "version": "5.0.2",
4
4
  "engines": {
5
5
  "node": ">=14"
6
6
  },
@@ -77,6 +77,9 @@
77
77
  },
78
78
  "dependencies": {
79
79
  "@aws-sdk/client-ec2": "^3.467.0",
80
+ "@aws-sdk/credential-providers": "^3.495.0",
81
+ "@smithy/config-resolver": "^2.1.0",
82
+ "@smithy/node-config-provider": "^2.2.0",
80
83
  "@smithy/smithy-client": "^2.1.18",
81
84
  "@smithy/util-retry": "^2.0.8",
82
85
  "ts-md5": "^1.3.1"