Skip to content
Open
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use libdd_trace_protobuf::pb::Span;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::config::aws::get_aws_partition_by_region;
use crate::lifecycle::invocation::{
processor::MS_TO_NS,
triggers::{
Expand Down Expand Up @@ -164,11 +165,10 @@ impl Trigger for LambdaFunctionUrlEvent {
}

fn get_arn(&self, region: &str) -> String {
let partition = get_aws_partition_by_region(region);
let function_name = env::var("AWS_LAMBDA_FUNCTION_NAME").unwrap_or_default();
format!(
"arn:aws:lambda:{region}:{}:url:{}",
self.request_context.account_id, function_name
)
let account_id = &self.request_context.account_id;
format!("arn:{partition}:lambda:{region}:{account_id}:url:{function_name}")
}

fn get_carrier(&self) -> HashMap<String, String> {
Expand Down Expand Up @@ -196,6 +196,7 @@ impl ServiceNameResolver for LambdaFunctionUrlEvent {
mod tests {
use super::*;
use crate::lifecycle::invocation::triggers::test_utils::read_json_file;
use serial_test::serial;

#[test]
fn test_new() {
Expand Down Expand Up @@ -326,6 +327,7 @@ mod tests {
}

#[test]
#[serial] // mutates the shared AWS_LAMBDA_FUNCTION_NAME env var
fn test_get_arn() {
unsafe { env::set_var("AWS_LAMBDA_FUNCTION_NAME", "mock-lambda") };
let json = read_json_file("lambda_function_url_event.json");
Expand All @@ -339,6 +341,32 @@ mod tests {
unsafe { env::remove_var("AWS_LAMBDA_FUNCTION_NAME") };
}

#[test]
#[serial] // coordinates with test_get_arn, which mutates AWS_LAMBDA_FUNCTION_NAME
fn test_get_arn_non_default_partitions() {
let json = read_json_file("lambda_function_url_event.json");
let payload = serde_json::from_str(&json).expect("Failed to deserialize into Value");
let event = LambdaFunctionUrlEvent::new(payload)
.expect("Failed to deserialize LambdaFunctionUrlEvent");

// This test only exercises the partition portion of the ARN, so derive the
// expected value from whatever AWS_LAMBDA_FUNCTION_NAME is currently set to
// rather than mutating the shared process environment.
let function_name = env::var("AWS_LAMBDA_FUNCTION_NAME").unwrap_or_default();

// GovCloud regions use the aws-us-gov partition.
assert_eq!(
event.get_arn("us-gov-west-1"),
format!("arn:aws-us-gov:lambda:us-gov-west-1:601427279990:url:{function_name}")
);

// China regions use the aws-cn partition.
assert_eq!(
event.get_arn("cn-north-1"),
format!("arn:aws-cn:lambda:cn-north-1:601427279990:url:{function_name}")
);
}

#[test]
fn test_resolve_service_name_with_representation_enabled() {
let json = read_json_file("lambda_function_url_event.json");
Expand Down