|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Imports |
| 5 | +const fs = require('fs'); |
| 6 | +const _regex = /[\w]*AssetParameters/g; //this regular express also takes into account lambda functions defined in nested stacks |
| 7 | + |
| 8 | +// Paths |
| 9 | +const global_s3_assets = '../global-s3-assets'; |
| 10 | + |
| 11 | +// For each template in global_s3_assets ... |
| 12 | +fs.readdirSync(global_s3_assets).forEach(file => { |
| 13 | + |
| 14 | + // Import and parse template file |
| 15 | + const raw_template = fs.readFileSync(`${global_s3_assets}/${file}`); |
| 16 | + let template = JSON.parse(raw_template); |
| 17 | + |
| 18 | + // Clean-up Lambda function code dependencies |
| 19 | + const resources = (template.Resources) ? template.Resources : {}; |
| 20 | + const lambdaFunctions = Object.keys(resources).filter(function (key) { |
| 21 | + return resources[key].Type === "AWS::Lambda::Function"; |
| 22 | + }); |
| 23 | + lambdaFunctions.forEach(function (f) { |
| 24 | + const fn = template.Resources[f]; |
| 25 | + if (fn.Properties.Code.hasOwnProperty('S3Bucket')) { |
| 26 | + // Set the S3 key reference |
| 27 | + let s3Key = Object.assign(fn.Properties.Code.S3Key); |
| 28 | + // https://github.com/aws/aws-cdk/issues/10608 |
| 29 | + if (!s3Key.endsWith('.zip')) { |
| 30 | + fn.Properties.Code.S3Key = `%%SOLUTION_NAME%%/%%VERSION%%/${s3Key}.zip`; |
| 31 | + } else { |
| 32 | + fn.Properties.Code.S3Key = `%%SOLUTION_NAME%%/%%VERSION%%/${s3Key}`; |
| 33 | + } |
| 34 | + // Set the S3 bucket reference |
| 35 | + fn.Properties.Code.S3Bucket = { |
| 36 | + 'Fn::Sub': '%%BUCKET_NAME%%-${AWS::Region}' |
| 37 | + }; |
| 38 | + } |
| 39 | + }); |
| 40 | + |
| 41 | + // Clean-up Lambda Layer code dependencies |
| 42 | + const lambdaLayers = Object.keys(resources).filter(function (key) { |
| 43 | + return resources[key].Type === "AWS::Lambda::LayerVersion"; |
| 44 | + }) |
| 45 | + lambdaLayers.forEach(function (l) { |
| 46 | + const layer = template.Resources[l]; |
| 47 | + if (layer.Properties.Content.hasOwnProperty('S3Bucket')) { |
| 48 | + let s3Key = Object.assign(layer.Properties.Content.S3Key); |
| 49 | + layer.Properties.Content.S3Key = `%%SOLUTION_NAME%%/%%VERSION%%/${s3Key}`; |
| 50 | + layer.Properties.Content.S3Bucket = { |
| 51 | + 'Fn::Sub': '%%BUCKET_NAME%%-${AWS::Region}' |
| 52 | + } |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + // Clean-up Custom::CDKBucketDeployment |
| 57 | + const bucketDeployments = Object.keys(resources).filter(function (key) { |
| 58 | + return resources[key].Type === "Custom::CDKBucketDeployment" |
| 59 | + }) |
| 60 | + bucketDeployments.forEach(function (d) { |
| 61 | + const deployment = template.Resources[d]; |
| 62 | + if (deployment.Properties.hasOwnProperty('SourceBucketNames')) { |
| 63 | + let s3Key = Object.assign(deployment.Properties.SourceObjectKeys[0]); |
| 64 | + deployment.Properties.SourceObjectKeys = [ |
| 65 | + `%%SOLUTION_NAME%%/%%VERSION%%/${s3Key}` |
| 66 | + ] |
| 67 | + deployment.Properties.SourceBucketNames = [ |
| 68 | + { |
| 69 | + 'Fn::Sub': '%%BUCKET_NAME%%-${AWS::Region}' |
| 70 | + } |
| 71 | + ] |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + // Clean-up CustomCDKBucketDeployment Policy |
| 76 | + const bucketDeploymentsPolicy = Object.keys(resources).filter(function (key) { |
| 77 | + return key.startsWith("CustomCDKBucketDeployment") && resources[key].Type === "AWS::IAM::Policy" |
| 78 | + }) |
| 79 | + |
| 80 | + bucketDeploymentsPolicy.forEach(function (d) { |
| 81 | + const policy = template.Resources[d]; |
| 82 | + let resources = policy.Properties.PolicyDocument.Statement[0].Resource |
| 83 | + resources.forEach((res) => { |
| 84 | + res['Fn::Join'].forEach((key) => { |
| 85 | + if (key[2] == ':s3:::') { |
| 86 | + key[3]['Fn::Sub'] = '%%BUCKET_NAME%%-${AWS::Region}' |
| 87 | + } |
| 88 | + }) |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + const nestedStacks = Object.keys(resources).filter(function (key) { |
| 93 | + return resources[key].Type === "AWS::CloudFormation::Stack"; |
| 94 | + }); |
| 95 | + |
| 96 | + nestedStacks.forEach(function (f) { |
| 97 | + const fn = template.Resources[f]; |
| 98 | + if (!fn.Metadata.hasOwnProperty("aws:asset:path")) { |
| 99 | + throw new Error("Nested stack construct missing file name metadata"); |
| 100 | + } |
| 101 | + fn.Properties.TemplateURL = { |
| 102 | + "Fn::Join": [ |
| 103 | + "", |
| 104 | + [ |
| 105 | + "https://%%TEMPLATE_BUCKET_NAME%%.s3.", |
| 106 | + { |
| 107 | + Ref: "AWS::URLSuffix", |
| 108 | + }, |
| 109 | + "/", |
| 110 | + `%%SOLUTION_NAME%%/%%VERSION%%/${fn.Metadata["aws:asset:path"].slice(0, -".json".length)}`, |
| 111 | + ], |
| 112 | + ], |
| 113 | + }; |
| 114 | + |
| 115 | + const params = fn.Properties.Parameters ? fn.Properties.Parameters : {}; |
| 116 | + const nestedStackParameters = Object.keys(params).filter(function (key) { |
| 117 | + if (key.search(_regex) > -1) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + return false; |
| 121 | + }); |
| 122 | + |
| 123 | + nestedStackParameters.forEach(function (stkParam) { |
| 124 | + fn.Properties.Parameters[stkParam] = undefined; |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + // Clean-up parameters section |
| 129 | + const parameters = (template.Parameters) ? template.Parameters : {}; |
| 130 | + const assetParameters = Object.keys(parameters).filter(function (key) { |
| 131 | + if (key.search(_regex) > -1) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + return false; |
| 135 | + }); |
| 136 | + assetParameters.forEach(function (a) { |
| 137 | + template.Parameters[a] = undefined; |
| 138 | + }); |
| 139 | + |
| 140 | + // Clean-up BootstrapVersion parameter |
| 141 | + if (parameters.hasOwnProperty('BootstrapVersion')) { |
| 142 | + parameters.BootstrapVersion = undefined |
| 143 | + } |
| 144 | + |
| 145 | + // Clean-up CheckBootstrapVersion Rule |
| 146 | + const rules = (template.Rules) ? template.Rules : {}; |
| 147 | + if (rules.hasOwnProperty('CheckBootstrapVersion')) { |
| 148 | + rules.CheckBootstrapVersion = undefined |
| 149 | + } |
| 150 | + |
| 151 | + |
| 152 | + // Output modified template file |
| 153 | + const output_template = JSON.stringify(template, null, 2); |
| 154 | + fs.writeFileSync(`${global_s3_assets}/${file}`, output_template); |
| 155 | +}); |
0 commit comments