By the end of this workshop, participants will:
- Understand how to implement a Lambda-based API using custom CDK constructs
- Deploy a real working serverless API to AWS
- Configure API Gateway to expose the Lambda function
- Test the deployed API endpoint
- Workshop overview
- Review what we learned about Lambda Runtime Interface Client (RIC) in the previous session
- Understanding our project structure
- Explore the
ApiLambdaStackcustom construct - Explore the
ApiGatewayStackcustom construct - Understanding how they work together
- Code walkthrough
- Implementing a simple API endpoint in our Lambda function
- Configuring and deploying the API Lambda stack
- Integrating with API Gateway
- Deploying the combined stack
- Testing the deployed API endpoint
- Exploring CloudWatch logs
- Troubleshooting common issues
Let's examine our existing .NET Minimal API implementation:
// Key API endpoints we'll expose
public static class ApiEndpoints
{
public const string Health = "/health";
public const string HelloWorld = "/hello";
}We'll use our custom CDK constructs to define the Lambda function and API Gateway:
// 1. Create the API Lambda Stack
const apiLambdaStack = this.createApiLambdaStack(
"ApiLambda",
this.props.dockerfileApi,
"api",
);
// 2. Create the API Gateway Stack and connect it to the Lambda
const apiGatewayStack = this.createApiGateway(
apiLambdaStack.lambdaFunction,
"ApiGateway",
"api-cdk-workshop",
);Review the Dockerfile that packages our API:
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["Minimal.Api.csproj", "."]
RUN dotnet restore "Minimal.Api.csproj"
COPY . .
RUN dotnet build "Minimal.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Minimal.Api.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Minimal.Api.dll"]# Deploy the stacks
cdk deploy m47-cdk-intro-workshop-apilambda-production-stack
cdk deploy m47-cdk-intro-workshop-apigateway-production-stack
# Test the deployed API
curl -v https://api-cdk-workshop.m47.io/hello- How AWS CDK constructs can be extended for reusable infrastructure patterns
- How Lambda and API Gateway work together in a serverless architecture
- Best practices for organizing CDK code
- Troubleshooting deployment issues
- Add a new API endpoint to the Lambda function
- Deploy the updated API using CDK
- Test the new endpoint to confirm successful deployment
- Explore CloudWatch logs to verify execution