-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Construct } from 'constructs'; | ||
import { | ||
BackendOutputStorageStrategy, | ||
FunctionResources, | ||
ResourceAccessAcceptor, | ||
ResourceAccessAcceptorFactory, | ||
ResourceProvider, | ||
} from '@aws-amplify/plugin-types'; | ||
import { Stack } from 'aws-cdk-lib'; | ||
import { | ||
FunctionOutput, | ||
functionOutputKey, | ||
} from '@aws-amplify/backend-output-schemas'; | ||
import { AttributionMetadataStorage } from '@aws-amplify/backend-output-storage'; | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
const functionStackType = 'function-Lambda'; | ||
|
||
/** | ||
* A base class for function constructs. | ||
*/ | ||
export abstract class AmplifyFunctionBase | ||
extends Construct | ||
implements ResourceProvider<FunctionResources>, ResourceAccessAcceptorFactory | ||
{ | ||
readonly stack: Stack; | ||
abstract resources: FunctionResources; | ||
|
||
abstract getResourceAccessAcceptor: () => ResourceAccessAcceptor; | ||
|
||
/** | ||
* Creates base function construct. | ||
*/ | ||
protected constructor( | ||
scope: Construct, | ||
id: string, | ||
private readonly outputStorageStrategy: BackendOutputStorageStrategy<FunctionOutput> | ||
) { | ||
super(scope, id); | ||
|
||
this.stack = Stack.of(scope); | ||
|
||
new AttributionMetadataStorage().storeAttributionMetadata( | ||
Stack.of(this), | ||
functionStackType, | ||
fileURLToPath(new URL('../package.json', import.meta.url)) | ||
); | ||
} | ||
|
||
protected storeOutput = (): void => { | ||
this.outputStorageStrategy.appendToBackendOutputList(functionOutputKey, { | ||
version: '1', | ||
payload: { | ||
definedFunctions: this.resources.lambda.functionName, | ||
}, | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
ResourceAccessAcceptor, | ||
SsmEnvironmentEntry, | ||
} from '@aws-amplify/plugin-types'; | ||
import { FunctionEnvironmentTranslator } from './function_env_translator.js'; | ||
import { AmplifyFunctionBase } from './function_construct_base.js'; | ||
import { Policy } from 'aws-cdk-lib/aws-iam'; | ||
|
||
/** | ||
* A function resource acceptor. | ||
*/ | ||
export class FunctionResourceAccessAcceptor implements ResourceAccessAcceptor { | ||
readonly identifier: string; | ||
|
||
/** | ||
* Creates function resource acceptor. | ||
*/ | ||
constructor( | ||
private readonly func: AmplifyFunctionBase, | ||
private readonly functionEnvironmentTranslator?: FunctionEnvironmentTranslator | ||
) { | ||
this.identifier = `${func.node.id}LambdaResourceAccessAcceptor`; | ||
} | ||
|
||
acceptResourceAccess = ( | ||
policy: Policy, | ||
ssmEnvironmentEntries: SsmEnvironmentEntry[] | ||
) => { | ||
const role = this.func.resources.lambda.role; | ||
if (!role) { | ||
// This should never happen since we are using the Function L2 construct | ||
throw new Error( | ||
'No execution role found to attach lambda permissions to' | ||
); | ||
} | ||
policy.attachToRole(role); | ||
if (this.functionEnvironmentTranslator) { | ||
for (const { name, path } of ssmEnvironmentEntries) { | ||
this.functionEnvironmentTranslator.addSsmEnvironmentEntry(name, path); | ||
} | ||
} | ||
}; | ||
} |