This guide provides step-by-step instructions for deploying an API using AWS Lambda with API Gateway through CDK custom constructs.
- AWS CLI configured with proper credentials
- AWS CDK installed (
npm install -g aws-cdk) - Docker installed and running
- .NET 7.0 SDK installed
- Access to the workshop repository
# Navigate to the workshop folder
cd workshop/src/iacFirst, let's understand our project structure:
- API Implementation: Located in
workshop/src/apps/Minimal.Api/ - CDK Infrastructure: Located in
workshop/src/iac/
This step validates your Docker build works correctly:
cd ../apps/Minimal.Api/
docker build -t minimal-api .Review the productionBackend.ts file which contains our stack definitions.
Key sections:
// Creating the Lambda stack
const apiLambdaStack = this.createApiLambdaStack(
"ApiLambda",
this.props.dockerfileApi,
"api",
);
// Creating the API Gateway stack
const apiGatewayStack = this.createApiGateway(
apiLambdaStack.lambdaFunction,
"ApiGateway",
"api-cdk-workshop",
);cd ../../iac
cdk bootstrapcdk synthThis command generates CloudFormation templates without deploying. Review the output in the cdk.out directory.
cdk deploy m47-cdk-intro-workshop-apilambda-production-stackThis will:
- Build your Docker image
- Push it to Amazon ECR
- Create the Lambda function with your container
- Configure IAM permissions
- Set up networking in your VPC
cdk deploy m47-cdk-intro-workshop-apigateway-production-stackThis will:
- Create an API Gateway REST API
- Connect it to your Lambda function
- Configure CORS settings
- Set up a custom domain name
- Create Route53 DNS records
# Note the API endpoint from the CloudFormation outputs
curl -v https://api-cdk-workshop.m47.io/health
# Test the Hello endpoint
curl -v https://api-cdk-workshop.m47.io/hello- Open the AWS Management Console
- Navigate to CloudWatch > Log Groups
- Find the log group for your Lambda function:
/aws/lambda/m47-cdk-intro-workshop-api-production
- Examine logs for any issues or to see request handling
After making changes to your API (e.g., adding new endpoints):
-
Build and test locally (optional)
-
Redeploy only the Lambda stack:
cdk deploy m47-cdk-intro-workshop-apilambda-production-stack
To avoid incurring costs, you can destroy the resources:
cdk destroy m47-cdk-intro-workshop-apigateway-production-stack
cdk destroy m47-cdk-intro-workshop-apilambda-production-stackYour deployment creates:
- ECR Repository: Stores your Docker image
- Lambda Function: Runs your containerized API
- API Gateway: Provides HTTP endpoints for your Lambda
- CloudWatch Logs: Stores execution logs
- IAM Roles: Grants necessary permissions
- Route53 Records: Maps your domain to API Gateway
Follow these steps to add new endpoints:
- Add a new endpoint file in the
Endpointsfolder - Register the endpoint in
Startup.cs - Build and redeploy the Lambda function
- Add the Weather endpoint from the exercise guide
- Deploy the updated Lambda function
- Test the new endpoint using curl
- Check CloudWatch logs to verify requests are being processed correctly