@kensio/yulin 1.20.14 → 1.20.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/{dist/config/oxlint/cffjs2.oxlintrc.json → cffjs2.oxlintrc.json} +1 -1
- package/dist/config/oxlint/cffjs2.oxlint.config.js +4 -3
- package/dist/sdk/module/sim-sdk-module-client-interceptor.d.ts +5 -0
- package/dist/sdk/module/sim-sdk-module-client-interceptor.js +14 -0
- package/dist/sdk/module/sim-sdk-static-client-factory.d.ts +26 -0
- package/dist/sdk/module/sim-sdk-static-client-factory.js +59 -0
- package/dist/sdk/send-patch.d.ts +8 -0
- package/dist/sdk/send-patch.js +11 -0
- package/dist/service/aws/caller/sim-aws-caller-resolver.d.ts +28 -6
- package/dist/service/aws/caller/sim-aws-caller-resolver.js +18 -11
- package/dist/service/aws/caller/sim-aws-caller.d.ts +9 -0
- package/dist/service/aws/caller/sim-aws-resolved-caller.d.ts +19 -0
- package/dist/service/aws/caller/sim-aws-resolved-caller.js +26 -0
- package/dist/service/aws/factory/sim-aws-account-region-service-builder.d.ts +6 -0
- package/dist/service/aws/factory/sim-aws-account-region-service-builder.js +3 -0
- package/dist/service/aws/factory/sim-aws-account-service-cache.d.ts +6 -0
- package/dist/service/aws/factory/sim-aws-account-service-cache.js +5 -0
- package/dist/service/aws/factory/sim-aws-service-factory.d.ts +5 -0
- package/dist/service/aws/factory/sim-aws-service-factory.js +6 -0
- package/dist/service/aws/sim-aws-properties.d.ts +15 -0
- package/dist/service/aws/sim-aws.js +2 -1
- package/dist/service/cloudformation/resource/resolve/property/sim-cfn-resource-property-resolver.d.ts +11 -4
- package/dist/service/cloudformation/resource/resolve/property/sim-cfn-resource-property-resolver.js +21 -8
- package/dist/service/cloudformation/resource/sim-cfn-resource.type.d.ts +9 -0
- package/dist/service/cloudformation/template/dynamic/make-sim-cfn-dynamic-references.d.ts +2 -0
- package/dist/service/cloudformation/template/dynamic/make-sim-cfn-dynamic-references.js +2 -2
- package/dist/service/cloudformation/template/dynamic/sim-cfn-dynamic-reference-resolvers.d.ts +10 -0
- package/dist/service/cloudformation/template/dynamic/sim-cfn-dynamic-reference-resolvers.js +4 -3
- package/dist/service/cloudformation/template/dynamic/sim-cfn-dynamic-reference-scan.d.ts +11 -0
- package/dist/service/cloudformation/template/dynamic/sim-cfn-dynamic-reference-scan.js +21 -0
- package/dist/service/cloudformation/template/dynamic/sim-cfn-dynamic-references.d.ts +24 -2
- package/dist/service/cloudformation/template/dynamic/sim-cfn-dynamic-references.js +50 -11
- package/dist/service/cloudformation/template/dynamic/sim-cfn-prefetched-dynamic-references.d.ts +42 -0
- package/dist/service/cloudformation/template/dynamic/sim-cfn-prefetched-dynamic-references.js +80 -0
- package/dist/service/eventbridge/delivery/sim-event-bridge-delivery-failures.d.ts +16 -0
- package/dist/service/eventbridge/delivery/sim-event-bridge-delivery-failures.js +19 -0
- package/dist/service/iam/authorize/context/sim-iam-auth-z-caller-context-builder.d.ts +30 -7
- package/dist/service/iam/authorize/context/sim-iam-auth-z-caller-context-builder.js +20 -11
- package/dist/service/iam/authorize/context/sim-iam-auth-z-context-builder.d.ts +2 -1
- package/dist/service/iam/authorize/context/sim-iam-auth-z-context-builder.js +5 -1
- package/dist/service/iam/authorize/match/sim-iam-policy-principal-matcher.d.ts +10 -0
- package/dist/service/iam/authorize/match/sim-iam-policy-principal-matcher.js +15 -3
- package/dist/service/iam/authorize/sim-iam-account-auth-z.d.ts +11 -4
- package/dist/service/iam/authorize/sim-iam-account-auth-z.js +5 -3
- package/dist/service/iam/authorize/sim-iam-authorizer.d.ts +2 -1
- package/dist/service/iam/authorize/sim-iam-authorizer.js +1 -0
- package/dist/service/iam/command/policy/create-policy/create-policy-input-resolver.js +4 -1
- package/dist/service/iam/command/policy/put-role-policy/put-role-policy.handler.js +5 -1
- package/dist/service/iam/command/policy/put-user-policy/put-user-policy.handler.js +5 -1
- package/dist/service/iam/policy/parse/sim-iam-document-parser.d.ts +4 -1
- package/dist/service/iam/policy/parse/sim-iam-document-parser.js +13 -15
- package/dist/service/iam/policy/sim-iam-statement-label.d.ts +28 -0
- package/dist/service/iam/policy/sim-iam-statement-label.js +17 -0
- package/dist/service/iam/policy/sim-iam-statement-strings.d.ts +11 -0
- package/dist/service/iam/policy/sim-iam-statement-strings.js +27 -0
- package/dist/service/iam/role/sim-iam-role-with-policy.factory.d.ts +9 -0
- package/dist/service/iam/role/sim-iam-role-with-policy.factory.js +4 -2
- package/dist/service/iam/sim-iam-account-parts.d.ts +8 -0
- package/dist/service/iam/sim-iam-account-parts.js +1 -0
- package/dist/service/iam/validate/sim-iam-policy-document-validator.d.ts +9 -2
- package/dist/service/iam/validate/sim-iam-policy-document-validator.js +37 -19
- package/dist/service/lambda/function/code/sim-lambda-vm-zip-function.factory.d.ts +7 -0
- package/dist/service/lambda/function/code/sim-lambda-vm-zip-function.factory.js +1 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-lambda-provided-sdk-module.d.ts +19 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-lambda-provided-sdk-module.js +78 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-lambda-sdk-packages-not-installed.error.d.ts +15 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-lambda-sdk-packages-not-installed.error.js +27 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-lambda-vm-sdk-module-provider.d.ts +9 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-sdk-lambda-vm-module-provider.d.ts +10 -0
- package/dist/service/lambda/function/code/vm/sdk/sim-sdk-lambda-vm-module-provider.js +28 -34
- package/dist/service/lambda/function/code/vm/sim-lambda-host-modules.d.ts +15 -0
- package/dist/service/lambda/function/code/vm/sim-lambda-host-modules.js +31 -0
- package/dist/service/lambda/function/code/vm/sim-lambda-vm-module-resolver.d.ts +7 -0
- package/dist/service/lambda/function/code/vm/sim-lambda-vm-module-resolver.js +15 -0
- package/dist/service/lambda/function/code/vm/sim-lambda-vm-modules.js +5 -4
- package/dist/service/s3/command/put-bucket-policy/put-bucket-policy.handler.js +4 -1
- package/dist/service/scheduler/delivery/sim-scheduler-delivery-failures.d.ts +16 -0
- package/dist/service/scheduler/delivery/sim-scheduler-delivery-failures.js +19 -0
- package/dist/service/scheduler/index.d.ts +1 -1
- package/dist/service/scheduler/index.js +1 -1
- package/dist/service/secretsmanager/cfn/dynamic/sim-cfn-secrets-manager-dynamic-reference-resolver.d.ts +13 -15
- package/dist/service/secretsmanager/cfn/dynamic/sim-cfn-secrets-manager-dynamic-reference-resolver.js +22 -43
- package/dist/service/secretsmanager/cfn/dynamic/sim-cfn-secrets-manager-reference-stand-in.d.ts +9 -0
- package/dist/service/secretsmanager/cfn/dynamic/sim-cfn-secrets-manager-reference-stand-in.js +14 -0
- package/dist/service/secretsmanager/cfn/dynamic/sim-cfn-secrets-manager-reference-value.d.ts +12 -0
- package/dist/service/secretsmanager/cfn/dynamic/sim-cfn-secrets-manager-reference-value.js +29 -0
- package/dist/service/ssm/cfn/dynamic/sim-cfn-ssm-dynamic-reference-resolver.d.ts +22 -6
- package/dist/service/ssm/cfn/dynamic/sim-cfn-ssm-dynamic-reference-resolver.js +39 -24
- package/dist/service/ssm/cfn/dynamic/sim-cfn-ssm-secure-dynamic-reference-resolver.d.ts +10 -5
- package/dist/service/ssm/cfn/dynamic/sim-cfn-ssm-secure-dynamic-reference-resolver.js +10 -8
- package/dist/service/ssm/sim-ssm.d.ts +10 -2
- package/dist/service/ssm/sim-ssm.js +12 -4
- package/dist/service/sts/auth-z/assume-role-auth-z-coordinator.d.ts +9 -2
- package/dist/service/sts/auth-z/assume-role-auth-z-coordinator.js +2 -2
- package/dist/service/sts/auth-z/assume-role-source-account-auth-z.d.ts +10 -4
- package/dist/service/sts/auth-z/assume-role-source-account-auth-z.js +8 -7
- package/dist/service/sts/auth-z/assume-role-target-auth-z.d.ts +2 -2
- package/dist/service/sts/auth-z/assume-role-trust-policy-authorizer.d.ts +6 -2
- package/dist/service/sts/auth-z/assume-role-trust-policy-authorizer.js +14 -11
- package/dist/service/sts/command/assume-role/assume-role.handler.d.ts +5 -1
- package/dist/service/sts/command/assume-role/assume-role.handler.js +5 -2
- package/dist/service/sts/command/get-caller-identity/get-caller-identity.handler.d.ts +5 -1
- package/dist/service/sts/command/get-caller-identity/get-caller-identity.handler.js +4 -1
- package/dist/service/sts/service-role/sim-service-role.js +5 -1
- package/dist/service/sts/sim-sts.d.ts +9 -1
- package/dist/service/sts/sim-sts.js +4 -0
- package/docs/README.md +54 -0
- package/docs/ai-skill/README.md +69 -0
- package/docs/cli/README.md +284 -0
- package/docs/factories/README.md +140 -0
- package/docs/lint/README.md +176 -0
- package/docs/non-aws-dependencies/README.md +261 -0
- package/docs/sdk/README.md +272 -0
- package/docs/serve/README.md +1126 -0
- package/docs/services/acm/README.md +823 -0
- package/docs/services/apigateway/README.md +1919 -0
- package/docs/services/apigatewayv2/README.md +2678 -0
- package/docs/services/athena/README.md +1044 -0
- package/docs/services/bedrock/README.md +350 -0
- package/docs/services/cloudformation/README.md +3440 -0
- package/docs/services/cloudfront/README.md +2874 -0
- package/docs/services/cloudwatch/README.md +420 -0
- package/docs/services/cognito/README.md +4923 -0
- package/docs/services/dynamodb/README.md +3784 -0
- package/docs/services/ecr/README.md +302 -0
- package/docs/services/ecs/README.md +2010 -0
- package/docs/services/elbv2/README.md +1897 -0
- package/docs/services/eventbridge/README.md +1110 -0
- package/docs/services/firehose/README.md +805 -0
- package/docs/services/glue/README.md +501 -0
- package/docs/services/iam/README.md +1283 -0
- package/docs/services/kinesis/README.md +454 -0
- package/docs/services/kms/README.md +659 -0
- package/docs/services/lambda/README.md +3645 -0
- package/docs/services/logs/README.md +868 -0
- package/docs/services/organizations/README.md +574 -0
- package/docs/services/personalize/README.md +835 -0
- package/docs/services/rekognition/README.md +1070 -0
- package/docs/services/route53/README.md +1783 -0
- package/docs/services/s3/README.md +3024 -0
- package/docs/services/scheduler/README.md +806 -0
- package/docs/services/secretsmanager/README.md +667 -0
- package/docs/services/ses/README.md +1011 -0
- package/docs/services/sns/README.md +1904 -0
- package/docs/services/sqs/README.md +1185 -0
- package/docs/services/ssm/README.md +1072 -0
- package/docs/services/stepfunctions/README.md +1332 -0
- package/docs/services/sts/README.md +245 -0
- package/docs/services/wafv2/README.md +1378 -0
- package/docs/terraform/README.md +248 -0
- package/docs/time/README.md +284 -0
- package/llms.txt +59 -0
- package/package.json +9 -5
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
# Simulated CloudWatch Logs
|
|
2
|
+
|
|
3
|
+
Yulin includes a simulated Amazon CloudWatch Logs for tests and local development. It holds log
|
|
4
|
+
groups, the streams inside them and the events written to those streams. A test can put log events
|
|
5
|
+
and read them back with `GetLogEvents`, or search them with `FilterLogEvents`, without an AWS
|
|
6
|
+
account.
|
|
7
|
+
|
|
8
|
+
The point of it is to make log data addressable. Code that writes to CloudWatch Logs is code teams
|
|
9
|
+
already have, and the alternative for a test is capturing process output.
|
|
10
|
+
|
|
11
|
+
CloudWatch Logs specific types are imported from the `@kensio/yulin/logs` subpath.
|
|
12
|
+
|
|
13
|
+
## Writing and searching log events
|
|
14
|
+
|
|
15
|
+
A log group holds streams, a stream holds events, and `FilterLogEvents` searches across every
|
|
16
|
+
stream in a group. A test can therefore name the group and leave the stream out, without knowing
|
|
17
|
+
which execution environment wrote the line.
|
|
18
|
+
|
|
19
|
+
```typescript sim-logs-write-and-search
|
|
20
|
+
/**
|
|
21
|
+
* Writing log events to a simulated log group and searching for one.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import {
|
|
25
|
+
CreateLogGroupCommand,
|
|
26
|
+
CreateLogStreamCommand,
|
|
27
|
+
FilterLogEventsCommand,
|
|
28
|
+
PutLogEventsCommand,
|
|
29
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
30
|
+
|
|
31
|
+
import { SimAws } from "@kensio/yulin";
|
|
32
|
+
|
|
33
|
+
const simAws = new SimAws();
|
|
34
|
+
const logs = simAws.logs();
|
|
35
|
+
|
|
36
|
+
const logGroupName = "/aws/lambda/orders";
|
|
37
|
+
const logStreamName = "2026/08/16/[$LATEST]0f7c1a";
|
|
38
|
+
|
|
39
|
+
await logs.createLogGroup(new CreateLogGroupCommand({ logGroupName }));
|
|
40
|
+
await logs.createLogStream(
|
|
41
|
+
new CreateLogStreamCommand({ logGroupName, logStreamName }),
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
await logs.putLogEvents(
|
|
45
|
+
new PutLogEventsCommand({
|
|
46
|
+
logGroupName,
|
|
47
|
+
logStreamName,
|
|
48
|
+
logEvents: [
|
|
49
|
+
{
|
|
50
|
+
timestamp: Date.parse("2026-08-16T09:00:00Z"),
|
|
51
|
+
message: "INFO handling order-1",
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
timestamp: Date.parse("2026-08-16T09:00:01Z"),
|
|
55
|
+
message: "ERROR order has no items",
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
const found = await logs.filterLogEvents(
|
|
62
|
+
new FilterLogEventsCommand({ logGroupName, filterPattern: "ERROR" }),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
// One event, from the stream that wrote it.
|
|
66
|
+
console.log(found.events?.length, found.events?.[0]?.logStreamName);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
A write needs both the group and the stream to exist already. Real CloudWatch Logs refuses a write
|
|
70
|
+
to either one that is absent. A missing `logs:CreateLogStream` permission therefore shows up as a failure, where
|
|
71
|
+
otherwise the logs would quietly never appear.
|
|
72
|
+
|
|
73
|
+
## Filter patterns
|
|
74
|
+
|
|
75
|
+
The plain text filter pattern syntax is supported. Terms are matched as case sensitive substrings,
|
|
76
|
+
every unprefixed term must appear, a `-` prefix excludes a term, a `?` prefix makes a term one of a
|
|
77
|
+
set of alternatives, and a quoted phrase matches with its spaces intact.
|
|
78
|
+
|
|
79
|
+
| Pattern | Matches |
|
|
80
|
+
| ---------------------- | ------------------------------------------------ |
|
|
81
|
+
| `ERROR` | messages containing `ERROR` |
|
|
82
|
+
| `ERROR orders` | messages containing both terms |
|
|
83
|
+
| `?ERROR ?WARN` | messages containing either term |
|
|
84
|
+
| `ERROR -Throttling` | messages containing `ERROR` but not `Throttling` |
|
|
85
|
+
| `"order has no items"` | messages containing that exact phrase |
|
|
86
|
+
|
|
87
|
+
An omitted or empty pattern matches everything.
|
|
88
|
+
|
|
89
|
+
The structured pattern syntaxes are refused. A JSON property pattern (`{ $.level = "ERROR" }`), a
|
|
90
|
+
space delimited field pattern (`[level=ERROR, message]`) and a regular expression term
|
|
91
|
+
(`%ERROR|WARN%`) each raise `SimLogsUnsupportedOperationException`. Approximating one would be
|
|
92
|
+
worse. A pattern quietly treated as matching everything would turn an assertion about one log line
|
|
93
|
+
into an assertion about any log line at all, and the test would keep passing while testing nothing.
|
|
94
|
+
|
|
95
|
+
## Reading one stream
|
|
96
|
+
|
|
97
|
+
`GetLogEvents` reads a single stream and pages in both directions. With no token it answers with
|
|
98
|
+
the newest events, as real CloudWatch Logs does. `startFromHead` starts at the oldest. Following
|
|
99
|
+
`nextForwardToken` walks towards newer events, and reaching the end gives the same token back. A
|
|
100
|
+
caller polling a stream keeps it and asks again.
|
|
101
|
+
|
|
102
|
+
Both readers narrow to a half open time window. An event whose timestamp equals `startTime` is
|
|
103
|
+
included, and one whose timestamp equals `endTime` is left out.
|
|
104
|
+
|
|
105
|
+
A token is an offset into the events the request selected, so keep `startTime` and `endTime` the
|
|
106
|
+
same across a walk. Changing the window part-way through counts the offset against a different set
|
|
107
|
+
of events, and the page comes back as a different page.
|
|
108
|
+
|
|
109
|
+
```typescript sim-logs-read-a-stream
|
|
110
|
+
/**
|
|
111
|
+
* Paging through one simulated log stream from the oldest event.
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
import {
|
|
115
|
+
CreateLogGroupCommand,
|
|
116
|
+
CreateLogStreamCommand,
|
|
117
|
+
GetLogEventsCommand,
|
|
118
|
+
PutLogEventsCommand,
|
|
119
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
120
|
+
|
|
121
|
+
import { SimAws } from "@kensio/yulin";
|
|
122
|
+
|
|
123
|
+
const simAws = new SimAws();
|
|
124
|
+
const logs = simAws.logs();
|
|
125
|
+
|
|
126
|
+
const logGroupName = "/aws/lambda/orders";
|
|
127
|
+
const logStreamName = "2026/08/16/[$LATEST]0f7c1a";
|
|
128
|
+
|
|
129
|
+
await logs.createLogGroup(new CreateLogGroupCommand({ logGroupName }));
|
|
130
|
+
await logs.createLogStream(
|
|
131
|
+
new CreateLogStreamCommand({ logGroupName, logStreamName }),
|
|
132
|
+
);
|
|
133
|
+
await logs.putLogEvents(
|
|
134
|
+
new PutLogEventsCommand({
|
|
135
|
+
logGroupName,
|
|
136
|
+
logStreamName,
|
|
137
|
+
logEvents: [1, 2, 3, 4, 5].map((second) => ({
|
|
138
|
+
timestamp: Date.parse("2026-08-16T09:00:00Z") + second * 1000,
|
|
139
|
+
message: `line ${second}`,
|
|
140
|
+
})),
|
|
141
|
+
}),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
let nextToken: string | undefined;
|
|
145
|
+
const read: string[] = [];
|
|
146
|
+
|
|
147
|
+
for (;;) {
|
|
148
|
+
const page = await logs.getLogEvents(
|
|
149
|
+
new GetLogEventsCommand({
|
|
150
|
+
logGroupName,
|
|
151
|
+
logStreamName,
|
|
152
|
+
startFromHead: true,
|
|
153
|
+
limit: 2,
|
|
154
|
+
nextToken,
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
if (page.events === undefined || page.events.length === 0) break;
|
|
159
|
+
|
|
160
|
+
read.push(...page.events.map((event) => event.message));
|
|
161
|
+
nextToken = page.nextForwardToken;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
console.log(read);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Retention
|
|
168
|
+
|
|
169
|
+
Retention is held as a property to assert on. Events stay where they are, and seeing one go would
|
|
170
|
+
mean moving the clock by months. What teams get wrong about retention is the value they deployed,
|
|
171
|
+
ahead of the deletion that eventually follows from it. A log group with no retention keeps its
|
|
172
|
+
events forever, the AWS default.
|
|
173
|
+
|
|
174
|
+
The accepted values are a fixed set. A reasonable-looking `retentionInDays: 10` is refused here
|
|
175
|
+
exactly as it is by an account.
|
|
176
|
+
|
|
177
|
+
```typescript sim-logs-retention
|
|
178
|
+
/**
|
|
179
|
+
* Asserting on the retention a simulated log group was given.
|
|
180
|
+
*/
|
|
181
|
+
|
|
182
|
+
import {
|
|
183
|
+
CreateLogGroupCommand,
|
|
184
|
+
DescribeLogGroupsCommand,
|
|
185
|
+
PutRetentionPolicyCommand,
|
|
186
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
187
|
+
|
|
188
|
+
import { SimAws } from "@kensio/yulin";
|
|
189
|
+
|
|
190
|
+
const simAws = new SimAws();
|
|
191
|
+
const logs = simAws.logs();
|
|
192
|
+
|
|
193
|
+
const logGroupName = "/aws/lambda/orders";
|
|
194
|
+
|
|
195
|
+
await logs.createLogGroup(new CreateLogGroupCommand({ logGroupName }));
|
|
196
|
+
await logs.putRetentionPolicy(
|
|
197
|
+
new PutRetentionPolicyCommand({ logGroupName, retentionInDays: 14 }),
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
const described = await logs.describeLogGroups(
|
|
201
|
+
new DescribeLogGroupsCommand({ logGroupNamePrefix: "/aws/lambda/" }),
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
// 14, and the ARN form a policy is written against.
|
|
205
|
+
console.log(
|
|
206
|
+
described.logGroups?.[0]?.retentionInDays,
|
|
207
|
+
described.logGroups?.[0]?.arn,
|
|
208
|
+
);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Lambda handler output
|
|
212
|
+
|
|
213
|
+
A Lambda function's output is recorded into `/aws/lambda/<function name>` as it runs, whether its
|
|
214
|
+
code is a zip archive or a real in-process handler. A test can then assert on what a handler logged
|
|
215
|
+
by searching its log group.
|
|
216
|
+
|
|
217
|
+
```typescript sim-logs-lambda-output
|
|
218
|
+
/**
|
|
219
|
+
* Asserting on what a simulated Lambda handler logged.
|
|
220
|
+
*/
|
|
221
|
+
|
|
222
|
+
import { FilterLogEventsCommand } from "@aws-sdk/client-cloudwatch-logs";
|
|
223
|
+
import { CreateFunctionCommand, InvokeCommand } from "@aws-sdk/client-lambda";
|
|
224
|
+
|
|
225
|
+
import { SimAws } from "@kensio/yulin";
|
|
226
|
+
import { makeLambdaCodeZip } from "@kensio/yulin/lambda";
|
|
227
|
+
|
|
228
|
+
const simAws = new SimAws();
|
|
229
|
+
|
|
230
|
+
await simAws.lambda().createFunction(
|
|
231
|
+
new CreateFunctionCommand({
|
|
232
|
+
FunctionName: "orders",
|
|
233
|
+
Role: `arn:aws:iam::${simAws.defaultAccountId}:role/OrdersRole`,
|
|
234
|
+
Handler: "index.handler",
|
|
235
|
+
Code: {
|
|
236
|
+
ZipFile: makeLambdaCodeZip({
|
|
237
|
+
"index.js":
|
|
238
|
+
"exports.handler = async () => {\n" +
|
|
239
|
+
' console.error("ERROR order has no items");\n' +
|
|
240
|
+
"};\n",
|
|
241
|
+
}),
|
|
242
|
+
},
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
await simAws.backgroundTasksComplete();
|
|
247
|
+
await simAws.lambda().invoke(new InvokeCommand({ FunctionName: "orders" }));
|
|
248
|
+
|
|
249
|
+
const found = await simAws.logs().filterLogEvents(
|
|
250
|
+
new FilterLogEventsCommand({
|
|
251
|
+
logGroupName: "/aws/lambda/orders",
|
|
252
|
+
filterPattern: "ERROR",
|
|
253
|
+
}),
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
console.log(found.events?.[0]?.message);
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
The output still reaches the terminal as well. Real Lambda sends it to CloudWatch Logs and nowhere
|
|
260
|
+
else, but a test tool that swallowed it would make a failing test harder to debug. Recording is a
|
|
261
|
+
tee.
|
|
262
|
+
|
|
263
|
+
Each invocation's `context.logGroupName` and `context.logStreamName` name the group and stream that
|
|
264
|
+
were actually written to. Stream names use the real `YYYY/MM/DD/[$LATEST]<hash>` format. The hash
|
|
265
|
+
identifies the execution environment, and one environment serves more than one request. Match the
|
|
266
|
+
shape in a test, and leave the value alone.
|
|
267
|
+
|
|
268
|
+
Writing on this path is unconditional. A real function needs `logs:CreateLogGroup` and
|
|
269
|
+
`logs:PutLogEvents` on its execution Role, and one without them produces no logs at all, in silence.
|
|
270
|
+
Simulating that would leave nearly every function in a test logging nothing, with no failure to
|
|
271
|
+
explain why.
|
|
272
|
+
|
|
273
|
+
## Declaring a log group in a template
|
|
274
|
+
|
|
275
|
+
`AWS::Logs::LogGroup` is deployed by simulated CloudFormation. A test can then assert on the
|
|
276
|
+
retention a stack gave a group.
|
|
277
|
+
|
|
278
|
+
```yaml
|
|
279
|
+
OrdersLogs:
|
|
280
|
+
Type: AWS::Logs::LogGroup
|
|
281
|
+
Properties:
|
|
282
|
+
LogGroupName: /aws/lambda/orders
|
|
283
|
+
RetentionInDays: 14
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
`Ref` resolves to the log group name, and `Fn::GetAtt Arn` to the ARN with its trailing `:*`. That is
|
|
287
|
+
the form a policy has to name. A template granting a function permission on its own log group gets a
|
|
288
|
+
resource that reaches the streams inside it.
|
|
289
|
+
|
|
290
|
+
`LogGroupName` and `RetentionInDays` are the two properties acted on. A `RetentionInDays` outside the
|
|
291
|
+
set AWS accepts fails the deploy, where otherwise it would only be found on a real one. Everything
|
|
292
|
+
else is recorded as an ignored property. A reader can see what a deployed group leaves out, and the
|
|
293
|
+
stack still deploys.
|
|
294
|
+
|
|
295
|
+
Two divergences to know about:
|
|
296
|
+
|
|
297
|
+
- **A group that already exists is taken over.** Real CloudFormation fails a deploy that declares a
|
|
298
|
+
log group already in the account. That is a genuine misconfiguration there and pure noise here,
|
|
299
|
+
where a Lambda function that logged during test setup has already created `/aws/lambda/orders`.
|
|
300
|
+
- **An update replaces the group.** Simulated CloudFormation has no in-place update at all. Any
|
|
301
|
+
resource whose template entry changed is deleted and created again. The retention ends up correct,
|
|
302
|
+
but the events the group held are gone, where a real update to `RetentionInDays` keeps them.
|
|
303
|
+
|
|
304
|
+
## Delivering logs from another service
|
|
305
|
+
|
|
306
|
+
CloudWatch Logs delivery carries the logs of another service somewhere. CloudFront standard logging
|
|
307
|
+
v2 is the clearest case. A distribution has no logging property of its own, and turning logging on
|
|
308
|
+
means three CloudWatch Logs resources.
|
|
309
|
+
|
|
310
|
+
A delivery source names what is being logged and which of its logs. A delivery destination names
|
|
311
|
+
where they land and in what form. A delivery joins one to the other.
|
|
312
|
+
|
|
313
|
+
```typescript sim-logs-delivery
|
|
314
|
+
/**
|
|
315
|
+
* Setting up CloudFront standard logging v2 delivery into a bucket.
|
|
316
|
+
*/
|
|
317
|
+
|
|
318
|
+
import { CreateDistributionCommand } from "@aws-sdk/client-cloudfront";
|
|
319
|
+
import {
|
|
320
|
+
CreateDeliveryCommand,
|
|
321
|
+
DescribeDeliveriesCommand,
|
|
322
|
+
PutDeliveryDestinationCommand,
|
|
323
|
+
PutDeliverySourceCommand,
|
|
324
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
325
|
+
|
|
326
|
+
import { SimAws } from "@kensio/yulin";
|
|
327
|
+
|
|
328
|
+
const simAws = new SimAws();
|
|
329
|
+
const logs = simAws.logs();
|
|
330
|
+
|
|
331
|
+
const distribution = await simAws.cloudFront().createDistribution(
|
|
332
|
+
new CreateDistributionCommand({
|
|
333
|
+
DistributionConfig: {
|
|
334
|
+
CallerReference: "site",
|
|
335
|
+
Comment: "Static site distribution",
|
|
336
|
+
Enabled: true,
|
|
337
|
+
Origins: {
|
|
338
|
+
Quantity: 1,
|
|
339
|
+
Items: [
|
|
340
|
+
{
|
|
341
|
+
Id: "site-origin",
|
|
342
|
+
DomainName: "origin.example.com",
|
|
343
|
+
CustomOriginConfig: {
|
|
344
|
+
HTTPPort: 80,
|
|
345
|
+
HTTPSPort: 443,
|
|
346
|
+
OriginProtocolPolicy: "https-only",
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
],
|
|
350
|
+
},
|
|
351
|
+
DefaultCacheBehavior: {
|
|
352
|
+
TargetOriginId: "site-origin",
|
|
353
|
+
ViewerProtocolPolicy: "redirect-to-https",
|
|
354
|
+
},
|
|
355
|
+
},
|
|
356
|
+
}),
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
await logs.putDeliverySource(
|
|
360
|
+
new PutDeliverySourceCommand({
|
|
361
|
+
name: "site-access-logs",
|
|
362
|
+
resourceArn: `arn:aws:cloudfront::${simAws.defaultAccountId}:distribution/${distribution.Distribution?.Id}`,
|
|
363
|
+
logType: "ACCESS_LOGS",
|
|
364
|
+
}),
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
const destination = await logs.putDeliveryDestination(
|
|
368
|
+
new PutDeliveryDestinationCommand({
|
|
369
|
+
name: "site-access-logs",
|
|
370
|
+
outputFormat: "json",
|
|
371
|
+
deliveryDestinationConfiguration: {
|
|
372
|
+
destinationResourceArn: "arn:aws:s3:::example-access-logs",
|
|
373
|
+
},
|
|
374
|
+
}),
|
|
375
|
+
);
|
|
376
|
+
|
|
377
|
+
await logs.createDelivery(
|
|
378
|
+
new CreateDeliveryCommand({
|
|
379
|
+
deliverySourceName: "site-access-logs",
|
|
380
|
+
deliveryDestinationArn: destination.deliveryDestination?.arn,
|
|
381
|
+
s3DeliveryConfiguration: {
|
|
382
|
+
suffixPath: "{DistributionId}/{yyyy}/{MM}/{dd}/{HH}",
|
|
383
|
+
enableHiveCompatiblePath: true,
|
|
384
|
+
},
|
|
385
|
+
}),
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
const deliveries = await logs.describeDeliveries(
|
|
389
|
+
new DescribeDeliveriesCommand({}),
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
// S3, and the layout the bucket will be partitioned by.
|
|
393
|
+
console.log(
|
|
394
|
+
deliveries.deliveries?.[0]?.deliveryDestinationType,
|
|
395
|
+
deliveries.deliveries?.[0]?.s3DeliveryConfiguration?.suffixPath,
|
|
396
|
+
);
|
|
397
|
+
```
|
|
398
|
+
|
|
399
|
+
The delivery destination has an ARN of its own, and `CreateDelivery` names that one. The bucket
|
|
400
|
+
behind it keeps its own ARN, and the two are easy to mix up.
|
|
401
|
+
|
|
402
|
+
The service a delivery source is for is read off the resource ARN. A caller never states it. These
|
|
403
|
+
rules from real AWS are modelled here, each of them a deploy that looks fine until it runs:
|
|
404
|
+
|
|
405
|
+
- **The distribution has to be there.** A CloudFront delivery source over a distribution the
|
|
406
|
+
simulated account never created fails with `ResourceNotFoundException`, and so does one whose ARN
|
|
407
|
+
names another account. Pin a real distribution id in a template and the deploy fails here the way
|
|
408
|
+
it would in an account. A `SimLogs` built on its own has no CloudFront to look in and keeps taking
|
|
409
|
+
any ARN, so a test about delivery alone needs no distribution.
|
|
410
|
+
- **One delivery source per resource.** A second source over a distribution that already has one
|
|
411
|
+
fails with `ConflictException`, carrying the message an account gives, "This ResourceId has
|
|
412
|
+
already been used in another Delivery Source in this account".
|
|
413
|
+
- **A delivery holds both its ends.** Deleting the source or the destination while a delivery joins
|
|
414
|
+
them fails with `ConflictException`. The delivery goes first, and CloudFormation orders that
|
|
415
|
+
itself when a stack is deleted.
|
|
416
|
+
- **CloudFront delivers `ACCESS_LOGS` and nothing else.** Any other `logType` over a distribution is
|
|
417
|
+
refused.
|
|
418
|
+
- **CloudFront delivery is set up from `us-east-1`**, whatever region the destination bucket is in.
|
|
419
|
+
A CloudFront delivery source put from anywhere else is refused.
|
|
420
|
+
- **Hive compatible paths write the `key=` half themselves.** A suffix path naming the partition
|
|
421
|
+
keys as well is refused. Delivery turns `{yyyy}` into `year=2026`, and `year={yyyy}` arrives
|
|
422
|
+
doubled, which an account answers with "Provided suffixPath is invalid".
|
|
423
|
+
- **Output format is fixed once the destination exists.** A `PutDeliveryDestination` that would
|
|
424
|
+
change it is refused. Changing a format means deleting the destination and making it again.
|
|
425
|
+
- **Parquet is written to S3 only.** A log group or Firehose destination asking for it is refused.
|
|
426
|
+
|
|
427
|
+
The suffix path decides the key each log file lands under. `{DistributionId}`, `{distributionid}`,
|
|
428
|
+
`{yyyy}`, `{MM}`, `{dd}`, `{HH}` and `{accountid}` are substituted, and a variable outside that set
|
|
429
|
+
is refused. Delivery would write the text out literally, and the bucket would look partitioned when
|
|
430
|
+
it was not. A path over the 256 characters CloudWatch Logs takes is refused too.
|
|
431
|
+
|
|
432
|
+
Under `enableHiveCompatiblePath`, delivery writes each segment as `key=value` and supplies the key
|
|
433
|
+
itself. `{yyyy}` lands as `year=2026` and `{distributionid}` as `distributionid=E1EXAMPLE1234`. A
|
|
434
|
+
suffix path naming those keys is refused, because the key would arrive twice. Leave the segments as
|
|
435
|
+
bare variables and let delivery name them.
|
|
436
|
+
|
|
437
|
+
### Declaring delivery in a template
|
|
438
|
+
|
|
439
|
+
The same three resources in a template, which is the whole of what a CDK construct for CloudFront
|
|
440
|
+
logging synthesises, alongside the distribution they are for. The source's `ResourceArn` is built
|
|
441
|
+
around a `Ref` to that distribution, the way CDK builds it. A pinned distribution id fails the
|
|
442
|
+
deploy (see [the rules above](#delivering-logs-from-another-service)).
|
|
443
|
+
|
|
444
|
+
```yaml
|
|
445
|
+
SiteDistribution:
|
|
446
|
+
Type: AWS::CloudFront::Distribution
|
|
447
|
+
Properties:
|
|
448
|
+
DistributionConfig:
|
|
449
|
+
Enabled: true
|
|
450
|
+
Origins:
|
|
451
|
+
Items:
|
|
452
|
+
- Id: site-origin
|
|
453
|
+
DomainName: origin.example.com
|
|
454
|
+
CustomOriginConfig:
|
|
455
|
+
OriginProtocolPolicy: https-only
|
|
456
|
+
DefaultCacheBehavior:
|
|
457
|
+
TargetOriginId: site-origin
|
|
458
|
+
ViewerProtocolPolicy: redirect-to-https
|
|
459
|
+
|
|
460
|
+
AccessLogsSource:
|
|
461
|
+
Type: AWS::Logs::DeliverySource
|
|
462
|
+
Properties:
|
|
463
|
+
Name: site-access-logs
|
|
464
|
+
ResourceArn:
|
|
465
|
+
!Join [
|
|
466
|
+
"",
|
|
467
|
+
[
|
|
468
|
+
"arn:aws:cloudfront::",
|
|
469
|
+
!Ref "AWS::AccountId",
|
|
470
|
+
":distribution/",
|
|
471
|
+
!Ref SiteDistribution,
|
|
472
|
+
],
|
|
473
|
+
]
|
|
474
|
+
LogType: ACCESS_LOGS
|
|
475
|
+
|
|
476
|
+
AccessLogsDestination:
|
|
477
|
+
Type: AWS::Logs::DeliveryDestination
|
|
478
|
+
Properties:
|
|
479
|
+
Name: site-access-logs
|
|
480
|
+
DestinationResourceArn: arn:aws:s3:::example-access-logs
|
|
481
|
+
OutputFormat: json
|
|
482
|
+
|
|
483
|
+
AccessLogsDelivery:
|
|
484
|
+
Type: AWS::Logs::Delivery
|
|
485
|
+
Properties:
|
|
486
|
+
DeliverySourceName: !Ref AccessLogsSource
|
|
487
|
+
DeliveryDestinationArn: !GetAtt AccessLogsDestination.Arn
|
|
488
|
+
S3SuffixPath: "{DistributionId}/{yyyy}/{MM}/{dd}/{HH}"
|
|
489
|
+
S3EnableHiveCompatiblePath: true
|
|
490
|
+
```
|
|
491
|
+
|
|
492
|
+
The template carries the S3 layout as two flat properties, and the API takes them nested under
|
|
493
|
+
`s3DeliveryConfiguration`.
|
|
494
|
+
|
|
495
|
+
`Ref` resolves to the name on the source and the destination, and to the delivery ID on the
|
|
496
|
+
delivery. CloudWatch Logs issues that ID. A template cannot predict it.
|
|
497
|
+
|
|
498
|
+
`Fn::GetAtt` gives `Arn` on all three. The source also publishes `Service` and `ResourceArns`, and
|
|
499
|
+
the delivery publishes `DeliveryId` and `DeliveryDestinationType`. The destination publishes nothing
|
|
500
|
+
else, and `DeliveryDestinationType` is a property of it rather than an attribute, so read that one
|
|
501
|
+
off the delivery. Anything outside this set is refused here, as CloudFormation refuses it.
|
|
502
|
+
|
|
503
|
+
`Tags` and `DeliveryDestinationPolicy` are recorded as ignored properties, and the stack still
|
|
504
|
+
deploys.
|
|
505
|
+
|
|
506
|
+
## Subscription filters
|
|
507
|
+
|
|
508
|
+
A subscription filter delivers the events matching its pattern to a Lambda function. Code written to
|
|
509
|
+
forward log lines to an error tracker or a metrics sink can be tested against the handler it
|
|
510
|
+
forwards from.
|
|
511
|
+
|
|
512
|
+
```typescript sim-logs-subscription-filter
|
|
513
|
+
/**
|
|
514
|
+
* Delivering matched log events to a simulated Lambda function.
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
import { gunzipSync } from "node:zlib";
|
|
518
|
+
|
|
519
|
+
import {
|
|
520
|
+
CreateLogGroupCommand,
|
|
521
|
+
CreateLogStreamCommand,
|
|
522
|
+
PutLogEventsCommand,
|
|
523
|
+
PutSubscriptionFilterCommand,
|
|
524
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
525
|
+
import {
|
|
526
|
+
AddPermissionCommand,
|
|
527
|
+
CreateFunctionCommand,
|
|
528
|
+
} from "@aws-sdk/client-lambda";
|
|
529
|
+
|
|
530
|
+
import { SimAws } from "@kensio/yulin";
|
|
531
|
+
import { makeLambdaZipFileInput } from "@kensio/yulin/lambda";
|
|
532
|
+
|
|
533
|
+
const simAws = new SimAws();
|
|
534
|
+
const logGroupName = "/aws/lambda/orders";
|
|
535
|
+
const logStreamName = "2026/08/16/[$LATEST]0f7c1a";
|
|
536
|
+
const received: string[] = [];
|
|
537
|
+
|
|
538
|
+
await simAws.lambda().createFunction(
|
|
539
|
+
new CreateFunctionCommand({
|
|
540
|
+
FunctionName: "error-tracker",
|
|
541
|
+
Role: `arn:aws:iam::${simAws.defaultAccountId}:role/TrackerRole`,
|
|
542
|
+
Code: {
|
|
543
|
+
ZipFile: makeLambdaZipFileInput(
|
|
544
|
+
(event: { awslogs: { data: string } }) => {
|
|
545
|
+
const decoded = JSON.parse(
|
|
546
|
+
gunzipSync(Buffer.from(event.awslogs.data, "base64")).toString(),
|
|
547
|
+
) as { logEvents: { message: string }[] };
|
|
548
|
+
|
|
549
|
+
received.push(...decoded.logEvents.map((line) => line.message));
|
|
550
|
+
|
|
551
|
+
return "recorded";
|
|
552
|
+
},
|
|
553
|
+
),
|
|
554
|
+
},
|
|
555
|
+
}),
|
|
556
|
+
);
|
|
557
|
+
|
|
558
|
+
// CloudWatch Logs invokes as a regional service principal, so this is the
|
|
559
|
+
// grant a subscription filter needs on the function's side.
|
|
560
|
+
await simAws.lambda().addPermission(
|
|
561
|
+
new AddPermissionCommand({
|
|
562
|
+
FunctionName: "error-tracker",
|
|
563
|
+
StatementId: "logs",
|
|
564
|
+
Action: "lambda:InvokeFunction",
|
|
565
|
+
Principal: `logs.${simAws.defaultRegionName}.amazonaws.com`,
|
|
566
|
+
}),
|
|
567
|
+
);
|
|
568
|
+
await simAws.backgroundTasksComplete();
|
|
569
|
+
|
|
570
|
+
await simAws.logs().createLogGroup(new CreateLogGroupCommand({ logGroupName }));
|
|
571
|
+
await simAws
|
|
572
|
+
.logs()
|
|
573
|
+
.createLogStream(new CreateLogStreamCommand({ logGroupName, logStreamName }));
|
|
574
|
+
|
|
575
|
+
await simAws.logs().putSubscriptionFilter(
|
|
576
|
+
new PutSubscriptionFilterCommand({
|
|
577
|
+
logGroupName,
|
|
578
|
+
filterName: "errors-to-tracker",
|
|
579
|
+
filterPattern: "ERROR",
|
|
580
|
+
destinationArn: `arn:aws:lambda:${simAws.defaultRegionName}:${simAws.defaultAccountId}:function:error-tracker`,
|
|
581
|
+
}),
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
await simAws.logs().putLogEvents(
|
|
585
|
+
new PutLogEventsCommand({
|
|
586
|
+
logGroupName,
|
|
587
|
+
logStreamName,
|
|
588
|
+
logEvents: [
|
|
589
|
+
{
|
|
590
|
+
timestamp: Date.parse("2026-08-16T09:00:00Z"),
|
|
591
|
+
message: "INFO starting",
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
timestamp: Date.parse("2026-08-16T09:00:01Z"),
|
|
595
|
+
message: "ERROR order has no items",
|
|
596
|
+
},
|
|
597
|
+
],
|
|
598
|
+
}),
|
|
599
|
+
);
|
|
600
|
+
|
|
601
|
+
// Delivery happens after the write is answered, as it does in an account.
|
|
602
|
+
await simAws.backgroundTasksComplete();
|
|
603
|
+
|
|
604
|
+
console.log(received);
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
The payload is the real one. An `awslogs.data` field holds the base64 of a gzipped JSON document
|
|
608
|
+
with `messageType`, `owner`, `logGroup`, `logStream`, `subscriptionFilters` and `logEvents`. A
|
|
609
|
+
handler written against a real subscription decodes it unchanged.
|
|
610
|
+
|
|
611
|
+
The behaviour in detail:
|
|
612
|
+
|
|
613
|
+
- **Delivery is asynchronous.** `PutLogEvents` is answered before anything is delivered. A test
|
|
614
|
+
waits with `await simAws.backgroundTasksComplete()`. A destination that throws leaves the write
|
|
615
|
+
that triggered it alone.
|
|
616
|
+
- **A failed delivery is kept.** Real CloudWatch Logs tells nobody when a delivery fails, and it
|
|
617
|
+
becomes a metric nobody is watching. `simAws.logs().subscriptionFailures` holds them. A test can
|
|
618
|
+
find out that the subscription it set up never reached anything.
|
|
619
|
+
- **The destination is checked when the filter is put.** A function that has yet to grant
|
|
620
|
+
`logs.<region>.amazonaws.com` permission to invoke it fails `PutSubscriptionFilter`, as it does in
|
|
621
|
+
an account. The alternative would be a filter that silently drops every event. The resource policy
|
|
622
|
+
is consulted again on every delivery, and a permission removed later stops delivery too.
|
|
623
|
+
- **What a Lambda function logged is delivered as well.** A subscription on `/aws/lambda/orders`
|
|
624
|
+
picks up what that function wrote. A forwarder can be tested against a real handler's output.
|
|
625
|
+
- **Lambda is the only destination.** Kinesis, Firehose and cross-account destinations are refused
|
|
626
|
+
outright.
|
|
627
|
+
- **A destination can name a version or an alias.** See
|
|
628
|
+
[Subscribing a Lambda alias](#subscribing-a-lambda-alias).
|
|
629
|
+
- **Two filters per log group**, the current AWS account default.
|
|
630
|
+
|
|
631
|
+
### Subscribing a Lambda alias
|
|
632
|
+
|
|
633
|
+
A `destinationArn` can carry a version number or an alias name on the end, and matched events go to
|
|
634
|
+
the version that qualifier names. The grant is made on the same qualifier:
|
|
635
|
+
|
|
636
|
+
```typescript sim-logs-subscription-alias
|
|
637
|
+
/**
|
|
638
|
+
* Delivering matched log events to a simulated Lambda alias.
|
|
639
|
+
*/
|
|
640
|
+
|
|
641
|
+
import {
|
|
642
|
+
CreateLogGroupCommand,
|
|
643
|
+
CreateLogStreamCommand,
|
|
644
|
+
PutLogEventsCommand,
|
|
645
|
+
PutSubscriptionFilterCommand,
|
|
646
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
647
|
+
import {
|
|
648
|
+
AddPermissionCommand,
|
|
649
|
+
CreateAliasCommand,
|
|
650
|
+
CreateFunctionCommand,
|
|
651
|
+
PublishVersionCommand,
|
|
652
|
+
} from "@aws-sdk/client-lambda";
|
|
653
|
+
|
|
654
|
+
import { SimAws } from "@kensio/yulin";
|
|
655
|
+
import { makeLambdaZipFileInput } from "@kensio/yulin/lambda";
|
|
656
|
+
|
|
657
|
+
const simAws = new SimAws();
|
|
658
|
+
const lambda = simAws.lambda();
|
|
659
|
+
const logGroupName = "/aws/lambda/orders";
|
|
660
|
+
const logStreamName = "2026/08/19/[$LATEST]0f7c1a";
|
|
661
|
+
const trackerArn = `arn:aws:lambda:${simAws.defaultRegionName}:${simAws.defaultAccountId}:function:error-tracker`;
|
|
662
|
+
|
|
663
|
+
await lambda.createFunction(
|
|
664
|
+
new CreateFunctionCommand({
|
|
665
|
+
FunctionName: "error-tracker",
|
|
666
|
+
Role: `arn:aws:iam::${simAws.defaultAccountId}:role/TrackerRole`,
|
|
667
|
+
Code: {
|
|
668
|
+
ZipFile: makeLambdaZipFileInput((_event, context) => {
|
|
669
|
+
console.log(context.functionVersion); // "1", the version behind `live`
|
|
670
|
+
|
|
671
|
+
return "recorded";
|
|
672
|
+
}),
|
|
673
|
+
},
|
|
674
|
+
}),
|
|
675
|
+
);
|
|
676
|
+
await simAws.backgroundTasksComplete();
|
|
677
|
+
|
|
678
|
+
const published = await lambda.publishVersion(
|
|
679
|
+
new PublishVersionCommand({ FunctionName: "error-tracker" }),
|
|
680
|
+
);
|
|
681
|
+
|
|
682
|
+
await lambda.createAlias(
|
|
683
|
+
new CreateAliasCommand({
|
|
684
|
+
FunctionName: "error-tracker",
|
|
685
|
+
Name: "live",
|
|
686
|
+
FunctionVersion: published.Version,
|
|
687
|
+
}),
|
|
688
|
+
);
|
|
689
|
+
|
|
690
|
+
await lambda.addPermission(
|
|
691
|
+
new AddPermissionCommand({
|
|
692
|
+
FunctionName: "error-tracker",
|
|
693
|
+
Qualifier: "live",
|
|
694
|
+
StatementId: "logs",
|
|
695
|
+
Action: "lambda:InvokeFunction",
|
|
696
|
+
Principal: `logs.${simAws.defaultRegionName}.amazonaws.com`,
|
|
697
|
+
}),
|
|
698
|
+
);
|
|
699
|
+
|
|
700
|
+
await simAws.logs().createLogGroup(new CreateLogGroupCommand({ logGroupName }));
|
|
701
|
+
await simAws
|
|
702
|
+
.logs()
|
|
703
|
+
.createLogStream(new CreateLogStreamCommand({ logGroupName, logStreamName }));
|
|
704
|
+
|
|
705
|
+
await simAws.logs().putSubscriptionFilter(
|
|
706
|
+
new PutSubscriptionFilterCommand({
|
|
707
|
+
logGroupName,
|
|
708
|
+
filterName: "errors-to-tracker",
|
|
709
|
+
filterPattern: "ERROR",
|
|
710
|
+
destinationArn: `${trackerArn}:live`,
|
|
711
|
+
}),
|
|
712
|
+
);
|
|
713
|
+
|
|
714
|
+
await simAws.logs().putLogEvents(
|
|
715
|
+
new PutLogEventsCommand({
|
|
716
|
+
logGroupName,
|
|
717
|
+
logStreamName,
|
|
718
|
+
logEvents: [{ timestamp: 1000, message: "ERROR order has no items" }],
|
|
719
|
+
}),
|
|
720
|
+
);
|
|
721
|
+
|
|
722
|
+
await simAws.backgroundTasksComplete();
|
|
723
|
+
```
|
|
724
|
+
|
|
725
|
+
A qualifier naming no version and no alias is refused where the filter is put, the way a missing
|
|
726
|
+
function is. `UpdateAlias` moves what the filter reaches, and the filter stays as it is.
|
|
727
|
+
|
|
728
|
+
## Permissions
|
|
729
|
+
|
|
730
|
+
Every operation goes through simulated IAM. An operation on a named log group authorizes against
|
|
731
|
+
that group's ARN with the trailing `:*`. That is the form CloudWatch Logs policies are written in.
|
|
732
|
+
Granting `logs:PutLogEvents` on a group grants it on the streams inside, and the wildcard is what
|
|
733
|
+
covers them. A policy naming `log-group:/aws/lambda/orders` without it reaches no stream here,
|
|
734
|
+
exactly as on real AWS.
|
|
735
|
+
|
|
736
|
+
`DescribeLogGroups` names no particular group. It authorizes against every log group in the account
|
|
737
|
+
and region, and a policy scoped to one group cannot describe them all.
|
|
738
|
+
|
|
739
|
+
```typescript sim-logs-permissions
|
|
740
|
+
/**
|
|
741
|
+
* A simulated IAM policy allowing a Role to write one function's logs.
|
|
742
|
+
*/
|
|
743
|
+
|
|
744
|
+
import {
|
|
745
|
+
CreateLogGroupCommand,
|
|
746
|
+
CreateLogStreamCommand,
|
|
747
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
748
|
+
import { CreateRoleCommand, PutRolePolicyCommand } from "@aws-sdk/client-iam";
|
|
749
|
+
|
|
750
|
+
import { SimAws } from "@kensio/yulin";
|
|
751
|
+
|
|
752
|
+
const simAws = new SimAws();
|
|
753
|
+
const accountId = simAws.defaultAccountId;
|
|
754
|
+
const regionName = simAws.defaultRegionName;
|
|
755
|
+
const logGroupName = "/aws/lambda/orders";
|
|
756
|
+
|
|
757
|
+
const role = await simAws.iam().createRole(
|
|
758
|
+
new CreateRoleCommand({
|
|
759
|
+
RoleName: "OrdersFunctionRole",
|
|
760
|
+
AssumeRolePolicyDocument: JSON.stringify({
|
|
761
|
+
Version: "2012-10-17",
|
|
762
|
+
Statement: {
|
|
763
|
+
Effect: "Allow",
|
|
764
|
+
Principal: { Service: "lambda.amazonaws.com" },
|
|
765
|
+
Action: "sts:AssumeRole",
|
|
766
|
+
},
|
|
767
|
+
}),
|
|
768
|
+
}),
|
|
769
|
+
);
|
|
770
|
+
|
|
771
|
+
await simAws.iam().putRolePolicy(
|
|
772
|
+
new PutRolePolicyCommand({
|
|
773
|
+
RoleName: "OrdersFunctionRole",
|
|
774
|
+
PolicyName: "WriteOwnLogs",
|
|
775
|
+
PolicyDocument: JSON.stringify({
|
|
776
|
+
Version: "2012-10-17",
|
|
777
|
+
Statement: {
|
|
778
|
+
Effect: "Allow",
|
|
779
|
+
Action: [
|
|
780
|
+
"logs:CreateLogGroup",
|
|
781
|
+
"logs:CreateLogStream",
|
|
782
|
+
"logs:PutLogEvents",
|
|
783
|
+
],
|
|
784
|
+
// The trailing wildcard covers the streams inside the group.
|
|
785
|
+
Resource: `arn:aws:logs:${regionName}:${accountId}:log-group:${logGroupName}:*`,
|
|
786
|
+
},
|
|
787
|
+
}),
|
|
788
|
+
}),
|
|
789
|
+
);
|
|
790
|
+
|
|
791
|
+
const asRole = { caller: { kind: "arn", arn: role.Role.Arn } } as const;
|
|
792
|
+
|
|
793
|
+
await simAws
|
|
794
|
+
.logs()
|
|
795
|
+
.createLogGroup(new CreateLogGroupCommand({ logGroupName }), asRole);
|
|
796
|
+
await simAws.logs().createLogStream(
|
|
797
|
+
new CreateLogStreamCommand({
|
|
798
|
+
logGroupName,
|
|
799
|
+
logStreamName: "2026/08/16/[$LATEST]0f7c1a",
|
|
800
|
+
}),
|
|
801
|
+
asRole,
|
|
802
|
+
);
|
|
803
|
+
|
|
804
|
+
console.log(simAws.logs().findLogGroup(logGroupName)?.logGroupArn);
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
## Through an intercepted SDK client
|
|
808
|
+
|
|
809
|
+
Application code that constructs its own `CloudWatchLogsClient` reaches the simulator through SDK
|
|
810
|
+
interception, with the code under test unchanged.
|
|
811
|
+
|
|
812
|
+
```typescript sim-logs-sdk-interception
|
|
813
|
+
/**
|
|
814
|
+
* Reaching simulated CloudWatch Logs through an intercepted SDK client.
|
|
815
|
+
*/
|
|
816
|
+
|
|
817
|
+
import {
|
|
818
|
+
CloudWatchLogsClient,
|
|
819
|
+
CreateLogGroupCommand,
|
|
820
|
+
DescribeLogGroupsCommand,
|
|
821
|
+
} from "@aws-sdk/client-cloudwatch-logs";
|
|
822
|
+
|
|
823
|
+
import { SimSdk } from "@kensio/yulin/sdk";
|
|
824
|
+
|
|
825
|
+
using simSdk = new SimSdk();
|
|
826
|
+
simSdk.intercept(CloudWatchLogsClient);
|
|
827
|
+
|
|
828
|
+
const client = new CloudWatchLogsClient({ region: "eu-west-2" });
|
|
829
|
+
|
|
830
|
+
await client.send(
|
|
831
|
+
new CreateLogGroupCommand({ logGroupName: "/aws/lambda/orders" }),
|
|
832
|
+
);
|
|
833
|
+
|
|
834
|
+
const described = await client.send(new DescribeLogGroupsCommand({}));
|
|
835
|
+
|
|
836
|
+
// The ARN names the Region the client was configured for.
|
|
837
|
+
console.log(described.logGroups?.[0]?.logGroupArn);
|
|
838
|
+
```
|
|
839
|
+
|
|
840
|
+
## Limitations
|
|
841
|
+
|
|
842
|
+
- **Events never expire.** Retention is stored and reported, never acted on.
|
|
843
|
+
- **Metric filters.** Absent, so `metricFilterCount` is always zero.
|
|
844
|
+
- **Subscription filter destinations other than Lambda**, and `Distribution`. `Distribution` is
|
|
845
|
+
accepted and reported, and with no shards to spread across it has no effect.
|
|
846
|
+
- **`AWS::Logs::SubscriptionFilter` and `AWS::Logs::MetricFilter`.** Both are recorded as gaps. The
|
|
847
|
+
log group and the three delivery resource types are what simulated CloudFormation deploys here.
|
|
848
|
+
- **Nothing is actually delivered.** A delivery records that a source was joined to a destination
|
|
849
|
+
and how the records would be written. No access log file ever reaches the bucket.
|
|
850
|
+
- **`GetDeliverySource`, `GetDeliveryDestination` and `GetDelivery`.** Absent. The three `Describe`
|
|
851
|
+
operations report the same resources.
|
|
852
|
+
- **Delivery resource tags and cross-account delivery.** `PutDeliverySource`,
|
|
853
|
+
`PutDeliveryDestination` and `CreateDelivery` refuse tags outright. `DeliveryDestinationPolicy` in
|
|
854
|
+
a template is recorded and acted on by nothing.
|
|
855
|
+
- **An `=` in a suffix path with Hive compatible paths off.** Taken. Whether real CloudWatch Logs
|
|
856
|
+
takes one is unverified. A path hand-rolling its own partition keys without the option is left
|
|
857
|
+
alone here, and refused with the option on.
|
|
858
|
+
- **Log types for services other than CloudFront.** Any `logType` is taken over a resource that is
|
|
859
|
+
not a distribution, because the valid set varies by service and this simulation does not carry it.
|
|
860
|
+
- **Logs Insights, export tasks, tags, encryption and data protection policies.** Absent. Tags and
|
|
861
|
+
`kmsKeyId` on `CreateLogGroup` are refused outright. A property cannot look set here and behave
|
|
862
|
+
differently in an account.
|
|
863
|
+
- **Per-stream `storedBytes`.** Always zero, matching real CloudWatch Logs, which stopped reporting
|
|
864
|
+
the figure per stream in 2019. `DescribeLogGroups` reports the bytes a group holds.
|
|
865
|
+
- **Log capture from a handler function reference.** Recorded through the process console and the
|
|
866
|
+
process standard streams, both of which a test runner is free to replace. `console.trace` and
|
|
867
|
+
`console.dir` reach the log group only where the host console passes them on to `process.stdout`.
|
|
868
|
+
See the Lambda docs for the detail.
|