Skip to content

Commit

Permalink
Prefix unique names with the stack name
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Aug 28, 2020
1 parent bd8dd4c commit d5a441b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion lift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: lift
region: us-east-1

s3:
tmp-test-avatars-123:
avatars:
# public: true
# cors: true

Expand Down
12 changes: 11 additions & 1 deletion src/components/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
abstract outputs(): Record<string, any>;
abstract permissions(): PolicyStatement[];
abstract envVariables(): Record<string, any>;

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,
});
Expand Down
10 changes: 4 additions & 6 deletions src/components/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import {Component} from "./Component";
import {PolicyStatement} from "../utils/cloudformation";

export class Database extends Component {
private stackName: string;
private props: Record<string, any>;
private dbResourceName: string;
private readonly props: Record<string, any>;
private readonly dbResourceName: string;

constructor(stackName: string, props: Record<string, any> | null) {
super();
this.stackName = stackName;
super(stackName);
this.props = props ? props : {};

this.dbResourceName = this.formatResourceName('Database');
this.dbResourceName = this.formatCloudFormationId('Database');
}

compile(): Record<string, any> {
Expand Down
38 changes: 19 additions & 19 deletions src/components/S3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>;
private bucketResourceName: string;
private readonly name: string;
private readonly bucketName: string;
private readonly props: Record<string, any>;
private readonly bucketResourceId: string;

constructor(stackName: string, name: string, props: Record<string, any> | 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<string, any> {
Expand All @@ -37,22 +37,22 @@ export class S3 extends Component {
}

const resources: Record<string, any> = {
[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: [
{
Effect: 'Allow',
Principal: '*',
Action: 's3:GetObject',
Resource: this.fnJoin('', [
this.fnGetAtt(this.bucketResourceName, 'Arn'),
this.fnGetAtt(this.bucketResourceId, 'Arn'),
'/*',
]),
},
Expand All @@ -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,
Expand All @@ -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,
};
Expand Down
10 changes: 4 additions & 6 deletions src/components/StaticWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ import {Component} from "./Component";
import {PolicyStatement} from "../utils/cloudformation";

export class StaticWebsite extends Component {
private stackName: string;
private props: Record<string, any>;
private bucketResourceName: string;
private readonly props: Record<string, any>;
private readonly bucketResourceName: string;

constructor(stackName: string, props: Record<string, any> | null) {
super();
this.stackName = stackName;
super(stackName);
this.props = props ? props : {};

this.bucketResourceName = this.formatResourceName('StaticWebsite');
this.bucketResourceName = this.formatCloudFormationId('StaticWebsite');
}

compile(): Record<string, any> {
Expand Down

0 comments on commit d5a441b

Please sign in to comment.