Skip to content

Commit c8c8686

Browse files
abetomoDeviaVir
authored andcommitted
Implemente to specify bucket name of S3 (#458)
As options increase too much, we do not add it to the options of the command. Set to .env as follows. ``` S3_US_EAST_1_BUCKET="bucket-name" S3_US_EAST_1_PREFIX="key-prefix" ```
1 parent 7dcd4e8 commit c8c8686

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

lib/s3_deploy.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,33 @@ class S3Deploy {
3232
.digest('hex')
3333
}
3434

35+
_convertRegionStringToEnvVarName (region) {
36+
if (region == null) return 'undefined'
37+
return region.replace(/-/g, '_').toUpperCase()
38+
}
39+
40+
_getBucketNameFromEnvVar (region) {
41+
const key = [
42+
'S3',
43+
this._convertRegionStringToEnvVarName(region),
44+
'BUCKET'
45+
].join('_')
46+
return process.env[key]
47+
}
48+
49+
_getS3KeyPrefixFromEnvVar (region) {
50+
const key = [
51+
'S3',
52+
this._convertRegionStringToEnvVarName(region),
53+
'PREFIX'
54+
].join('_')
55+
return process.env[key]
56+
}
57+
3558
_bucketName (params) {
59+
const bucketNameFromEnvVar = this._getBucketNameFromEnvVar(params.region)
60+
if (bucketNameFromEnvVar != null) return bucketNameFromEnvVar
61+
3662
return [
3763
params.FunctionName,
3864
params.region,
@@ -43,7 +69,12 @@ class S3Deploy {
4369
}
4470

4571
_s3Key (params) {
46-
return `deploy-package-${params.FunctionName}.zip`
72+
const s3Prefix = this._getS3KeyPrefixFromEnvVar(params.region)
73+
const keys = [`deploy-package-${params.FunctionName}.zip`]
74+
if (s3Prefix != null) {
75+
keys.unshift(s3Prefix.replace(/\/$/, ''))
76+
}
77+
return keys.join('/')
4778
}
4879

4980
_getS3Location (region) {

test/s3_deploy.js

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,65 @@ describe('lib/s3_deploy', () => {
3636
})
3737
})
3838

39+
describe('_convertRegionStringToEnvVarName', () => {
40+
it('Upper case. Replace "-" with "_".', () => {
41+
[{
42+
value: 'us-west-1',
43+
expected: 'US_WEST_1'
44+
}, {
45+
value: 'ap-southeast-2',
46+
expected: 'AP_SOUTHEAST_2'
47+
}].forEach((test) => {
48+
assert.equal(
49+
s3Deploy._convertRegionStringToEnvVarName(test.value),
50+
test.expected,
51+
test
52+
)
53+
})
54+
})
55+
})
56+
57+
describe('_getBucketNameFromEnvVar', () => {
58+
after(() => {
59+
delete process.env.S3_US_WEST_1_BUCKET
60+
})
61+
62+
it('is undefined', () => {
63+
assert.isUndefined(s3Deploy._getBucketNameFromEnvVar('us-west-1'))
64+
})
65+
66+
it('Get values from environment variables', () => {
67+
process.env.S3_US_WEST_1_BUCKET = 'bucketName'
68+
assert.equal(
69+
s3Deploy._getBucketNameFromEnvVar('us-west-1'),
70+
'bucketName'
71+
)
72+
})
73+
})
74+
75+
describe('_getS3KeyPrefixFromEnvVar', () => {
76+
after(() => {
77+
delete process.env.S3_US_WEST_1_PREFIX
78+
})
79+
80+
it('is undefined', () => {
81+
assert.isUndefined(s3Deploy._getS3KeyPrefixFromEnvVar('us-west-1'))
82+
})
83+
84+
it('Get values from environment variables', () => {
85+
process.env.S3_US_WEST_1_PREFIX = 's3KeyPrefix'
86+
assert.equal(
87+
s3Deploy._getS3KeyPrefixFromEnvVar('us-west-1'),
88+
's3KeyPrefix'
89+
)
90+
})
91+
})
92+
3993
describe('_bucketName', () => {
94+
after(() => {
95+
delete process.env.S3_TEST_REGION_BUCKET
96+
})
97+
4098
it('FunctionName + region + md5()', () => {
4199
const params = {
42100
FunctionName: 'node-lambda-name',
@@ -47,16 +105,44 @@ describe('lib/s3_deploy', () => {
47105
'node-lambda-name-test_region-aac849d59d2be828b793609e03d8241d'
48106
)
49107
})
108+
109+
it('Use environment variables', () => {
110+
process.env.S3_TEST_REGION_BUCKET = 's3-test-region-bucket'
111+
const params = {
112+
FunctionName: 'node-lambda-name',
113+
region: 'test_region'
114+
}
115+
assert.equal(s3Deploy._bucketName(params), 's3-test-region-bucket')
116+
})
50117
})
51118

52119
describe('_s3Key', () => {
120+
after(() => {
121+
delete process.env.S3_TEST_REGION_PREFIX
122+
})
123+
53124
it('"deploy-package" + FunctionName + ".zip"', () => {
54-
const params = {FunctionName: 'node-lambda-name'}
125+
const params = {
126+
FunctionName: 'node-lambda-name',
127+
region: 'test_region'
128+
}
55129
assert.equal(
56130
s3Deploy._s3Key(params),
57131
'deploy-package-node-lambda-name.zip'
58132
)
59133
})
134+
135+
it('Use environment variables', () => {
136+
process.env.S3_TEST_REGION_PREFIX = 's3-test-region-prefix/'
137+
const params = {
138+
FunctionName: 'node-lambda-name',
139+
region: 'test_region'
140+
}
141+
assert.equal(
142+
s3Deploy._s3Key(params),
143+
's3-test-region-prefix/deploy-package-node-lambda-name.zip'
144+
)
145+
})
60146
})
61147

62148
describe('_getS3Location', () => {

0 commit comments

Comments
 (0)