Skip to content

Commit

Permalink
Fix bug that causes deployment buckets to be misconfigured when first…
Browse files Browse the repository at this point in the history
… building a service (#6)
  • Loading branch information
mdial89f authored Sep 17, 2021
1 parent a6016a9 commit f0f6d6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,4 @@ plugins:

## Background

This plugin has two hooks:

- package:createDeploymentArtifacts: This ensures the deployment bucket is configured correctly.
- package:compileEvents: This hook ensures all buckets built by the service are configured correctly.
This plugin hooks into the serverless lifecycle at "before:deploy:deploy". There, it looks for S3 buckets and potentially modifies each. Currently, versioning is enabled for all buckets and public access is blocked by default.
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ class ServerlessPlugin {
this.options = options;

this.hooks = {
// This will ensure the serverless deployment bucket is configured correctly.
"aws:deploy:deploy:createStack": this.configureBuckets.bind(this),

// This will configure all S3 buckets according to this plugin.
"before:deploy:deploy": this.configureBuckets.bind(this),
};
Expand Down Expand Up @@ -42,18 +39,21 @@ class ServerlessPlugin {
}

function setPropertyForTypes(types, property, value, overrideExistingValue) {
const template =
this.serverless.service.provider.compiledCloudFormationTemplate;
Object.keys(template.Resources).forEach(function (key) {
if (types.includes(template.Resources[key]["Type"])) {
// Set the target property to the target value... if it's not already set OR we want to override any existing value.
if (
!(property in template.Resources[key]["Properties"]) ||
overrideExistingValue
) {
template.Resources[key]["Properties"][property] = value;
[
this.serverless.service.provider.compiledCloudFormationTemplate,
this.serverless.service.provider.coreCloudFormationTemplate,
].forEach(function (template) {
Object.keys(template.Resources).forEach(function (key) {
if (types.includes(template.Resources[key]["Type"])) {
// Set the target property to the target value... if it's not already set OR we want to override any existing value.
if (
!(property in template.Resources[key]["Properties"]) ||
overrideExistingValue
) {
template.Resources[key]["Properties"][property] = value;
}
}
}
});
});
}

Expand Down

0 comments on commit f0f6d6a

Please sign in to comment.