Skip to content

Commit

Permalink
chore(infra): analytics integ test gen 2 backend (#5104)
Browse files Browse the repository at this point in the history
  • Loading branch information
khatruong2009 authored Aug 13, 2024
1 parent 0634b07 commit 3f519a6
Show file tree
Hide file tree
Showing 43 changed files with 672 additions and 16 deletions.
5 changes: 5 additions & 0 deletions infra-gen2/backends/analytics/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/analytics/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,
},
});
28 changes: 28 additions & 0 deletions infra-gen2/backends/analytics/main/amplify/backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineBackend } from "@aws-amplify/backend";
import { addAnalyticsExtensions } from "infra-common";
import { auth } from "./auth/resource";

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/

const backend = defineBackend({
auth,
});

const pinpointRole = backend.auth.resources.authenticatedUserIamRole;
const unauthPinpointRole = backend.auth.resources.unauthenticatedUserIamRole;

const resources = backend.auth.resources;
const { userPool, cfnResources } = resources;
const { stack } = userPool;

const customOutputs = addAnalyticsExtensions({
name: "analytics-main",
stack: stack,
authenticatedRole: pinpointRole,
unauthenticatedRole: unauthPinpointRole,
});

// patch the custom Pinpoint resource to the expected output configuration
backend.addOutput(customOutputs);
3 changes: 3 additions & 0 deletions infra-gen2/backends/analytics/main/amplify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
17 changes: 17 additions & 0 deletions infra-gen2/backends/analytics/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/analytics/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "analytics-main",
"version": "1.0.0",
"main": "index.js"
}
22 changes: 22 additions & 0 deletions infra-gen2/backends/analytics/main/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
type Query {
getRecord(id: ID!): Record @aws_api_key
listRecords: [Record] @aws_api_key
}

type Mutation {
createRecord(input: RecordInput!): Record @aws_api_key
}

type Subscription {
onCreateRecord: Record @aws_subscribe(mutations: ["createRecord"])
}

type Record {
id: ID!
payload: String!
}

input RecordInput {
id: ID!
payload: String!
}
5 changes: 5 additions & 0 deletions infra-gen2/backends/analytics/no-unauth-access/.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;
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,
},
});
26 changes: 26 additions & 0 deletions infra-gen2/backends/analytics/no-unauth-access/amplify/backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { defineBackend } from "@aws-amplify/backend";
import { addAnalyticsExtensions } from "infra-common";
import { auth } from "./auth/resource";

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/

const backend = defineBackend({
auth,
});

const pinpointRole = backend.auth.resources.authenticatedUserIamRole;

const resources = backend.auth.resources;
const { userPool, cfnResources } = resources;
const { stack } = userPool;

const customOutputs = addAnalyticsExtensions({
name: "analytics-main",
stack: stack,
authenticatedRole: pinpointRole,
});

// patch the custom Pinpoint resource to the expected output configuration
backend.addOutput(customOutputs);
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,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/analytics/no-unauth-access/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "no-unauth-access",
"version": "1.0.0",
"main": "index.js"
}
5 changes: 5 additions & 0 deletions infra-gen2/backends/analytics/no-unauth-identities/.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;
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,
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { defineBackend } from "@aws-amplify/backend";
import { addAnalyticsExtensions } from "infra-common";
import { auth } from "./auth/resource";

/**
* @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/

const backend = defineBackend({
auth,
});

const { cfnIdentityPool } = backend.auth.resources.cfnResources;
cfnIdentityPool.allowUnauthenticatedIdentities = false;

const pinpointRole = backend.auth.resources.authenticatedUserIamRole;
const unauthPinpointRole = backend.auth.resources.unauthenticatedUserIamRole;

const resources = backend.auth.resources;
const { userPool, cfnResources } = resources;
const { stack } = userPool;

const customOutputs = addAnalyticsExtensions({
name: "analytics-main",
stack: stack,
authenticatedRole: pinpointRole,
unauthenticatedRole: unauthPinpointRole,
});

// patch the custom Pinpoint resource to the expected output configuration
backend.addOutput(customOutputs);
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,17 @@
{
"compilerOptions": {
"target": "es2022",
"module": "es2022",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"paths": {
"$amplify/*": [
"../.amplify/generated/*"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "no-unauth-identities",
"version": "1.0.0",
"main": "index.js"
}
9 changes: 9 additions & 0 deletions infra-gen2/infra-common/src/analytics-extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# analytics-extensions

This directory contains extensions for the analytics backend, including:

- Setting up Pinpoint
- Setting up a Kinesis Data stream and configure Pinpoint to stream events to it
- Create a Records table to store events from the Kinesis stream
- Create the GraphQL API to which we will publish Kinesis records
- Create the Kinesis consumer Lambda which will capture events from the Kinesis Data Stream and forward them to AppSync.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BackendBase } from "@aws-amplify/backend";
import { Stack } from "aws-cdk-lib";
import { IRole } from "aws-cdk-lib/aws-iam";
import { createAppSyncAPI } from "./data";
import { createKinesisStream } from "./kinesis";
import { createPinpointApp } from "./pinpoint";

type AmplifyOutputs = Parameters<BackendBase["addOutput"]>[0];

export const addAnalyticsExtensions = ({
name,
stack,
authenticatedRole,
unauthenticatedRole,
}: {
name: string;
stack: Stack;
authenticatedRole: IRole;
unauthenticatedRole?: IRole;
}): AmplifyOutputs => {
const pinpointApp = createPinpointApp(
name,
stack,
authenticatedRole,
unauthenticatedRole
);

const kinesisStream = createKinesisStream(name, stack, pinpointApp);

const graphQLApi = createAppSyncAPI(name, stack, kinesisStream);

return {
analytics: {
amazon_pinpoint: {
app_id: pinpointApp.ref,
aws_region: Stack.of(pinpointApp).region,
},
},
data: {
aws_region: stack.region,
url: graphQLApi.graphqlUrl,
api_key: graphQLApi.apiKey,
default_authorization_type: "API_KEY",
authorization_types: [],
},
};
};
Loading

0 comments on commit 3f519a6

Please sign in to comment.