Skip to content

Commit

Permalink
chore(lambda): allow the architecture property to be readable from th…
Browse files Browse the repository at this point in the history
…e Function (aws#17121)

We currently have layers that are designed specifically for arm64 and x86 based functions, and a single stack may have multiple functions with varying architectures. Applying the correct layer to a large list of functions can be a tedious process that may result in applying the incorrect layer to a function. Exposing the architecture at the Function level would allow a user to easily apply the correct layer automatically during deploy by checking the value and pulling the appropriate layer for that architecture. This would eliminate or greatly reduce the possibility of applying the wrong layer type.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jcstorms1 authored and TikiTDO committed Feb 21, 2022
1 parent 98fba93 commit c4b2367
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,13 @@ export class Function extends FunctionBase {
*/
public readonly deadLetterQueue?: sqs.IQueue;

/**
* The architecture of this Lambda Function (this is an optional attribute and defaults to X86_64).
*/
public readonly architecture?: Architecture;
public readonly permissionsNode = this.node;


protected readonly canCreatePermissions = true;

private readonly layers: ILayerVersion[] = [];
Expand Down Expand Up @@ -721,6 +726,8 @@ export class Function extends FunctionBase {

this.runtime = props.runtime;

this.architecture = props.architecture;

if (props.layers) {
if (props.runtime === Runtime.FROM_IMAGE) {
throw new Error('Layers are not supported for container image functions');
Expand Down
11 changes: 10 additions & 1 deletion packages/@aws-cdk/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2226,7 +2226,16 @@ describe('function', () => {
architectures: [lambda.Architecture.X86_64, lambda.Architecture.ARM_64],
})).toThrow(/one architecture must be specified/);
});

test('Architecture is properly readable from the function', () => {
const stack = new cdk.Stack();
const fn = new lambda.Function(stack, 'MyFunction', {
code: lambda.Code.fromInline('foo'),
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
architecture: lambda.Architecture.ARM_64,
});
expect(fn.architecture?.name).toEqual('arm64');
});
});

function newTestLambda(scope: constructs.Construct) {
Expand Down

0 comments on commit c4b2367

Please sign in to comment.