diff --git a/packages/core/src/shared/sam/debugger/awsSamDebugger.ts b/packages/core/src/shared/sam/debugger/awsSamDebugger.ts index d6cbe113dee..7e3eca9d53c 100644 --- a/packages/core/src/shared/sam/debugger/awsSamDebugger.ts +++ b/packages/core/src/shared/sam/debugger/awsSamDebugger.ts @@ -537,7 +537,8 @@ export class SamDebugConfigProvider implements vscode.DebugConfigurationProvider } const runtimeFamily = getFamily(runtime) - const region = this.ctx.awsContext.getCredentialDefaultRegion() + // use region in debug config first, if not found, fall back to toolkit default region. + const region = config.aws?.region ?? this.ctx.awsContext.getCredentialDefaultRegion() const documentUri = vscode.window.activeTextEditor?.document.uri ?? // XXX: don't know what URI to choose... diff --git a/packages/core/src/shared/sam/localLambdaRunner.ts b/packages/core/src/shared/sam/localLambdaRunner.ts index c65bf373be0..25946299d6c 100644 --- a/packages/core/src/shared/sam/localLambdaRunner.ts +++ b/packages/core/src/shared/sam/localLambdaRunner.ts @@ -31,6 +31,7 @@ import { ToolkitError, UnknownError } from '../errors' import { SamCliError } from './cli/samCliInvokerUtils' import fs from '../fs/fs' import { getSpawnEnv } from '../env/resolveEnv' +import { asEnvironmentVariables } from '../../auth/credentials/utils' const localize = nls.loadMessageBundle() @@ -287,10 +288,17 @@ export async function runLambdaFunction( getLogger().info(localize('AWS.output.sam.local.startRun', 'Preparing to run locally: {0}', config.handlerName)) } - const envVars = await getSpawnEnv({ - ...process.env, - ...(config.aws?.region ? { AWS_DEFAULT_REGION: config.aws.region } : {}), - }) + const envVars = await getSpawnEnv( + { + ...process.env, + ...(config.awsCredentials ? asEnvironmentVariables(config.awsCredentials) : {}), + ...(config.aws?.region ? { AWS_DEFAULT_REGION: config.aws.region } : {}), + }, + { + // only inject toolkit credential if config credential is not set + injectCredential: config.awsCredentials ? false : true, + } + ) const settings = SamCliSettings.instance const timer = new Timeout(settings.getLocalInvokeTimeout()) diff --git a/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts b/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts index 972cb45db4a..60edb29af07 100644 --- a/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts +++ b/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts @@ -2914,6 +2914,7 @@ describe('SamDebugConfigurationProvider', async function () { const tempDir = path.dirname(actual.codeRoot) const expected: SamLaunchRequestArgs = { + // The `aws.credentials` field in debug config, overrides default toolkit credentials. awsCredentials: configCredentials, ...awsSection, type: AWS_SAM_DEBUG_TYPE, @@ -2949,7 +2950,8 @@ describe('SamDebugConfigurationProvider', async function () { templatePath: pathutil.normalize(path.join(actual.baseBuildDir!, 'app___vsctk___template.yaml')), parameterOverrides: undefined, architecture: 'x86_64', - region: 'us-west-2', + // The `aws.region` field in debug config, overrides default toolkit region. + region: 'us-weast-9', // // Node-related fields diff --git a/packages/toolkit/.changes/next-release/Bug Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json b/packages/toolkit/.changes/next-release/Bug Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json new file mode 100644 index 00000000000..56031c8fead --- /dev/null +++ b/packages/toolkit/.changes/next-release/Bug Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json @@ -0,0 +1,4 @@ +{ + "type": "Bug Fix", + "description": "SAM local debugging: `aws.region` and `aws.credentials` specified in launch config should override default Toolkit region and credentials" +}