Skip to content

Commit 0d98870

Browse files
abetomoDeviaVir
authored andcommitted
Add file to configure aws authentication settings (#482)
1 parent 8bd84a4 commit 0d98870

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

lib/aws.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
'use strict'
2+
3+
const aws = require('aws-sdk')
4+
const proxy = require('proxy-agent')
5+
6+
module.exports = {
7+
sdk: aws,
8+
updateConfig (config, region) {
9+
const awsSecurity = { region: region }
10+
11+
if (config.profile) {
12+
aws.config.credentials = new aws.SharedIniFileCredentials({
13+
profile: config.profile
14+
})
15+
} else {
16+
awsSecurity.accessKeyId = config.accessKey
17+
awsSecurity.secretAccessKey = config.secretKey
18+
}
19+
20+
if (config.sessionToken) {
21+
awsSecurity.sessionToken = config.sessionToken
22+
}
23+
24+
if (config.deployTimeout) {
25+
aws.config.httpOptions.timeout = parseInt(config.deployTimeout)
26+
}
27+
28+
if (config.proxy) {
29+
aws.config.httpOptions.agent = proxy(config.proxy)
30+
}
31+
32+
if (config.endpoint) {
33+
aws.config.endpoint = config.endpoint
34+
}
35+
36+
aws.config.update(awsSecurity)
37+
}
38+
}

lib/main.js

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
const path = require('path')
44
const os = require('os')
5-
const aws = require('aws-sdk')
5+
const aws = require(path.join(__dirname, 'aws'))
66
const { exec, execSync, execFile } = require('child_process')
77
const fs = require('fs-extra')
88
const klaw = require('klaw')
99
const packageJson = require(path.join(__dirname, '..', 'package.json'))
1010
const minimatch = require('minimatch')
1111
const archiver = require('archiver')
1212
const dotenv = require('dotenv')
13-
const proxy = require('proxy-agent')
1413
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
1514
const S3Events = require(path.join(__dirname, 's3_events'))
1615
const S3Deploy = require(path.join(__dirname, 's3_deploy'))
@@ -822,51 +821,20 @@ they may not work as expected in the Lambda environment.
822821
})
823822
}
824823

825-
_awsConfigUpdate (program, region) {
826-
const awsSecurity = { region: region }
827-
828-
if (program.profile) {
829-
aws.config.credentials = new aws.SharedIniFileCredentials({
830-
profile: program.profile
831-
})
832-
} else {
833-
awsSecurity.accessKeyId = program.accessKey
834-
awsSecurity.secretAccessKey = program.secretKey
835-
}
836-
837-
if (program.sessionToken) {
838-
awsSecurity.sessionToken = program.sessionToken
839-
}
840-
841-
if (program.deployTimeout) {
842-
aws.config.httpOptions.timeout = parseInt(program.deployTimeout)
843-
}
844-
845-
if (program.proxy) {
846-
aws.config.httpOptions.agent = proxy(program.proxy)
847-
}
848-
849-
if (program.endpoint) {
850-
aws.config.endpoint = program.endpoint
851-
}
852-
853-
aws.config.update(awsSecurity)
854-
}
855-
856824
_isFunctionDoesNotExist (err) {
857825
return err.code === 'ResourceNotFoundException' &&
858826
!!err.message.match(/^Function not found:/)
859827
}
860828

861829
_deployToRegion (program, params, region, buffer) {
862-
this._awsConfigUpdate(program, region)
830+
aws.updateConfig(program, region)
863831

864832
console.log('=> Reading event source file to memory')
865833
const eventSourceList = this._eventSourceList(program)
866834

867835
return Promise.resolve().then(() => {
868836
if (this._isUseS3(program)) {
869-
const s3Deploy = new S3Deploy(aws, region)
837+
const s3Deploy = new S3Deploy(aws.sdk, region)
870838
return s3Deploy.putPackage(params, region, buffer)
871839
}
872840
return null
@@ -880,13 +848,13 @@ they may not work as expected in the Lambda environment.
880848
}
881849
console.log(params)
882850

883-
const lambda = new aws.Lambda({
851+
const lambda = new aws.sdk.Lambda({
884852
region: region,
885853
apiVersion: '2015-03-31'
886854
})
887-
const scheduleEvents = new ScheduleEvents(aws, region)
888-
const s3Events = new S3Events(aws, region)
889-
const cloudWatchLogs = new CloudWatchLogs(aws, region)
855+
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
856+
const s3Events = new S3Events(aws.sdk, region)
857+
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
890858

891859
// Checking function
892860
return lambda.getFunction({

0 commit comments

Comments
 (0)