Skip to content

Commit e1db9cf

Browse files
committed
Add multi-architecture Lambda support to canary runner
1 parent b5d2951 commit e1db9cf

4 files changed

Lines changed: 79 additions & 21 deletions

File tree

.github/workflows/ci.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ jobs:
344344
fail-fast: false
345345
matrix:
346346
target:
347-
- x86_64-unknown-linux-gnu
348-
- x86_64-unknown-linux-musl
349-
- aarch64-unknown-linux-gnu
350-
- aarch64-unknown-linux-musl
347+
- x86_64-unknown-linux-gnu
348+
- x86_64-unknown-linux-musl
349+
- aarch64-unknown-linux-gnu
350+
- aarch64-unknown-linux-musl
351351
steps:
352352
- uses: actions/checkout@v4
353353
with:
@@ -394,10 +394,10 @@ jobs:
394394
fail-fast: false
395395
matrix:
396396
include:
397-
- arch: x86_64
398-
runner: smithy_ubuntu-latest_8-core
399-
- arch: aarch64
400-
runner: smithy_ubuntu-latest_8-core-arm
397+
- arch: x86_64
398+
runner: smithy_ubuntu-latest_8-core
399+
- arch: aarch64
400+
runner: smithy_ubuntu-latest_8-core-arm
401401
steps:
402402
- uses: GitHubSecurityLab/actions-permissions/monitor@v1
403403
- uses: actions/checkout@v4
@@ -415,7 +415,7 @@ jobs:
415415
uses: ./smithy-rs/.github/actions/docker-build
416416
with:
417417
action: run-canary
418-
action-arguments: ${{ secrets.CANARY_STACK_CDK_OUTPUTS_BUCKET_NAME }} ${{ steps.creds.outputs.aws-access-key-id }} ${{ steps.creds.outputs.aws-secret-access-key }} ${{ steps.creds.outputs.aws-session-token }}
418+
action-arguments: ${{ secrets.CANARY_STACK_CDK_OUTPUTS_BUCKET_NAME }} ${{ steps.creds.outputs.aws-access-key-id }} ${{ steps.creds.outputs.aws-secret-access-key }} ${{ steps.creds.outputs.aws-session-token }} ${{ matrix.arch }}
419419

420420
# This is always a failing job since forked repositories do not have necessary repository secrets
421421
# to run the PR bot workflow or the canary workflow

tools/ci-cdk/canary-runner/src/build_bundle.rs

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,39 @@ lazy_static! {
204204
];
205205
}
206206

