Skip to content

Commit 4d8932e

Browse files
authored
fix(appbuilder): SAM local debugging: respect aws.region, aws.credential in launch config #6011
## Problem Appbuilder added `--region` parameter to `sam local invoke`, however this region is always reading region prop from default toolkit connection. Ignoring region set in sam debug config. ## Solution region and credential set in sam debug config should override region and credential in current active toolkit connection.
1 parent c138cb2 commit 4d8932e

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

packages/core/src/shared/sam/debugger/awsSamDebugger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider
537537
}
538538

539539
const runtimeFamily = getFamily(runtime)
540-
const region = this.ctx.awsContext.getCredentialDefaultRegion()
540+
// use region in debug config first, if not found, fall back to toolkit default region.
541+
const region = config.aws?.region ?? this.ctx.awsContext.getCredentialDefaultRegion()
541542
const documentUri =
542543
vscode.window.activeTextEditor?.document.uri ??
543544
// XXX: don't know what URI to choose...

packages/core/src/shared/sam/localLambdaRunner.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { ToolkitError, UnknownError } from '../errors'
3131
import { SamCliError } from './cli/samCliInvokerUtils'
3232
import fs from '../fs/fs'
3333
import { getSpawnEnv } from '../env/resolveEnv'
34+
import { asEnvironmentVariables } from '../../auth/credentials/utils'
3435

3536
const localize = nls.loadMessageBundle()
3637

@@ -287,10 +288,17 @@ export async function runLambdaFunction(
287288
getLogger().info(localize('AWS.output.sam.local.startRun', 'Preparing to run locally: {0}', config.handlerName))
288289
}
289290

290-
const envVars = await getSpawnEnv({
291-
...process.env,
292-
...(config.aws?.region ? { AWS_DEFAULT_REGION: config.aws.region } : {}),
293-
})
291+
const envVars = await getSpawnEnv(
292+
{
293+
...process.env,
294+
...(config.awsCredentials ? asEnvironmentVariables(config.awsCredentials) : {}),
295+
...(config.aws?.region ? { AWS_DEFAULT_REGION: config.aws.region } : {}),
296+
},
297+
{
298+
// only inject toolkit credential if config credential is not set
299+
injectCredential: config.awsCredentials ? false : true,
300+
}
301+
)
294302

295303
const settings = SamCliSettings.instance
296304
const timer = new Timeout(settings.getLocalInvokeTimeout())

packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2914,6 +2914,7 @@ describe('SamDebugConfigurationProvider', async function () {
29142914
const tempDir = path.dirname(actual.codeRoot)
29152915

29162916
const expected: SamLaunchRequestArgs = {
2917+
// The `aws.credentials` field in debug config, overrides default toolkit credentials.
29172918
awsCredentials: configCredentials,
29182919
...awsSection,
29192920
type: AWS_SAM_DEBUG_TYPE,
@@ -2949,7 +2950,8 @@ describe('SamDebugConfigurationProvider', async function () {
29492950
templatePath: pathutil.normalize(path.join(actual.baseBuildDir!, 'app___vsctk___template.yaml')),
29502951
parameterOverrides: undefined,
29512952
architecture: 'x86_64',
2952-
region: 'us-west-2',
2953+
// The `aws.region` field in debug config, overrides default toolkit region.
2954+
region: 'us-weast-9',
29532955

29542956
//
29552957
// Node-related fields
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "SAM local debugging: `aws.region` and `aws.credentials` specified in launch config should override default Toolkit region and credentials"
4+
}

0 commit comments

Comments
 (0)