Building And Deploying Web API

Build and deploy a basic DotNet Core 6 Web API to AWS Lambda function.

This post walks you through:

  1. Configuring Visual Studio.

  2. Building a basic DotNet Core 6 Web API.

  3. Creating AWS Lambda funcion.

  4. Deploying the project to the Lambda function.

  5. Troubleshooting.

The example project used here is available on my GitHub repo.

1. Setup Visual Studio for AWS Lambda

Before getting to the code, let's set up Visual Studio to work with AWS.

- AWS Toolkit

Download AWS Toolkit Extension on visual studio and close Visual Studio to start the installation.

  1. Open Visual Studio - if you do not have any projects, click on ‘Continue without code‘

  2. Open Extensions tab → Manage Extensions → Search for AWS Toolkit → Download

  3. After the download is done, close VS. This will start the installation.

  4. Once installed, open visual studio.

- IAM User

  1. Log in to your AWS console and open IAM → Users.

  2. Add User

    Provide a name

  3. In the next option, select AWSLambda_FullAccess from the Attach existing policies directly option. Attaching this policy will provide access to the role to deploy code to the AWS Lambda function.

  4. Add tags

  5. Review and create the user.

  6. Open the created user and click on Create access key.

  7. Select Command Line Interface (CLI)

  8. Save the Access Key Id and Secret access key or download the CSV file

  9. Now, go to Visual Studio → View → AWS Explorer. In the AWS Explorer tab, click on the Add AWS Credentials Profile.

  10. Add the required fields or import the .csv file. Make sure you can expand the AWS lambda under the AWS services list.

  11. For deploying the application to AWS Lambda, install lambda tools.

    dotnet tool install --global Amazon.Lambda.Tools --version 5.6.2
    

2. Building Web API

Start by opening Visual Studio and creating an ASP.NET Core Web API project.

I’m gonna using the default boilerplate API implementation for now.

- Making ASP.NET Core Web API compatible with AWS Lambda

The API project will not be supported by AWS Lambda function. Some configurations should be made to make it compatible

  1. Install Amazon.Lambda.AspNetCoreServer.Hosting NuGet package.

  2. Go to the Program.cs file and add this extension method.

     builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi);
    
  3. Add a aws-lambda-tools-defaults.json file at the project level.

    This file will contain the values required for the deployment of the service to the AWS Lambda function.

     {
       "Information": [
         "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
         "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
         "dotnet lambda help",
         "All the command line options for the Lambda command can be specified in this file."
       ],
       "profile": "YOUR-AWS-IAM-PROFILE_NAME",
       "region": "REGION",
       "configuration": "Release",
       "function-runtime": "dotnet6",
       "function-memory-size": 256,
       "function-timeout": 30,
       "function-handler": "WeatherForecast",
       "function-url-enable": true
     }
    

    Note that I’m enabling function-url. This will create a publicly accessible URL that can be used to hit the API.

  4. Run the API locally and test using swagger to make sure the API works.

3. Creating Lambda Function

  1. Open the AWS Lambda → Functions

  2. Use the Author from scratch option

  3. Fill Basic information

  4. Advanced settings

    Enable Function URL to create a public URL to use the API. Note that I am not enabling Auth for now. But it's best practice enable IAM or implement your own Auth logic and configure CORS in production apps.

  5. Create the Function.

4. Deploying Web API to Lambda Function

  1. Open Terminal at the Web API project path.

  2. Run the deploy command.

     dotnet lambda invoke-function -fn WeatherForecast
    

    ‘-fn’ argument is the Lambda function name you created.

  3. You can test the endpoint using the Function URL.

    The URL should be {functionURL}/{WeatherForecast}

    You should be able to see the GET response

5. Troubleshooting

  1. Deployment failure

    1. Make sure there are no build errors.

    2. Make sure the IAM user profile used in Visual Studio has AWSLambda_FullAccess policy attached.

    3. Verify values in the aws-lambda-tools-defaults.json file. Especially the profile and region values.

  2. Not getting a success response from the get endpoint?

    1. Make sure the controller name is added as a path in the function-url ‘/weatherforecast’.

    2. Make sure the ‘AddAWSLambdaHosting’ extension method is added in the Web API program.cs.

    3. Check the cloud-watch logs for more info.

Let me know if you have any questions, feedbacks or suggestions in the comments.