Skip to content

Commit

Permalink
ci: Fix storage bucket name
Browse files Browse the repository at this point in the history
Bucket names must be globally unique. To ensure the infra stacks can be deployed to multiple accounts, use the bucket name as a prefix instead of the actual bucket name.
  • Loading branch information
Dillon Nys committed Jun 14, 2023
1 parent f4fef7e commit c7eb6f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
34 changes: 19 additions & 15 deletions infra/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ export abstract class IntegrationTestStack<
public get bucket(): s3.Bucket {
if (!this._bucket) {
this._bucket = new s3.Bucket(this, "Bucket", {
bucketName: this.bucketName,
bucketName: randomBucketName({
prefix: `amplify-test-${this.baseName.toLowerCase()}`,
stack: this,
}),
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: true,
enforceSSL: true,
Expand All @@ -89,20 +92,6 @@ export abstract class IntegrationTestStack<
return this._bucket;
}

/**
* The bucket name for this stack where configuration files can be saved.
*
* Naming matches Amplify CLI for discoverability, suffixed with a segment of the stack ID.
* https://github.com/aws-amplify/amplify-ci-support/blob/1abe7f7a1d75fa19675ad8ca17ab625a299b765e/src/integ_test_resources/flutter/amplify/cloudformation_template.yaml#L32
*/
private get bucketName(): string {
return Fn.join("-", [
`amplify-test-${this.baseName.toLowerCase()}`,
// https://stackoverflow.com/questions/54897459/how-to-set-semi-random-name-for-s3-bucket-using-cloud-formation
Fn.select(0, Fn.split("-", Fn.select(2, Fn.split("/", this.stackId)))),
]);
}

/**
* Saves the Amplify configuration for an environment as a CDK output.
* @param config The Amplify config values.
Expand Down Expand Up @@ -167,6 +156,21 @@ export abstract class IntegrationTestStackEnvironment<
}
}

/**
* Creates a semi-random bucket name which is stable for a particular stack and guaranteed to
* not conflict with other buckets of the same prefix.
*
* Naming matches Amplify CLI for discoverability, suffixed with a segment of the stack ID.
* https://github.com/aws-amplify/amplify-ci-support/blob/1abe7f7a1d75fa19675ad8ca17ab625a299b765e/src/integ_test_resources/flutter/amplify/cloudformation_template.yaml#L32
*/
export const randomBucketName = ({ prefix, stack }: { prefix: string, stack: cdk.Stack }): string => {
return Fn.join("-", [
prefix,
// https://stackoverflow.com/questions/54897459/how-to-set-semi-random-name-for-s3-bucket-using-cloud-formation
Fn.select(0, Fn.split("-", Fn.select(2, Fn.split("/", stack.stackId)))),
]);
}

/**
* Creates an amplifyconfiguration.dart compatible JSON representation of config.
* @param region The stack's region.
Expand Down
2 changes: 1 addition & 1 deletion infra/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ export class AmplifyFlutterIntegStack extends cdk.Stack {
{
environmentName: "dots-in-name",
enableTransferAcceleration: false,
bucketName: "amplify.integ-test.stack.com",
bucketNamePrefix: "amplify.integ-test.stack",
},
]);

Expand Down
16 changes: 10 additions & 6 deletions infra/lib/storage/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
AmplifyCategory,
IntegrationTestStack,
IntegrationTestStackEnvironment,
IntegrationTestStackEnvironmentProps
IntegrationTestStackEnvironmentProps,
randomBucketName
} from "../common";

export enum StorageAccessLevel {
Expand Down Expand Up @@ -52,9 +53,12 @@ interface StorageIntegrationTestEnvironmentProps
prefixOverrides?: Record<StorageAccessLevel, String>;

/**
* The name of the bucket. If not provided, it will be auto-generated.
* The prefix name of the bucket. If not provided, it will be auto-generated.
*
* Since bucket names are unique globally, a dedicated bucket name cannot be
* used since that would disallow multiple accounts to deploy this stack.
*/
bucketName?: string;
bucketNamePrefix?: string;

enableTransferAcceleration?: boolean;
}
Expand Down Expand Up @@ -88,16 +92,16 @@ export class StorageIntegrationTestStack extends IntegrationTestStack<

class StorageIntegrationTestEnvironment extends IntegrationTestStackEnvironment<StorageIntegrationTestEnvironmentProps> {
constructor(
scope: Construct,
stack: StorageIntegrationTestStack,
baseName: string,
props: StorageIntegrationTestEnvironmentProps
) {
super(scope, baseName, props);
super(stack, baseName, props);

// Create the bucket

const bucket = new s3.Bucket(this, "Bucket", {
bucketName: props.bucketName,
bucketName: props.bucketNamePrefix ? randomBucketName({ prefix: props.bucketNamePrefix, stack: this }) : undefined,
transferAcceleration: props.enableTransferAcceleration,
removalPolicy: RemovalPolicy.DESTROY,
autoDeleteObjects: true,
Expand Down

0 comments on commit c7eb6f8

Please sign in to comment.