How to Run Aspose.CAD Docker image in AWS Lambda Function
Contents
[
Hide
]
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
Make sure you have sufficient rights to create AWS Lambda functions and images in the Amazon Elastic Container Registry.
To create the AWS Lambda Function program, follow the steps below:
- Create AWS Lambda Project.
- Select .NET 6(Container Image) and click ‘Finish’ button.
- Open AWS Explorer in Visual Studio(View->AWS Explorer).
- Add AWS credentials profile in AWS Explorer.
- 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.
- Install the latest libraries from NuGet.
- 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" } } }; } }
- Edit the DockerFile as in the Configuring a Dockerfile section.
- Start Docker Desktop.
- Publish to AWS Lambda.
- Edit upload configuration.
- Click ‘Upload’ button.
- Go to AWS and select Lambda.
- Select your new function and create url function.
- 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.
- 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
- Postman settings.
- Select any DXF, DWG, DGN, DWF, DWFX, IFC, STL, DWT, IGES, PLT, CF2, OBJ, HPGL, IGS, PCL, FBX, PDF, SVG file.
- Click the send button.
If the answer is successful, click Save to file and you will receive the converted file in pdf format
More Examples
For more samples of how you can use Aspose.CAD in Docker, see the examples.