Deploying AWS Lambda from AWS ECR registry with Terraform

Amazon AWS is rather slow when it comes to supporting new .NET versions on its Lambda feature. This is where running your functions from a docker image comes in handy, allowing you to run any version of .NET.

In this short blog article, I will demonstrate how to deploy AWS Lambda from an Elastic Container Registry (ECR) using Terraform. I will focus on the DevOps aspects and assume that you already have the function that you want to deploy.

Push Docker Image to AWS ECR

Let’s start with creating a simple Dockerfile.

FROM public.ecr.aws/lambda/dotnet:7
WORKDIR /var/task
COPY "bin/net7.0" .

I am assuming here that you use .NET 7 because, at the time of writing this, Lambda currently supports .NET 6, so you wouldn’t have to use the ECR approach if you’re targeting an earlier version of .NET. Now, let’s configure the AWS Command Line Interface (CLI) if you haven’t already done so.

aws configure

Now you need to retrieve an authentication token and authenticate your Docker client to your ECR registry (you need to provide the region and the repository uri).

aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <repo_uri>.dkr.ecr.<region>.amazonaws.com

You are now ready to build the image (change the image tag to one that fits your needs):

docker build -t th-aws-workers:1.2.0 .

Next, let’s now create the tag and push the image to the repo:

docker tag th-aws-workers:1.2.0 <repo_uri>.dkr.ecr.<region>.amazonaws.com/threg:1.2.0
docker push 666097061481.dkr.ecr.us-east-2.amazonaws.com/threg:1.2.0   

You might want to navigate to AWS console and check if the image is there.

Image published to ECR

Terraform

In the previous step, we created and published an image to a repository.

An aws_lambda_function resource has an image_uri parameter where you provide, well … your image URI. It couldn’t be simpler to do:


data "aws_ecr_repository" "trailhead-ecr-repo" {
  name = "threg"
}

resource "aws_lambda_function" "api_function" {
  function_name = "th-api-framework-lambda-${var.environment}"
  timeout       = 5 # seconds
  image_uri     = "${data.aws_ecr_repository.trailhead-ecr-repo.repository_url}:1.2.0"
  package_type  = "Image"

  role = aws_iam_role.api_function_role.arn

  environment {
    variables = {
      ENVIRONMENT = var.environment
    }
  }
}

You will also want to define aws_iam_role along with aws_iam_role_policy_attachment. Permission AWSLambdaBasicExecutionRole is needed if you want to be able to log to a CloudWatch (aws lambda runtime logs AND/OR function logs).

data "aws_iam_policy_document" "policy-document" {
  statement {
    actions = ["sts:AssumeRole"]

    principals {
      type        = "Service"
      identifiers = ["lambda.amazonaws.com"]
    }
  }
}

resource "aws_iam_role" "api_function_role" {
  name = "lambda_iam_role"

  assume_role_policy = data.aws_iam_policy_document.policy-document.json
}

resource "aws_iam_role_policy_attachment" "basic" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
  role       = aws_iam_role.api_function_role.name
}

Conclusion

In this brief guide, you’ve learned how to leverage Docker, ECR, and Terraform to deploy as-of-yet unsupported versions of .NET to AWS Lambda, allowing you to use the latest .NET version in your Lambda applications.

Picture of Piotr Kolodziej

Piotr Kolodziej

Born and raised in Poland, Piotr did his first programming on Atari 800XL. He covered his first dial-up modem with a duve, so his mom wouldn’t hear it and freak over the internet bills. He graduated with a degree in Telecommunication and Business Application Programming. Piotr is a certified .NET and Azure Developer, and is passionate about excellent software architecture.

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Document APIs Done Right

At Trailhead, we have built many enterprise-grade .NET APIs that deal with managing documents – files, photos, PDFs, etc – and in this blog I will distill some best practices that will help you optimize your API and download performance, re

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.