Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infra: Fix storage bucket name #3222

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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