Skip to content

Commit 81a78a3

Browse files
authored
fix(ec2): Throw error on empty InitFile content (#13009) (#13119)
Validate InitFile content to catch error during synth instead of deploy. Fixes issue #13009 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c174f6c commit 81a78a3

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

packages/@aws-cdk/aws-ec2/lib/cfn-init-elements.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ export abstract class InitFile extends InitElement {
327327
* Use a literal string as the file content
328328
*/
329329
public static fromString(fileName: string, content: string, options: InitFileOptions = {}): InitFile {
330+
if (!content) {
331+
throw new Error(`InitFile ${fileName}: cannot create empty file. Please supply at least one character of content.`);
332+
}
330333
return new class extends InitFile {
331334
protected _doBind(bindOptions: InitBindOptions) {
332335
return {

packages/@aws-cdk/aws-ec2/test/cfn-init-element.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ describe('InitFile', () => {
206206
});
207207
});
208208

209+
test('empty content string throws error', () => {
210+
expect(() => {
211+
ec2.InitFile.fromString('/tmp/foo', '');
212+
}).toThrow('InitFile /tmp/foo: cannot create empty file. Please supply at least one character of content.');
213+
});
214+
209215
test('symlink throws an error if mode is set incorrectly', () => {
210216
expect(() => {
211217
ec2.InitFile.symlink('/tmp/foo', '/tmp/bar', {

0 commit comments

Comments
 (0)