Skip to content

Commit 6b1cf79

Browse files
authored
fix: prevent region override for post-deploy commands (#595)
1 parent cb26199 commit 6b1cf79

7 files changed

Lines changed: 19 additions & 8 deletions

File tree

src/cli/commands/deploy/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export async function handleDeploy(options: ValidatedDeployOptions): Promise<Dep
6868

6969
// Load targets and find the specified one
7070
startStep('Load deployment target');
71-
const targets = await configIO.readAWSDeploymentTargets();
71+
const targets = await configIO.resolveAWSDeploymentTargets();
7272
const target = targets.find(t => t.name === options.target);
7373
if (!target) {
7474
endStep('error', `Target "${options.target}" not found`);

src/cli/operations/deploy/__tests__/preflight.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ vi.mock('../../../../lib/index.js', () => ({
2929
}
3030
readProjectSpec = mockReadProjectSpec;
3131
readAWSDeploymentTargets = mockReadAWSDeploymentTargets;
32+
resolveAWSDeploymentTargets = mockReadAWSDeploymentTargets;
3233
readDeployedState = mockReadDeployedState;
3334
configExists = mockConfigExists;
3435
},

src/cli/operations/deploy/__tests__/teardown-utils.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ vi.mock('../../../../lib/index.js', () => ({
2727
ConfigIO: class {
2828
readProjectSpec = mockReadProjectSpec;
2929
readAWSDeploymentTargets = mockReadAWSDeploymentTargets;
30+
resolveAWSDeploymentTargets = mockReadAWSDeploymentTargets;
3031
readDeployedState = mockReadDeployedState;
3132
writeDeployedState = mockWriteDeployedState;
3233
},

src/cli/operations/deploy/preflight.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export async function validateProject(): Promise<PreflightContext> {
7171

7272
const configIO = new ConfigIO({ baseDir: configRoot });
7373
const projectSpec = await configIO.readProjectSpec();
74-
const awsTargets = await configIO.readAWSDeploymentTargets();
74+
const awsTargets = await configIO.resolveAWSDeploymentTargets();
7575

7676
// Validate that at least one agent or gateway is defined, unless this is a teardown deploy.
7777
//

src/cli/operations/deploy/teardown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface DiscoverDeployedResult {
2323
export async function discoverDeployedTargets(configBaseDir?: string): Promise<DiscoverDeployedResult> {
2424
const configIO = new ConfigIO(configBaseDir ? { baseDir: configBaseDir } : undefined);
2525
const projectSpec = await configIO.readProjectSpec();
26-
const targets = await configIO.readAWSDeploymentTargets();
26+
const targets = await configIO.resolveAWSDeploymentTargets();
2727

2828
const deployedTargets: DeployedTarget[] = [];
2929
for (const target of targets) {
@@ -124,7 +124,7 @@ export async function performStackTeardown(targetName: string): Promise<StackTea
124124
throw err;
125125
}
126126
}
127-
const remainingTargets = (await configIO.readAWSDeploymentTargets()).filter(t => t.name !== targetName);
127+
const remainingTargets = (await configIO.resolveAWSDeploymentTargets()).filter(t => t.name !== targetName);
128128
await configIO.writeAWSDeploymentTargets(remainingTargets);
129129

130130
return { success: true };

src/cli/tui/hooks/useAwsTargetConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export function useAwsTargetConfig(): AwsTargetConfigState {
110110
}
111111

112112
const configIO = new ConfigIO({ baseDir: configRoot });
113-
const targets = await configIO.readAWSDeploymentTargets();
113+
const targets = await configIO.resolveAWSDeploymentTargets();
114114

115115
if (targets.length > 1) {
116116
// Multiple targets - show selection

src/lib/schemas/io/config-io.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ export class ConfigIO {
108108

109109
/**
110110
* Read and validate the AWS configuration file.
111-
* Applies overrides following AWS SDK precedence:
112-
* - Account: from current credentials if AWS_PROFILE is set
113-
* - Region: AWS_REGION > AWS_DEFAULT_REGION > profile config > saved value
111+
* Region is preserved as saved. Use resolveAWSDeploymentTargets() for environment/profile overrides.
112+
* TODO: Account is still overridden via AWS_PROFILE — consider moving to resolveAWSDeploymentTargets() for consistency.
114113
*/
115114
async readAWSDeploymentTargets(): Promise<AwsDeploymentTarget[]> {
116115
const filePath = this.pathResolver.getAWSTargetsConfigPath();
@@ -124,6 +123,16 @@ export class ConfigIO {
124123
}
125124
}
126125

126+
return targets;
127+
}
128+
129+
/**
130+
* Read AWS deployment targets with region overrides from environment/profile.
131+
* Region precedence: AWS_REGION > AWS_DEFAULT_REGION > profile config > saved value.
132+
*/
133+
async resolveAWSDeploymentTargets(): Promise<AwsDeploymentTarget[]> {
134+
const targets = await this.readAWSDeploymentTargets();
135+
127136
// Override region from env vars
128137
const envRegion = process.env.AWS_REGION ?? process.env.AWS_DEFAULT_REGION;
129138
if (envRegion && AgentCoreRegionSchema.safeParse(envRegion).success) {

0 commit comments

Comments
 (0)