Skip to content

Commit

Permalink
chore(infra): Storage migrate to Gen 2 E2E
Browse files Browse the repository at this point in the history
chore(infra): Storage migrate to Gen 2 E2E
  • Loading branch information
khatruong2009 authored Jul 25, 2024
1 parent 85fe599 commit d62d543
Show file tree
Hide file tree
Showing 35 changed files with 1,415 additions and 311 deletions.
5 changes: 5 additions & 0 deletions infra-gen2/backends/storage/dots-in-name/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# amplify
node_modules
.amplify
amplify_outputs*
amplifyconfiguration*
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PreSignUpTriggerHandler } from "aws-lambda";
import { preSignUpTriggerHandler } from "infra-common";

export const handler: PreSignUpTriggerHandler = preSignUpTriggerHandler;
19 changes: 19 additions & 0 deletions infra-gen2/backends/storage/dots-in-name/amplify/auth/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineAuth, defineFunction } from "@aws-amplify/backend";

export const preSignUp = defineFunction({
name: "pre-sign-up",
entry: "./pre-sign-up-handler.ts",
});

/**
* Define and configure your auth resource
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
*/
export const auth = defineAuth({
loginWith: {
email: true,
},
triggers: {
preSignUp,
},
});
40 changes: 40 additions & 0 deletions infra-gen2/backends/storage/dots-in-name/amplify/backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { defineBackend } from "@aws-amplify/backend";
import * as s3 from "aws-cdk-lib/aws-s3";
import { v4 as uuidv4 } from "uuid";
import { auth } from "./auth/resource";
import { storage } from "./storage/resource";

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
storage,
});

// custom storage configurations
const s3Bucket = backend.storage.resources.bucket;
const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket;

const randomBucketName = `dots.in.name-${uuidv4()}`;

cfnBucket.bucketName = randomBucketName;

// required to add the metadata header, which amplify-backend does not support
backend.storage.resources.cfnResources.cfnBucket.corsConfiguration = {
corsRules: [
{
allowedHeaders: ["*"],
allowedMethods: ["GET", "HEAD", "PUT", "POST", "DELETE"],
allowedOrigins: ["*"],
exposedHeaders: [
"x-amz-server-side-encryption",
"x-amz-request-id",
"x-amz-id-2",
"ETag",
"x-amz-meta-description",
],
maxAge: 3000,
},
],
};
3 changes: 3 additions & 0 deletions infra-gen2/backends/storage/dots-in-name/amplify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineStorage } from "@aws-amplify/backend";

export const storage = defineStorage({
name: "dots in name",
access: (allow) => ({
"public/*": [
allow.guest.to(["read", "write", "delete"]),
allow.authenticated.to(["read", "delete", "write"]),
],
"protected/{entity_id}/*": [
allow.authenticated.to(["read"]),
allow.entity("identity").to(["read", "write", "delete"]),
],
"private/{entity_id}/*": [
allow.entity("identity").to(["read", "write", "delete"]),
],
}),
});
17 changes: 17 additions & 0 deletions infra-gen2/backends/storage/dots-in-name/amplify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"paths": {
"$amplify/*": [
"../.amplify/generated/*"
]
}
}
}
5 changes: 5 additions & 0 deletions infra-gen2/backends/storage/dots-in-name/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "dots-in-name",
"version": "1.0.0",
"main": "index.js"
}
5 changes: 5 additions & 0 deletions infra-gen2/backends/storage/main/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# amplify
node_modules
.amplify
amplify_outputs*
amplifyconfiguration*
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PreSignUpTriggerHandler } from "aws-lambda";
import { preSignUpTriggerHandler } from "infra-common";

export const handler: PreSignUpTriggerHandler = preSignUpTriggerHandler;
19 changes: 19 additions & 0 deletions infra-gen2/backends/storage/main/amplify/auth/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineAuth, defineFunction } from "@aws-amplify/backend";

export const preSignUp = defineFunction({
name: "pre-sign-up",
entry: "./pre-sign-up-handler.ts",
});

/**
* Define and configure your auth resource
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
*/
export const auth = defineAuth({
loginWith: {
email: true,
},
triggers: {
preSignUp,
},
});
39 changes: 39 additions & 0 deletions infra-gen2/backends/storage/main/amplify/backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { defineBackend } from "@aws-amplify/backend";
import * as s3 from "aws-cdk-lib/aws-s3";
import { auth } from "./auth/resource";
import { storage } from "./storage/resource";

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
storage,
});

// custom storage configurations
const s3Bucket = backend.storage.resources.bucket;
const cfnBucket = s3Bucket.node.defaultChild as s3.CfnBucket;

cfnBucket.accelerateConfiguration = {
accelerationStatus: "Enabled",
};

// required to add the metadata header, which amplify-backend does not support
backend.storage.resources.cfnResources.cfnBucket.corsConfiguration = {
corsRules: [
{
allowedHeaders: ["*"],
allowedMethods: ["GET", "HEAD", "PUT", "POST", "DELETE"],
allowedOrigins: ["*"],
exposedHeaders: [
"x-amz-server-side-encryption",
"x-amz-request-id",
"x-amz-id-2",
"ETag",
"x-amz-meta-description",
],
maxAge: 3000,
},
],
};
3 changes: 3 additions & 0 deletions infra-gen2/backends/storage/main/amplify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
18 changes: 18 additions & 0 deletions infra-gen2/backends/storage/main/amplify/storage/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineStorage } from "@aws-amplify/backend";

export const storage = defineStorage({
name: "Storage Integ Test main",
access: (allow) => ({
"public/*": [
allow.guest.to(["read", "write", "delete"]),
allow.authenticated.to(["read", "delete", "write"]),
],
"protected/{entity_id}/*": [
allow.authenticated.to(["read"]),
allow.entity("identity").to(["read", "write", "delete"]),
],
"private/{entity_id}/*": [
allow.entity("identity").to(["read", "write", "delete"]),
],
}),
});
17 changes: 17 additions & 0 deletions infra-gen2/backends/storage/main/amplify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"paths": {
"$amplify/*": [
"../.amplify/generated/*"
]
}
}
}
5 changes: 5 additions & 0 deletions infra-gen2/backends/storage/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "main",
"version": "1.0.0",
"main": "index.js"
}
Loading

0 comments on commit d62d543

Please sign in to comment.