0

I'm trying to figure out how to use AWS toolkit for vscode. I go to the AWS extension and click Create New SAM Application, point to project directory and it creates a hello world function. Above it, it says Add Debug Configuration. I click that, choose nodejs 12.x and save the launch.json, but I don't get the run option. It still says Add Debug Configuration for some reason. How can I run my lambda functions locally in the console?
The launch.json file generates, but I can never run the code.

enter image description here

launch.json

{
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "new test:app.lambdaHandler (nodejs12.x)",
            "invokeTarget": {
                "target": "code",
                "projectRoot": "new test/hello-world",
                "lambdaHandler": "app.lambdaHandler"
            },
            "lambda": {
                "runtime": "nodejs12.x",
                "payload": {},
                "environmentVariables": {}
            }
        }
    ]
}

I also tried navigating to the hello-world directory in terminal and executing node app.js, but it doesn't return anything

enter image description here

What am I doing wrong? I appreciate the help!

user6680
  • 79
  • 6
  • 34
  • 78

1 Answers1

0

Make sure you have SAM CLI install in local, here are the instructions for installation https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html

Then run the command sam local start-api. You should be able to access the api at http://127.0.0.1:3000/hello

You can also do the same via vscode by selecting Run > Run without debugging (shortcut: ctrl + F5)

Tummala Dhanvi
  • 3,007
  • 2
  • 19
  • 35
  • I already have docker and sam cli installed. I was able to run docker and sam --version in cmd with no issues. I also have a crendential profile created if that's relevant to IAM user with admin permission. When I added the credentials, this appeared: https://postimg.cc/kVR4whkS The profile credentials I would think allow the access. Maybe this is my problem? – user6680 Aug 31 '20 at 13:57
  • have you tried running `sam local start-api` from cmd? are you facing any issue there? – Tummala Dhanvi Aug 31 '20 at 14:07
  • I get with that cmd: ```Error: Template file not found at C:\Users\Phil\Documents\AWS LAMBDA TESTS\new test\template.yml``` – user6680 Aug 31 '20 at 14:11
  • Also I read somewhere in a forum that I have to execute cmd inside template.yaml folder. I did that and I got the following: https://pastebin.com/ptATULGJ – user6680 Aug 31 '20 at 14:36
  • try reinstalling sam – Tummala Dhanvi Aug 31 '20 at 15:26
  • I reinstalled SAM and updated aws cli to latest version, but same issue – user6680 Aug 31 '20 at 15:39
  • I managed to almost get it working. I realized that while docker cmds work, docker desktop wasn't running. Then when I ran it and got an error. It wasn't running right because I disabled Hyper-V for something I was doing in virtualbox a couple weeks ago. I re-enabled hyper-v and rebooted. Now the ```sam local start-api``` cmd works. – user6680 Aug 31 '20 at 16:16
  • 1
    When I try and call my hello world app.js file using postman and POST ```http://127.0.0.1:3000/hello``` I get ```"message": "Missing Authentication Token"``` It's just a simple function. Not really using any aws sdk atm. But I added aws signature with access key, but same issue. postman reference: https://postimg.cc/McGGGChk Do you know what the issue is? – user6680 Aug 31 '20 at 16:16
  • have you changed any config after creating the project? – Tummala Dhanvi Sep 01 '20 at 03:25
  • If you mean the launch.json then no. I also created a fresh project unaltered and worked from there, but same issue. What is the correct value to put at the end of ```http://127.0.0.1:3000/``` for postman calls when referencing project structure in image below? I'm wondering if my putting the wrong value – user6680 Sep 01 '20 at 16:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220832/discussion-between-tummala-dhanvi-and-user6680). – Tummala Dhanvi Sep 02 '20 at 05:14