From 43ede3a800f112c30edea74b9d7852405d2ccc52 Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Wed, 13 Nov 2024 14:42:41 -0800 Subject: [PATCH 1/4] region in debug config should override default toolkit region --- .../src/shared/sam/debugger/awsSamDebugger.ts | 3 ++- .../core/src/shared/sam/localLambdaRunner.ts | 16 ++++++++++++---- .../sam/debugger/samDebugConfigProvider.test.ts | 4 +++- ...Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json | 4 ++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 packages/toolkit/.changes/next-release/Bug Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json 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..39dc4542f10 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 credential in debug config will override default toolkit credential 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 region in debug config should override 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..c72578fd7b8 --- /dev/null +++ b/packages/toolkit/.changes/next-release/Bug Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json @@ -0,0 +1,4 @@ +{ + "type": "Bug Fix", + "description": "region in debug config should override default toolkit region" +} From 37a8926dea6a8836c5c4e66f4e85476b0caeccbd Mon Sep 17 00:00:00 2001 From: Roger Zhang Date: Wed, 13 Nov 2024 15:34:34 -0800 Subject: [PATCH 2/4] update changelog --- .../Bug Fix-e5a76bd7-06a7-4a59-aead-8ea3e3924ee9.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index c72578fd7b8..56031c8fead 100644 --- 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 @@ -1,4 +1,4 @@ { "type": "Bug Fix", - "description": "region in debug config should override default toolkit region" + "description": "SAM local debugging: `aws.region` and `aws.credentials` specified in launch config should override default Toolkit region and credentials" } From 23de9c4c147400b4f58a6af189faaa0d86c1a1e1 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 14 Nov 2024 08:59:12 -0800 Subject: [PATCH 3/4] Update packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts --- .../src/test/shared/sam/debugger/samDebugConfigProvider.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 39dc4542f10..00961b2b4ec 100644 --- a/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts +++ b/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts @@ -2914,7 +2914,7 @@ describe('SamDebugConfigurationProvider', async function () { const tempDir = path.dirname(actual.codeRoot) const expected: SamLaunchRequestArgs = { - // the credential in debug config will override default toolkit credential + // The `aws.credentials` field in debug config, overrides default toolkit credentials. awsCredentials: configCredentials, ...awsSection, type: AWS_SAM_DEBUG_TYPE, From bb76a626f0e3a0f516199e85f32a9f7c96cd9020 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 14 Nov 2024 08:59:37 -0800 Subject: [PATCH 4/4] Update packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts --- .../src/test/shared/sam/debugger/samDebugConfigProvider.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 00961b2b4ec..60edb29af07 100644 --- a/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts +++ b/packages/core/src/test/shared/sam/debugger/samDebugConfigProvider.test.ts @@ -2950,7 +2950,7 @@ describe('SamDebugConfigurationProvider', async function () { templatePath: pathutil.normalize(path.join(actual.baseBuildDir!, 'app___vsctk___template.yaml')), parameterOverrides: undefined, architecture: 'x86_64', - // the region in debug config should override default toolkit region + // The `aws.region` field in debug config, overrides default toolkit region. region: 'us-weast-9', //