Skip to content

Commit 87dbf41

Browse files
ShadowCat567Vieltojarvisobolk
authored
Exposing stack parameter at higher level for backend and related resources (#2021)
* test with adding tags * testing stack change * implementation attempts * revert accidental package-lock change * added stack param to function resource * added changeset * api updates * Revert "api updates" This reverts commit 80c981e. * adjusted getting stack param for auth * attempt to fix auth stack param * api updates * trying to make api happy * Revert "api updates" This reverts commit 79678cc. * removed comment * update to exposed stack for auth, function, and backend * exposed stack for storage * update changesets * updated storage stack param * api updates * added tests to backend, auth, storage, and function * updated factory tests for function * updates to factory test for auth * updates to api * trying to make api happy * stack parameter exposed for data * updated changeset * api update * reverted exposing stack param for data * Revert "api update" This reverts commit 558692f. * removed changeset that is not needed * Update .changeset/chatty-beans-begin.md Co-authored-by: Kamil Sobol <sobol.k.r@gmail.com> * update changeset, variable name changes * minor adjustment to backend-function factory test --------- Co-authored-by: Vieltojarvi <lvielto@amazon.com> Co-authored-by: Kamil Sobol <sobol.k.r@gmail.com>
1 parent 9e11e5d commit 87dbf41

File tree

20 files changed

+103
-37
lines changed

20 files changed

+103
-37
lines changed

.changeset/chatty-beans-begin.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@aws-amplify/backend-function': minor
3+
'@aws-amplify/backend-auth': minor
4+
'@aws-amplify/backend': minor
5+
'@aws-amplify/backend-storage': minor
6+
---
7+
8+
expose stack property for backend, function resource, storage resource, and auth resource

.changeset/four-drinks-yell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/plugin-types': minor
3+
---
4+
5+
add new type to handle exposing stack

packages/backend-auth/API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { OidcProviderProps } from '@aws-amplify/auth-construct';
2020
import { ResourceAccessAcceptor } from '@aws-amplify/plugin-types';
2121
import { ResourceAccessAcceptorFactory } from '@aws-amplify/plugin-types';
2222
import { ResourceProvider } from '@aws-amplify/plugin-types';
23+
import { StackProvider } from '@aws-amplify/plugin-types';
2324
import { TriggerEvent } from '@aws-amplify/auth-construct';
2425

2526
// @public
@@ -77,7 +78,7 @@ export type AuthLoginWithFactoryProps = Omit<AuthProps['loginWith'], 'externalPr
7778
};
7879

7980
// @public (undocumented)
80-
export type BackendAuth = ResourceProvider<AuthResources> & ResourceAccessAcceptorFactory<AuthRoleName | string>;
81+
export type BackendAuth = ResourceProvider<AuthResources> & ResourceAccessAcceptorFactory<AuthRoleName | string> & StackProvider;
8182

8283
// @public
8384
export const defineAuth: (props: AmplifyAuthProps) => ConstructFactory<BackendAuth>;

packages/backend-auth/src/factory.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,15 @@ void describe('AmplifyAuthFactory', () => {
7979
assert.strictEqual(instance1, instance2);
8080
});
8181

