Skip to content

Commit

Permalink
fix(appbuilder): SAM local debugging: respect aws.region, aws.credent…
Browse files Browse the repository at this point in the history
…ial 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.
  • Loading branch information
roger-zhangg authored Nov 14, 2024
1 parent c138cb2 commit 4d8932e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/shared/sam/debugger/awsSamDebugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
16 changes: 12 additions & 4 deletions packages/core/src/shared/sam/localLambdaRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 4d8932e

Please sign in to comment.