@anthusai/papyrus 5.130.0-next.1 → 5.130.0
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/amplify/agents/admin-policy-grant.feature +20 -0
- package/amplify/agents/admin-policy-grant.js +31 -0
- package/amplify/agents/admin-policy-grant.ts +35 -0
- package/amplify/agents/pieces.js +11 -0
- package/amplify/agents/pieces.ts +13 -0
- package/amplify/agents/schedule-group.js +75 -0
- package/amplify/agents/schedule-group.ts +82 -0
- package/amplify/agents/stack.js +27 -0
- package/amplify/agents/stack.ts +53 -0
- package/amplify/agents/wake-queue.js +121 -0
- package/amplify/agents/wake-queue.ts +138 -0
- package/amplify/data/resource.js +25 -0
- package/amplify/data/resource.ts +25 -0
- package/amplify/data/schema.js +27 -1
- package/amplify/data/schema.ts +27 -1
- package/amplify/functions/announcements/schema.js +86 -0
- package/amplify/functions/announcements/schema.ts +86 -0
- package/amplify/functions/assignment-action/handler.ts +4 -0
- package/amplify/functions/automation-view/schema.js +8 -1
- package/amplify/functions/automation-view/schema.ts +8 -1
- package/amplify/functions/automation-view/wire.js +7 -11
- package/amplify/functions/automation-view/wire.ts +7 -14
- package/amplify/functions/localization-dispatch/handler.py +4 -3
- package/amplify/functions/localization-dispatch/wire.js +6 -5
- package/amplify/functions/localization-dispatch/wire.ts +6 -5
- package/amplify/functions/model-attachment-upload/handler.ts +4 -0
- package/amplify/functions/model-attachment-upload/rules.js +2 -0
- package/amplify/functions/model-attachment-upload/rules.ts +2 -0
- package/amplify/functions/procedure-action/handler.ts +44 -27
- package/amplify/functions/procedure-action/wake-key.js +48 -0
- package/amplify/functions/procedure-action/wake-key.ts +49 -0
- package/amplify/site-backend.js +12 -2
- package/amplify/site-backend.ts +16 -2
- package/components/automation-panel.tsx +6 -0
- package/components/automation-view.tsx +6 -4
- package/components/newsroom-ops-overview.tsx +2 -0
- package/components/today-panel.tsx +43 -0
- package/components/today-view.tsx +171 -0
- package/infra/agent-sync.test.js +34 -0
- package/infra/agent-sync.test.ts +35 -0
- package/infra/build-specs.js +21 -0
- package/infra/build-specs.ts +25 -0
- package/infra/ci-gate.test.js +1 -1
- package/infra/ci-gate.test.ts +1 -1
- package/infra/procedure-sync.test.js +48 -0
- package/infra/procedure-sync.test.ts +50 -0
- package/lib/automation-fixture.ts +14 -9
- package/lib/automation-overview.ts +18 -5
- package/lib/guidelines/writing-guidelines-admin.ts +1 -1
- package/lib/images/operations.generated.ts +72 -42
- package/lib/tenant-keys.js +1 -0
- package/lib/tenant-keys.ts +1 -0
- package/lib/today-client.ts +34 -0
- package/lib/today-fixture.ts +52 -0
- package/lib/today-overview.ts +111 -0
- package/package.json +6 -1
- package/routes.manifest.json +1 -1
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Feature: A signed-in admin may save the agent policy document, and no one else may through a role
|
|
2
|
+
`papyrus agents set-rung --apply` puts the next version of the policy at `tenants/<tenant>/site/agents/<version>.json` in the site's media bucket, with the
|
|
3
|
+
signed-in person's own credentials. Amplify's storage rules take a prefix ending in `/*` and this key has a wildcard in the middle, so the grant is an
|
|
4
|
+
inline policy on the `admin` group's role, in a stack of its own so the storage and auth stacks do not depend on each other. It allows get and put on that
|
|
5
|
+
prefix and nothing else: no delete (every version stays), no list. The editor and curator groups and the plain authenticated role get nothing.
|
|
6
|
+
A shared stack does not get it, because the prefix names every tenant. Executable by scripts/test-admin-agent-policy-grant.ts.
|
|
7
|
+
|
|
8
|
+
Scenario: The admin group's role may get and put the policy versions
|
|
9
|
+
Given a dedicated site's backend
|
|
10
|
+
Then the admin group's role has one statement on "tenants/*/site/agents/*" in the media bucket
|
|
11
|
+
And the statement allows "s3:GetObject" and "s3:PutObject" and nothing else
|
|
12
|
+
|
|
13
|
+
Scenario: No other role carries the grant
|
|
14
|
+
Given a dedicated site's backend
|
|
15
|
+
Then the editor group's role does not carry the statement
|
|
16
|
+
And the storage rules do not name the agent policy prefix
|
|
17
|
+
|
|
18
|
+
Scenario: A shared stack does not carry the grant
|
|
19
|
+
Given a shared stack's backend
|
|
20
|
+
Then no role has the statement
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Effect, Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
2
|
+
/**
|
|
3
|
+
* The signed-in admin's one write on the agent policy document (PPY-51ea61; docs/release-5-130-plan.md, 1.5).
|
|
4
|
+
*
|
|
5
|
+
* `papyrus agents set-rung --apply` saves the next version of the policy as `tenants/<tenant>/site/agents/<version>.json` in the site's media bucket,
|
|
6
|
+
* through the signed-in person's own credentials (the site store only puts and gets that object). Amplify's `defineStorage` rules take a prefix that
|
|
7
|
+
* ends in `/*`, and this key has a wildcard in the middle, so a storage rule cannot say it; the grant is an inline policy on the `admin` group's role.
|
|
8
|
+
*
|
|
9
|
+
* It sits in its own stack, not on the role in the auth stack: the storage stack already depends on the group roles (its own rules attach to them), so a
|
|
10
|
+
* statement on the role that names the bucket would make the two depend on each other. Put and get on that prefix only, no delete (every version stays),
|
|
11
|
+
* no list, and the `editor` and `curator` groups and the plain authenticated role get nothing (the CLI refuses an editor, but the role is the control).
|
|
12
|
+
*/
|
|
13
|
+
export const ADMIN_AGENT_POLICY_GRANT_STACK_NAME = "admin-agent-policy-grant";
|
|
14
|
+
export const ADMIN_AGENT_POLICY_GRANT_SID = "AdminWriteAgentPolicyBody";
|
|
15
|
+
export const ADMIN_AGENT_POLICY_GROUP = "admin";
|
|
16
|
+
export const ADMIN_AGENT_POLICY_KEY_PATTERN = "tenants/*/site/agents/*";
|
|
17
|
+
export function adminAgentPolicyStatement(mediaBucketArn) {
|
|
18
|
+
return new PolicyStatement({
|
|
19
|
+
sid: ADMIN_AGENT_POLICY_GRANT_SID,
|
|
20
|
+
effect: Effect.ALLOW,
|
|
21
|
+
actions: ["s3:GetObject", "s3:PutObject"],
|
|
22
|
+
resources: [`${mediaBucketArn}/${ADMIN_AGENT_POLICY_KEY_PATTERN}`],
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/** Adds the grant to `adminRole` as a policy in `scope`'s stack. `scope` must be a stack of its own, apart from the auth and storage stacks. */
|
|
26
|
+
export function addAdminAgentPolicyGrant(scope, adminRole, mediaBucketArn) {
|
|
27
|
+
return new Policy(scope, "AdminAgentPolicyWrite", {
|
|
28
|
+
roles: [adminRole],
|
|
29
|
+
statements: [adminAgentPolicyStatement(mediaBucketArn)],
|
|
30
|
+
});
|
|
31
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Effect, Policy, PolicyStatement, type IRole } from "aws-cdk-lib/aws-iam";
|
|
2
|
+
import type { Construct } from "constructs";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The signed-in admin's one write on the agent policy document (PPY-51ea61; docs/release-5-130-plan.md, 1.5).
|
|
6
|
+
*
|
|
7
|
+
* `papyrus agents set-rung --apply` saves the next version of the policy as `tenants/<tenant>/site/agents/<version>.json` in the site's media bucket,
|
|
8
|
+
* through the signed-in person's own credentials (the site store only puts and gets that object). Amplify's `defineStorage` rules take a prefix that
|
|
9
|
+
* ends in `/*`, and this key has a wildcard in the middle, so a storage rule cannot say it; the grant is an inline policy on the `admin` group's role.
|
|
10
|
+
*
|
|
11
|
+
* It sits in its own stack, not on the role in the auth stack: the storage stack already depends on the group roles (its own rules attach to them), so a
|
|
12
|
+
* statement on the role that names the bucket would make the two depend on each other. Put and get on that prefix only, no delete (every version stays),
|
|
13
|
+
* no list, and the `editor` and `curator` groups and the plain authenticated role get nothing (the CLI refuses an editor, but the role is the control).
|
|
14
|
+
*/
|
|
15
|
+
export const ADMIN_AGENT_POLICY_GRANT_STACK_NAME = "admin-agent-policy-grant";
|
|
16
|
+
export const ADMIN_AGENT_POLICY_GRANT_SID = "AdminWriteAgentPolicyBody";
|
|
17
|
+
export const ADMIN_AGENT_POLICY_GROUP = "admin";
|
|
18
|
+
export const ADMIN_AGENT_POLICY_KEY_PATTERN = "tenants/*/site/agents/*";
|
|
19
|
+
|
|
20
|
+
export function adminAgentPolicyStatement(mediaBucketArn: string): PolicyStatement {
|
|
21
|
+
return new PolicyStatement({
|
|
22
|
+
sid: ADMIN_AGENT_POLICY_GRANT_SID,
|
|
23
|
+
effect: Effect.ALLOW,
|
|
24
|
+
actions: ["s3:GetObject", "s3:PutObject"],
|
|
25
|
+
resources: [`${mediaBucketArn}/${ADMIN_AGENT_POLICY_KEY_PATTERN}`],
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Adds the grant to `adminRole` as a policy in `scope`'s stack. `scope` must be a stack of its own, apart from the auth and storage stacks. */
|
|
30
|
+
export function addAdminAgentPolicyGrant(scope: Construct, adminRole: IRole, mediaBucketArn: string): Policy {
|
|
31
|
+
return new Policy(scope, "AdminAgentPolicyWrite", {
|
|
32
|
+
roles: [adminRole],
|
|
33
|
+
statements: [adminAgentPolicyStatement(mediaBucketArn)],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { scheduleGroupPiece } from "./schedule-group.js";
|
|
2
|
+
import { wakeQueuePiece } from "./wake-queue.js";
|
|
3
|
+
/**
|
|
4
|
+
* The agent pieces a site's `agents` stack holds. Each slice of docs/design/always-on-agents-architecture.md adds its
|
|
5
|
+
* piece here (the wake queue, the schedule group and reconciler, the change feeds, the alarms) and nothing else:
|
|
6
|
+
* `scripts/test-agents-stack.ts` prints what each costs against the first-deploy budget. The pieces are named after the
|
|
7
|
+
* site, so they are built for one site's brand id.
|
|
8
|
+
*/
|
|
9
|
+
export function agentPieces(brandId) {
|
|
10
|
+
return [wakeQueuePiece(brandId), scheduleGroupPiece(brandId)];
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AgentPiece } from "./stack";
|
|
2
|
+
import { scheduleGroupPiece } from "./schedule-group";
|
|
3
|
+
import { wakeQueuePiece } from "./wake-queue";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The agent pieces a site's `agents` stack holds. Each slice of docs/design/always-on-agents-architecture.md adds its
|
|
7
|
+
* piece here (the wake queue, the schedule group and reconciler, the change feeds, the alarms) and nothing else:
|
|
8
|
+
* `scripts/test-agents-stack.ts` prints what each costs against the first-deploy budget. The pieces are named after the
|
|
9
|
+
* site, so they are built for one site's brand id.
|
|
10
|
+
*/
|
|
11
|
+
export function agentPieces(brandId: string): readonly AgentPiece[] {
|
|
12
|
+
return [wakeQueuePiece(brandId), scheduleGroupPiece(brandId)];
|
|
13
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Duration, Stack } from "aws-cdk-lib";
|
|
2
|
+
import { Alarm, ComparisonOperator, Metric, TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import { Effect, PolicyDocument, PolicyStatement, Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
|
|
4
|
+
import { CfnScheduleGroup } from "aws-cdk-lib/aws-scheduler";
|
|
5
|
+
import { assertLiteralResourceName, sanitizeAwsName } from "../site-backend-names.js";
|
|
6
|
+
import { wakeQueueArn, wakeQueueNames } from "./wake-queue.js";
|
|
7
|
+
/**
|
|
8
|
+
* The schedule trigger's group and the role the scheduler assumes (docs/design/always-on-agents-architecture.md, slice C1).
|
|
9
|
+
*
|
|
10
|
+
* Every agent that names a schedule gets one EventBridge Scheduler entry in this group, made by the deploy step (`papyrus agents schedules`,
|
|
11
|
+
* src/papyrus_content/agent_definitions/schedules.py) and by nothing else: no entry is made by hand. An entry's only target is the site's wake queue, and a
|
|
12
|
+
* timer does nothing but put a wake on it.
|
|
13
|
+
*
|
|
14
|
+
* This piece holds three resources: the group, the role the scheduler assumes (its one permission is in the role, as an inline policy) and the alarm that
|
|
15
|
+
* says a tick was dropped. It creates no queue and no function: the reconciler is the deploy step, run by the site's build.
|
|
16
|
+
*
|
|
17
|
+
* What the role may do: send a message to this site's wake queue, and nothing else. It may not read or delete a message, touch another site's queue or
|
|
18
|
+
* the dead-letter queue, or call any other service. Only the scheduler service, acting for this account, may assume it.
|
|
19
|
+
*/
|
|
20
|
+
export const SCHEDULE_GROUP_NAME_PREFIX = "papyrus-";
|
|
21
|
+
export const SCHEDULE_GROUP_NAME_SUFFIX = "-agent-schedules";
|
|
22
|
+
export const SCHEDULER_ROLE_NAME_SUFFIX = "-agent-scheduler";
|
|
23
|
+
/** The site id in a group or role name: lowercase letters, digits and hyphens, at most 40, so each name is at most 64 characters (the group and role limit). */
|
|
24
|
+
export const SCHEDULE_SITE_ID_LENGTH = 40;
|
|
25
|
+
export function scheduleNames(brandId) {
|
|
26
|
+
const siteId = sanitizeAwsName(brandId, SCHEDULE_SITE_ID_LENGTH);
|
|
27
|
+
if (siteId === "")
|
|
28
|
+
throw new Error(`The schedule group is named after the site's brand id, which must contain a letter or digit (got ${JSON.stringify(brandId)}).`);
|
|
29
|
+
return {
|
|
30
|
+
group: assertLiteralResourceName("Schedule group name", `${SCHEDULE_GROUP_NAME_PREFIX}${siteId}${SCHEDULE_GROUP_NAME_SUFFIX}`),
|
|
31
|
+
role: assertLiteralResourceName("Scheduler role name", `${SCHEDULE_GROUP_NAME_PREFIX}${siteId}${SCHEDULER_ROLE_NAME_SUFFIX}`),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/** The group, the role the scheduler assumes and the alarm on a tick that could not be delivered. */
|
|
35
|
+
export function scheduleGroupPiece(brandId) {
|
|
36
|
+
const names = scheduleNames(brandId);
|
|
37
|
+
return {
|
|
38
|
+
id: "schedule-group",
|
|
39
|
+
add(scope) {
|
|
40
|
+
const stack = Stack.of(scope);
|
|
41
|
+
new CfnScheduleGroup(scope, "ScheduleGroup", { name: names.group });
|
|
42
|
+
new Role(scope, "SchedulerRole", {
|
|
43
|
+
roleName: names.role,
|
|
44
|
+
description: `Lets EventBridge Scheduler put a wake on ${wakeQueueNames(brandId).queue}: one permission, send to that queue.`,
|
|
45
|
+
assumedBy: new ServicePrincipal("scheduler.amazonaws.com", { conditions: { StringEquals: { "aws:SourceAccount": stack.account } } }),
|
|
46
|
+
inlinePolicies: {
|
|
47
|
+
SendWakes: new PolicyDocument({
|
|
48
|
+
statements: [
|
|
49
|
+
new PolicyStatement({
|
|
50
|
+
effect: Effect.ALLOW,
|
|
51
|
+
actions: ["sqs:SendMessage"],
|
|
52
|
+
resources: [wakeQueueArn(brandId, stack.partition, stack.region, stack.account)],
|
|
53
|
+
}),
|
|
54
|
+
],
|
|
55
|
+
}),
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
// No alarm action: the account's operator listener reads every alarm on the default bus (docs/operator-alerts.md), as for every dead-letter queue.
|
|
59
|
+
new Alarm(scope, "DroppedTickAlarm", {
|
|
60
|
+
alarmDescription: `${names.group}: the scheduler gave up on a tick, so an agent did not wake at its time. Read the agent's schedule entry and the wake queue; the next tick is not affected.`,
|
|
61
|
+
metric: new Metric({
|
|
62
|
+
namespace: "AWS/Scheduler",
|
|
63
|
+
metricName: "InvocationDroppedCount",
|
|
64
|
+
dimensionsMap: { ScheduleGroup: names.group },
|
|
65
|
+
statistic: "Sum",
|
|
66
|
+
period: Duration.minutes(5),
|
|
67
|
+
}),
|
|
68
|
+
threshold: 1,
|
|
69
|
+
evaluationPeriods: 1,
|
|
70
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
71
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Duration, Stack } from "aws-cdk-lib";
|
|
2
|
+
import { Alarm, ComparisonOperator, Metric, TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import { Effect, PolicyDocument, PolicyStatement, Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
|
|
4
|
+
import { CfnScheduleGroup } from "aws-cdk-lib/aws-scheduler";
|
|
5
|
+
import type { Construct } from "constructs";
|
|
6
|
+
import { assertLiteralResourceName, sanitizeAwsName } from "../site-backend-names";
|
|
7
|
+
import type { AgentPiece } from "./stack";
|
|
8
|
+
import { wakeQueueArn, wakeQueueNames } from "./wake-queue";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The schedule trigger's group and the role the scheduler assumes (docs/design/always-on-agents-architecture.md, slice C1).
|
|
12
|
+
*
|
|
13
|
+
* Every agent that names a schedule gets one EventBridge Scheduler entry in this group, made by the deploy step (`papyrus agents schedules`,
|
|
14
|
+
* src/papyrus_content/agent_definitions/schedules.py) and by nothing else: no entry is made by hand. An entry's only target is the site's wake queue, and a
|
|
15
|
+
* timer does nothing but put a wake on it.
|
|
16
|
+
*
|
|
17
|
+
* This piece holds three resources: the group, the role the scheduler assumes (its one permission is in the role, as an inline policy) and the alarm that
|
|
18
|
+
* says a tick was dropped. It creates no queue and no function: the reconciler is the deploy step, run by the site's build.
|
|
19
|
+
*
|
|
20
|
+
* What the role may do: send a message to this site's wake queue, and nothing else. It may not read or delete a message, touch another site's queue or
|
|
21
|
+
* the dead-letter queue, or call any other service. Only the scheduler service, acting for this account, may assume it.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export const SCHEDULE_GROUP_NAME_PREFIX = "papyrus-";
|
|
25
|
+
export const SCHEDULE_GROUP_NAME_SUFFIX = "-agent-schedules";
|
|
26
|
+
export const SCHEDULER_ROLE_NAME_SUFFIX = "-agent-scheduler";
|
|
27
|
+
/** The site id in a group or role name: lowercase letters, digits and hyphens, at most 40, so each name is at most 64 characters (the group and role limit). */
|
|
28
|
+
export const SCHEDULE_SITE_ID_LENGTH = 40;
|
|
29
|
+
|
|
30
|
+
export type ScheduleNames = { group: string; role: string };
|
|
31
|
+
|
|
32
|
+
export function scheduleNames(brandId: string): ScheduleNames {
|
|
33
|
+
const siteId = sanitizeAwsName(brandId, SCHEDULE_SITE_ID_LENGTH);
|
|
34
|
+
if (siteId === "") throw new Error(`The schedule group is named after the site's brand id, which must contain a letter or digit (got ${JSON.stringify(brandId)}).`);
|
|
35
|
+
return {
|
|
36
|
+
group: assertLiteralResourceName("Schedule group name", `${SCHEDULE_GROUP_NAME_PREFIX}${siteId}${SCHEDULE_GROUP_NAME_SUFFIX}`),
|
|
37
|
+
role: assertLiteralResourceName("Scheduler role name", `${SCHEDULE_GROUP_NAME_PREFIX}${siteId}${SCHEDULER_ROLE_NAME_SUFFIX}`),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The group, the role the scheduler assumes and the alarm on a tick that could not be delivered. */
|
|
42
|
+
export function scheduleGroupPiece(brandId: string): AgentPiece {
|
|
43
|
+
const names = scheduleNames(brandId);
|
|
44
|
+
return {
|
|
45
|
+
id: "schedule-group",
|
|
46
|
+
add(scope: Construct) {
|
|
47
|
+
const stack = Stack.of(scope);
|
|
48
|
+
new CfnScheduleGroup(scope, "ScheduleGroup", { name: names.group });
|
|
49
|
+
new Role(scope, "SchedulerRole", {
|
|
50
|
+
roleName: names.role,
|
|
51
|
+
description: `Lets EventBridge Scheduler put a wake on ${wakeQueueNames(brandId).queue}: one permission, send to that queue.`,
|
|
52
|
+
assumedBy: new ServicePrincipal("scheduler.amazonaws.com", { conditions: { StringEquals: { "aws:SourceAccount": stack.account } } }),
|
|
53
|
+
inlinePolicies: {
|
|
54
|
+
SendWakes: new PolicyDocument({
|
|
55
|
+
statements: [
|
|
56
|
+
new PolicyStatement({
|
|
57
|
+
effect: Effect.ALLOW,
|
|
58
|
+
actions: ["sqs:SendMessage"],
|
|
59
|
+
resources: [wakeQueueArn(brandId, stack.partition, stack.region, stack.account)],
|
|
60
|
+
}),
|
|
61
|
+
],
|
|
62
|
+
}),
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
// No alarm action: the account's operator listener reads every alarm on the default bus (docs/operator-alerts.md), as for every dead-letter queue.
|
|
66
|
+
new Alarm(scope, "DroppedTickAlarm", {
|
|
67
|
+
alarmDescription: `${names.group}: the scheduler gave up on a tick, so an agent did not wake at its time. Read the agent's schedule entry and the wake queue; the next tick is not affected.`,
|
|
68
|
+
metric: new Metric({
|
|
69
|
+
namespace: "AWS/Scheduler",
|
|
70
|
+
metricName: "InvocationDroppedCount",
|
|
71
|
+
dimensionsMap: { ScheduleGroup: names.group },
|
|
72
|
+
statistic: "Sum",
|
|
73
|
+
period: Duration.minutes(5),
|
|
74
|
+
}),
|
|
75
|
+
threshold: 1,
|
|
76
|
+
evaluationPeriods: 1,
|
|
77
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
78
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
79
|
+
});
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { NestedStack } from "aws-cdk-lib";
|
|
2
|
+
import { CfnResource } from "aws-cdk-lib";
|
|
3
|
+
import { Construct } from "constructs";
|
|
4
|
+
export const AGENTS_STACK_NAME = "agents";
|
|
5
|
+
/** The `agents` stack is created whenever the stage is not the bootstrap stage and at least one piece exists. */
|
|
6
|
+
export function shouldCreateAgentsStack(pieces, bootstrapStage) {
|
|
7
|
+
return !bootstrapStage && pieces.length > 0;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates the `agents` stack with `createStack` (the Amplify backend's `createStack`) and adds every piece under its
|
|
11
|
+
* own construct. Returns the stack, or undefined when the stage defers it or there is nothing to add.
|
|
12
|
+
*/
|
|
13
|
+
export function addAgentsStack(createStack, pieces, bootstrapStage) {
|
|
14
|
+
const ids = pieces.map((piece) => piece.id);
|
|
15
|
+
if (new Set(ids).size !== ids.length)
|
|
16
|
+
throw new Error(`Agent pieces must have distinct ids; got ${ids.join(", ")}`);
|
|
17
|
+
if (!shouldCreateAgentsStack(pieces, bootstrapStage))
|
|
18
|
+
return undefined;
|
|
19
|
+
const stack = createStack(AGENTS_STACK_NAME);
|
|
20
|
+
for (const piece of pieces)
|
|
21
|
+
piece.add(new Construct(stack, piece.id));
|
|
22
|
+
return stack;
|
|
23
|
+
}
|
|
24
|
+
/** The CloudFormation resources a construct adds to its stack (nested stacks count separately and are not included). */
|
|
25
|
+
export function countPieceResources(piece) {
|
|
26
|
+
return piece.node.findAll().filter((node) => node instanceof CfnResource && !(node instanceof NestedStack)).length;
|
|
27
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { NestedStack, type Stack } from "aws-cdk-lib";
|
|
2
|
+
import { CfnResource } from "aws-cdk-lib";
|
|
3
|
+
import { Construct } from "constructs";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Every always-on agent resource of a site (the wake queue and its dead-letter queue, the schedule group, the
|
|
7
|
+
* reconciler, the change feeds, their alarms) lives in ONE nested stack named `agents`.
|
|
8
|
+
*
|
|
9
|
+
* Why one stack: a brand-new site's first deploy may create at most 2,500 resources in one CloudFormation operation,
|
|
10
|
+
* nested stacks included, and the first stage is held under 2,000 (`scripts/test-bootstrap-stage.ts`). The agent
|
|
11
|
+
* resources are not models, so they cannot join an optional model group; instead the whole stack is left out of the
|
|
12
|
+
* bootstrap stage (`PAPYRUS_BOOTSTRAP_STAGE=1`) exactly as `reproject-on-deploy` is, and the second deploy adds it as an
|
|
13
|
+
* ordinary update. Existing sites are not under the first-deploy limit.
|
|
14
|
+
*
|
|
15
|
+
* Each piece is a named unit with its own construct, so `scripts/test-agents-stack.ts` can price it before it merges.
|
|
16
|
+
* The stack exists only when there is at least one piece: CloudFormation does not accept an empty stack, and a
|
|
17
|
+
* placeholder resource would cost a resource for nothing.
|
|
18
|
+
*/
|
|
19
|
+
export type AgentPiece = {
|
|
20
|
+
/** Stable construct id; also the name in the per-piece count (for example `wake-queue`). */
|
|
21
|
+
id: string;
|
|
22
|
+
/** Adds this piece's resources under `scope`, a construct of the `agents` stack. */
|
|
23
|
+
add(scope: Construct): void;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const AGENTS_STACK_NAME = "agents";
|
|
27
|
+
|
|
28
|
+
/** The `agents` stack is created whenever the stage is not the bootstrap stage and at least one piece exists. */
|
|
29
|
+
export function shouldCreateAgentsStack(pieces: readonly AgentPiece[], bootstrapStage: boolean): boolean {
|
|
30
|
+
return !bootstrapStage && pieces.length > 0;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Creates the `agents` stack with `createStack` (the Amplify backend's `createStack`) and adds every piece under its
|
|
35
|
+
* own construct. Returns the stack, or undefined when the stage defers it or there is nothing to add.
|
|
36
|
+
*/
|
|
37
|
+
export function addAgentsStack(
|
|
38
|
+
createStack: (name: string) => Stack,
|
|
39
|
+
pieces: readonly AgentPiece[],
|
|
40
|
+
bootstrapStage: boolean,
|
|
41
|
+
): Stack | undefined {
|
|
42
|
+
const ids = pieces.map((piece) => piece.id);
|
|
43
|
+
if (new Set(ids).size !== ids.length) throw new Error(`Agent pieces must have distinct ids; got ${ids.join(", ")}`);
|
|
44
|
+
if (!shouldCreateAgentsStack(pieces, bootstrapStage)) return undefined;
|
|
45
|
+
const stack = createStack(AGENTS_STACK_NAME);
|
|
46
|
+
for (const piece of pieces) piece.add(new Construct(stack, piece.id));
|
|
47
|
+
return stack;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The CloudFormation resources a construct adds to its stack (nested stacks count separately and are not included). */
|
|
51
|
+
export function countPieceResources(piece: Construct): number {
|
|
52
|
+
return piece.node.findAll().filter((node) => node instanceof CfnResource && !(node instanceof NestedStack)).length;
|
|
53
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Duration, Stack } from "aws-cdk-lib";
|
|
2
|
+
import { Alarm, ComparisonOperator, Metric, TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
4
|
+
import { CfnEventSourceMapping } from "aws-cdk-lib/aws-lambda";
|
|
5
|
+
import { Queue, QueueEncryption } from "aws-cdk-lib/aws-sqs";
|
|
6
|
+
import { assertLiteralResourceName, sanitizeAwsName } from "../site-backend-names.js";
|
|
7
|
+
/**
|
|
8
|
+
* The per-site wake queue (docs/design/always-on-agents-architecture.md, slice B1). A wake is one message; the shared procedure
|
|
9
|
+
* runner is the queue's consumer, one message at a time, and holds it for the whole run. The message group is the agent plus its serial
|
|
10
|
+
* scope and the deduplication id is the wake key (src/papyrus_content/agent_wakes.py builds both).
|
|
11
|
+
*
|
|
12
|
+
* This piece holds six resources: the queue and its dead-letter queue with one transport policy each, the alarm on the dead-letter queue,
|
|
13
|
+
* and the mapping that makes the runner the consumer. `scripts/test-agents-stack.ts` prints the count and the headroom.
|
|
14
|
+
*
|
|
15
|
+
* What the piece does not create is any permission. The mapping reads the queue with the runner's own role, which a statement in
|
|
16
|
+
* infra/procedures/papyrus-procedure-runner.yaml allows for every site's queue by name pattern (the pattern is `WAKE_QUEUE_NAME_PATTERN`).
|
|
17
|
+
*/
|
|
18
|
+
/** The shared procedure runner Lambda (infra/procedures/papyrus-procedure-runner.yaml). Every site's queue maps to it. */
|
|
19
|
+
export const PROCEDURE_RUNNER_FUNCTION_NAME = "papyrus-procedure-runner";
|
|
20
|
+
/**
|
|
21
|
+
* The runner's real limits, read by the queue so the queue never invents a time of its own. `RUNNER_TIMEOUT_SECONDS` is the template's
|
|
22
|
+
* `Timeout` and `RUNNER_RECORDING_MARGIN_SECONDS` is `SECONDS_KEPT_FOR_RECORDING` in procedure_runner.py: the runner keeps that long at the
|
|
23
|
+
* end of its 15 minutes to record the outcome. `scripts/test-agents-stack.ts` fails if either file disagrees with these two numbers.
|
|
24
|
+
*/
|
|
25
|
+
export const RUNNER_TIMEOUT_SECONDS = 900;
|
|
26
|
+
export const RUNNER_RECORDING_MARGIN_SECONDS = 60;
|
|
27
|
+
/**
|
|
28
|
+
* A message the runner is working on stays hidden for the run's whole possible length (the Lambda's 15 minutes, which already contains the
|
|
29
|
+
* recording margin) plus the margin once more, so a live run is never delivered to a second runner. If the runner dies, the message comes
|
|
30
|
+
* back this long after it was taken. Lambda also refuses a mapping whose queue hides messages for less than the function's timeout.
|
|
31
|
+
*/
|
|
32
|
+
export const WAKE_VISIBILITY_TIMEOUT_SECONDS = RUNNER_TIMEOUT_SECONDS + RUNNER_RECORDING_MARGIN_SECONDS;
|
|
33
|
+
/**
|
|
34
|
+
* A wake is delivered at most twice: once to run, once to settle a run that died (slice A3). A third delivery can only mean that settling
|
|
35
|
+
* itself failed, so the third receive moves the message to the dead-letter queue, whose alarm tells a person.
|
|
36
|
+
*/
|
|
37
|
+
export const WAKE_MAXIMUM_RECEIVES = 2;
|
|
38
|
+
/** The longest SQS keeps a message (14 days). A wake that waits is never dropped sooner. */
|
|
39
|
+
export const WAKE_RETENTION = Duration.days(14);
|
|
40
|
+
/** The site id in a queue name: lowercase letters, digits and hyphens, at most 40 (80 characters is the FIFO queue name limit). */
|
|
41
|
+
export const WAKE_QUEUE_SITE_ID_LENGTH = 40;
|
|
42
|
+
export const WAKE_QUEUE_NAME_PREFIX = "papyrus-";
|
|
43
|
+
export const WAKE_QUEUE_NAME_SUFFIX = "-agent-wakes.fifo";
|
|
44
|
+
export const WAKE_DEAD_LETTER_QUEUE_NAME_SUFFIX = "-agent-wakes-dead-letters.fifo";
|
|
45
|
+
/** The runner's role reads queues that match this, in its own account and region: one statement for every site. */
|
|
46
|
+
export const WAKE_QUEUE_NAME_PATTERN = `${WAKE_QUEUE_NAME_PREFIX}*${WAKE_QUEUE_NAME_SUFFIX}`;
|
|
47
|
+
export function wakeQueueNames(brandId) {
|
|
48
|
+
const siteId = sanitizeAwsName(brandId, WAKE_QUEUE_SITE_ID_LENGTH);
|
|
49
|
+
if (siteId === "")
|
|
50
|
+
throw new Error(`The wake queue is named after the site's brand id, which must contain a letter or digit (got ${JSON.stringify(brandId)}).`);
|
|
51
|
+
return {
|
|
52
|
+
queue: assertLiteralResourceName("Wake queue name", `${WAKE_QUEUE_NAME_PREFIX}${siteId}${WAKE_QUEUE_NAME_SUFFIX}`),
|
|
53
|
+
deadLetterQueue: assertLiteralResourceName("Wake dead-letter queue name", `${WAKE_QUEUE_NAME_PREFIX}${siteId}${WAKE_DEAD_LETTER_QUEUE_NAME_SUFFIX}`),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function wakeQueueArn(brandId, partition, region, account) {
|
|
57
|
+
return `arn:${partition}:sqs:${region}:${account}:${wakeQueueNames(brandId).queue}`;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Lets a site function start a procedure: it may look up and send to this site's wake queue, and nothing else. It may not read or delete
|
|
61
|
+
* a message, and it may not touch another site's queue or any dead-letter queue. The queue is named by the site's brand id, so no
|
|
62
|
+
* reference to the `agents` stack is made and the function's stack does not depend on it.
|
|
63
|
+
*/
|
|
64
|
+
export function allowWakeSending(lambda, brandId) {
|
|
65
|
+
const stack = Stack.of(lambda);
|
|
66
|
+
lambda.addToRolePolicy(new PolicyStatement({
|
|
67
|
+
effect: Effect.ALLOW,
|
|
68
|
+
actions: ["sqs:GetQueueUrl", "sqs:SendMessage"],
|
|
69
|
+
resources: [wakeQueueArn(brandId, stack.partition, stack.region, stack.account)],
|
|
70
|
+
}));
|
|
71
|
+
}
|
|
72
|
+
/** The wake queue, its dead-letter queue, the alarm on the dead-letter queue and the mapping that makes the runner the consumer. */
|
|
73
|
+
export function wakeQueuePiece(brandId) {
|
|
74
|
+
const names = wakeQueueNames(brandId);
|
|
75
|
+
return {
|
|
76
|
+
id: "wake-queue",
|
|
77
|
+
add(scope) {
|
|
78
|
+
const deadLetterQueue = new Queue(scope, "WakeDeadLetterQueue", {
|
|
79
|
+
queueName: names.deadLetterQueue,
|
|
80
|
+
fifo: true,
|
|
81
|
+
retentionPeriod: WAKE_RETENTION,
|
|
82
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
83
|
+
enforceSSL: true,
|
|
84
|
+
});
|
|
85
|
+
const queue = new Queue(scope, "WakeQueue", {
|
|
86
|
+
queueName: names.queue,
|
|
87
|
+
fifo: true,
|
|
88
|
+
// A sender that can name the deduplication id (the wake key's digest) does, and that id wins. EventBridge Scheduler cannot name one, so a schedule's wake is
|
|
89
|
+
// deduplicated by its body, which holds the scheduled time: a tick delivered twice is one message, and the run record's own condition still makes it one run.
|
|
90
|
+
contentBasedDeduplication: true,
|
|
91
|
+
visibilityTimeout: Duration.seconds(WAKE_VISIBILITY_TIMEOUT_SECONDS),
|
|
92
|
+
retentionPeriod: WAKE_RETENTION,
|
|
93
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
94
|
+
enforceSSL: true,
|
|
95
|
+
deadLetterQueue: { queue: deadLetterQueue, maxReceiveCount: WAKE_MAXIMUM_RECEIVES },
|
|
96
|
+
});
|
|
97
|
+
// One message at a time: the runner holds it for the whole run. No maximum concurrency is set here; that is the model provider's rate limit (slice B4).
|
|
98
|
+
new CfnEventSourceMapping(scope, "RunnerMapping", {
|
|
99
|
+
functionName: PROCEDURE_RUNNER_FUNCTION_NAME,
|
|
100
|
+
eventSourceArn: queue.queueArn,
|
|
101
|
+
batchSize: 1,
|
|
102
|
+
enabled: true,
|
|
103
|
+
});
|
|
104
|
+
// No alarm action: the account's operator listener reads every alarm on the default bus (docs/operator-alerts.md), as for every dead-letter queue.
|
|
105
|
+
new Alarm(scope, "WakeDeadLetterAlarm", {
|
|
106
|
+
alarmDescription: `${names.queue}: a wake the runner could not run after its second delivery is waiting in the dead-letter queue. Nothing was lost; read the message and the run record before it is moved back.`,
|
|
107
|
+
metric: new Metric({
|
|
108
|
+
namespace: "AWS/SQS",
|
|
109
|
+
metricName: "ApproximateNumberOfMessagesVisible",
|
|
110
|
+
dimensionsMap: { QueueName: deadLetterQueue.queueName },
|
|
111
|
+
statistic: "Maximum",
|
|
112
|
+
period: Duration.minutes(5),
|
|
113
|
+
}),
|
|
114
|
+
threshold: 1,
|
|
115
|
+
evaluationPeriods: 1,
|
|
116
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
117
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
118
|
+
});
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { Duration, Stack } from "aws-cdk-lib";
|
|
2
|
+
import { Alarm, ComparisonOperator, Metric, TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import { Effect, PolicyStatement } from "aws-cdk-lib/aws-iam";
|
|
4
|
+
import { CfnEventSourceMapping, type Function as LambdaFunction } from "aws-cdk-lib/aws-lambda";
|
|
5
|
+
import { Queue, QueueEncryption } from "aws-cdk-lib/aws-sqs";
|
|
6
|
+
import type { Construct } from "constructs";
|
|
7
|
+
import { assertLiteralResourceName, sanitizeAwsName } from "../site-backend-names";
|
|
8
|
+
import type { AgentPiece } from "./stack";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The per-site wake queue (docs/design/always-on-agents-architecture.md, slice B1). A wake is one message; the shared procedure
|
|
12
|
+
* runner is the queue's consumer, one message at a time, and holds it for the whole run. The message group is the agent plus its serial
|
|
13
|
+
* scope and the deduplication id is the wake key (src/papyrus_content/agent_wakes.py builds both).
|
|
14
|
+
*
|
|
15
|
+
* This piece holds six resources: the queue and its dead-letter queue with one transport policy each, the alarm on the dead-letter queue,
|
|
16
|
+
* and the mapping that makes the runner the consumer. `scripts/test-agents-stack.ts` prints the count and the headroom.
|
|
17
|
+
*
|
|
18
|
+
* What the piece does not create is any permission. The mapping reads the queue with the runner's own role, which a statement in
|
|
19
|
+
* infra/procedures/papyrus-procedure-runner.yaml allows for every site's queue by name pattern (the pattern is `WAKE_QUEUE_NAME_PATTERN`).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** The shared procedure runner Lambda (infra/procedures/papyrus-procedure-runner.yaml). Every site's queue maps to it. */
|
|
23
|
+
export const PROCEDURE_RUNNER_FUNCTION_NAME = "papyrus-procedure-runner";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The runner's real limits, read by the queue so the queue never invents a time of its own. `RUNNER_TIMEOUT_SECONDS` is the template's
|
|
27
|
+
* `Timeout` and `RUNNER_RECORDING_MARGIN_SECONDS` is `SECONDS_KEPT_FOR_RECORDING` in procedure_runner.py: the runner keeps that long at the
|
|
28
|
+
* end of its 15 minutes to record the outcome. `scripts/test-agents-stack.ts` fails if either file disagrees with these two numbers.
|
|
29
|
+
*/
|
|
30
|
+
export const RUNNER_TIMEOUT_SECONDS = 900;
|
|
31
|
+
export const RUNNER_RECORDING_MARGIN_SECONDS = 60;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A message the runner is working on stays hidden for the run's whole possible length (the Lambda's 15 minutes, which already contains the
|
|
35
|
+
* recording margin) plus the margin once more, so a live run is never delivered to a second runner. If the runner dies, the message comes
|
|
36
|
+
* back this long after it was taken. Lambda also refuses a mapping whose queue hides messages for less than the function's timeout.
|
|
37
|
+
*/
|
|
38
|
+
export const WAKE_VISIBILITY_TIMEOUT_SECONDS = RUNNER_TIMEOUT_SECONDS + RUNNER_RECORDING_MARGIN_SECONDS;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* A wake is delivered at most twice: once to run, once to settle a run that died (slice A3). A third delivery can only mean that settling
|
|
42
|
+
* itself failed, so the third receive moves the message to the dead-letter queue, whose alarm tells a person.
|
|
43
|
+
*/
|
|
44
|
+
export const WAKE_MAXIMUM_RECEIVES = 2;
|
|
45
|
+
|
|
46
|
+
/** The longest SQS keeps a message (14 days). A wake that waits is never dropped sooner. */
|
|
47
|
+
export const WAKE_RETENTION = Duration.days(14);
|
|
48
|
+
|
|
49
|
+
/** The site id in a queue name: lowercase letters, digits and hyphens, at most 40 (80 characters is the FIFO queue name limit). */
|
|
50
|
+
export const WAKE_QUEUE_SITE_ID_LENGTH = 40;
|
|
51
|
+
|
|
52
|
+
export const WAKE_QUEUE_NAME_PREFIX = "papyrus-";
|
|
53
|
+
export const WAKE_QUEUE_NAME_SUFFIX = "-agent-wakes.fifo";
|
|
54
|
+
export const WAKE_DEAD_LETTER_QUEUE_NAME_SUFFIX = "-agent-wakes-dead-letters.fifo";
|
|
55
|
+
/** The runner's role reads queues that match this, in its own account and region: one statement for every site. */
|
|
56
|
+
export const WAKE_QUEUE_NAME_PATTERN = `${WAKE_QUEUE_NAME_PREFIX}*${WAKE_QUEUE_NAME_SUFFIX}`;
|
|
57
|
+
|
|
58
|
+
export type WakeQueueNames = { queue: string; deadLetterQueue: string };
|
|
59
|
+
|
|
60
|
+
export function wakeQueueNames(brandId: string): WakeQueueNames {
|
|
61
|
+
const siteId = sanitizeAwsName(brandId, WAKE_QUEUE_SITE_ID_LENGTH);
|
|
62
|
+
if (siteId === "") throw new Error(`The wake queue is named after the site's brand id, which must contain a letter or digit (got ${JSON.stringify(brandId)}).`);
|
|
63
|
+
return {
|
|
64
|
+
queue: assertLiteralResourceName("Wake queue name", `${WAKE_QUEUE_NAME_PREFIX}${siteId}${WAKE_QUEUE_NAME_SUFFIX}`),
|
|
65
|
+
deadLetterQueue: assertLiteralResourceName("Wake dead-letter queue name", `${WAKE_QUEUE_NAME_PREFIX}${siteId}${WAKE_DEAD_LETTER_QUEUE_NAME_SUFFIX}`),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function wakeQueueArn(brandId: string, partition: string, region: string, account: string): string {
|
|
70
|
+
return `arn:${partition}:sqs:${region}:${account}:${wakeQueueNames(brandId).queue}`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Lets a site function start a procedure: it may look up and send to this site's wake queue, and nothing else. It may not read or delete
|
|
75
|
+
* a message, and it may not touch another site's queue or any dead-letter queue. The queue is named by the site's brand id, so no
|
|
76
|
+
* reference to the `agents` stack is made and the function's stack does not depend on it.
|
|
77
|
+
*/
|
|
78
|
+
export function allowWakeSending(lambda: LambdaFunction, brandId: string): void {
|
|
79
|
+
const stack = Stack.of(lambda);
|
|
80
|
+
lambda.addToRolePolicy(
|
|
81
|
+
new PolicyStatement({
|
|
82
|
+
effect: Effect.ALLOW,
|
|
83
|
+
actions: ["sqs:GetQueueUrl", "sqs:SendMessage"],
|
|
84
|
+
resources: [wakeQueueArn(brandId, stack.partition, stack.region, stack.account)],
|
|
85
|
+
}),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The wake queue, its dead-letter queue, the alarm on the dead-letter queue and the mapping that makes the runner the consumer. */
|
|
90
|
+
export function wakeQueuePiece(brandId: string): AgentPiece {
|
|
91
|
+
const names = wakeQueueNames(brandId);
|
|
92
|
+
return {
|
|
93
|
+
id: "wake-queue",
|
|
94
|
+
add(scope: Construct) {
|
|
95
|
+
const deadLetterQueue = new Queue(scope, "WakeDeadLetterQueue", {
|
|
96
|
+
queueName: names.deadLetterQueue,
|
|
97
|
+
fifo: true,
|
|
98
|
+
retentionPeriod: WAKE_RETENTION,
|
|
99
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
100
|
+
enforceSSL: true,
|
|
101
|
+
});
|
|
102
|
+
const queue = new Queue(scope, "WakeQueue", {
|
|
103
|
+
queueName: names.queue,
|
|
104
|
+
fifo: true,
|
|
105
|
+
// A sender that can name the deduplication id (the wake key's digest) does, and that id wins. EventBridge Scheduler cannot name one, so a schedule's wake is
|
|
106
|
+
// deduplicated by its body, which holds the scheduled time: a tick delivered twice is one message, and the run record's own condition still makes it one run.
|
|
107
|
+
contentBasedDeduplication: true,
|
|
108
|
+
visibilityTimeout: Duration.seconds(WAKE_VISIBILITY_TIMEOUT_SECONDS),
|
|
109
|
+
retentionPeriod: WAKE_RETENTION,
|
|
110
|
+
encryption: QueueEncryption.SQS_MANAGED,
|
|
111
|
+
enforceSSL: true,
|
|
112
|
+
deadLetterQueue: { queue: deadLetterQueue, maxReceiveCount: WAKE_MAXIMUM_RECEIVES },
|
|
113
|
+
});
|
|
114
|
+
// One message at a time: the runner holds it for the whole run. No maximum concurrency is set here; that is the model provider's rate limit (slice B4).
|
|
115
|
+
new CfnEventSourceMapping(scope, "RunnerMapping", {
|
|
116
|
+
functionName: PROCEDURE_RUNNER_FUNCTION_NAME,
|
|
117
|
+
eventSourceArn: queue.queueArn,
|
|
118
|
+
batchSize: 1,
|
|
119
|
+
enabled: true,
|
|
120
|
+
});
|
|
121
|
+
// No alarm action: the account's operator listener reads every alarm on the default bus (docs/operator-alerts.md), as for every dead-letter queue.
|
|
122
|
+
new Alarm(scope, "WakeDeadLetterAlarm", {
|
|
123
|
+
alarmDescription: `${names.queue}: a wake the runner could not run after its second delivery is waiting in the dead-letter queue. Nothing was lost; read the message and the run record before it is moved back.`,
|
|
124
|
+
metric: new Metric({
|
|
125
|
+
namespace: "AWS/SQS",
|
|
126
|
+
metricName: "ApproximateNumberOfMessagesVisible",
|
|
127
|
+
dimensionsMap: { QueueName: deadLetterQueue.queueName },
|
|
128
|
+
statistic: "Maximum",
|
|
129
|
+
period: Duration.minutes(5),
|
|
130
|
+
}),
|
|
131
|
+
threshold: 1,
|
|
132
|
+
evaluationPeriods: 1,
|
|
133
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
134
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
135
|
+
});
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
}
|