Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,287 changes: 1,219 additions & 68 deletions bun.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"typescript": "^5"
},
"dependencies": {
"@aws-cdk/toolkit-lib": "^1.38.2",
"@aws-sdk/client-bedrock-agentcore": "^3.1092.0",
"@aws-sdk/client-bedrock-agentcore-control": "^3.1102.0",
"@aws-sdk/client-cloudwatch-logs": "^3.1092.0",
Expand Down
62 changes: 62 additions & 0 deletions src/core/project/assembly.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Reads the cloud assembly `build` synthesized, so deploy can find the stack that
// belongs to a deployment target.
import { existsSync } from "node:fs";
import { join } from "node:path";
import z from "zod";
import { ProjectStateError } from "../../errors/errors";
import type { ReadWriteJson } from "../../io";

// The tag the generated CDK app puts on every stack, naming the deployment target
// the stack was synthesized for. Selecting on it means the CLI never has to
// reproduce the app's stack-naming convention, so a project that renames its
// stacks still deploys.
const TARGET_TAG = "agentcore:target-name";

const STACK_ARTIFACT = "aws:cloudformation:stack";

// Only the parts of the manifest deploy reads. Artifacts are keyed by their
// hierarchical id, which is what the CDK toolkit matches stack patterns against.
const AssemblyManifestSchema = z.object({
artifacts: z
.record(
z.string(),
z.object({
type: z.string(),
properties: z.object({ tags: z.record(z.string(), z.string()).optional() }).optional(),
}),
)
.default({}),
});

/**
* The name of the stack in the synthesized assembly that belongs to `target`.
*
* The generated CDK app synthesizes one stack per deployment target and tags each
* with the target's name, so deploy asks the assembly which stack to ship rather
* than deriving the name itself and hoping the two agree.
*/
export async function stackForTarget(
json: ReadWriteJson,
assemblyDirectory: string,
target: string,
): Promise<string> {
const path = join(assemblyDirectory, "manifest.json");
// deploy synthesizes immediately before this, so a missing manifest means synth
// wrote somewhere else entirely rather than that the user skipped a step.
if (!existsSync(path)) {
throw new ProjectStateError(`No synthesized cloud assembly was found at ${path}.`);
}

const manifest = await json.read(path, AssemblyManifestSchema);
const stacks = Object.entries(manifest.artifacts).filter(
([, artifact]) => artifact.type === STACK_ARTIFACT,
);
const match = stacks.find(([, artifact]) => artifact.properties?.tags?.[TARGET_TAG] === target);
if (!match) {
throw new ProjectStateError(
`The synthesized cloud assembly has no stack for deployment target '${target}'. ` +
`${path} defines ${stacks.length} stack(s), none tagged ${TARGET_TAG}='${target}'.`,
);
}
return match[0];
}
Loading