Skip to content

Commit 5cbe318

Browse files
stocaaroKamil Sobol
andauthored
feat: Add lamda data client support (#2224)
--------- Co-authored-by: Kamil Sobol <[email protected]>
1 parent 6015595 commit 5cbe318

28 files changed

+845
-27
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@aws-amplify/backend': minor
3+
'@aws-amplify/backend-function': minor
4+
'@aws-amplify/backend-data': patch
5+
---
6+
7+
Add lambda data client

package-lock.json

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

packages/backend-data/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"license": "Apache-2.0",
2121
"devDependencies": {
22-
"@aws-amplify/data-schema": "^1.0.0",
22+
"@aws-amplify/data-schema": "^1.13.4",
2323
"@aws-amplify/backend-platform-test-stubs": "^0.3.6",
2424
"@aws-amplify/platform-core": "^1.2.1"
2525
},
@@ -31,7 +31,8 @@
3131
"@aws-amplify/backend-output-storage": "^1.1.3",
3232
"@aws-amplify/backend-output-schemas": "^1.4.0",
3333
"@aws-amplify/data-construct": "^1.10.1",
34-
"@aws-amplify/plugin-types": "^1.5.0",
35-
"@aws-amplify/data-schema-types": "^1.2.0"
34+
"@aws-amplify/data-schema-types": "^1.2.0",
35+
"@aws-amplify/graphql-generator": "^0.5.1",
36+
"@aws-amplify/plugin-types": "^1.4.0"
3637
}
3738
}

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.test.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ const createConstructContainerWithUserPoolAuthRegistered = (
8585
authenticatedUserIamRole: new Role(stack, 'testAuthRole', {
8686
assumedBy: new ServicePrincipal('test.amazon.com'),
8787
}),
88-
identityPoolId: 'identityPoolId',
8988
cfnResources: {
9089
cfnUserPool: new CfnUserPool(stack, 'CfnUserPool', {}),
9190
cfnUserPoolClient: new CfnUserPoolClient(stack, 'CfnUserPoolClient', {
@@ -101,6 +100,7 @@ const createConstructContainerWithUserPoolAuthRegistered = (
101100
),
102101
},
103102
groups: {},
103+
identityPoolId: 'identityPool',
104104
},
105105
}),
106106
});
@@ -567,6 +567,23 @@ void describe('DataFactory', () => {
567567
},
568568
],
569569
},
570+
{
571+
Action: 's3:GetObject',
572+
Resource: {
573+
'Fn::Join': [
574+
'',
575+
[
576+
{
577+
'Fn::GetAtt': [
578+
'modelIntrospectionSchemaBucketF566B665',
579+
'Arn',
580+
],
581+
},
582+
'/modelIntrospectionSchema.json',
583+
],
584+
],
585+
},
586+
},
570587
],
571588
},
572589
Roles: [
@@ -675,6 +692,23 @@ void describe('DataFactory', () => {
675692
],
676693
},
677694
},
695+
{
696+
Action: 's3:GetObject',
697+
Resource: {
698+
'Fn::Join': [
699+
'',
700+
[
701+
{
702+
'Fn::GetAtt': [
703+
'modelIntrospectionSchemaBucketF566B665',
704+
'Arn',
705+
],
706+
},
707+
'/modelIntrospectionSchema.json',
708+
],
709+
],
710+
},
711+
},
678712
],
679713
},
680714
Roles: [
@@ -701,6 +735,23 @@ void describe('DataFactory', () => {
701735
],
702736
},
703737
},
738+
{
739+
Action: 's3:GetObject',
740+
Resource: {
741+
'Fn::Join': [
742+
'',
743+
[
744+
{
745+
'Fn::GetAtt': [
746+
'modelIntrospectionSchemaBucketF566B665',
747+
'Arn',
748+
],
749+
},
750+
'/modelIntrospectionSchema.json',
751+
],
752+
],
753+
},
754+
},
704755
],
705756
},
706757
Roles: [

packages/backend-data/src/factory.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
TranslationBehavior,
1919
} from '@aws-amplify/data-construct';
2020
import { GraphqlOutput } from '@aws-amplify/backend-output-schemas';
21+
import { generateModelsSync } from '@aws-amplify/graphql-generator';
2122
import * as path from 'path';
2223
import { AmplifyDataError, DataProps } from './types.js';
2324
import {
@@ -40,13 +41,17 @@ import {
4041
CDKContextKey,
4142
TagName,
4243
} from '@aws-amplify/platform-core';
43-
import { Aspects, IAspect, Tags } from 'aws-cdk-lib';
44+
import { Aspects, IAspect, RemovalPolicy, Tags } from 'aws-cdk-lib';
4445
import { convertJsResolverDefinition } from './convert_js_resolvers.js';
4546
import { AppSyncPolicyGenerator } from './app_sync_policy_generator.js';
4647
import {
4748
FunctionSchemaAccess,
4849
JsResolver,
4950
} from '@aws-amplify/data-schema-types';
51+
import { Bucket } from 'aws-cdk-lib/aws-s3';
52+
import { BucketDeployment, Source } from 'aws-cdk-lib/aws-s3-deployment';
53+
54+
const modelIntrospectionSchemaKey = 'modelIntrospectionSchema.json';
5055

5156
/**
5257
* Singleton factory for AmplifyGraphqlApi constructs that can be used in Amplify project files.
@@ -233,14 +238,21 @@ class DataGenerator implements ConstructContainerEntryGenerator {
233238
...schemasLambdaFunctions,
234239
});
235240
let amplifyApi = undefined;
241+
let modelIntrospectionSchema: string | undefined = undefined;
236242

237243
const isSandboxDeployment =
238244
scope.node.tryGetContext(CDKContextKey.DEPLOYMENT_TYPE) === 'sandbox';
239245

240246
try {
247+
const combinedSchema = combineCDKSchemas(amplifyGraphqlDefinitions);
248+
modelIntrospectionSchema = generateModelsSync({
249+
schema: combinedSchema.schema,
250+
target: 'introspection',
251+
})['model-introspection.json'];
252+
241253
amplifyApi = new AmplifyData(scope, this.name, {
242254
apiName: this.name,
243-
definition: combineCDKSchemas(amplifyGraphqlDefinitions),
255+
definition: combinedSchema,
244256
authorizationModes,
245257
outputStorageStrategy: this.outputStorageStrategy,
246258
functionNameMap,
@@ -265,6 +277,24 @@ class DataGenerator implements ConstructContainerEntryGenerator {
265277
);
266278
}
267279

280+
const modelIntrospectionSchemaBucket = new Bucket(
281+
scope,
282+
'modelIntrospectionSchemaBucket',
283+
{
284+
enforceSSL: true,
285+
autoDeleteObjects: true,
286+
removalPolicy: RemovalPolicy.DESTROY,
287+
}
288+
);
289+
new BucketDeployment(scope, 'modelIntrospectionSchemaBucketDeployment', {
290+
// See https://github.com/aws-amplify/amplify-category-api/pull/1939
291+
memoryLimit: 1536,
292+
destinationBucket: modelIntrospectionSchemaBucket,
293+
sources: [
294+
Source.data(modelIntrospectionSchemaKey, modelIntrospectionSchema),
295+
],
296+
});
297+
268298
Tags.of(amplifyApi).add(TagName.FRIENDLY_NAME, this.name);
269299

270300
/**;
@@ -281,10 +311,15 @@ class DataGenerator implements ConstructContainerEntryGenerator {
281311
ssmEnvironmentEntriesGenerator.generateSsmEnvironmentEntries({
282312
[`${this.name}_GRAPHQL_ENDPOINT`]:
283313
amplifyApi.resources.cfnResources.cfnGraphqlApi.attrGraphQlUrl,
314+
[`${this.name}_MODEL_INTROSPECTION_SCHEMA_BUCKET_NAME`]:
315+
modelIntrospectionSchemaBucket.bucketName,
316+
[`${this.name}_MODEL_INTROSPECTION_SCHEMA_KEY`]:
317+
modelIntrospectionSchemaKey,
284318
});
285319

286320
const policyGenerator = new AppSyncPolicyGenerator(
287-
amplifyApi.resources.graphqlApi
321+
amplifyApi.resources.graphqlApi,
322+
`${modelIntrospectionSchemaBucket.bucketArn}/${modelIntrospectionSchemaKey}`
288323
);
289324

290325
schemasFunctionSchemaAccess.forEach((accessDefinition) => {

0 commit comments

Comments
 (0)