{"id":910,"date":"2020-12-02T05:50:42","date_gmt":"2020-12-01T20:50:42","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=910"},"modified":"2020-12-15T19:11:45","modified_gmt":"2020-12-15T10:11:45","slug":"migration-from-azure-functions-to-aws-lambda-with-aws-lambda-container-image-support","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2020\/12\/02\/migration-from-azure-functions-to-aws-lambda-with-aws-lambda-container-image-support\/","title":{"rendered":"Migration from Azure Functions to AWS Lambda with AWS Lambda Container Image Support"},"content":{"rendered":"

AWS Lambda Container Image Support<\/a> was announced at AWS re:Invent 2020<\/a>!<\/p>\n

What is AWS Lambda Container Image Support?<\/h2>\n

With the release of AWS Lambda Container Image Support, AWS Lambda can now take advantage of Docker container images in Lambda.<\/p>\n

By using Docker images, you can take advantage of library-dependent packages (such as the AWS CLI) that were previously difficult to use in Lambda, and run Docker container-based applications in Lambda.<\/p>\n

Use cases of AWS Lambda Container Image Support<\/h2>\n

Here are some use cases for AWS Lambda Container Image Support.<\/p>\n

1. Running the AWS CLI in Lambda<\/h3>\n

Now it is possible to use features or commands on Lambda that depend on OS libraries or specific packages.<\/p>\n

For example, if you want to use "s3 sync", which is not available in the AWS SDK, you can use it by installing the AWS CLI on the Docker image.<\/p>\n

2. Migrating a serverless environment running on other platforms<\/h3>\n

Serverless applications that run on Docker container images, such as Azure Functions, can also run on Lambda.<\/p>\n

For example, if your Web-API server container application is running on Azure Functions, you can consider moving it to Lambda by using the Proxy library and Amazon API Gateway.<\/p>\n

In this case, we will assume this use case and create a sample application<\/a> to validate the migration to Lambda.<\/p>\n

We used a Azure Functions sample application<\/a> and validated the migration to Lambda.<\/p>\n

Migration approach<\/h2>\n

\"image.png<\/p>\n

How to migrate<\/h3>\n

We Installed the AWS Lambda Runtime Interface Client, based on the Azure Functions Container Image.<\/p>\n

We added a Lambda handler to this container and configure the Dockerfile so that the entrypoint specifies the handler.<\/p>\n

Example of Dockerfile configuration:<\/h4>\n
# Azure Functions Container\nFROM xblood\/azurefunctionsimage:v1.0.0\n\nARG FUNCTION_DIR="\/home\/app\/"\nARG RUNTIME_VERSION\nARG DISTRO_VERSION\n\nRUN apt-get install -y libtool autoconf automake make cmake\n# build-base, libexecinfo-dev, libcurl\nRUN apt-get install -y curl build-essential\nRUN pip install awscli\nARG AWS_ACCESS_KEY_ID\nARG AWS_SECRET_ACCESS_KEY\nARG AWS_SESSION_TOKEN\nENV AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID}\nENV AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}\nRUN mkdir -p ${FUNCTION_DIR}\n\n# Copy Lambda Handler\nCOPY handler.py ${FUNCTION_DIR}\n\n# Install AWS Lambda Runtime Interface Client\nRUN python${RUNTIME_VERSION} -m pip install \\\n    awslambdaric \\\n    --target \/home\/app\/\n\nWORKDIR ${FUNCTION_DIR}\n\n# Set the CMD to handler\nENTRYPOINT [ "\/usr\/local\/bin\/python", "-m", "awslambdaruntimeclient" ]\nCMD [ "handler.lambda_handler" ]<\/code><\/pre>\n

The xblood\/azurefunctionsimage<\/code> specified in this Dockerfile is a pre-built image based on Azure Functions tutorial<\/a>.<\/p>\n

Building Docker Image<\/h4>\n

In this example, we built the container by executing the following command.<\/p>\n

docker build . \\\n-f Dockerfile \\\n-t lambda-container-image \\\n--build-arg AWS_ACCESS_KEY_ID="[YOUR AWS ACCESS KEY ID]" \\\n--build-arg AWS_SECRET_ACCESS_KEY="[YOUR AWS SECRET ACCESS KEY]" \\\n--build-arg RUNTIME_VERSION="3.8" \\\n--build-arg DISTRO_VERSION="3.12"<\/code><\/pre>\n

Push to ECR<\/h4>\n

We set tags on the Docker image and pushed it to Amazon Elastic Container Registry (ECR).
\nECR repository must be created and the login to ECR is required beforehand.<\/p>\n

docker push xxxxxxxxxxxx.dkr.ekr.sa-east-1.amazonaws.com\/lambda-container-image:latest<\/code><\/pre>\n

Creating Lambda Function<\/h4>\n

Next, we created a Lambda Function using AWS Lambda Container Image Support.<\/p>\n

In this case, we created the IAM role in advance with the following policy.<\/p>\n

Example IAM role policy ( lambda-container-image-iam-role<\/code> )<\/h5>\n
{\n   "Statement":[[\n      {\n         "Sid": "VisualEditor0",\n         "Effect": "Allow",\n         "Action":["Action":[\n            "ecr:SetRepositoryPolicy",\n            "ecr:GetRepositoryPolicy"\n         ],\n         "Resource": "arn:aws:ecr:<region>:<account>:repository\/<repo name>\/"\n      }\n   ]\n}<\/code><\/pre>\n

Specify the ImageUri<\/code> option, added by AWS Lambda Container Image Support, for the container image you have just pushed to the ECR.<\/p>\n

Aws lambda create-function\n--function-name lambda-container-image-function\n--package-type Image\n--Image URI=xxxxxxxxxxxxxxxxxxxx.dkr.ekr.sa-east-1.amazonaws.com\/lambda-container-image:latest\n--role arn:aws:iam::xxxxxxxxxxxxxxxxxxxxx:role\/lambda-container-image-iam-role<\/code><\/pre>\n

Launch Lambda Function<\/h4>\n

We tested the Lambda function from the AWS Management Console and confirmed the Lambda function has been successfully executed.<\/p>\n

\"image.png<\/p>\n

For a more practical use case<\/h4>\n

In this article, we have used sample code for validation purposes. However, when migrating a real serverless application to AWS Lambda, or in a multi-cloud strategy, you need to consider the dependencies of each cloud vendor with their interfaces.<\/p>\n

Nowadays, in some cases, this can be solved by leveraging libraries such as Serverless Multicloud Library<\/a>.<\/p>\n

Take advantage of AWS Lambda with MMM!<\/h2>\n

We have been certified as an AWS Service Delivery Partner (AWS Lambda Partner)<\/a>. We have a strong technical expertise and a proven track record in the business use of AWS Lambda, including the new AWS Lambda Container Image Support feature.<\/p>\n

Including the new AWS Lambda Container Image Support feature, we are strongly good at embedding the advantages of the cloud - flexibility and agility - into our customers' businesses by using a wide variety of AWS managed services in a best-practice manner.<\/p>\n

Find more information at Serverless Architecture (AWS Lambda)<\/a>, and feel free to contact us at https:\/\/mmmcorp.co.jp\/inquiry<\/a> !<\/p>\n","protected":false},"excerpt":{"rendered":"

AWS Lambda Container Image Support was announced at AWS re:Invent 2020! What is AWS Lambda Container Image Support? With the release of AWS Lambda Container Image Support, AWS Lambda can now take adva […]<\/p>\n","protected":false},"author":1,"featured_media":895,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[344,73,136,391],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/910"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=910"}],"version-history":[{"count":2,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/910\/revisions"}],"predecessor-version":[{"id":912,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/910\/revisions\/912"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media\/895"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=910"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=910"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=910"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}