Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Architecture Property on defineFunction #2370

Merged
merged 3 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/tall-parrots-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@aws-amplify/backend-function': minor
'@aws-amplify/backend': minor
---

adds support for architecture property on defineFunction
4 changes: 4 additions & 0 deletions packages/backend-function/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ type DataClientReturn<T> = T extends DataClientEnv ? DataClientConfig : DataClie
// @public
export const defineFunction: (props?: FunctionProps) => ConstructFactory<ResourceProvider<FunctionResources> & ResourceAccessAcceptorFactory & AddEnvironmentFactory & StackProvider>;

// @public (undocumented)
export type FunctionArchitecture = 'x86_64' | 'arm64';

// @public (undocumented)
export type FunctionBundlingOptions = {
minify?: boolean;
Expand Down Expand Up @@ -94,6 +97,7 @@ export type FunctionProps = {
ephemeralStorageSizeMB?: number;
environment?: Record<string, string | BackendSecret>;
runtime?: NodeVersion;
architecture?: FunctionArchitecture;
schedule?: FunctionSchedule | FunctionSchedule[];
layers?: Record<string, string>;
bundling?: FunctionBundlingOptions;
Expand Down
36 changes: 34 additions & 2 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import {
} from '@aws-amplify/backend-platform-test-stubs';
import { defaultLambda } from './test-assets/default-lambda/resource.js';
import { Template } from 'aws-cdk-lib/assertions';
import { NodeVersion, defineFunction } from './factory.js';
import {
FunctionArchitecture,
NodeVersion,
defineFunction,
} from './factory.js';
import { lambdaWithDependencies } from './test-assets/lambda-with-dependencies/resource.js';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { Architecture, Runtime } from 'aws-cdk-lib/aws-lambda';
import { Policy, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import fsp from 'fs/promises';
import path from 'node:path';
Expand Down Expand Up @@ -454,6 +458,34 @@ void describe('AmplifyFunctionFactory', () => {
});
});

void describe('architecture property', () => {
void it('sets valid architecture', () => {
const lambda = defineFunction({
entry: './test-assets/default-lambda/handler.ts',
architecture: 'arm64',
}).getInstance(getInstanceProps);
const template = Template.fromStack(lambda.stack);

template.hasResourceProperties('AWS::Lambda::Function', {
Architectures: [Architecture.ARM_64.name],
});
});
});

void it('throws on invalid architecture', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
architecture: 'invalid' as FunctionArchitecture,
}).getInstance(getInstanceProps),
new AmplifyUserError('InvalidArchitectureError', {
message: `Invalid function architecture of invalid`,
resolution: 'architecture must be one of the following: arm64, x86_64',
})
);
});

void describe('schedule property', () => {
void it('sets valid schedule - rate', () => {
const lambda = defineFunction({
Expand Down
35 changes: 35 additions & 0 deletions packages/backend-function/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { Rule } from 'aws-cdk-lib/aws-events';
import * as targets from 'aws-cdk-lib/aws-events-targets';
import { Policy } from 'aws-cdk-lib/aws-iam';
import {
Architecture,
CfnFunction,
ILayerVersion,
LayerVersion,
Expand Down Expand Up @@ -134,6 +135,12 @@ export type FunctionProps = {
*/
runtime?: NodeVersion;

/**
* The architecture of the target platform for the lambda environment.
* Defaults to X86_64.
*/
architecture?: FunctionArchitecture;

/**
* A time interval string to periodically run the function.
* This can be either a string of `"every <positive whole number><m (minute) or h (hour)>"`, `"every day|week|month|year"` or cron expression.
Expand Down Expand Up @@ -246,6 +253,7 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
ephemeralStorageSizeMB: this.resolveEphemeralStorageSize(),
environment: this.resolveEnvironment(),
runtime: this.resolveRuntime(),
architecture: this.resolveArchitecture(),
schedule: this.resolveSchedule(),
bundling: this.resolveBundling(),
layers: this.props.layers ?? {},
Expand Down Expand Up @@ -401,6 +409,25 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
return this.props.runtime;
};

private resolveArchitecture = () => {
const architectureDefault = 'x86_64';

if (!this.props.architecture) {
return architectureDefault;
}

if (!(this.props.architecture in architectureMap)) {
throw new AmplifyUserError('InvalidArchitectureError', {
message: `Invalid function architecture of ${this.props.architecture}`,
resolution: `architecture must be one of the following: ${Object.keys(
architectureMap
).join(', ')}`,
});
}

return this.props.architecture;
};

private resolveSchedule = () => {
if (!this.props.schedule) {
return [];
Expand Down Expand Up @@ -539,6 +566,7 @@ class AmplifyFunction
entry: props.entry,
timeout: Duration.seconds(props.timeoutSeconds),
memorySize: props.memoryMB,
architecture: architectureMap[props.architecture],
ephemeralStorageSize: Size.mebibytes(props.ephemeralStorageSizeMB),
runtime: nodeVersionMap[props.runtime],
layers: props.resolvedLayers,
Expand Down Expand Up @@ -678,3 +706,10 @@ const nodeVersionMap: Record<NodeVersion, Runtime> = {
20: Runtime.NODEJS_20_X,
22: Runtime.NODEJS_22_X,
};

export type FunctionArchitecture = 'x86_64' | 'arm64';

const architectureMap: Record<FunctionArchitecture, Architecture> = {
arm64: Architecture.ARM_64,
x86_64: Architecture.X86_64,
};
Loading