Skip to content

Commit 23570e1

Browse files
authored
fix internal Serverless custom resources S3 path addressing (#209)
1 parent 3050f61 commit 23570e1

File tree

4 files changed

+71
-7
lines changed

4 files changed

+71
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ custom:
205205
206206
## Change Log
207207
208+
* v1.0.3: Set S3 Path addressing for internal Serverless Custom Resources - allow configuring S3 Events Notification for functions
208209
* v1.0.2: Add check to prevent IPv6 connection issue with `localhost` on MacOS
209210
* v1.0.1: Add support for Serverless projects with esbuild source config; enable config via environment variables
210211
* v1.0.0: Allow specifying path for mountCode, to point to a relative Lambda mount path

package-lock.json

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-localstack",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Connect Serverless to LocalStack!",
55
"main": "src/index.js",
66
"scripts": {
@@ -25,14 +25,16 @@
2525
"Waldemar Hummer (whummer)",
2626
"Justin McCormick <me@justinmccormick.com>",
2727
"djKooks",
28-
"yohei1126"
28+
"yohei1126",
29+
"bentsku"
2930
],
3031
"license": "MIT",
3132
"bugs": {
3233
"url": "https://github.com/localstack/serverless-localstack/issues"
3334
},
3435
"homepage": "https://github.com/localstack/serverless-localstack",
3536
"dependencies": {
37+
"adm-zip": "^0.5.10",
3638
"aws-sdk": "^2.402.0",
3739
"es6-promisify": "^6.0.1"
3840
},

src/index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
'use strict';
2+
const AdmZip = require("adm-zip");
23
const AWS = require('aws-sdk');
34
const fs = require('fs');
45
const path = require('path');
@@ -49,6 +50,11 @@ class LocalstackPlugin {
4950
// Add a hook to fix TypeError when accessing undefined state attribute
5051
this.addHookInFirstPosition('before:aws:deploy:deploy:checkForChanges', this.beforeDeployCheckForChanges);
5152

53+
const compileEventsHooks = this.serverless.pluginManager.hooks['package:compileEvents'] || [];
54+
compileEventsHooks.push({
55+
pluginName: 'LocalstackPlugin', hook: this.patchCustomResourceLambdaS3ForcePathStyle.bind(this)
56+
});
57+
5258
this.awsServices = [
5359
'acm',
5460
'amplify',
@@ -731,7 +737,63 @@ class LocalstackPlugin {
731737
plugin.display = newDisplay;
732738
}
733739
}
740+
patchCustomResourceLambdaS3ForcePathStyle () {
741+
const awsProvider = this.awsProvider;
742+
const patchMarker = path.join(
743+
awsProvider.serverless.serviceDir,
744+
'.serverless',
745+
'.internal-custom-resources-patched'
746+
);
747+
const zipFilePath = path.join(
748+
awsProvider.serverless.serviceDir,
749+
'.serverless',
750+
awsProvider.naming.getCustomResourcesArtifactName()
751+
);
752+
753+
function fileExists (filePath) {
754+
try {
755+
const stats = fs.statSync(filePath);
756+
return stats.isFile();
757+
} catch (e) {
758+
return false;
759+
}
760+
}
761+
762+
function createPatchMarker () {
763+
try {
764+
fs.open(patchMarker, 'a').close()
765+
} catch (err) {
766+
return;
767+
}
768+
}
769+
770+
if (fileExists(patchMarker)) {
771+
this.debug("serverless-localstack: Serverless internal CustomResources already patched")
772+
return;
773+
}
774+
775+
const customResourceZipExists = fileExists(zipFilePath)
734776

777+
if (!customResourceZipExists) {
778+
return;
779+
}
780+
781+
const customResources = new AdmZip(zipFilePath);
782+
const utilFile = customResources.getEntry('utils.js')
783+
if (utilFile == null) return;
784+
const data = utilFile.getData().toString()
785+
const patch = "AWS.config.s3ForcePathStyle = true;"
786+
if (data.includes(patch)) {
787+
createPatchMarker()
788+
return;
789+
}
790+
const indexPatch = data.indexOf('AWS.config.logger = console;')
791+
const newData = data.slice(0, indexPatch) + patch + '\n' + data.slice(indexPatch)
792+
utilFile.setData(newData)
793+
customResources.writeZip()
794+
createPatchMarker()
795+
this.debug('serverless-localstack: Serverless internal CustomResources patched')
796+
}
735797
}
736798

737799
module.exports = LocalstackPlugin;

0 commit comments

Comments
 (0)