Skip to content

Commit 7efe5f7

Browse files
committed
Add inital sample
1 parent 8e50b04 commit 7efe5f7

23 files changed

+4594
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vsdbg/
2+
volume/

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
install:
2+
cd test-stack; npm install
3+
./download-vsdbg.sh
4+
5+
.PHONY: install

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
# lambda-dotnet-debugging
1+
# Lambda .NET debugging sample
2+
23
A repository to show how to debug .NET lambda functions running in LocalStack

docker-compose.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
services:
2+
localstack:
3+
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
4+
image: localstack/localstack-pro # required for Pro
5+
ports:
6+
- "127.0.0.1:4566:4566" # LocalStack Gateway
7+
- "127.0.0.1:4510-4559:4510-4559" # external services port range
8+
- "127.0.0.1:443:443" # LocalStack HTTPS Gateway (Pro)
9+
environment:
10+
# Activate LocalStack Pro: https://docs.localstack.cloud/getting-started/auth-token/
11+
- LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN:?} # required for Pro
12+
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
13+
- DEBUG=${DEBUG:-0}
14+
- PERSISTENCE=${PERSISTENCE:-0}
15+
- LAMBDA_DOCKER_FLAGS=-v ${PWD}/vsdbg:/vsdbg
16+
volumes:
17+
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
18+
- "/var/run/docker.sock:/var/run/docker.sock"

download-vsdbg.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -r linux-x64 -l ./vsdbg

lambda-code/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin/
2+
obj/

lambda-code/.vscode/launch.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Attach to .NET Lambda",
9+
"type": "coreclr",
10+
"request": "attach",
11+
"sourceFileMap": {
12+
"/asset-input": "${workspaceFolder}/src"
13+
},
14+
"pipeTransport": {
15+
"pipeCwd": "${workspaceFolder}",
16+
"pipeProgram": "docker",
17+
"quoteArgs": false,
18+
"pipeArgs": [
19+
"exec",
20+
"-i",
21+
"-u",
22+
"sbx_user1051",
23+
"${command:vscode-docker.containers.select}",
24+
],
25+
"debuggerPath": "/vsdbg/vsdbg"
26+
},
27+
"preLaunchTask": "init-container"
28+
}
29+
]
30+
}

lambda-code/.vscode/tasks.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/lambda-dotnet-debugging.sln",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/lambda-dotnet-debugging.sln",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary;ForceNoAlign"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/lambda-dotnet-debugging.sln"
37+
],
38+
"problemMatcher": "$msCompile"
39+
},
40+
{
41+
"label": "init-container",
42+
"command": "docker",
43+
"type": "process",
44+
"args": [
45+
"exec",
46+
"${command:vscode-docker.containers.select}",
47+
"dnf",
48+
"install",
49+
"-y",
50+
"findutils",
51+
"procps"
52+
]
53+
}
54+
]
55+
}

lambda-code/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
UNAME := $(shell uname -m)
2+
ifeq ($(UNAME),x86_64)
3+
ARCHITECTURE ?= x86_64
4+
else
5+
ARCHITECTURE ?= arm64
6+
endif
7+
# The packaging function architecture is x86_64 by default and needs to be set explicitly for arm64
8+
# https://github.com/aws/aws-extensions-for-dotnet-cli/blob/cdd490450e0407139d49248d94a4a899367e84df/src/Amazon.Lambda.Tools/LambdaDefinedCommandOptions.cs#L111
9+
FUNCTION_ARCHITECTURE ?= $(ARCHITECTURE)
10+
11+
# Target Dotnet framework version
12+
FRAMEWORK ?= net8.0
13+
# Workaround for a Docker race condition causing an I/O error upon zipping to /out/handler.zip if
14+
# two builds are executed in short succession. Example: `make -C dotnet build && make -C dotnet6 build`
15+
BUILD_DIR ?= build-$(FRAMEWORK)
16+
17+
# https://gallery.ecr.aws/sam/build-dotnet8
18+
IMAGE ?= public.ecr.aws/sam/build-dotnet8:1.112.0
19+
20+
# Emulated builds with Dotnet8 are currently (2024-03-19) broken as discussed in many issues:
21+
# https://github.com/NuGet/Home/issues/12227
22+
# https://github.com/dotnet/runtime/issues/78340
23+
# https://github.com/dotnet/msbuild/issues/8508
24+
# Root cause QEMU issue: https://gitlab.com/qemu-project/qemu/-/issues/249
25+
# Workaround: Instead of emulating the build (works for Dotnet6), we use the native Docker image
26+
# and cross-build the Dotnet package using the flag `--function-architecture` (x86_64 or arm64).
27+
28+
build:
29+
mkdir -p $(BUILD_DIR) && \
30+
docker run --rm -v $$(pwd)/src:/app -v $$(pwd)/$(BUILD_DIR):/out $(IMAGE) bash -c "mkdir -p /app2 && cp /app/* /app2 && cd /app2 && dotnet lambda package --framework $(FRAMEWORK) --function-architecture $(FUNCTION_ARCHITECTURE) -o ../out/handler.zip" && \
31+
cp $(BUILD_DIR)/handler.zip handler.zip
32+
33+
clean:
34+
$(RM) -r $(BUILD_DIR) handler.zip
35+
36+
.PHONY: build clean
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet", "src\dotnet.csproj", "{7E8B858D-9F21-44D4-959B-DAAAA3E80521}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{7E8B858D-9F21-44D4-959B-DAAAA3E80521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7E8B858D-9F21-44D4-959B-DAAAA3E80521}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7E8B858D-9F21-44D4-959B-DAAAA3E80521}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7E8B858D-9F21-44D4-959B-DAAAA3E80521}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {2AC5CDEC-F8B8-46C9-915C-5564653965BF}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)