Skip to content

Commit 7dcd4e8

Browse files
abetomoDeviaVir
authored andcommitted
Implement deployment using S3 (Create a bucket for each region.) (#455)
* Add class for uploading deploy package to S3 * Sort regions * Fix to set region at client creation Currently, a region is set to the aws instance to be used in common. Since it may not be as expected as it is in parallel processing, it is set when each client is created. * Implement deployment function using S3 Also create buckets * Update README
1 parent 6500e23 commit 7dcd4e8

File tree

7 files changed

+50
-73
lines changed

7 files changed

+50
-73
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ SRC_DIRECTORY // (default: '')
9393
DEPLOY_TIMEOUT // (default: '120000')
9494
DOCKER_IMAGE // (default: '')
9595
DEPLOY_ZIPFILE // (default: '')
96-
DEPLOY_S3_BUCKET // (default: '')
97-
DEPLOY_S3_KEY // (default: '')
96+
DEPLOY_USE_S3 // (default: false)
9897
AWS_DLQ_TARGET_ARN // (default: not set)
9998
```
10099

@@ -190,8 +189,7 @@ Options:
190189
-D, --prebuiltDirectory [] Prebuilt directory
191190
-T, --deployTimeout [120000] Deploy Timeout
192191
-z, --deployZipfile [] Deploy zipfile
193-
-B, --deployS3Bucket [] Use S3 to deploy. Specify it with `--deployS3Key`. (default: )
194-
-O, --deployS3Key [] Use S3 to deploy. Specify it with `--deployS3Bucket`. (default: )
192+
-B, --deployUseS3 [] Use S3 to deploy.
195193
-y, --proxy [] Proxy server
196194
```
197195

bin/node-lambda

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ const SRC_DIRECTORY = process.env.SRC_DIRECTORY || ''
4646
const DEPLOY_TIMEOUT = process.env.DEPLOY_TIMEOUT || 120000
4747
const DOCKER_IMAGE = process.env.DOCKER_IMAGE || ''
4848
const DEPLOY_ZIPFILE = process.env.DEPLOY_ZIPFILE || ''
49-
const DEPLOY_S3_BUCKET = process.env.DEPLOY_S3_BUCKET || ''
50-
const DEPLOY_S3_KEY = process.env.DEPLOY_S3_KEY || ''
49+
const DEPLOY_USE_S3 = process.env.DEPLOY_USE_S3 || false
5150
const AWS_KMS_KEY_ARN = process.env.AWS_KMS_KEY_ARN || ''
5251
const AWS_DLQ_TARGET_ARN = (() => {
5352
// You can clear the setting by passing an empty string
@@ -103,10 +102,7 @@ program
103102
.option('-D, --prebuiltDirectory [PREBUILT_DIRECTORY]', 'Prebuilt directory', PREBUILT_DIRECTORY)
104103
.option('-T, --deployTimeout [DEPLOY_TIMEOUT]', 'Deploy Timeout', DEPLOY_TIMEOUT)
105104
.option('-z, --deployZipfile [DEPLOY_ZIPFILE]', 'Deploy zipfile', DEPLOY_ZIPFILE)
106-
.option('-B, --deployS3Bucket [DEPLOY_S3_BUCKET]',
107-
'Use S3 to deploy. Specify it with `--deployS3Key`.', DEPLOY_S3_BUCKET)
108-
.option('-O, --deployS3Key [DEPLOY_S3_KEY]',
109-
'Use S3 to deploy. Specify it with `--deployS3Bucket`.', DEPLOY_S3_KEY)
105+
.option('-B, --deployUseS3 [DEPLOY_USE_S3]', 'Use S3 to deploy.', DEPLOY_USE_S3)
110106
.option('-y, --proxy [PROXY]', 'Proxy server', PROXY)
111107
.action((prg) => lambda.deploy(prg))
112108

lib/cloudwatch_logs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

33
class CloudWatchLogs {
4-
constructor (aws) {
4+
constructor (aws, region) {
55
// Authenticated `aws` object in `lib/main.js`
66
this.lambda = new aws.Lambda({
7+
region: region,
78
apiVersion: '2015-03-31'
89
})
910
this.cloudwatchlogs = new aws.CloudWatchLogs({

lib/main.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const dotenv = require('dotenv')
1313
const proxy = require('proxy-agent')
1414
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
1515
const S3Events = require(path.join(__dirname, 's3_events'))
16+
const S3Deploy = require(path.join(__dirname, 's3_deploy'))
1617
const CloudWatchLogs = require(path.join(__dirname, 'cloudwatch_logs'))
1718

1819
const AWSXRay = require('aws-xray-sdk-core')
@@ -177,10 +178,10 @@ so you can easily test run multiple events.
177178
}
178179

179180
_isUseS3 (program) {
180-
return program.deployS3Bucket != null &&
181-
program.deployS3Key != null &&
182-
program.deployS3Bucket.length > 0 &&
183-
program.deployS3Key.length > 0
181+
if (typeof program.deployUseS3 === 'boolean') {
182+
return program.deployUseS3
183+
}
184+
return program.deployUseS3 === 'true'
184185
}
185186

186187
_params (program, buffer) {
@@ -219,8 +220,8 @@ so you can easily test run multiple events.
219220

220221
if (this._isUseS3(program)) {
221222
params.Code = {
222-
S3Bucket: program.deployS3Bucket,
223-
S3Key: program.deployS3Key
223+
S3Bucket: null,
224+
S3Key: null
224225
}
225226
} else {
226227
params.Code = { ZipFile: buffer }
@@ -779,23 +780,6 @@ they may not work as expected in the Lambda environment.
779780
})
780781
}
781782

782-
_s3PutObject (program, region, buffer) {
783-
this._awsConfigUpdate(program, region)
784-
const s3 = new aws.S3()
785-
return new Promise((resolve, reject) => {
786-
const params = {
787-
Body: buffer,
788-
Bucket: program.deployS3Bucket,
789-
Key: program.deployS3Key
790-
}
791-
console.log(`=> Uploading zip file to S3 ${region}`)
792-
s3.putObject(params, (err, data) => {
793-
if (err) reject(err)
794-
resolve(data)
795-
})
796-
})
797-
}
798-
799783
_awsConfigUpdate (program, region) {
800784
const awsSecurity = { region: region }
801785

@@ -832,23 +816,35 @@ they may not work as expected in the Lambda environment.
832816
!!err.message.match(/^Function not found:/)
833817
}
834818

835-
_deployToRegion (program, params, region) {
819+
_deployToRegion (program, params, region, buffer) {
820+
this._awsConfigUpdate(program, region)
821+
836822
console.log('=> Reading event source file to memory')
837823
const eventSourceList = this._eventSourceList(program)
838824

839825
return Promise.resolve().then(() => {
826+
if (this._isUseS3(program)) {
827+
const s3Deploy = new S3Deploy(aws, region)
828+
return s3Deploy.putPackage(params, region, buffer)
829+
}
830+
return null
831+
}).then((code) => {
832+
if (code != null) params.Code = code
833+
}).then(() => {
840834
if (!this._isUseS3(program)) {
841835
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
842836
} else {
843837
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
844838
}
845839
console.log(params)
846840

847-
this._awsConfigUpdate(program, region)
848-
const lambda = new aws.Lambda({ apiVersion: '2015-03-31' })
849-
const scheduleEvents = new ScheduleEvents(aws)
850-
const s3Events = new S3Events(aws)
851-
const cloudWatchLogs = new CloudWatchLogs(aws)
841+
const lambda = new aws.Lambda({
842+
region: region,
843+
apiVersion: '2015-03-31'
844+
})
845+
const scheduleEvents = new ScheduleEvents(aws, region)
846+
const s3Events = new S3Events(aws, region)
847+
const cloudWatchLogs = new CloudWatchLogs(aws, region)
852848

853849
// Checking function
854850
return lambda.getFunction({
@@ -945,15 +941,17 @@ they may not work as expected in the Lambda environment.
945941
const regions = program.region.split(',')
946942
return this._archive(program).then((buffer) => {
947943
console.log('=> Reading zip file to memory')
948-
if (this._isUseS3(program)) {
949-
return this._s3PutObject(program, regions[0], buffer)
950-
}
951944
return buffer
952945
}).then((buffer) => {
953946
const params = this._params(program, buffer)
954947

955948
return Promise.all(regions.map((region) => {
956-
return this._deployToRegion(program, params, region)
949+
return this._deployToRegion(
950+
program,
951+
params,
952+
region,
953+
this._isUseS3(program) ? buffer : null
954+
)
957955
})).then(results => {
958956
this._printDeployResults(results, true)
959957
})

lib/s3_events.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
* Put the Notification Configuration in the existing Bucket.
66
*/
77
class S3Events {
8-
constructor (aws) {
8+
constructor (aws, region) {
99
// Authenticated `aws` object in `lib/main.js`
1010
this.lambda = new aws.Lambda({
11+
region: region,
1112
apiVersion: '2015-03-31'
1213
})
1314
this.s3 = new aws.S3({
15+
region: region,
1416
apiVersion: '2006-03-01'
1517
})
1618
}

lib/schedule_events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict'
22

33
class ScheduleEvents {
4-
constructor (aws) {
4+
constructor (aws, region) {
55
// Authenticated `aws` object in `lib/main.js`
66
this.lambda = new aws.Lambda({
7+
region: region,
78
apiVersion: '2015-03-31'
89
})
910
this.cloudwatchevents = new aws.CloudWatchEvents({

test/main.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,16 @@ describe('lib/main', function () {
194194

195195
describe('_isUseS3', () => {
196196
it('=== true', () => {
197-
assert.isTrue(lambda._isUseS3({
198-
deployS3Bucket: 'bucket',
199-
deployS3Key: 'key'
200-
}))
197+
assert.isTrue(lambda._isUseS3({deployUseS3: true}))
198+
assert.isTrue(lambda._isUseS3({deployUseS3: 'true'}))
201199
})
202200

203201
it('=== false', () => {
204202
[
205203
{},
206-
{deployS3Bucket: '', deployS3Key: ''},
207-
{deployS3Bucket: 'bucket'},
208-
{deployS3Key: 'key'}
204+
{deployUseS3: false},
205+
{deployUseS3: 'false'},
206+
{deployUseS3: 'foo'}
209207
].forEach((params) => {
210208
assert.isFalse(lambda._isUseS3(params), params)
211209
})
@@ -310,15 +308,12 @@ describe('lib/main', function () {
310308
})
311309

312310
it('Use S3 deploy', () => {
313-
const params = lambda._params(Object.assign({
314-
deployS3Bucket: 'S3Bucket-value',
315-
deployS3Key: 'S3Key-value'
316-
}, program), 'Buffer')
311+
const params = lambda._params(Object.assign({deployUseS3: true}, program), 'Buffer')
317312
assert.deepEqual(
318313
params.Code,
319314
{
320-
S3Bucket: 'S3Bucket-value',
321-
S3Key: 'S3Key-value'
315+
S3Bucket: null,
316+
S3Key: null
322317
}
323318
)
324319
})
@@ -1246,20 +1241,6 @@ describe('lib/main', function () {
12461241
})
12471242
})
12481243

1249-
describe('Lambda.prototype._s3PutObject()', () => {
1250-
it('simple test with mock', () => {
1251-
disableLog()
1252-
const params = {
1253-
deployS3Bucket: 'test',
1254-
deployS3Key: 'test'
1255-
}
1256-
return lambda._s3PutObject(params, 'us-east-1', 'buffer').then((result) => {
1257-
enableLog()
1258-
assert.deepEqual(result, {'test': 'putObject'})
1259-
})
1260-
})
1261-
})
1262-
12631244
describe('Lambda.prototype._deployToRegion()', () => {
12641245
it('simple test with mock', () => {
12651246
const params = lambda._params(program, null)

0 commit comments

Comments
 (0)