2

I want to run tests against my AWS environment using a run configuration in vscode.

The tests need access to the AWS account to retrieve information like the name of a bucket, a user pool id ..

I have my credentials configured in ~/.aws/credentials under [dev-user] like:

[dev-user]
aws_access_key_id=*************
aws_secret_access_key=***************************************

I can run this from the command line with export AWS_PROFILE=dev-user and then npm run test.

How do I create a launch config in vscode that does the same? Currently it is not working. I've tried the following: adding env variables or a prelaunch task. But neither of them work:

Env vars: launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "name": "vscode-jest-tests",
      "request": "launch",
      "args": ["--runInBand"],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "internalConsoleOptions": "openOnSessionStart",
      "disableOptimisticBPs": true,
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "env": { "AWS_PROFILE": "dev-user"}
    }
  ]
}

Pre launch task: launch.json

{
  "version": "2.0.0",
  "configurations": [
    {
      "type": "node",
      "name": "vscode-jest-tests",
      "request": "launch",
      "args": ["--runInBand"],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "preLaunchTask": "setProfile",
      "internalConsoleOptions": "openOnSessionStart",
      "disableOptimisticBPs": true,
      "program": "${workspaceFolder}/node_modules/jest/bin/jest"
    }
  ]
}

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "setProfile",
      "command": "export AWS_PROFILE=dev-user",
      "args": ["test"],
      "type": "shell"
    }
  ]
}

None of the above gives me access to the AWS account from my tests. How do I need to configure launch.json and / or tasks.json to get access to the AWS account in my tests?

xtra
  • 1,957
  • 4
  • 22
  • 40
  • Have you ever thought about using the AWS SDK for JS: https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-started-nodejs.html? – Marko E Dec 21 '20 at 13:43
  • Yes, I am using the SDK. But how can I pass a profile to my script so that it uses the right credentials? – xtra Dec 21 '20 at 18:54
  • I am not very proficient in NodeJS, but shouldn't it be something like this: `process.env.AWS_PROFILE`? Could that work? – Marko E Dec 22 '20 at 16:32

2 Answers2

2

It turns out that adding "env": { "AWS_PROFILE": "dev-user"} to your configuration in launch.json does work.

So this is correct:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "name": "vscode-jest-tests",
      "request": "launch",
      "args": ["--runInBand"],
      "cwd": "${workspaceFolder}",
      "console": "internalConsole",
      "internalConsoleOptions": "openOnSessionStart",
      "disableOptimisticBPs": true,
      "program": "${workspaceFolder}/node_modules/jest/bin/jest",
      "env": { "AWS_PROFILE": "dev-user"}
    }
  ]
}
xtra
  • 1,957
  • 4
  • 22
  • 40
0

I don't know if it will help anyone, but for me I had a key and password that should be sent in the request, I downloaded aws plugin and it didn't work, searching a lot I managed to solve it, you will have to post vscode and edit the launch file, the vscode has "env" property for variables .... I did it like this: .... env": { "AWS_DEFAULT_REGION":"xxxxxx", "AWS_ACCESS_KEY_ID":"xxxxxxxxxxxx", "AWS_SECRET_ACCESS_KEY":"xxxxxxxxxxxxxxxxxx" } ...

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 09 '22 at 18:27