207+
/// Lambda architecture to target
208+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
209+
pub enum LambdaArchitecture {
210+
#[default]
211+
X86_64,
212+
Arm64,
213+
}
214+
215+
impl std::str::FromStr for LambdaArchitecture {
216+
type Err = String;
217+
fn from_str(s: &str) -> Result<Self, Self::Err> {
218+
match s.to_lowercase().as_str() {
219+
"x86_64" | "x86-64" | "amd64" => Ok(LambdaArchitecture::X86_64),
220+
"arm64" | "aarch64" => Ok(LambdaArchitecture::Arm64),
221+
_ => Err(format!(
222+
"Unknown architecture: {}. Use 'x86_64' or 'arm64'",
223+
s
224+
)),
225+
}
226+
}
227+
}
228+
229+
impl LambdaArchitecture {
230+
pub fn rust_target(&self, musl: bool) -> &'static str {
231+
match (self, musl) {
232+
(LambdaArchitecture::X86_64, false) => "x86_64-unknown-linux-gnu",
233+
(LambdaArchitecture::X86_64, true) => "x86_64-unknown-linux-musl",
234+
(LambdaArchitecture::Arm64, false) => "aarch64-unknown-linux-gnu",
235+
(LambdaArchitecture::Arm64, true) => "aarch64-unknown-linux-musl",
236+
}
237+
}
238+
}
239+
207240
#[derive(Debug, Parser, Eq, PartialEq)]
208241
pub struct BuildBundleArgs {
209242
/// Canary Lambda source code path (defaults to current directory)
@@ -234,6 +267,10 @@ pub struct BuildBundleArgs {
234267
#[clap(long)]
235268
pub musl: bool,
236269

270+
/// Lambda architecture to target (x86_64 or arm64)
271+
#[clap(long, default_value = "x86_64")]
272+
pub architecture: LambdaArchitecture,
273+
237274
/// Only generate the `Cargo.toml` file rather than building the entire bundle
238275
#[clap(long)]
239276
pub manifest_only: bool,
@@ -367,15 +404,14 @@ pub async fn build_bundle(opt: BuildBundleArgs) -> Result<Option<PathBuf>> {
367404

368405
if !opt.manifest_only {
369406
// Compile the canary Lambda
407+
let target = opt.architecture.rust_target(opt.musl);
370408
let mut command = Command::new("cargo");
371409
command
372410
.arg("build")
373411
.arg("--release")
374412
.arg("--manifest-path")
375-
.arg(&manifest_path);
376-
if opt.musl {
377-
command.arg("--target=x86_64-unknown-linux-musl");
378-
}
413+
.arg(&manifest_path)
414+
.arg(format!("--target={}", target));
379415
handle_failure("cargo build", &command.output()?)?;
380416

381417
// Compile the wasm canary to a .wasm binary
@@ -390,13 +426,12 @@ pub async fn build_bundle(opt: BuildBundleArgs) -> Result<Option<PathBuf>> {
390426

391427
// Bundle the Lambda
392428
let repository_root = find_git_repository_root("smithy-rs", canary_path)?;
393-
let target_path = {
394-
let mut path = repository_root.join("tools").join("target");
395-
if opt.musl {
396-
path = path.join("x86_64-unknown-linux-musl");
397-
}
398-
path.join("release")
399-
};
429+
let target = opt.architecture.rust_target(opt.musl);
430+
let target_path = repository_root
431+
.join("tools")
432+
.join("target")
433+
.join(target)
434+
.join("release");
400435
let wasm_bin_path = {
401436
repository_root
402437
.join("tools")
@@ -492,6 +527,7 @@ mod tests {
492527
sdk_release_tag: Some(ReleaseTag::from_str("release-2022-07-26").unwrap()),
493528
sdk_path: None,
494529
musl: false,
530+
architecture: LambdaArchitecture::X86_64,
495531
manifest_only: false,
496532
}),
497533
Args::try_parse_from([
@@ -509,6 +545,7 @@ mod tests {
509545
sdk_release_tag: None,
510546
sdk_path: Some("some-sdk-path".into()),
511547
musl: false,
548+
architecture: LambdaArchitecture::X86_64,
512549
manifest_only: false,
513550
}),
514551
Args::try_parse_from([
@@ -528,6 +565,7 @@ mod tests {
528565
sdk_release_tag: Some(ReleaseTag::from_str("release-2022-07-26").unwrap()),
529566
sdk_path: None,
530567
musl: true,
568+
architecture: LambdaArchitecture::X86_64,
531569
manifest_only: true,
532570
}),
533571
Args::try_parse_from([
@@ -547,6 +585,7 @@ mod tests {
547585
sdk_release_tag: None,
548586
sdk_path: Some("some-sdk-path".into()),
549587
musl: false,
588+
architecture: LambdaArchitecture::X86_64,
550589
manifest_only: false,
551590
}),
552591
Args::try_parse_from([

tools/ci-cdk/canary-runner/src/run.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ pub struct RunArgs {
119119
/// The ARN of the role that the Lambda will execute as
120120
#[clap(long, required_unless_present = "cdk-output")]
121121
lambda_execution_role_arn: Option<String>,
122+
123+
/// Lambda architecture to target (x86_64 or arm64)
124+
#[clap(long, default_value = "x86_64")]
125+
architecture: crate::build_bundle::LambdaArchitecture,
122126
}
123127

124128
#[derive(Debug, Eq, PartialEq)]
@@ -127,6 +131,7 @@ struct Options {
127131
sdk_release_tag: Option<ReleaseTag>,
128132
sdk_path: Option<PathBuf>,
129133
musl: bool,
134+
architecture: crate::build_bundle::LambdaArchitecture,
130135
expected_speech_text_by_transcribe: Option<String>,
131136
lambda_function_memory_size_in_mb: i32,
132137
lambda_code_s3_bucket_name: String,
@@ -194,6 +199,7 @@ impl Options {
194199
sdk_release_tag: run_opt.sdk_release_tag,
195200
sdk_path: run_opt.sdk_path,
196201
musl: run_opt.musl,
202+
architecture: run_opt.architecture,
197203
expected_speech_text_by_transcribe: run_opt.expected_speech_text_by_transcribe,
198204
lambda_function_memory_size_in_mb: run_opt
199205
.lambda_function_memory_size_in_mb
@@ -210,6 +216,7 @@ impl Options {
210216
sdk_release_tag: run_opt.sdk_release_tag,
211217
sdk_path: run_opt.sdk_path,
212218
musl: run_opt.musl,
219+
architecture: run_opt.architecture,
213220
expected_speech_text_by_transcribe: run_opt.expected_speech_text_by_transcribe,
214221
lambda_function_memory_size_in_mb: run_opt
215222
.lambda_function_memory_size_in_mb
@@ -362,6 +369,7 @@ async fn build_bundle(options: &Options) -> Result<PathBuf> {
362369
sdk_release_tag: options.sdk_release_tag.clone(),
363370
sdk_path: options.sdk_path.clone(),
364371
musl: options.musl,
372+
architecture: options.architecture,
365373
manifest_only: false,
366374
};
367375
info!("Compiling the canary bundle for Lambda with {build_args:?}. This may take a few minutes...");
@@ -430,10 +438,16 @@ async fn create_lambda_fn(
430438
),
431439
};
432440

441+
let lambda_arch = match options.architecture {
442+
crate::build_bundle::LambdaArchitecture::X86_64 => Architecture::X8664,
443+
crate::build_bundle::LambdaArchitecture::Arm64 => Architecture::Arm64,
444+
};
445+
433446
lambda_client
434447
.create_function()
435448
.function_name(bundle_name)
436449
.runtime(Runtime::Providedal2)
450+
.architectures(lambda_arch)
437451
.role(&options.lambda_execution_role_arn)
438452
.handler("aws-sdk-rust-lambda-canary")
439453
.code(
@@ -544,6 +558,7 @@ async fn delete_lambda_fn(lambda_client: lambda::Client, bundle_name: &str) -> R
544558

545559
#[cfg(test)]
546560
mod tests {
561+
use crate::build_bundle::LambdaArchitecture;
547562
use crate::run::{Options, RunArgs, DEFAULT_LAMBDA_FUNCTION_MEMORY_SIZE_IN_MB};
548563
use clap::Parser;
549564

@@ -562,7 +577,8 @@ mod tests {
562577
lambda_test_s3_bucket_name: None,
563578
lambda_execution_role_arn: None,
564579
lambda_test_s3_mrap_bucket_arn: None,
565-
lambda_test_s3_express_bucket_name: None
580+
lambda_test_s3_express_bucket_name: None,
581+
architecture: LambdaArchitecture::X86_64,
566582
},
567583
RunArgs::try_parse_from([
568584
"run",
@@ -605,6 +621,7 @@ mod tests {
605621
sdk_release_tag: None,
606622
sdk_path: Some("artifact-aws-sdk-rust/sdk".into()),
607623
musl: false,
624+
architecture: LambdaArchitecture::X86_64,
608625
expected_speech_text_by_transcribe: Some("Good day to you transcribe.".to_owned()),
609626
lambda_function_memory_size_in_mb: DEFAULT_LAMBDA_FUNCTION_MEMORY_SIZE_IN_MB,
610627
lambda_code_s3_bucket_name: "bucket-for-code".to_owned(),

tools/ci-scripts/run-canary

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CANARY_STACK_CDK_OUTPUTS_BUCKET_NAME=$1
1010
export AWS_ACCESS_KEY_ID=$2
1111
export AWS_SECRET_ACCESS_KEY=$3
1212
export AWS_SESSION_TOKEN=$4
13+
ARCHITECTURE=${5:-x86_64}
1314
export AWS_REGION=us-west-2
1415
export RUST_LOG=debug
1516

@@ -21,4 +22,5 @@ cargo run -- \
2122
run --rust-version "${RUST_STABLE_VERSION}" \
2223
--sdk-path "${SDK_PATH}" \
2324
--cdk-output cdk-outputs.json \
25+
--architecture "${ARCHITECTURE}" \
2426
--musl

0 commit comments

Comments
 (0)