Skip to content

Commit

Permalink
feat(lambda): inline function code can exceed 4096 bytes (#20624)
Browse files Browse the repository at this point in the history
CloudFormation removed the 4k charcaters limit for inline function code.

Latest public documentation: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#cfn-lambda-function-code-zipfile

Historic documentation: https://web.archive.org/web/20220309033724/https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-code.html#cfn-lambda-function-code-zipfile

Internal Code Review reference: CR-53662636


----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
seyeong committed Jun 14, 2022
1 parent 4d0cb00 commit a014c30
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ runtime code.
* `lambda.Code.fromBucket(bucket, key[, objectVersion])` - specify an S3 object
that contains the archive of your runtime code.
* `lambda.Code.fromInline(code)` - inline the handle code as a string. This is
limited to supported runtimes and the code cannot exceed 4KiB.
limited to supported runtimes.
* `lambda.Code.fromAsset(path)` - specify a directory or a .zip file in the local
filesystem which will be zipped and uploaded to S3 before deployment. See also
[bundling asset code](#bundling-asset-code).
Expand Down
6 changes: 1 addition & 5 deletions packages/@aws-cdk/aws-lambda/lib/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class S3Code extends Code {
}

/**
* Lambda code from an inline string (limited to 4KiB).
* Lambda code from an inline string.
*/
export class InlineCode extends Code {
public readonly isInline = true;
Expand All @@ -249,10 +249,6 @@ export class InlineCode extends Code {
if (code.length === 0) {
throw new Error('Lambda inline code cannot be empty');
}

if (code.length > 4096) {
throw new Error('Lambda source is too large, must be <= 4096 but is ' + code.length);
}
}

public bind(_scope: Construct): CodeConfig {
Expand Down
12 changes: 0 additions & 12 deletions packages/@aws-cdk/aws-lambda/test/code.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ describe('code', () => {
expect(() => defineFunction(lambda.Code.fromInline('boom'), lambda.Runtime.GO_1_X)).toThrow(/Inline source not allowed for go1\.x/);
expect(() => defineFunction(lambda.Code.fromInline('boom'), lambda.Runtime.JAVA_8)).toThrow(/Inline source not allowed for java8/);
});
test('fails if larger than 4096 bytes', () => {
expect(() => defineFunction(lambda.Code.fromInline(generateRandomString(4097)), lambda.Runtime.NODEJS_14_X))
.toThrow(/Lambda source is too large, must be <= 4096 but is 4097/);
});
});

describe('lambda.Code.fromAsset', () => {
Expand Down Expand Up @@ -525,11 +521,3 @@ function defineFunction(code: lambda.Code, runtime: lambda.Runtime = lambda.Runt
runtime,
});
}

function generateRandomString(bytes: number) {
let s = '';
for (let i = 0; i < bytes; ++i) {
s += String.fromCharCode(Math.round(Math.random() * 256));
}
return s;
}

0 comments on commit a014c30

Please sign in to comment.