fix(lambda): return event source mapping ARN and support tagging mappings (#4587)

* fix(lambda): return event source mapping ARN and support tagging mappings

CreateEventSourceMapping now stores Tags, every mapping response carries
EventSourceMappingArn, and TagResource/ListTags/UntagResource route
event-source-mapping ARNs to the mapping instead of rejecting them as
non-function ARNs.

* fix(lambda): address review on event source mapping ARN and tags

- CloudFormation builds EventSourceMappingArn from the region and account
  the mapping is stored in, not the stack's, so it matches Lambda's ARN.
- TagResource rejects a non-object Tags or a non-string tag value with
  SerializationException instead of persisting it.
- The tag APIs match a mapping ARN exactly, so an ARN in another partition
  is reported as ResourceNotFoundException.
- Explain the tolerated teardown failures in LambdaEsmTagsTest.
This commit is contained in:
Abhigya authored and GitHub committed 2026-09-28 23:43:54 -04:00
1 parent 76f8c09341
commit 0a786207d6
10 files changed
+484 -18

No files matched your search

@@ -43,6 +43,19 @@ class LambdaEventSourceMappingCfnProvisionerTest {
new LambdaEventSourceMappingCfnProvisioner(lambdaService);
private final ObjectMapper mapper = new ObjectMapper();
LambdaEventSourceMappingCfnProvisionerTest() {
when(lambdaService.updateEventSourceMapping(anyString(), any()))
.thenAnswer(inv -> storedMapping(inv.getArgument(0), "us-east-1"));
}
private static EventSourceMapping storedMapping(String uuid, String region) {
EventSourceMapping esm = new EventSourceMapping();
esm.setUuid(uuid);
esm.setRegion(region);
esm.setAccountId("000000000000");
return esm;
}
private ProvisionContext ctx() {
return ctx(false, null);
}
@@ -133,8 +146,7 @@ class LambdaEventSourceMappingCfnProvisionerTest {
props.put("Enabled", "true");
props.put("StartingPosition", "TRIM_HORIZON");
EventSourceMapping esm = new EventSourceMapping();
esm.setUuid("test-esm-uuid-1234");
EventSourceMapping esm = storedMapping("test-esm-uuid-1234", "us-east-1");
when(lambdaService.createEventSourceMapping(eq("us-east-1"), any())).thenReturn(esm);
provisioner.provision(r, props, ctx());
@@ -155,6 +167,24 @@ class LambdaEventSourceMappingCfnProvisionerTest {
assertEquals("TRIM_HORIZON", req.get("StartingPosition"));
}
@Test
void eventSourceMappingArnUsesTheRegionTheMappingIsStoredIn() {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
ObjectNode props = mapper.createObjectNode();
props.put("FunctionName", "my-function");
props.put("EventSourceArn", "arn:aws:sqs:eu-west-1:000000000000:my-queue");
EventSourceMapping esm = storedMapping("cross-region-esm-uuid", "eu-west-1");
when(lambdaService.createEventSourceMapping(eq("us-east-1"), any())).thenReturn(esm);
provisioner.provision(r, props, ctx());
assertEquals("arn:aws:lambda:eu-west-1:000000000000:event-source-mapping:cross-region-esm-uuid",
r.getAttributes().get("EventSourceMappingArn"));
}
@Test
void provisionResolvesNestedIntrinsicsForKafka() {
StackResource r = new StackResource();
@@ -175,8 +205,7 @@ class LambdaEventSourceMappingCfnProvisionerTest {
props.set("Topics", mapper.createArrayNode().add("my-topic"));
EventSourceMapping esm = new EventSourceMapping();
esm.setUuid("kafka-esm-uuid");
EventSourceMapping esm = storedMapping("kafka-esm-uuid", "us-east-1");
when(lambdaService.createEventSourceMapping(eq("us-east-1"), any())).thenReturn(esm);
provisioner.provision(r, props, ctx());
@@ -294,8 +323,7 @@ class LambdaEventSourceMappingCfnProvisionerTest {
private Map<String, Object> provisionAndCaptureCreateRequest(ObjectNode props) {
StackResource r = new StackResource();
r.setResourceType("AWS::Lambda::EventSourceMapping");
EventSourceMapping esm = new EventSourceMapping();
esm.setUuid("options-esm-uuid");
EventSourceMapping esm = storedMapping("options-esm-uuid", "us-east-1");
when(lambdaService.createEventSourceMapping(eq("us-east-1"), any())).thenReturn(esm);
provisioner.provision(r, props, ctx());
@@ -1466,4 +1466,199 @@ class EsmIntegrationTest {
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuidWith).then().statusCode(202);
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuidWithout).then().statusCode(202);
}
// ──────────────────────────── ARN and tags ────────────────────────────
private static String esmArn(String region, String uuid) {
return "arn:aws:lambda:" + region + ":" + ACCOUNT_ID + ":event-source-mapping:" + uuid;
}
@Test
@Order(82)
void eventSourceMappingArnIsReturnedByCreateGetListAndDelete() {
String uuid = given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s" }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(202)
.body("EventSourceMappingArn", startsWith("arn:aws:lambda:" + REGION + ":" + ACCOUNT_ID
+ ":event-source-mapping:"))
.extract().path("UUID");
String arn = esmArn(REGION, uuid);
given().get(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then()
.statusCode(200)
.body("EventSourceMappingArn", equalTo(arn));
given().queryParam("FunctionName", FUNCTION_ARN).get(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(200)
.body("EventSourceMappings.find { it.UUID == '" + uuid + "' }.EventSourceMappingArn", equalTo(arn));
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid)
.then()
.statusCode(202)
.body("EventSourceMappingArn", equalTo(arn));
}
@Test
@Order(83)
void createEventSourceMappingStoresTagsReadableThroughListTags() {
String uuid = given()
.contentType("application/json")
.body("""
{
"FunctionName": "%s",
"EventSourceArn": "%s",
"Tags": { "Environment": "uat", "ManagedBy": "terraform" }
}
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(202)
.body("$", not(hasKey("Tags")))
.extract().path("UUID");
given().get("/2017-03-31/tags/" + esmArn(REGION, uuid))
.then()
.statusCode(200)
.body("Tags.Environment", equalTo("uat"))
.body("Tags.ManagedBy", equalTo("terraform"))
.body("Tags.size()", equalTo(2));
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
}
@Test
@Order(84)
void tagAndUntagResourceOnEventSourceMappingArn() {
String uuid = given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s" }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then().statusCode(202).extract().path("UUID");
String arn = esmArn(REGION, uuid);
given().get("/2017-03-31/tags/" + arn)
.then()
.statusCode(200)
.body("Tags.size()", equalTo(0));
given()
.contentType("application/json")
.body("{\"Tags\": {\"Layer\": \"backend\", \"Target\": \"email\"}}")
.when().post("/2017-03-31/tags/" + arn)
.then().statusCode(204);
given().queryParam("tagKeys", "Target").delete("/2017-03-31/tags/" + arn)
.then().statusCode(204);
given().get("/2017-03-31/tags/" + arn)
.then()
.statusCode(200)
.body("Tags.Layer", equalTo("backend"))
.body("Tags.size()", equalTo(1));
given().get("/2017-03-31/tags/" + FUNCTION_ARN)
.then()
.statusCode(200)
.body("Tags", not(hasKey("Layer")));
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
given().get("/2017-03-31/tags/" + arn)
.then()
.statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
}
@Test
@Order(85)
void tagApisReportUnknownOrForeignEventSourceMappingArnAsNotFound() {
given().get("/2017-03-31/tags/" + esmArn(REGION, UUID.randomUUID().toString()))
.then()
.statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
String uuid = given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s" }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then().statusCode(202).extract().path("UUID");
given()
.contentType("application/json")
.body("{\"Tags\": {\"k\": \"v\"}}")
.when().post("/2017-03-31/tags/" + esmArn("eu-west-1", uuid))
.then()
.statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
given().get("/2017-03-31/tags/" + esmArn(REGION, uuid).replaceFirst("^arn:aws:", "arn:aws-cn:"))
.then()
.statusCode(404)
.body("__type", equalTo("ResourceNotFoundException"));
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
}
@Test
@Order(86)
void createEventSourceMappingRejectsNonObjectTags() {
given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s", "Tags": ["Environment"] }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(400)
.body("__type", equalTo("SerializationException"));
given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s", "Tags": { "Environment": 1 } }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then()
.statusCode(400)
.body("__type", equalTo("SerializationException"));
}
@Test
@Order(87)
void tagResourceRejectsNonStringTagValuesOnEventSourceMapping() {
String uuid = given()
.contentType("application/json")
.body("""
{ "FunctionName": "%s", "EventSourceArn": "%s" }
""".formatted(FUNCTION_NAME, QUEUE_ARN))
.when().post(LAMBDA_BASE + "/event-source-mappings")
.then().statusCode(202).extract().path("UUID");
String arn = esmArn(REGION, uuid);
given()
.contentType("application/json")
.body("{\"Tags\": {\"k\": 1}}")
.when().post("/2017-03-31/tags/" + arn)
.then()
.statusCode(400)
.body("__type", equalTo("SerializationException"));
given().get("/2017-03-31/tags/" + arn)
.then()
.statusCode(200)
.body("Tags.size()", equalTo(0));
given().delete(LAMBDA_BASE + "/event-source-mappings/" + uuid).then().statusCode(202);
}
}