82+
void it('verifies stack property exists and is equivalent to auth stack', () => {
83+
const backendAuth = authFactory.getInstance(getInstanceProps);
84+
assert.equal(backendAuth.stack, Stack.of(backendAuth.resources.userPool));
85+
});
86+
8287
void it('adds construct to stack', () => {
8388
const backendAuth = authFactory.getInstance(getInstanceProps);
8489

85-
const template = Template.fromStack(
86-
Stack.of(backendAuth.resources.userPool)
87-
);
90+
const template = Template.fromStack(backendAuth.stack);
8891

8992
template.resourceCountIs('AWS::Cognito::UserPool', 1);
9093
});
@@ -98,9 +101,7 @@ void describe('AmplifyAuthFactory', () => {
98101

99102
const backendAuth = authFactory.getInstance(getInstanceProps);
100103

101-
const template = Template.fromStack(
102-
Stack.of(backendAuth.resources.userPool)
103-
);
104+
const template = Template.fromStack(backendAuth.stack);
104105

105106
template.resourceCountIs('AWS::Cognito::UserPool', 1);
106107
template.hasResourceProperties('AWS::Cognito::UserPool', {
@@ -247,9 +248,7 @@ void describe('AmplifyAuthFactory', () => {
247248

248249
const backendAuth = authWithTriggerFactory.getInstance(getInstanceProps);
249250

250-
const template = Template.fromStack(
251-
Stack.of(backendAuth.resources.userPool)
252-
);
251+
const template = Template.fromStack(backendAuth.stack);
253252
template.hasResourceProperties('AWS::Cognito::UserPool', {
254253
LambdaConfig: {
255254
// The key in the CFN template is the trigger event name with the first character uppercase

packages/backend-auth/src/factory.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ResourceAccessAcceptor,
1919
ResourceAccessAcceptorFactory,
2020
ResourceProvider,
21+
StackProvider,
2122
} from '@aws-amplify/plugin-types';
2223
import { translateToAuthConstructLoginWith } from './translate_auth_props.js';
2324
import { authAccessBuilder as _authAccessBuilder } from './access_builder.js';
@@ -28,10 +29,11 @@ import {
2829
Expand,
2930
} from './types.js';
3031
import { UserPoolAccessPolicyFactory } from './userpool_access_policy_factory.js';
31-
import { Tags } from 'aws-cdk-lib';
32+
import { Stack, Tags } from 'aws-cdk-lib';
3233

3334
export type BackendAuth = ResourceProvider<AuthResources> &
34-
ResourceAccessAcceptorFactory<AuthRoleName | string>;
35+
ResourceAccessAcceptorFactory<AuthRoleName | string> &
36+
StackProvider;
3537

3638
export type AmplifyAuthProps = Expand<
3739
Omit<AuthProps, 'outputStorageStrategy' | 'loginWith'> & {
@@ -195,6 +197,7 @@ class AmplifyAuthGenerator implements ConstructContainerEntryGenerator {
195197
policy.attachToRole(role);
196198
},
197199
}),
200+
stack: Stack.of(authConstruct),
198201
};
199202
if (!this.props.access) {
200203
return authConstructMixin;

packages/backend-function/API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ConstructFactory } from '@aws-amplify/plugin-types';
99
import { FunctionResources } from '@aws-amplify/plugin-types';
1010
import { ResourceAccessAcceptorFactory } from '@aws-amplify/plugin-types';
1111
import { ResourceProvider } from '@aws-amplify/plugin-types';
12+
import { StackProvider } from '@aws-amplify/plugin-types';
1213

1314
// @public (undocumented)
1415
export type AddEnvironmentFactory = {
@@ -19,7 +20,7 @@ export type AddEnvironmentFactory = {
1920
export type CronSchedule = `${string} ${string} ${string} ${string} ${string}` | `${string} ${string} ${string} ${string} ${string} ${string}`;
2021

2122
// @public
22-
export const defineFunction: (props?: FunctionProps) => ConstructFactory<ResourceProvider<FunctionResources> & ResourceAccessAcceptorFactory & AddEnvironmentFactory>;
23+
export const defineFunction: (props?: FunctionProps) => ConstructFactory<ResourceProvider<FunctionResources> & ResourceAccessAcceptorFactory & AddEnvironmentFactory & StackProvider>;
2324

2425
// @public (undocumented)
2526
export type FunctionProps = {

packages/backend-function/src/factory.test.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,16 @@ void describe('AmplifyFunctionFactory', () => {
5959
assert.strictEqual(instance1, instance2);
6060
});
6161

62+
void it('verifies stack property exists and is equal to function stack', () => {
63+
const functionFactory = defaultLambda;
64+
const lambda = functionFactory.getInstance(getInstanceProps);
65+
assert.equal(lambda.stack, Stack.of(lambda.resources.lambda));
66+
});
67+
6268
void it('resolves default name and entry when no args specified', () => {
6369
const functionFactory = defaultLambda;
6470
const lambda = functionFactory.getInstance(getInstanceProps);
65-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
71+
const template = Template.fromStack(lambda.stack);
6672
template.resourceCountIs('AWS::Lambda::Function', 1);
6773
template.hasResourceProperties('AWS::Lambda::Function', {
6874
Handler: 'index.handler',
@@ -79,7 +85,7 @@ void describe('AmplifyFunctionFactory', () => {
7985
entry: './test-assets/default-lambda/handler.ts',
8086
});
8187
const lambda = functionFactory.getInstance(getInstanceProps);
82-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
88+
const template = Template.fromStack(lambda.stack);
8389
template.resourceCountIs('AWS::Lambda::Function', 1);
8490
template.hasResourceProperties('AWS::Lambda::Function', {
8591
Handler: 'index.handler',
@@ -96,7 +102,7 @@ void describe('AmplifyFunctionFactory', () => {
96102
name: 'myCoolLambda',
97103
});
98104
const lambda = functionFactory.getInstance(getInstanceProps);
99-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
105+
const template = Template.fromStack(lambda.stack);
100106
template.resourceCountIs('AWS::Lambda::Function', 1);
101107
template.hasResourceProperties('AWS::Lambda::Function', {
102108
Handler: 'index.handler',
@@ -113,7 +119,7 @@ void describe('AmplifyFunctionFactory', () => {
113119
name: 'myCoolLambda',
114120
});
115121
const lambda = functionFactory.getInstance(getInstanceProps);
116-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
122+
const template = Template.fromStack(lambda.stack);
117123
template.resourceCountIs('AWS::Lambda::Function', 1);
118124
template.hasResourceProperties('AWS::Lambda::Function', {
119125
Tags: [{ Key: 'amplify:friendly-name', Value: 'myCoolLambda' }],
@@ -137,7 +143,7 @@ void describe('AmplifyFunctionFactory', () => {
137143

138144
void it('builds lambda with local and 3p dependencies', () => {
139145
const lambda = lambdaWithDependencies.getInstance(getInstanceProps);
140-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
146+
const template = Template.fromStack(lambda.stack);
141147
// There isn't a way to check the contents of the bundled lambda using the CDK Template utility
142148
// So we just check that the lambda was created properly in the CFN template.
143149
// There is an e2e test that validates proper lambda bundling
@@ -159,7 +165,7 @@ void describe('AmplifyFunctionFactory', () => {
159165
});
160166
const lambda = functionFactory.getInstance(getInstanceProps);
161167
lambda.addEnvironment('key1', 'value1');
162-
const stack = Stack.of(lambda.resources.lambda);
168+
const stack = lambda.stack;
163169
const template = Template.fromStack(stack);
164170
template.resourceCountIs('AWS::Lambda::Function', 1);
165171
template.hasResourceProperties('AWS::Lambda::Function', {
@@ -177,7 +183,7 @@ void describe('AmplifyFunctionFactory', () => {
177183
entry: './test-assets/default-lambda/handler.ts',
178184
timeoutSeconds: 10,
179185
}).getInstance(getInstanceProps);
180-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
186+
const template = Template.fromStack(lambda.stack);
181187

182188
template.hasResourceProperties('AWS::Lambda::Function', {
183189
Timeout: 10,
@@ -230,7 +236,7 @@ void describe('AmplifyFunctionFactory', () => {
230236
entry: './test-assets/default-lambda/handler.ts',
231237
memoryMB: 234,
232238
}).getInstance(getInstanceProps);
233-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
239+
const template = Template.fromStack(lambda.stack);
234240

235241
template.hasResourceProperties('AWS::Lambda::Function', {
236242
MemorySize: 234,
@@ -241,7 +247,7 @@ void describe('AmplifyFunctionFactory', () => {
241247
const lambda = defineFunction({
242248
entry: './test-assets/default-lambda/handler.ts',
243249
}).getInstance(getInstanceProps);
244-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
250+
const template = Template.fromStack(lambda.stack);
245251

246252
template.hasResourceProperties('AWS::Lambda::Function', {
247253
MemorySize: 512,
@@ -294,7 +300,7 @@ void describe('AmplifyFunctionFactory', () => {
294300
entry: './test-assets/default-lambda/handler.ts',
295301
runtime: 16,
296302
}).getInstance(getInstanceProps);
297-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
303+
const template = Template.fromStack(lambda.stack);
298304

299305
template.hasResourceProperties('AWS::Lambda::Function', {
300306
Runtime: Runtime.NODEJS_16_X.name,
@@ -305,7 +311,7 @@ void describe('AmplifyFunctionFactory', () => {
305311
const lambda = defineFunction({
306312
entry: './test-assets/default-lambda/handler.ts',
307313
}).getInstance(getInstanceProps);
308-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
314+
const template = Template.fromStack(lambda.stack);
309315

310316
template.hasResourceProperties('AWS::Lambda::Function', {
311317
Runtime: Runtime.NODEJS_18_X.name,
@@ -340,7 +346,7 @@ void describe('AmplifyFunctionFactory', () => {
340346
entry: './test-assets/default-lambda/handler.ts',
341347
schedule: 'every 5m',
342348
}).getInstance(getInstanceProps);
343-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
349+
const template = Template.fromStack(lambda.stack);
344350

345351
template.hasResourceProperties('AWS::Events::Rule', {
346352
ScheduleExpression: 'cron(*/5 * * * ? *)',
@@ -361,7 +367,7 @@ void describe('AmplifyFunctionFactory', () => {
361367
entry: './test-assets/default-lambda/handler.ts',
362368
schedule: '0 1 * * ?',
363369
}).getInstance(getInstanceProps);
364-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
370+
const template = Template.fromStack(lambda.stack);
365371

366372
template.hasResourceProperties('AWS::Events::Rule', {
367373
ScheduleExpression: 'cron(0 1 * * ? *)',
@@ -382,7 +388,7 @@ void describe('AmplifyFunctionFactory', () => {
382388
entry: './test-assets/default-lambda/handler.ts',
383389
schedule: ['0 1 * * ?', 'every 5m'],
384390
}).getInstance(getInstanceProps);
385-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
391+
const template = Template.fromStack(lambda.stack);
386392

387393
template.resourceCountIs('AWS::Events::Rule', 2);
388394

@@ -399,7 +405,7 @@ void describe('AmplifyFunctionFactory', () => {
399405
const lambda = defineFunction({
400406
entry: './test-assets/default-lambda/handler.ts',
401407
}).getInstance(getInstanceProps);
402-
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
408+
const template = Template.fromStack(lambda.stack);
403409

404410
template.resourceCountIs('AWS::Events::Rule', 0);
405411
});
@@ -412,7 +418,7 @@ void describe('AmplifyFunctionFactory', () => {
412418
name: 'myCoolLambda',
413419
});
414420
const lambda = functionFactory.getInstance(getInstanceProps);
415-
const stack = Stack.of(lambda.resources.lambda);
421+
const stack = lambda.stack;
416422
const policy = new Policy(stack, 'testPolicy', {
417423
statements: [
418424
new PolicyStatement({
@@ -503,9 +509,7 @@ void describe('AmplifyFunctionFactory', () => {
503509
entry: './test-assets/default-lambda/handler.ts',
504510
name: 'anotherName',
505511
});
506-
const functionStack = Stack.of(
507-
functionFactory.getInstance(getInstanceProps).resources.lambda
508-
);
512+
const functionStack = functionFactory.getInstance(getInstanceProps).stack;
509513
anotherFunction.getInstance(getInstanceProps);
510514
const template = Template.fromStack(functionStack);
511515
assert.equal(

packages/backend-function/src/factory.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
ResourceNameValidator,
1212
ResourceProvider,
1313
SsmEnvironmentEntry,
14+
StackProvider,
1415
} from '@aws-amplify/plugin-types';
1516
import { Construct } from 'constructs';
1617
import { NodejsFunction, OutputFormat } from 'aws-cdk-lib/aws-lambda-nodejs';
@@ -64,7 +65,8 @@ export const defineFunction = (
6465
): ConstructFactory<
6566
ResourceProvider<FunctionResources> &
6667
ResourceAccessAcceptorFactory &
67-
AddEnvironmentFactory
68+
AddEnvironmentFactory &
69+
StackProvider
6870
> => new FunctionFactory(props, new Error().stack);
6971

7072
export type FunctionProps = {
@@ -308,6 +310,7 @@ class AmplifyFunction
308310
AddEnvironmentFactory
309311
{
310312
readonly resources: FunctionResources;
313+
readonly stack: Stack;
311314
private readonly functionEnvironmentTranslator: FunctionEnvironmentTranslator;
312315
constructor(
313316
scope: Construct,
@@ -318,6 +321,8 @@ class AmplifyFunction
318321
) {
319322
super(scope, id);
320323

324+
this.stack = Stack.of(scope);
325+
321326
const runtime = nodeVersionMap[props.runtime];
322327

323328
const require = createRequire(import.meta.url);

packages/backend-storage/API.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { IBucket } from 'aws-cdk-lib/aws-s3';
1414
import { ResourceAccessAcceptor } from '@aws-amplify/plugin-types';
1515
import { ResourceAccessAcceptorFactory } from '@aws-amplify/plugin-types';
1616
import { ResourceProvider } from '@aws-amplify/plugin-types';
17+
import { StackProvider } from '@aws-amplify/plugin-types';
1718
import { StorageOutput } from '@aws-amplify/backend-output-schemas';
1819

1920
// @public (undocumented)
@@ -34,7 +35,7 @@ export type AmplifyStorageProps = {
3435
export type AmplifyStorageTriggerEvent = 'onDelete' | 'onUpload';
3536

3637
// @public
37-
export const defineStorage: (props: AmplifyStorageFactoryProps) => ConstructFactory<ResourceProvider<StorageResources>>;
38+
export const defineStorage: (props: AmplifyStorageFactoryProps) => ConstructFactory<ResourceProvider<StorageResources> & StackProvider>;
3839

3940
// @public
4041
export type EntityId = 'identity';

packages/backend-storage/src/construct.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ConstructFactory,
1313
FunctionResources,
1414
ResourceProvider,
15+
StackProvider,
1516
} from '@aws-amplify/plugin-types';
1617
import { StorageOutput } from '@aws-amplify/backend-output-schemas';
1718
import { RemovalPolicy, Stack } from 'aws-cdk-lib';
@@ -77,8 +78,9 @@ export type StorageResources = {
7778
*/
7879
export class AmplifyStorage
7980
extends Construct
80-
implements ResourceProvider<StorageResources>
81+
implements ResourceProvider<StorageResources>, StackProvider
8182
{
83+
readonly stack: Stack;
8284
readonly resources: StorageResources;
8385
readonly isDefault: boolean;
8486
readonly name: string;
@@ -89,6 +91,7 @@ export class AmplifyStorage
8991
super(scope, id);
9092
this.isDefault = props.isDefault || false;
9193
this.name = props.name;
94+
this.stack = Stack.of(scope);
9295

9396
const bucketProps: BucketProps = {
9497
versioned: props.versioned || false,

0 commit comments

Comments
 (0)