From d5a441ba17a93698f4a93f017235e76e789551e8 Mon Sep 17 00:00:00 2001 From: Matthieu Napoli Date: Fri, 28 Aug 2020 15:27:32 +0200 Subject: [PATCH] Prefix unique names with the stack name --- lift.yml | 2 +- src/components/Component.ts | 12 ++++++++++- src/components/Database.ts | 10 ++++----- src/components/S3.ts | 38 ++++++++++++++++----------------- src/components/StaticWebsite.ts | 10 ++++----- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lift.yml b/lift.yml index 767ca189..aaedf33b 100644 --- a/lift.yml +++ b/lift.yml @@ -2,7 +2,7 @@ name: lift region: us-east-1 s3: - tmp-test-avatars-123: + avatars: # public: true # cors: true diff --git a/src/components/Component.ts b/src/components/Component.ts index 0a62e0d2..006f3bf4 100644 --- a/src/components/Component.ts +++ b/src/components/Component.ts @@ -4,12 +4,22 @@ import {constantCase} from "constant-case"; import {PolicyStatement} from "../utils/cloudformation"; export abstract class Component { + protected readonly stackName: string; + abstract compile(): Record; abstract outputs(): Record; abstract permissions(): PolicyStatement[]; abstract envVariables(): Record; - formatResourceName(name: string): string { + protected constructor(stackName: string) { + this.stackName = stackName; + } + + formatUniqueResourceName(name: string): string { + return this.stackName + '-' + name; + } + + formatCloudFormationId(name: string): string { return pascalCase(name, { transform: pascalCaseTransformMerge, }); diff --git a/src/components/Database.ts b/src/components/Database.ts index a3fe4a00..0d8d1506 100644 --- a/src/components/Database.ts +++ b/src/components/Database.ts @@ -2,16 +2,14 @@ import {Component} from "./Component"; import {PolicyStatement} from "../utils/cloudformation"; export class Database extends Component { - private stackName: string; - private props: Record; - private dbResourceName: string; + private readonly props: Record; + private readonly dbResourceName: string; constructor(stackName: string, props: Record | null) { - super(); - this.stackName = stackName; + super(stackName); this.props = props ? props : {}; - this.dbResourceName = this.formatResourceName('Database'); + this.dbResourceName = this.formatCloudFormationId('Database'); } compile(): Record { diff --git a/src/components/S3.ts b/src/components/S3.ts index 09ec529d..38958027 100644 --- a/src/components/S3.ts +++ b/src/components/S3.ts @@ -2,18 +2,18 @@ import {Component} from "./Component"; import {PolicyStatement} from "../utils/cloudformation"; export class S3 extends Component { - private stackName: string; - private bucketName: string; - private props: Record; - private bucketResourceName: string; + private readonly name: string; + private readonly bucketName: string; + private readonly props: Record; + private readonly bucketResourceId: string; constructor(stackName: string, name: string, props: Record | null) { - super(); - this.stackName = stackName; - this.bucketName = name; + super(stackName); + this.name = name; + this.bucketName = this.formatUniqueResourceName(name); this.props = props ? props : {}; - this.bucketResourceName = this.formatResourceName(this.bucketName); + this.bucketResourceId = this.formatCloudFormationId(this.name); } compile(): Record { @@ -37,14 +37,14 @@ export class S3 extends Component { } const resources: Record = { - [this.bucketResourceName]: bucket, + [this.bucketResourceId]: bucket, }; if (this.props.public) { - resources[this.bucketResourceName + 'BucketPolicy'] = { + resources[this.bucketResourceId + 'BucketPolicy'] = { Type: 'AWS::S3::BucketPolicy', Properties: { - Bucket: this.fnRef(this.bucketResourceName), + Bucket: this.fnRef(this.bucketResourceId), PolicyDocument: { Statement: [ { @@ -52,7 +52,7 @@ export class S3 extends Component { Principal: '*', Action: 's3:GetObject', Resource: this.fnJoin('', [ - this.fnGetAtt(this.bucketResourceName, 'Arn'), + this.fnGetAtt(this.bucketResourceId, 'Arn'), '/*', ]), }, @@ -67,22 +67,22 @@ export class S3 extends Component { outputs() { return { - [this.bucketResourceName + 'Bucket']: { + [this.bucketResourceId + 'Bucket']: { Description: 'Name of the S3 bucket.', - Value: this.fnRef(this.bucketResourceName), + Value: this.fnRef(this.bucketResourceId), }, - [this.bucketResourceName + 'BucketArn']: { + [this.bucketResourceId + 'BucketArn']: { Description: 'ARN of the S3 bucket.', - Value: this.fnGetAtt(this.bucketResourceName, 'Arn'), + Value: this.fnGetAtt(this.bucketResourceId, 'Arn'), Export: { - Name: this.stackName + '-' + this.bucketResourceName + 'BucketArn', + Name: this.stackName + '-' + this.bucketResourceId + 'BucketArn', }, }, }; } permissions(): PolicyStatement[] { - const bucketArn = this.fnImportValue(this.stackName + '-' + this.bucketResourceName + 'BucketArn'); + const bucketArn = this.fnImportValue(this.stackName + '-' + this.bucketResourceId + 'BucketArn'); return [ new PolicyStatement('s3:*', [ bucketArn, @@ -92,7 +92,7 @@ export class S3 extends Component { } envVariables() { - const variableName = this.formatEnvVariableName('BUCKET_' + this.bucketName); + const variableName = this.formatEnvVariableName('BUCKET_' + this.name); return { [variableName]: this.bucketName, }; diff --git a/src/components/StaticWebsite.ts b/src/components/StaticWebsite.ts index 247c8a71..7fdfce9f 100644 --- a/src/components/StaticWebsite.ts +++ b/src/components/StaticWebsite.ts @@ -2,16 +2,14 @@ import {Component} from "./Component"; import {PolicyStatement} from "../utils/cloudformation"; export class StaticWebsite extends Component { - private stackName: string; - private props: Record; - private bucketResourceName: string; + private readonly props: Record; + private readonly bucketResourceName: string; constructor(stackName: string, props: Record | null) { - super(); - this.stackName = stackName; + super(stackName); this.props = props ? props : {}; - this.bucketResourceName = this.formatResourceName('StaticWebsite'); + this.bucketResourceName = this.formatCloudFormationId('StaticWebsite'); } compile(): Record {