Skip to content

Commit 20e49d0

Browse files
committed
feat: Data client generation using SSM/S3 to provide the MIS to the runtime
1 parent 57c4e70 commit 20e49d0

File tree

11 files changed

+295
-79
lines changed

11 files changed

+295
-79
lines changed

package-lock.json

Lines changed: 143 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/backend-data/src/app_sync_policy_generator.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ export class AppSyncPolicyGenerator {
1414
/**
1515
* Initialize with the GraphqlAPI that the policies will be scoped to
1616
*/
17-
constructor(private readonly graphqlApi: IGraphqlApi) {
17+
constructor(
18+
private readonly graphqlApi: IGraphqlApi,
19+
private readonly modelIntrospectionSchemaArn?: string
20+
) {
1821
this.stack = Stack.of(graphqlApi);
1922
}
2023
/**
@@ -29,13 +32,25 @@ export class AppSyncPolicyGenerator {
2932
.map((action) => actionToTypeMap[action])
3033
// convert Type to resourceName
3134
.map((type) => [this.graphqlApi.arn, 'types', type, '*'].join('/'));
32-
return new Policy(this.stack, `${this.policyPrefix}${this.policyCount++}`, {
33-
statements: [
35+
36+
const statements = [
37+
new PolicyStatement({
38+
actions: ['appsync:GraphQL'],
39+
resources,
40+
}),
41+
];
42+
43+
if (this.modelIntrospectionSchemaArn) {
44+
statements.push(
3445
new PolicyStatement({
35-
actions: ['appsync:GraphQL'],
36-
resources,
37-
}),
38-
],
46+
actions: ['s3:GetObject'],
47+
resources: [this.modelIntrospectionSchemaArn],
48+
})
49+
);
50+
}
51+
52+
return new Policy(this.stack, `${this.policyPrefix}${this.policyCount++}`, {
53+
statements,
3954
});
4055
}
4156
}

packages/backend-data/src/factory.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ import {
4646
FunctionSchemaAccess,
4747
JsResolver,
4848
} from '@aws-amplify/data-schema-types';
49+
import { Bucket } from 'aws-cdk-lib/aws-s3';
50+
import { BucketDeployment, Source } from 'aws-cdk-lib/aws-s3-deployment';
51+
52+
const modelIntrospectionSchemaKey = 'modelIntrospectionSchema.json';
4953

5054
/**
5155
* Singleton factory for AmplifyGraphqlApi constructs that can be used in Amplify project files.
@@ -234,13 +238,14 @@ class DataGenerator implements ConstructContainerEntryGenerator {
234238
...schemasLambdaFunctions,
235239
});
236240
let amplifyApi = undefined;
241+
let modelIntrospectionSchema: string | undefined = undefined;
237242

238243
const isSandboxDeployment =
239244
scope.node.tryGetContext(CDKContextKey.DEPLOYMENT_TYPE) === 'sandbox';
240245

241246
try {
242247
const combinedSchema = combineCDKSchemas(amplifyGraphqlDefinitions);
243-
const modelIntrospectionSchema = generateModelsSync({
248+
modelIntrospectionSchema = generateModelsSync({
244249
schema: combinedSchema.schema,
245250
target: 'introspection',
246251
})['model-introspection.json'];
@@ -260,7 +265,6 @@ class DataGenerator implements ConstructContainerEntryGenerator {
260265
allowDestructiveGraphqlSchemaUpdates: true,
261266
_provisionHotswapFriendlyResources: isSandboxDeployment,
262267
},
263-
modelIntrospectionSchema,
264268
});
265269
} catch (error) {
266270
throw new AmplifyUserError(
@@ -273,6 +277,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
273277
);
274278
}
275279

280+
// TODO Any risk that this throws?
281+
const modelIntrospectionSchemaBucket = new Bucket(
282+
scope,
283+
'modelIntrospectionSchemaBucket',
284+
{ enforceSSL: true }
285+
);
286+
new BucketDeployment(scope, 'modelIntrospectionSchemaBucketDeployment', {
287+
// See https://github.com/aws-amplify/amplify-category-api/pull/1939
288+
memoryLimit: 1536,
289+
destinationBucket: modelIntrospectionSchemaBucket,
290+
sources: [
291+
Source.data(modelIntrospectionSchemaKey, modelIntrospectionSchema),
292+
],
293+
});
294+
276295
Tags.of(amplifyApi).add(TagName.FRIENDLY_NAME, this.name);
277296

278297
/**;
@@ -289,10 +308,15 @@ class DataGenerator implements ConstructContainerEntryGenerator {
289308
ssmEnvironmentEntriesGenerator.generateSsmEnvironmentEntries({
290309
[`${this.name}_GRAPHQL_ENDPOINT`]:
291310
amplifyApi.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl,
311+
[`${this.name}_MODEL_INTROSPECTION_SCHEMA_BUCKET_NAME`]:
312+
modelIntrospectionSchemaBucket.bucketName,
313+
[`${this.name}_MODEL_INTROSPECTION_SCHEMA_KEY`]:
314+
modelIntrospectionSchemaKey,
292315
});
293316

294317
const policyGenerator = new AppSyncPolicyGenerator(
295-
amplifyApi.resources.graphqlApi
318+
amplifyApi.resources.graphqlApi,
319+
`${modelIntrospectionSchemaBucket.bucketArn}/${modelIntrospectionSchemaKey}`
296320
);
297321

298322
schemasFunctionSchemaAccess.forEach((accessDefinition) => {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "@aws-amplify/backend-function/internal",
3+
"main": "../dist/cjs/internal/index.js",
4+
"react-native": "../src/internal/index.ts",
5+
"browser": "../dist/esm/internal/index.mjs",
6+
"module": "../dist/esm/internal/index.mjs",
7+
"typings": "../dist/esm/internal/index.d.ts"
8+
}

packages/backend-function/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"types": "./lib/index.d.ts",
1111
"import": "./lib/index.js",
1212
"require": "./lib/index.js"
13+
},
14+
"./internal": {
15+
"types": "./lib/internal/index.d.ts",
16+
"import": "./lib/internal/index.js",
17+
"require": "./lib/internal/index.js"
1318
}
1419
},
1520
"main": "lib/index.js",
@@ -27,6 +32,7 @@
2732
"devDependencies": {
2833
"@aws-amplify/backend-platform-test-stubs": "^0.3.6",
2934
"@aws-amplify/platform-core": "^1.1.0",
35+
"@aws-sdk/client-s3": "^3.624.0",
3036
"@aws-sdk/client-ssm": "^3.624.0",
3137
"aws-sdk": "^2.1550.0",
3238
"uuid": "^9.0.1"

0 commit comments

Comments
 (0)