Terraform — Deploy docker image on AWS AppRunner from ECR Repository

In this article, we are talking about How we can deploy docker image from ECR Repository on AWS AppRunner service using terraform.

AWS App Runner is a fully managed service that makes it easy for developers to quickly deploy containerized web applications and APIs, at scale and with no prior infrastructure experience required.

Start with your source code or a container image. App Runner automatically builds and deploys the web application and load balances traffic with encryption.

App Runner also scales up or down automatically to meet your traffic needs. With App Runner, rather than thinking about servers or scaling, you have more time to focus on your applications.

App Runner Features

AWS has put a lot of effort into making things easy for developers, especially for small-scale projects that don’t need beefy infrastructure. It has:

  • Simple auto scaling: instances are started and stopped as demand changes, between configurable min and max limits.
  • Load balancing: the service includes a transparent, non-configurable load balancer.
  • SSL enabled: you get HTTPS endpoints for all your applications with AWS-managed certificates. You don’t need to issue or renew certificates.
  • Build service: you can push your own images or let AWS build them for you from code.
  • Persistent URLs: the service assigns randomly-generated URLs for each environment. You can optionally map them to domains of your own.

App Runner can run in two modes.

  1. In build mode, AWS pulls code from GitHub and builds the application on every change.

2. In container mode, it deploys Docker-compatible images from public or private AWS ECR registries.

In this article we will using ECR public as well as private repository with App Runner deployment which mean we are using container mode

Prerequisites

  • We require AWS IAM API keys (access key and secret key) for creating and deleting permissions for all AWS resources.
  • ECR Repository and docker image pushed on it.
  • Terraform should be installed on the machine. If Terraform does not exist you can download and install it from here.

version

To build AWS App Runner, you need to meet the following versions:

This time I built it with the following version.

$ terraform version
Terraform v1.0.0
on linux_amd64

Amazon Resources Created Using Terraform

A Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more .tf files is a module. When you run Terraform commands directly from such a directory, it is considered the root module

1. IAM Module

  • IAM Role and policy

2. AppRunner Module

  • auto scaling configuration
  • aws apprunner service

Create an IAM role to grant to App Runner

Set up an IAM role to build AWS App Runner.
The key build.apprunner.amazonaws.comis tasks.apprunner.amazonaws.comto specify and for the service to which AssumeRole is assigned .
After that, AWS has prepared a policy for App Runner, so attach it to the IAM role.

Create an App Runner

Finally, create an App Runner resource in Terraform.
There are some App Runner related resources, but they are the main resources for actually creating App Runner aws_apprunner_service,
source_configuration

We have 2 ways to deploy App Runner with ECR repository.

  • Deploy App Runner with ECR private repository
  • Deploy App Runner with ECR public repository

Deploy App Runner with ECR private repository

Deploy App Runner with ECR public repository

Note: In this approach we don't need IAM role.

Check the URL of App Runner created by applying

I want to check the URL of the created App Runner as the execution result of the apply command, so outputset.

output "app_runner_url" {
value = aws_apprunner_service.example.service_url
}

After that, just run the following commands.

terraform init

terraform plan

terraform apply

it will take 2 to 3 minutes to complete the execution.
When the execution is completed, the URL will be displayed as shown below, so let’s access it.

app_runner_url = "xxxxx.us-east-2.awsapprunner.com/"

Thank you for reading, if you have anything to add please send a response or add a note!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Bhatasana

AWS Community Builder | Terraform Associate | Infra. Automation Engineer, Love to work with #AWS #Terraform #Jenkins #kubernetes #Docker #Ansible #Selenium