Introduction
In this article, you will learn how to list and create test files in AWS Lambda tmp directory using C#. Each AWS Lambda execution environment provides 512 MB of disk space in the /tmp directory which can be used for some data processing and can be used for temporary storage. This /tmp disk space is preserved for the lifetime of the execution environment and provides a transient cache for data between invocations. Refer this blog to learn more about choosing between AWS Lambda data storage options in web apps.
Topics covered
This article demonstrates how to build the following:
- Create AWS Serverless Application
- Deployment
- Testing
- Clean up resources
Pre-requisites
- Download and install Visual Studio 2019 or above.
- Download and install AWS Toolkit for Visual Studio 2019 or above.
- An Active AWS Account. (See AWS Account Page.)
- User with sufficient access to create AWS resources for this article.(See IAM role in AWS IAM Roles Documentation.)
- User should have programmatic access keys. (See IAM user and Access in the AWS IAM documentation.)
- Download and install Amazon command line interface (AWS CLI). Configure AWS CLI.
Tools
- AWS Console
- Visual Studio 2019
Task 1 - Create AWS Serverless Application (.Net Core - C#)
In this task, you will see how to create an AWS Serverless Application using Visual Studio 2019.
Step 1: Open Visual Studio 2019, click Create a new project.
Step 2: Search AWS Serverless in the search bar, select AWS Serverless Application (.NET Core – C#) project template and click Next.
Step 3: Enter the project name as AWSServerlessLambdaDemo. Click Next.
Step 4: Select ASP.NET Core 5(Container Image) blueprint. Click Finish.
Step 5: Expand Controllers folder, open ValuesController.cs file.
Step 6: Update the class with following code.
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace AWSServerlessLambdaDemo.Controllers
{
[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
string directory = @"/tmp/";
List<string> result = new List<string>();
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(directory);
foreach (string fileName in fileEntries)
{
result.Add(fileName);
}
return result;
}
// POST api/values
[HttpPost]
public void Post()
{
Guid guid = Guid.NewGuid();
string directory = @"/tmp/";
string path = directory + guid + ".txt";
List<string> result = new List<string>();
// Create the file, or overwrite if the file exists.
using (FileStream fs = System.IO.File.Create(path))
{
byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
}
}
NOTE: Hard-coded values are used only for testing purpose and this should be used in PRODUCTION environment. The values should be retrieved dynamically.
Task 2 - Deployment
In this task, you will see how to publish the project to AWS Lambda.
Step 1: Right click on the project, click Publish to AWS Lambda.
Step 2: Select the AWS Profile, Region, Stack Name and S3 bucket. Click Publish. It will take few minutes to create the required resources.
Step 3: Once the deployment is completed, copy the AWS Serverless URL from Visual Studio. Note: You can also navigate to AWS Lambda and get the API endpoint URL.
Task 3 - Testing
In this task, you will see how to test the API to list all the files and create a new test file.
Step 1: Open Postman and do the following testing.
Step 2: GET Method – list all the files from /tmp directory.
GET https://<ck6tnxud5h.execute-api.us-east-1.amazonaws.com>/Prod/api/values
Step 3: POST Method – list all the files from /tmp directory.
POST https://<ck6tnxud5h.execute-api.us-east-1.amazonaws.com>/Prod/api/values
Task 4 - Clean up resources
Delete all the resources to ensure that you're not charged for any services you aren't using.
Summary
This article described how to list and create test files in AWS Lambda tmp directory using C#.