Skip to content

Commit b1c0f0d

Browse files
caioquirinosobolkAmplifiyer
authored
Adds Architecture Property on defineFunction (#2370)
* Adds Architecture Property on defineFunction * Apply suggestions from code review Co-authored-by: Kamil Sobol <[email protected]> Co-authored-by: Amplifiyer <[email protected]> * Fixed error in tests --------- Co-authored-by: Kamil Sobol <[email protected]> Co-authored-by: Amplifiyer <[email protected]>
1 parent 22089b7 commit b1c0f0d

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

.changeset/tall-parrots-sort.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/backend-function': minor
3+
'@aws-amplify/backend': minor
4+
---
5+
6+
adds support for architecture property on defineFunction

packages/backend-function/API.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ type DataClientReturn<T> = T extends DataClientEnv ? DataClientConfig : DataClie
6464
// @public
6565
export const defineFunction: (props?: FunctionProps) => ConstructFactory<ResourceProvider<FunctionResources> & ResourceAccessAcceptorFactory & AddEnvironmentFactory & StackProvider>;
6666

67+
// @public (undocumented)
68+
export type FunctionArchitecture = 'x86_64' | 'arm64';
69+
6770
// @public (undocumented)
6871
export type FunctionBundlingOptions = {
6972
minify?: boolean;
@@ -94,6 +97,7 @@ export type FunctionProps = {
9497
ephemeralStorageSizeMB?: number;
9598
environment?: Record<string, string | BackendSecret>;
9699
runtime?: NodeVersion;
100+
architecture?: FunctionArchitecture;
97101
schedule?: FunctionSchedule | FunctionSchedule[];
98102
layers?: Record<string, string>;
99103
bundling?: FunctionBundlingOptions;

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ import {
1313
} from '@aws-amplify/backend-platform-test-stubs';
1414
import { defaultLambda } from './test-assets/default-lambda/resource.js';
1515
import { Template } from 'aws-cdk-lib/assertions';
16-
import { NodeVersion, defineFunction } from './factory.js';
16+
import {
17+
FunctionArchitecture,
18+
NodeVersion,
19+
defineFunction,
20+
} from './factory.js';
1721
import { lambdaWithDependencies } from './test-assets/lambda-with-dependencies/resource.js';
18-
import { Runtime } from 'aws-cdk-lib/aws-lambda';
22+
import { Architecture, Runtime } from 'aws-cdk-lib/aws-lambda';
1923
import { Policy, PolicyStatement } from 'aws-cdk-lib/aws-iam';
2024
import fsp from 'fs/promises';
2125
import path from 'node:path';
@@ -454,6 +458,34 @@ void describe('AmplifyFunctionFactory', () => {
454458
});
455459
});
456460

461+
void describe('architecture property', () => {
462+
void it('sets valid architecture', () => {
463+
const lambda = defineFunction({
464+
entry: './test-assets/default-lambda/handler.ts',
465+
architecture: 'arm64',
466+
}).getInstance(getInstanceProps);
467+
const template = Template.fromStack(lambda.stack);
468+
469+
template.hasResourceProperties('AWS::Lambda::Function', {
470+
Architectures: [Architecture.ARM_64.name],
471+
});
472+
});
473+
});
474+
475+
void it('throws on invalid architecture', () => {
476+
assert.throws(
477+
() =>
478+
defineFunction({
479+
entry: './test-assets/default-lambda/handler.ts',
480+
architecture: 'invalid' as FunctionArchitecture,
481+
}).getInstance(getInstanceProps),
482+
new AmplifyUserError('InvalidArchitectureError', {
483+
message: `Invalid function architecture of invalid`,
484+
resolution: 'architecture must be one of the following: arm64, x86_64',
485+
})
486+
);
487+
});
488+
457489
void describe('schedule property', () => {
458490
void it('sets valid schedule - rate', () => {
459491
const lambda = defineFunction({

packages/backend-function/src/factory.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { Rule } from 'aws-cdk-lib/aws-events';
3131
import * as targets from 'aws-cdk-lib/aws-events-targets';
3232
import { Policy } from 'aws-cdk-lib/aws-iam';
3333
import {
34+
Architecture,
3435
CfnFunction,
3536
ILayerVersion,
3637
LayerVersion,
@@ -134,6 +135,12 @@ export type FunctionProps = {
134135
*/
135136
runtime?: NodeVersion;
136137

138+
/**
139+
* The architecture of the target platform for the lambda environment.
140+
* Defaults to X86_64.
141+
*/
142+
architecture?: FunctionArchitecture;
143+
137144
/**
138145
* A time interval string to periodically run the function.
139146
* This can be either a string of `"every <positive whole number><m (minute) or h (hour)>"`, `"every day|week|month|year"` or cron expression.
@@ -246,6 +253,7 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
246253
ephemeralStorageSizeMB: this.resolveEphemeralStorageSize(),
247254
environment: this.resolveEnvironment(),
248255
runtime: this.resolveRuntime(),
256+
architecture: this.resolveArchitecture(),
249257
schedule: this.resolveSchedule(),
250258
bundling: this.resolveBundling(),
251259
layers: this.props.layers ?? {},
@@ -401,6 +409,25 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
401409
return this.props.runtime;
402410
};
403411

412+
private resolveArchitecture = () => {
413+
const architectureDefault = 'x86_64';
414+
415+
if (!this.props.architecture) {
416+
return architectureDefault;
417+
}
418+
419+
if (!(this.props.architecture in architectureMap)) {
420+
throw new AmplifyUserError('InvalidArchitectureError', {
421+
message: `Invalid function architecture of ${this.props.architecture}`,
422+
resolution: `architecture must be one of the following: ${Object.keys(
423+
architectureMap
424+
).join(', ')}`,
425+
});
426+
}
427+
428+
return this.props.architecture;
429+
};
430+
404431
private resolveSchedule = () => {
405432
if (!this.props.schedule) {
406433
return [];
@@ -539,6 +566,7 @@ class AmplifyFunction
539566
entry: props.entry,
540567
timeout: Duration.seconds(props.timeoutSeconds),
541568
memorySize: props.memoryMB,
569+
architecture: architectureMap[props.architecture],
542570
ephemeralStorageSize: Size.mebibytes(props.ephemeralStorageSizeMB),
543571
runtime: nodeVersionMap[props.runtime],
544572
layers: props.resolvedLayers,
@@ -678,3 +706,10 @@ const nodeVersionMap: Record<NodeVersion, Runtime> = {
678706
20: Runtime.NODEJS_20_X,
679707
22: Runtime.NODEJS_22_X,
680708
};
709+
710+
export type FunctionArchitecture = 'x86_64' | 'arm64';
711+
712+
const architectureMap: Record<FunctionArchitecture, Architecture> = {
713+
arm64: Architecture.ARM_64,
714+
x86_64: Architecture.X86_64,
715+
};

0 commit comments

Comments
 (0)