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

Add ephemeralStorageSizeMB option to defineFunction #2283

Merged
merged 8 commits into from
Dec 14, 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
5 changes: 5 additions & 0 deletions .changeset/nasty-tables-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/backend-function': minor
---

Add ephemeralStorageSizeMB option to defineFunction
1 change: 1 addition & 0 deletions .eslint_dictionary.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"lstat",
"macos",
"matchers",
"mebibytes",
"mfas",
"minify",
"mkdtemp",
Expand Down
1 change: 1 addition & 0 deletions packages/backend-function/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type FunctionProps = {
entry?: string;
timeoutSeconds?: number;
memoryMB?: number;
ephemeralStorageSizeMB?: number;
environment?: Record<string, string | BackendSecret>;
runtime?: NodeVersion;
schedule?: FunctionSchedule | FunctionSchedule[];
Expand Down
67 changes: 67 additions & 0 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,4 +682,71 @@ void describe('AmplifyFunctionFactory', () => {
'function-Lambda'
);
});

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

template.hasResourceProperties('AWS::Lambda::Function', {
EphemeralStorage: { Size: 1024 },
});
});

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

template.hasResourceProperties('AWS::Lambda::Function', {
EphemeralStorage: { Size: 512 },
});
});

void it('throws on ephemeralStorageSizeMB below 512 MB', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
ephemeralStorageSizeMB: 511,
}).getInstance(getInstanceProps),
new AmplifyUserError('InvalidEphemeralStorageSizeMBError', {
message: `Invalid function ephemeralStorageSizeMB of 511`,
resolution: `ephemeralStorageSizeMB must be a whole number between 512 and 10240 inclusive`,
})
);
});

void it('throws on ephemeralStorageSizeMB above 10240 MB', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
ephemeralStorageSizeMB: 10241,
}).getInstance(getInstanceProps),
new AmplifyUserError('InvalidEphemeralStorageSizeMBError', {
message: `Invalid function ephemeralStorageSizeMB of 10241`,
resolution: `ephemeralStorageSizeMB must be a whole number between 512 and 10240 inclusive`,
})
);
});

void it('throws on fractional ephemeralStorageSizeMB', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
ephemeralStorageSizeMB: 512.5,
}).getInstance(getInstanceProps),
new AmplifyUserError('InvalidEphemeralStorageSizeMBError', {
message: `Invalid function ephemeralStorageSizeMB of 512.5`,
resolution: `ephemeralStorageSizeMB must be a whole number between 512 and 10240 inclusive`,
})
);
});
});
});
33 changes: 32 additions & 1 deletion packages/backend-function/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
SsmEnvironmentEntry,
StackProvider,
} from '@aws-amplify/plugin-types';
import { Duration, Stack, Tags } from 'aws-cdk-lib';
import { Duration, Size, Stack, Tags } from 'aws-cdk-lib';
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';
Expand Down Expand Up @@ -115,6 +115,13 @@ export type FunctionProps = {
*/
memoryMB?: number;

/**
* The size of the function's /tmp directory in MB.
* Must be a whole number.
* @default 512
*/
ephemeralStorageSizeMB?: number;

/**
* Environment variables that will be available during function execution
*/
Expand Down Expand Up @@ -236,6 +243,7 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
entry: this.resolveEntry(),
timeoutSeconds: this.resolveTimeout(),
memoryMB: this.resolveMemory(),
ephemeralStorageSizeMB: this.resolveEphemeralStorageSize(),
environment: this.resolveEnvironment(),
runtime: this.resolveRuntime(),
schedule: this.resolveSchedule(),
Expand Down Expand Up @@ -324,6 +332,28 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
return this.props.memoryMB;
};

private resolveEphemeralStorageSize = () => {
const ephemeralStorageSizeMin = 512;
const ephemeralStorageSizeMax = 10240;
const ephemeralStorageSizeDefault = 512;
if (this.props.ephemeralStorageSizeMB === undefined) {
return ephemeralStorageSizeDefault;
}
if (
!isWholeNumberBetweenInclusive(
this.props.ephemeralStorageSizeMB,
ephemeralStorageSizeMin,
ephemeralStorageSizeMax
)
) {
throw new AmplifyUserError('InvalidEphemeralStorageSizeMBError', {
message: `Invalid function ephemeralStorageSizeMB of ${this.props.ephemeralStorageSizeMB}`,
resolution: `ephemeralStorageSizeMB must be a whole number between ${ephemeralStorageSizeMin} and ${ephemeralStorageSizeMax} inclusive`,
});
}
return this.props.ephemeralStorageSizeMB;
};

private resolveEnvironment = () => {
if (this.props.environment === undefined) {
return {};
Expand Down Expand Up @@ -509,6 +539,7 @@ class AmplifyFunction
entry: props.entry,
timeout: Duration.seconds(props.timeoutSeconds),
memorySize: props.memoryMB,
ephemeralStorageSize: Size.mebibytes(props.ephemeralStorageSizeMB),
runtime: nodeVersionMap[props.runtime],
layers: props.resolvedLayers,
bundling: {
Expand Down
Loading