How to Run Aspose.CAD Docker image in AWS Lambda Function

Prerequisites

  • Docker must be installed on your system. For information on how to install Docker on Windows or Mac, refer to the links in the “See Also” section.
  • Visual Studio 2022.
  • AWS Toolkit for Visual Studio 2022.
  • NET 6 SDK is used in the example.
  • Postman

AWS Lambda Function

Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, and logging. With Lambda, you can run code for virtually any type of application or backend service.

Creating the AWS Lambda Function

To create the AWS Lambda Function program, follow the steps below:

  1. Create AWS Lambda Project.
    Create AWS function button
  2. Select .NET 6(Container Image) and click ‘Finish’ button.
    Create container function button
  3. Open AWS Explorer in Visual Studio(View->AWS Explorer).
  4. Add AWS credentials profile in AWS Explorer.
    Credential profile
  5. Enter Access Key ID and Secret Access Key, you can get these keys in Security credentials or contact the administrator and get a csv file for authorization.
    Acount profile settings
  6. Install the latest libraries from NuGet.
    NuGet Manager
  7. Code example for convert cad image to pdf file.
    public APIGatewayHttpApiV2ProxyResponse FunctionHandler(APIGatewayHttpApiV2ProxyRequest stream, ILambdaContext context)
    {
        try
        {            
            var parser = HttpMultipartParser.MultipartFormDataParser.Parse(new MemoryStream(Convert.FromBase64String(stream.Body)));
            var file = parser.Files.First();
            Stream fileStream = file.Data;
    
            using (var img = Aspose.CAD.Image.Load(fileStream))
            {
                var ms = new MemoryStream();
                img.Save(ms, new PdfOptions());
                ms.Seek(0, (System.IO.SeekOrigin)SeekOrigin.Begin);
              
                return new APIGatewayHttpApiV2ProxyResponse
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Body = Convert.ToBase64String(ms.ToArray()),
                    IsBase64Encoded = true,
                    Headers = new Dictionary<string, string>
                    {
                        {"Content-Type", "application/pdf" },
                        {"Content-Disposition", "attachment;filename=filename.pdf" }
                    }
                };
            }
        }
        catch (Exception e)
        {           
            return new APIGatewayHttpApiV2ProxyResponse
            {
                StatusCode = (int)HttpStatusCode.OK,
                Body = e.Message,
                Headers = new Dictionary<string, string>
                {
                    {
                        "Content-Type", "text/html"
                    }
                }
            };
        }
    }
  8. Edit the DockerFile as in the Configuring a Dockerfile section.
  9. Start Docker Desktop.
  10. Publish to AWS Lambda.
    AWS lambda publish
  11. Edit upload configuration.
    Upload aws lambda
  12. Click ‘Upload’ button.
    Upload aws lambda last
  13. Go to AWS and select Lambda.
    AWS Lambda
  14. Select your new function and create url function.
    Configuration url function
  15. Select auth type
  • AWS_IAM - Only authenticated IAM users and roles can make requests to your function URL.
  • NONE - Lambda won’t perform IAM authentication on requests to your function URL. The URL endpoint will be public unless you implement your own authorization logic in your function.

Configuring a Dockerfile

The next step is to edit configure the Dockerfile in project.

  1. In the Dockerfile, specify:
FROM public.ecr.aws/lambda/dotnet:6

WORKDIR /var/task

COPY "bin/Release/lambda-publish"  .

RUN yum install -y amazon-linux-extras 
RUN amazon-linux-extras install epel -y
RUN yum install -y libgdiplus  

CMD ["AWSLambda::AWSLambda.Function::FunctionHandler"]

The above is a simple Dockerfile, which contains the following instructions:

  • The SDK image to be used. Here it is the Net 6 image. Docker will download it when the build is run. The version of SDK is specified as a tag.
  • After, you may need to install Fonts because the SDK image contains very few fonts. Also, you can use local fonts copied to docker image.
  • The working directory, which is specified in the next line.
  • The command to copy everything to container, publish the application, and specify the entry point.

Execution example

  1. Postman settings.
    Overview menu
  2. Select any DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG file.
  3. Click the send button.

More Examples

For more samples of how you can use Aspose.CAD in Docker, see the examples.

See Also.