- 
                Notifications
    
You must be signed in to change notification settings  - Fork 128
 
revamp sam docs + add vs code launch config #1234
Conversation
| 
           🎊 PR Preview has been successfully built and deployed to https://localstack-docs-preview-pr-1234.surge.sh 🎊  | 
    
| ## Debugging on VS Code | ||
| 
               | 
          ||
| To debug your Lambda functions in VS Code while using the SAM CLI with LocalStack, set up a launch configuration in the `.vscode/launch.json` file. Insert the following settings into the file: | ||
| 
               | 
          ||
| 
               | 
          ||
| ```json | ||
| { | ||
| "type": "aws-sam", | ||
| "request": "direct-invoke", | ||
| "name": "Job dispatcher lambda", | ||
| "invokeTarget": { | ||
| "target": "code", | ||
| "projectRoot": "${workspaceFolder}", | ||
| "lambdaHandler": "lambda/lambda.handler" | ||
| }, | ||
| "lambda": { | ||
| "runtime": "python3.8", | ||
| "payload": {}, | ||
| "environmentVariables": { | ||
| "ENDPOINT_URL": "http://localstack:4566/", | ||
| "S3_ENDPOINT_URL": "http://s3.localhost.localstack.cloud:4566/", | ||
| "AWS_ACCESS_KEY_ID": "test", | ||
| "AWS_SECRET_ACCESS_KEY": "test", | ||
| "AWS_SESSION_TOKEN": "test", | ||
| "AWS_REGION": "us-east-1", | ||
| "MAIN_DOCKER_NETWORK": "localstack-network" | ||
| } | ||
| }, | ||
| "sam": { | ||
| "dockerNetwork": "localstack-network" | ||
| } | ||
| } | ||
| ``` | ||
| 
               | 
          ||
| The `dockerNetwork` property is essential as it allows the LocalStack container to use the `samlocal invoke` commands within the same network as the LocalStack container itself. Adjust the Lambda function handler and environment variables as needed. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The mentioned issue (#657) is about sam local and NOT samlocal. By default the container is span up in the main docker network and accessible, however IF the user decides to run LocalStack on a different network ONLY then they need to modify this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the communication from the reporter, channel.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
He luckily shared the actual LS startup command and it seems he puts his LS instance deliberately to a different network.

Regarding the question if he uses samlocal local or sam local is unclear, but doesn't really matter as he's setting the env vars for the debug session in the shared config.
Long story short as long as you define the right network, which in this case is not the default you can debug lambdas and communicate with LS. So I'd add it as a recommendation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the reply, it would be sam local invoke
Anyhow, I believe that at that time this was necessary to use and set the docker network. I did not have time to test it and confirm. I think we can still add this, but I would wait for confirmation from our testing.
The communication is clear at what we proposed. It was later followed, and he added the network setup based on our docs.
I’m reading through the docs on accessing Localstack from another container, and so I tried adding the following to my docker-compose.yml file
Then it was successfully running:
Note the
dockerNetworkproperty; this causes the container that thesam local invokecommand starts to run in the same network as the LocalStack container, thereby allowing it to connect successfully to the http://localstack:4566/ endpoint (see https://docs.aws.amazon.com/cloud9/latest/user-guide/sam-debug-config-ref.html).
Co-authored-by: Gábor Németh <[email protected]>
| ## AWS SAM CLI for LocalStack | ||
| 
               | 
          ||
| To deploy SAM applications on [LocalStack](https://github.com/localstack/localstack) you can use [samlocal](https://github.com/localstack/aws-sam-cli-local), a wrapper for the [AWS SAM CLI](https://github.com/aws/aws-sam-cli). | ||
| ## `samlocal` wrapper script | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| ## `samlocal` wrapper script | |
| ## samlocal wrapper script | 
Should we be using code format in the headings? For me, it affects the visual appeal of headings and the message.
| ## Debugging on VS Code | ||
| 
               | 
          ||
| To debug your Lambda functions in VS Code while using the SAM CLI with LocalStack, set up a launch configuration in the `.vscode/launch.json` file. Insert the following settings into the file: | ||
| 
               | 
          ||
| 
               | 
          ||
| ```json | ||
| { | ||
| "type": "aws-sam", | ||
| "request": "direct-invoke", | ||
| "name": "Job dispatcher lambda", | ||
| "invokeTarget": { | ||
| "target": "code", | ||
| "projectRoot": "${workspaceFolder}", | ||
| "lambdaHandler": "lambda/lambda.handler" | ||
| }, | ||
| "lambda": { | ||
| "runtime": "python3.8", | ||
| "payload": {}, | ||
| "environmentVariables": { | ||
| "ENDPOINT_URL": "http://localstack:4566/", | ||
| "S3_ENDPOINT_URL": "http://s3.localhost.localstack.cloud:4566/", | ||
| "AWS_ACCESS_KEY_ID": "test", | ||
| "AWS_SECRET_ACCESS_KEY": "test", | ||
| "AWS_SESSION_TOKEN": "test", | ||
| "AWS_REGION": "us-east-1", | ||
| "MAIN_DOCKER_NETWORK": "localstack-network" | ||
| } | ||
| }, | ||
| "sam": { | ||
| "dockerNetwork": "localstack-network" | ||
| } | ||
| } | ||
| ``` | ||
| 
               | 
          ||
| The `dockerNetwork` property is essential as it allows the LocalStack container to use the `samlocal invoke` commands within the same network as the LocalStack container itself. Adjust the Lambda function handler and environment variables as needed. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the reply, it would be sam local invoke
Anyhow, I believe that at that time this was necessary to use and set the docker network. I did not have time to test it and confirm. I think we can still add this, but I would wait for confirmation from our testing.
The communication is clear at what we proposed. It was later followed, and he added the network setup based on our docs.
I’m reading through the docs on accessing Localstack from another container, and so I tried adding the following to my docker-compose.yml file
Then it was successfully running:
Note the
dockerNetworkproperty; this causes the container that thesam local invokecommand starts to run in the same network as the LocalStack container, thereby allowing it to connect successfully to the http://localstack:4566/ endpoint (see https://docs.aws.amazon.com/cloud9/latest/user-guide/sam-debug-config-ref.html).
Co-authored-by: MarcelStranak <[email protected]>
Co-authored-by: MarcelStranak <[email protected]>
Co-authored-by: MarcelStranak <[email protected]>
Co-authored-by: MarcelStranak <[email protected]>
Co-authored-by: MarcelStranak <[email protected]>
Co-authored-by: MarcelStranak <[email protected]>
Co-authored-by: MarcelStranak <[email protected]>
Fixes #657