Skip to content

Commit d7fb70a

Browse files
committed
feat(import): auto-create deployment target from ARN when none exist
When no deployment targets are configured, import runtime/memory now parses the --arn to extract region and account, then creates a default target automatically instead of requiring `agentcore deploy` first.
1 parent aa70560 commit d7fb70a

3 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/cli/commands/import/import-memory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export async function handleImportMemory(options: ImportResourceOptions): Promis
9292
const target = await resolveImportTarget({
9393
configIO: ctx.configIO,
9494
targetName: options.target,
95+
arn: options.arn,
9596
onProgress,
9697
});
9798
logger.endStep('success');

src/cli/commands/import/import-runtime.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ function toAgentEnvSpec(
4848
entrypoint: string
4949
): AgentEnvSpec {
5050
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */
51+
const runtimeVersion =
52+
runtime.build === 'Container' ? runtime.runtimeVersion : (runtime.runtimeVersion ?? 'PYTHON_3_12');
5153
const spec: AgentEnvSpec = {
5254
name: localName,
5355
build: runtime.build,
5456
entrypoint: entrypoint as any,
5557
codeLocation: codeLocation as any,
56-
runtimeVersion: (runtime.runtimeVersion ?? 'PYTHON_3_12') as any,
58+
runtimeVersion: runtimeVersion as any,
5759
protocol: runtime.protocol as any,
5860
networkMode: runtime.networkMode as any,
5961
instrumentation: { enableOtel: true },
@@ -114,6 +116,7 @@ export async function handleImportRuntime(options: ImportResourceOptions): Promi
114116
const target = await resolveImportTarget({
115117
configIO: ctx.configIO,
116118
targetName: options.target,
119+
arn: options.arn,
117120
onProgress,
118121
});
119122
logger.endStep('success');

src/cli/commands/import/import-utils.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export async function resolveProjectContext(): Promise<ProjectContext> {
4848
export interface ResolveTargetOptions {
4949
configIO: ConfigIO;
5050
targetName?: string;
51+
arn?: string;
5152
logger?: ExecLogger;
5253
onProgress?: (message: string) => void;
5354
}
@@ -57,14 +58,35 @@ export interface ResolveTargetOptions {
5758
* Validates AWS credentials.
5859
*/
5960
export async function resolveImportTarget(options: ResolveTargetOptions): Promise<AwsDeploymentTarget> {
60-
const { configIO, targetName, onProgress } = options;
61+
const { configIO, targetName, arn, onProgress } = options;
6162

62-
const targets = await configIO.readAWSDeploymentTargets();
63+
let targets = await configIO.readAWSDeploymentTargets();
6364

6465
if (targets.length === 0) {
65-
throw new Error(
66-
'No deployment targets found in project.\nRun `agentcore deploy` first to set up a target, then re-run import.'
67-
);
66+
if (!arn) {
67+
throw new Error(
68+
'No deployment targets found in project.\nRun `agentcore deploy` first to set up a target, or use --arn so a target can be created automatically.'
69+
);
70+
}
71+
72+
const arnMatch = /^arn:aws:bedrock-agentcore:([^:]+):([^:]+):/.exec(arn);
73+
if (!arnMatch) {
74+
throw new Error(
75+
'No deployment targets found in project and could not parse region/account from ARN.\nRun `agentcore deploy` first to set up a target, then re-run import.'
76+
);
77+
}
78+
79+
const [, arnRegion, arnAccount] = arnMatch;
80+
const newTarget: AwsDeploymentTarget = {
81+
name: 'default',
82+
description: `Default target (${arnRegion})`,
83+
account: arnAccount!,
84+
region: arnRegion! as AwsDeploymentTarget['region'],
85+
};
86+
87+
onProgress?.(`No deployment targets found. Creating default target from ARN (${arnRegion}, ${arnAccount})...`);
88+
await configIO.writeAWSDeploymentTargets([newTarget]);
89+
targets = [newTarget];
6890
}
6991

7092
let target: AwsDeploymentTarget | undefined;

0 commit comments

Comments
 (0)