Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 ephemeralStorageSize option to defineFunction
1 change: 1 addition & 0 deletions packages/backend-function/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type FunctionProps = {
entry?: string;
timeoutSeconds?: number;
memoryMB?: number;
ephemeralStorageSize?: number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we suffix this with MB ? we do that in property above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some context:
It seems that CDK is just passing through this value:
see https://github.com/aws/aws-cdk/blob/b5c21890cf67ddda78c18eb0425f11a59fcadb34/packages/aws-cdk-lib/aws-lambda/lib/function.ts#L1057
and perhaps multiplies it by 1024 if necessary.
see https://github.com/aws/aws-cdk/blob/62a0638e325024fc0469ed50f9accd448d87bac7/packages/aws-cdk-lib/core/lib/size.ts#L133

But the lambda claims it's MB not MiB here https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-ephemeralstorage.html .

To confirm this. It might be good to deploy lambda with ephemeralStorageSize given to CDK construct and checking what value is printed in the AWS Console.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sobolk
In CloudFormation and AWS Console, the unit of ehpemeralStorageSize for Lambda was MB.
I understood that only CDK uses MiB as a unit and it is appropriate to give MB to the suffix in Amplify.
I fixed the part of JSDoc for ephemeralStorageSizeMB that used MiB has been corrected to MB.
Thank you very much.

environment?: Record<string, string | BackendSecret>;
runtime?: NodeVersion;
schedule?: FunctionSchedule | FunctionSchedule[];
Expand Down
64 changes: 64 additions & 0 deletions packages/backend-function/src/factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,68 @@ void describe('AmplifyFunctionFactory', () => {
'function-Lambda'
);
});

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

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

void it('sets default ephemeralStorageSize', () => {
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 ephemeralStorageSize below 512 MB', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
ephemeralStorageSize: 511,
}).getInstance(getInstanceProps),
new Error(
'ephemeralStorageSize must be a whole number between 512 and 10240 inclusive'
)
);
});

void it('throws on ephemeralStorageSize above 10240 MB', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
ephemeralStorageSize: 10241,
}).getInstance(getInstanceProps),
new Error(
'ephemeralStorageSize must be a whole number between 512 and 10240 inclusive'
)
);
});

void it('throws on fractional ephemeralStorageSize', () => {
assert.throws(
() =>
defineFunction({
entry: './test-assets/default-lambda/handler.ts',
ephemeralStorageSize: 512.5,
}).getInstance(getInstanceProps),
new Error(
'ephemeralStorageSize must be a whole number between 512 and 10240 inclusive'
)
);
});
});
});
32 changes: 31 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 MiB.
* Must be a whole number.
* Default is 512MiB.
*/
ephemeralStorageSize?: number;

/**
* Environment variables that will be available during function execution
*/
Expand Down Expand Up @@ -232,6 +239,7 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
entry: this.resolveEntry(),
timeoutSeconds: this.resolveTimeout(),
memoryMB: this.resolveMemory(),
ephemeralStorageSize: this.resolveEphemeralStorageSize(),
environment: this.props.environment ?? {},
runtime: this.resolveRuntime(),
schedule: this.resolveSchedule(),
Expand Down Expand Up @@ -318,6 +326,27 @@ class FunctionFactory implements ConstructFactory<AmplifyFunction> {
return this.props.memoryMB;
};

private resolveEphemeralStorageSize = () => {
const ephemeralStorageSizeMin = 512;
const ephemeralStorageSizeMax = 10240;
const ephemeralStorageSizeDefault = 512;
if (this.props.ephemeralStorageSize === undefined) {
return ephemeralStorageSizeDefault;
}
if (
!isWholeNumberBetweenInclusive(
this.props.ephemeralStorageSize,
ephemeralStorageSizeMin,
ephemeralStorageSizeMax
)
) {
throw new Error(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please throw an AmplifyUserError instead of a regular error. See line 327 for an example
https://github.com/aws-amplify/amplify-backend/pull/2283/files#diff-e354c6c4617ce06e0e16948f4521eca5f9269445265b80ed16720372d336c886R327

`ephemeralStorageSize must be a whole number between ${ephemeralStorageSizeMin} and ${ephemeralStorageSizeMax} inclusive`
);
}
return this.props.ephemeralStorageSize;
};

private resolveRuntime = () => {
const runtimeDefault = 18;

Expand Down Expand Up @@ -465,6 +494,7 @@ class AmplifyFunction
entry: props.entry,
timeout: Duration.seconds(props.timeoutSeconds),
memorySize: props.memoryMB,
ephemeralStorageSize: Size.mebibytes(props.ephemeralStorageSize),
runtime: nodeVersionMap[props.runtime],
layers: props.resolvedLayers,
bundling: {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-config/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ export type CustomClientConfig = {
export const DEFAULT_CLIENT_CONFIG_VERSION: ClientConfigVersion;

// @public
export const generateClientConfig: <T extends "1" | "1.1" | "1.2" | "1.3" | "0">(backendIdentifier: DeployedBackendIdentifier, version: T, awsClientProvider?: AWSClientProvider<{
export const generateClientConfig: <T extends "1.3" | "0" | "1" | "1.1" | "1.2">(backendIdentifier: DeployedBackendIdentifier, version: T, awsClientProvider?: AWSClientProvider<{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the changes in packages/client-config/API.md and packages/platform-core/API.md are unexpected.

You can either revert these two files.
Or run npm run clean && npm install && npm run build && npm update:api . I.e. re-generate api reports from clean workspace.

getS3Client: S3Client;
getAmplifyClient: AmplifyClient;
getCloudFormationClient: CloudFormationClient;
Expand Down
4 changes: 2 additions & 2 deletions packages/platform-core/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ export const packageJsonSchema: z.ZodObject<{
type: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"module">, z.ZodLiteral<"commonjs">]>>;
}, "strip", z.ZodTypeAny, {
name?: string | undefined;
type?: "module" | "commonjs" | undefined;
version?: string | undefined;
type?: "module" | "commonjs" | undefined;
}, {
name?: string | undefined;
type?: "module" | "commonjs" | undefined;
version?: string | undefined;
type?: "module" | "commonjs" | undefined;
}>;

// @public
Expand Down