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

feat: allow s3 cors options to be configured #1972

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/seven-buttons-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-storage': minor
---

Enable bucket CORS to be set via defineStorage
2 changes: 2 additions & 0 deletions packages/backend-storage/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
import { CfnBucket } from 'aws-cdk-lib/aws-s3';
import { ConstructFactory } from '@aws-amplify/plugin-types';
import { ConstructFactoryGetInstanceProps } from '@aws-amplify/plugin-types';
import { CorsRule } from 'aws-cdk-lib/aws-s3';
import { FunctionResources } from '@aws-amplify/plugin-types';
import { IBucket } from 'aws-cdk-lib/aws-s3';
import { ResourceAccessAcceptor } from '@aws-amplify/plugin-types';
Expand All @@ -28,6 +29,7 @@ export type AmplifyStorageProps = {
versioned?: boolean;
outputStorageStrategy?: BackendOutputStorageStrategy<StorageOutput>;
triggers?: Partial<Record<AmplifyStorageTriggerEvent, ConstructFactory<ResourceProvider<FunctionResources>>>>;
cors?: CorsRule[];
};

// @public (undocumented)
Expand Down
32 changes: 32 additions & 0 deletions packages/backend-storage/src/construct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AmplifyStorage } from './construct.js';
import { App, Stack } from 'aws-cdk-lib';
import { Capture, Template } from 'aws-cdk-lib/assertions';
import assert from 'node:assert';
import { HttpMethods } from 'aws-cdk-lib/aws-s3';

void describe('AmplifyStorage', () => {
void it('creates a bucket', () => {
Expand Down Expand Up @@ -62,6 +63,37 @@ void describe('AmplifyStorage', () => {
});
});

void it('allows the user to override the default cors settings', () => {
const expectedCorsSettings = {
maxAge: 100,
allowedHeaders: ['example-header'],
allowedMethods: [HttpMethods.GET],
allowedOrigins: ['my-origin.aws.com'],
exposedHeaders: ['*'],
};
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'testAuth', {
name: 'testName',
cors: [expectedCorsSettings],
});

const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::S3::Bucket', {
CorsConfiguration: {
CorsRules: [
{
AllowedHeaders: expectedCorsSettings.allowedHeaders,
AllowedMethods: expectedCorsSettings.allowedMethods,
AllowedOrigins: expectedCorsSettings.allowedOrigins,
ExposedHeaders: expectedCorsSettings.exposedHeaders,
MaxAge: expectedCorsSettings.maxAge,
},
],
},
});
});

void it('sets destroy retain policy and auto-delete objects true', () => {
const app = new App();
const stack = new Stack(app);
Expand Down
4 changes: 3 additions & 1 deletion packages/backend-storage/src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Bucket,
BucketProps,
CfnBucket,
CorsRule,
EventType,
HttpMethods,
IBucket,
Expand Down Expand Up @@ -61,6 +62,7 @@ export type AmplifyStorageProps = {
ConstructFactory<ResourceProvider<FunctionResources>>
>
>;
cors?: CorsRule[];
};

export type StorageResources = {
Expand Down Expand Up @@ -92,7 +94,7 @@ export class AmplifyStorage

const bucketProps: BucketProps = {
versioned: props.versioned || false,
cors: [
cors: props.cors ?? [
{
maxAge: 3000,
exposedHeaders: [
Expand Down