Skip to content

Commit 3552641

Browse files
authored
feat: add architecture prop to RustExtension (#77)
* feat: add `architecture` prop to `RustExtension` * fix readme import * add test
1 parent 5abe356 commit 3552641

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ Define a `RustExtension` that get's deployed as a layer to use it with any other
7373

7474
```ts
7575
import { RustExtension, RustFunction } from 'cargo-lambda-cdk';
76+
import { Architecture } from 'aws-cdk-lib/aws-lambda';
7677

7778
const extensionLayer = new RustExtension(this, 'Rust extension', {
7879
manifestPath: 'path/to/package/directory/with/Cargo.toml',
80+
architecture: Architecture.ARM_64,
7981
});
8082

8183
new RustFunction(this, 'Rust function', {

src/extension.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { LayerVersion, LayerVersionOptions } from 'aws-cdk-lib/aws-lambda';
1+
import {
2+
LayerVersion,
3+
LayerVersionOptions,
4+
Architecture,
5+
} from 'aws-cdk-lib/aws-lambda';
26
import { Construct } from 'constructs';
37
import { Bundling } from './bundling';
48
import { getManifestPath } from './cargo';
@@ -9,10 +13,10 @@ import { BundlingOptions } from './types';
913
*/
1014
export interface RustExtensionProps extends LayerVersionOptions {
1115
/**
12-
* Bundling options
13-
*
14-
* @default - use default bundling options
15-
*/
16+
* Bundling options
17+
*
18+
* @default - use default bundling options
19+
*/
1620
readonly bundling?: BundlingOptions;
1721

1822
/**
@@ -57,24 +61,38 @@ export interface RustExtensionProps extends LayerVersionOptions {
5761
* temporary directory.
5862
*/
5963
readonly gitForceClone?: boolean;
64+
65+
/**
66+
* The system architecture of the lambda extension
67+
*
68+
* @default - Architecture.X86_64
69+
*/
70+
readonly architecture?: Architecture;
6071
}
6172

6273
/**
6374
* A Lambda extension written in Rust
6475
*/
6576
export class RustExtension extends LayerVersion {
66-
constructor(scope: Construct, resourceName: string, props?: RustExtensionProps) {
77+
constructor(
78+
scope: Construct,
79+
resourceName: string,
80+
props?: RustExtensionProps,
81+
) {
6782
const manifestPath = getManifestPath(props || {});
6883
const bundling = props?.bundling ?? {};
84+
const architecture = props?.architecture ?? Architecture.X86_64;
6985

7086
super(scope, resourceName, {
7187
...props,
88+
compatibleArchitectures: [architecture],
7289
code: Bundling.bundle({
7390
...bundling,
7491
manifestPath,
7592
binaryName: props?.binaryName,
7693
lambdaExtension: true,
94+
architecture,
7795
}),
7896
});
7997
}
80-
}
98+
}

test/extension.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { join } from 'path';
22
import { env } from 'process';
33
import { App, Stack } from 'aws-cdk-lib';
4+
import { Template } from 'aws-cdk-lib/assertions';
5+
import { Architecture } from 'aws-cdk-lib/aws-lambda';
46
import { RustExtension, cargoLambdaVersion } from '../src/index';
57

68
const forcedDockerBundling = !!env.FORCE_DOCKER_RUN || !cargoLambdaVersion();
@@ -48,4 +50,27 @@ describe('CargoLambda.RustExtension', () => {
4850
app.synth();
4951
});
5052
});
51-
});
53+
54+
describe('With ARM64 architecture', () => {
55+
const app = new App();
56+
const stack = new Stack(app);
57+
const testSource = join(__dirname, 'fixtures/single-package');
58+
59+
new RustExtension(stack, 'rust extension arm64', {
60+
manifestPath: testSource,
61+
architecture: Architecture.ARM_64,
62+
});
63+
64+
test('bundle extension with ARM64 architecture', () => {
65+
// Synthesize the stack to a CloudFormation template
66+
const template = Template.fromStack(stack);
67+
68+
// Verify that the Lambda layer has ARM64 in compatibleArchitectures
69+
template.hasResourceProperties('AWS::Lambda::LayerVersion', {
70+
CompatibleArchitectures: ['arm64'],
71+
});
72+
73+
app.synth();
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)