@@ -93,33 +93,45 @@ If you specify a custom S3 bucket, no sandbox storage resource will be created.
93
93
Below are several examples of configuring the backend to define a custom S3 bucket:
94
94
95
95
<BlockSwitcher >
96
- <Block name = " Authenticated Users" >
97
- Below is an example of expanding the original backend object to grant all authenticated (i.e. signed in) users with full access to files under ` public/ ` :
96
+ <Block name = " Guest Users" >
97
+ Below is an example of expanding the original backend object to grant all guest (i.e. not signed in) users read access to files under ` public/ ` :
98
+
98
99
``` ts title="amplify/backend.ts"
99
- import { defineBackend } from " @aws-amplify/backend" ;
100
+ import { defineBackend } from ' @aws-amplify/backend' ;
101
+ import { Effect , Policy , PolicyStatement } from ' aws-cdk-lib/aws-iam' ;
102
+ import { Bucket } from ' aws-cdk-lib/aws-s3' ;
100
103
import { auth } from " ./auth/resource" ;
101
104
102
105
const backend = defineBackend ({
103
106
auth ,
104
107
});
105
108
// highlight-start
109
+ const customBucketStack = backend .createStack (" custom-bucket-stack" );
110
+
111
+ // Import existing bucket
112
+ const customBucket = Bucket .fromBucketAttributes (bucketStack , " MyCustomBucket" , {
113
+ bucketArn: " arn:aws:s3:::<bucket-name>" ,
114
+ region: " <region>"
115
+ });
116
+
106
117
backend .addOutput ({
107
118
storage: {
108
- aws_region: " < region> " ,
109
- bucket_name: " <bucket-name> " ,
119
+ aws_region: customBucket . env . region ,
120
+ bucket_name: customBucket . bucketName ,
110
121
// optional: `buckets` can be used when setting up more than one existing bucket
111
122
buckets: [
112
123
{
113
- aws_region: " < region> " ,
114
- bucket_name: " <bucket-name> " ,
115
- name: " <bucket-name> " ,
124
+ aws_region: customBucket . env . region ,
125
+ bucket_name: customBucket . bucketName ,
126
+ name: customBucket . bucketName ,
116
127
/*
117
128
optional: `paths` can be used to set up access to specific
118
129
bucket prefixes and configure user access types to them
119
130
*/
120
131
paths: {
121
132
" public/*" : {
122
- authenticated: [" get" , " list" , " write" , " delete" ],
133
+ // "write" and "delete" can also be added depending on your use case
134
+ guest: [" get" , " list" ],
123
135
},
124
136
},
125
137
}
@@ -128,131 +140,149 @@ backend.addOutput({
128
140
});
129
141
130
142
/*
131
- Define an inline policy to attach to Amplify's auth role
132
- This policy defines how authenticated users can access your existing bucket
143
+ Define an inline policy to attach to Amplify's unauth role
144
+ This policy defines how unauthenticated/guest users can access your existing bucket
133
145
*/
134
- const authPolicy = new Policy (backend .stack , " customBucketAuthPolicy " , {
146
+ const unauthPolicy = new Policy (backend .stack , " customBucketUnauthPolicy " , {
135
147
statements: [
136
148
new PolicyStatement ({
137
149
effect: Effect .ALLOW ,
138
- actions: [
139
- " s3:GetObject" ,
140
- " s3:PutObject" ,
141
- " s3:DeleteObject"
142
- ],
143
- resources: [" arn:aws:s3:::<bucket-name>/public/*" ,],
150
+ actions: [" s3:GetObject" ],
151
+ resources: [` ${customBucket .bucketArn }/public/* ` ],
144
152
}),
145
153
new PolicyStatement ({
146
154
effect: Effect .ALLOW ,
147
155
actions: [" s3:ListBucket" ],
148
156
resources: [
149
- " arn:aws:s3:::<bucket-name> " ,
150
- " arn:aws:s3:::<bucket-name>/* "
151
- ],
157
+ ` ${ customBucket . bucketArn } ` ,
158
+ ` ${ customBucket . bucketArn }/* `
159
+ ],
152
160
conditions: {
153
161
StringLike: {
154
- " s3:prefix" : [" public/* " , " public/" ],
162
+ " s3:prefix" : [" public/" , " public/* " ],
155
163
},
156
164
},
157
165
}),
158
166
],
159
167
});
160
168
161
- // Add the policies to the authenticated user role
162
- backend .auth .resources .authenticatedUserIamRole .attachInlinePolicy (authPolicy );
169
+ // Add the policies to the unauthenticated user role
170
+ backend .auth .resources .unauthenticatedUserIamRole .attachInlinePolicy (
171
+ unauthPolicy ,
172
+ );
163
173
// highlight-end
164
174
```
165
175
</Block >
166
- <Block name = " Guest Users" >
167
- Below is an example of expanding the original backend object to grant all guest (i.e. not signed in) users read access to files under ` public/ ` :
168
-
176
+ <Block name = " Authenticated Users" >
177
+ Below is an example of expanding the original backend object to grant all authenticated (i.e. signed in) users with full access to files under ` public/ ` :
169
178
``` ts title="amplify/backend.ts"
170
- import { defineBackend } from " @aws-amplify/backend" ;
179
+ import { defineBackend } from ' @aws-amplify/backend' ;
180
+ import { Effect , Policy , PolicyStatement } from ' aws-cdk-lib/aws-iam' ;
181
+ import { Bucket } from ' aws-cdk-lib/aws-s3' ;
171
182
import { auth } from " ./auth/resource" ;
172
183
173
184
const backend = defineBackend ({
174
185
auth ,
175
186
});
176
187
188
+ const customBucketStack = backend .createStack (" custom-bucket-stack" );
189
+
190
+ // Import existing bucket
191
+ const customBucket = Bucket .fromBucketAttributes (bucketStack , " MyCustomBucket" , {
192
+ bucketArn: " arn:aws:s3:::<bucket-name>" ,
193
+ region: " <region>"
194
+ });
195
+
177
196
backend .addOutput ({
178
197
storage: {
179
- aws_region: " < region> " ,
180
- bucket_name: " <bucket-name> " ,
198
+ aws_region: customBucket . env . region ,
199
+ bucket_name: customBucket . bucketName ,
181
200
buckets: [
182
201
{
183
- aws_region: " < region> " ,
184
- bucket_name: " <bucket-name> " ,
185
- name: " <bucket-name> " ,
202
+ aws_region: customBucket . env . region ,
203
+ bucket_name: customBucket . bucketName ,
204
+ name: customBucket . bucketName ,
186
205
paths: {
187
206
" public/*" : {
207
+ guest: [" get" , " list" ],
188
208
// highlight-start
189
- // "write" and "delete" can also be added depending on your use case
190
- guest: [" get" , " list" ],
191
- // highlight-end
192
209
authenticated: [" get" , " list" , " write" , " delete" ],
210
+ // highlight-end
193
211
},
194
212
},
195
213
}
196
214
]
197
215
},
198
216
});
199
217
200
- // ... Authenticated user policy and role attachment goes here ...
218
+ // ... Unauthenticated/guest user policies and role attachments go here ...
201
219
// highlight-start
202
220
/*
203
- Define an inline policy to attach to Amplify's un- auth role
204
- This policy defines how unauthenticated/guest users can access your existing bucket
221
+ Define an inline policy to attach to Amplify's auth role
222
+ This policy defines how authenticated users can access your existing bucket
205
223
*/
206
- const unauthPolicy = new Policy (backend .stack , " customBucketUnauthPolicy " , {
224
+ const authPolicy = new Policy (backend .stack , " customBucketAuthPolicy " , {
207
225
statements: [
208
226
new PolicyStatement ({
209
227
effect: Effect .ALLOW ,
210
- actions: [" s3:GetObject" ],
211
- resources: [" arn:aws:s3:::<bucket-name>/public/*" ],
228
+ actions: [
229
+ " s3:GetObject" ,
230
+ " s3:PutObject" ,
231
+ " s3:DeleteObject"
232
+ ],
233
+ resources: [` ${customBucket .bucketArn }/public/* ` ,],
212
234
}),
213
235
new PolicyStatement ({
214
236
effect: Effect .ALLOW ,
215
237
actions: [" s3:ListBucket" ],
216
238
resources: [
217
- " arn:aws:s3:::<bucket-name> " ,
218
- " arn:aws:s3:::<bucket-name>/* "
219
- ],
239
+ ` ${ customBucket . bucketArn } ` ,
240
+ ` ${ customBucket . bucketArn }/* `
241
+ ],
220
242
conditions: {
221
243
StringLike: {
222
- " s3:prefix" : [" public/" , " public/* " ],
244
+ " s3:prefix" : [" public/* " , " public/" ],
223
245
},
224
246
},
225
247
}),
226
248
],
227
249
});
228
250
229
- // Add the policies to the unauthenticated user role
230
- backend .auth .resources .unauthenticatedUserIamRole .attachInlinePolicy (
231
- unauthPolicy ,
232
- );
251
+ // Add the policies to the authenticated user role
252
+ backend .auth .resources .authenticatedUserIamRole .attachInlinePolicy (authPolicy );
233
253
// highlight-end
234
254
```
235
255
</Block >
236
256
<Block name = " User Groups" >
237
257
Below is an example of expanding the original backend object to have an ` admin/ ` folder that authenticated users can read, but only users belonging to the "admin" user group can manage:
238
258
{ /* cSpell:disable */ }
239
259
``` ts title="amplify/backend.ts"
240
- import { defineBackend } from " @aws-amplify/backend" ;
241
- import { auth } from " ./auth/resource" ;
260
+ import { defineBackend } from ' @aws-amplify/backend' ;
261
+ import { Effect , Policy , PolicyStatement } from ' aws-cdk-lib/aws-iam' ;
262
+ import { Bucket } from ' aws-cdk-lib/aws-s3' ;
263
+ import { auth } from ' ./auth/resource' ;
242
264
243
265
const backend = defineBackend ({
244
266
auth ,
245
267
});
246
268
269
+ const customBucketStack = backend .createStack (" custom-bucket-stack" );
270
+
271
+ // Import existing bucket
272
+ const customBucket = Bucket .fromBucketAttributes (bucketStack , " MyCustomBucket" , {
273
+ bucketArn: " arn:aws:s3:::<bucket-name>" ,
274
+ region: " <region>"
275
+ });
276
+
247
277
backend .addOutput ({
248
278
storage: {
249
- aws_region: " < region> " ,
250
- bucket_name: " <bucket-name> " ,
279
+ aws_region: customBucket . env . region ,
280
+ bucket_name: customBucket . bucketName ,
251
281
buckets: [
252
282
{
253
- aws_region: " < region> " ,
254
- bucket_name: " <bucket-name> " ,
255
- name: " <bucket-name> " ,
283
+ aws_region: customBucket . env . region ,
284
+ bucket_name: customBucket . bucketName ,
285
+ name: customBucket . bucketName ,
256
286
/*
257
287
@ts-expect-error: Amplify backend type issue
258
288
https://github.com/aws-amplify/amplify-backend/issues/2569
@@ -289,14 +319,14 @@ const adminPolicy = new Policy(backend.stack, "customBucketAdminPolicy", {
289
319
" s3:PutObject" ,
290
320
" s3:DeleteObject"
291
321
],
292
- resources: [" arn:aws:s3:::<bucket-name> /admin/*" ],
322
+ resources: [ ` ${ customBucket . bucketArn } /admin/*` ],
293
323
}),
294
324
new PolicyStatement ({
295
325
effect: Effect .ALLOW ,
296
326
actions: [" s3:ListBucket" ],
297
327
resources: [
298
- " arn:aws:s3:::<bucket-name> " ,
299
- " arn:aws:s3:::<bucket-name>/* " ,
328
+ ` ${ customBucket . bucketArn } `
329
+ ` ${ customBucket . bucketArn }/* `
300
330
],
301
331
conditions: {
302
332
StringLike: {
@@ -320,22 +350,32 @@ Below is an example of expanding the original backend object to define read acce
320
350
321
351
{ /* cSpell:disable */ }
322
352
``` ts title="amplify/backend.ts"
323
- import { defineBackend } from " @aws-amplify/backend" ;
353
+ import { defineBackend } from ' @aws-amplify/backend' ;
354
+ import { Effect , Policy , PolicyStatement } from ' aws-cdk-lib/aws-iam' ;
355
+ import { Bucket } from ' aws-cdk-lib/aws-s3' ;
324
356
import { auth } from " ./auth/resource" ;
325
357
326
358
const backend = defineBackend ({
327
359
auth ,
328
360
});
329
361
362
+ const customBucketStack = backend .createStack (" custom-bucket-stack" );
363
+
364
+ // Import existing bucket
365
+ const customBucket = s3 .Bucket .fromBucketAttributes (bucketStack , " MyCustomBucket" , {
366
+ bucketArn: " arn:aws:s3:::<bucket-name>" ,
367
+ region: " <region>"
368
+ });
369
+
330
370
backend .addOutput ({
331
371
storage: {
332
- aws_region: " < region> " ,
333
- bucket_name: " <bucket-name> " ,
372
+ aws_region: customBucket . env . region ,
373
+ bucket_name: customBucket . bucketName ,
334
374
buckets: [
335
375
{
336
- aws_region: " < region> " ,
337
- bucket_name: " <bucket-name> " ,
338
- name: " <bucket-name> " ,
376
+ aws_region: customBucket . env . region ,
377
+ bucket_name: customBucket . bucketName ,
378
+ name: customBucket . bucketName ,
339
379
/*
340
380
@ts-expect-error: Amplify backend type issue
341
381
https://github.com/aws-amplify/amplify-backend/issues/2569
@@ -361,11 +401,9 @@ backend.addOutput({
361
401
]
362
402
},
363
403
});
364
-
365
404
// highlight-start
366
-
367
405
/*
368
- Define an inline policy to attach to Amplify's un-auth role
406
+ Define an inline policy to attach to Amplify's unauth role
369
407
This policy defines how unauthenticated users/guests
370
408
can access your existing bucket
371
409
*/
@@ -375,16 +413,16 @@ const unauthPolicy = new Policy(backend.stack, "customBucketUnauthPolicy", {
375
413
effect: Effect .ALLOW ,
376
414
actions: [" s3:GetObject" ],
377
415
resources: [
378
- " arn:aws:s3:::<bucket-name> /public/*" ,
379
- " arn:aws:s3:::<bucket-name> /protected/*"
416
+ ` ${ customBucket . bucketArn } /public/*`
417
+ ` ${ customBucket . bucketArn } /protected/*`
380
418
],
381
419
}),
382
420
new PolicyStatement ({
383
421
effect: Effect .ALLOW ,
384
422
actions: [" s3:ListBucket" ],
385
423
resources: [
386
- " arn:aws:s3:::<bucket-name> " ,
387
- " arn:aws:s3:::<bucket-name>/* "
424
+ ` ${ customBucket . bucketArn } `
425
+ ` ${ customBucket . bucketArn }/* `
388
426
],
389
427
conditions: {
390
428
StringLike: {
@@ -411,16 +449,16 @@ const authPolicy = new Policy(backend.stack, "customBucketAuthPolicy", {
411
449
effect: Effect .ALLOW ,
412
450
actions: [" s3:GetObject" ],
413
451
resources: [
414
- " arn:aws:s3:::<bucket-name> /public/*" ,
415
- " arn:aws:s3:::<bucket-name> /protected/*"
452
+ ` ${ customBucket . bucketArn } /public/*`
453
+ ` ${ customBucket . bucketArn } /protected/*`
416
454
],
417
455
}),
418
456
new PolicyStatement ({
419
457
effect: Effect .ALLOW ,
420
458
actions: [" s3:ListBucket" ],
421
459
resources: [
422
- " arn:aws:s3:::<bucket-name> " ,
423
- " arn:aws:s3:::<bucket-name>/* "
460
+ ` ${ customBucket . bucketArn } `
461
+ ` ${ customBucket . bucketArn }/* `
424
462
],
425
463
conditions: {
426
464
StringLike: {
@@ -437,15 +475,15 @@ const authPolicy = new Policy(backend.stack, "customBucketAuthPolicy", {
437
475
effect: Effect .ALLOW ,
438
476
actions: [" s3:PutObject" ],
439
477
resources: [
440
- " arn:aws:s3:::<bucket-name> /public/*" ,
441
- " arn:aws:s3:::<bucket-name> /protected/${cognito-identity.amazonaws.com:sub}/*"
478
+ ` ${ customBucket . bucketArn } /public/*`
479
+ ` ${ customBucket . bucketArn } /protected/${cognito - identity .amazonaws .com :sub }/*`
442
480
],
443
481
}),
444
482
new PolicyStatement ({
445
483
effect: Effect .ALLOW ,
446
484
actions: [" s3:DeleteObject" ],
447
485
resources: [
448
- " arn:aws:s3:::<bucket-name> /protected/${cognito-identity.amazonaws.com:sub}/*"
486
+ ` ${ customBucket . bucketArn } /protected/${cognito - identity .amazonaws .com :sub }/*`
449
487
],
450
488
}),
451
489
],
0 commit comments