How to Install and Deploy Laravel 9 on AWS App Runner

Lonare
7 min readNov 28, 2022

--

Are you planning to deploy your next big project on AWS?

And you are struggling to understand how actually it works.

Well, by the end of this article, you will learn everything about how to deploy your Laravel 9 app on AWS APP Runner.

How to Install and Deploy Laravel 9 on AWS App Runner

Now, the first thing you should know before proceeding is you cannot deploy your Laravel 9 docker image directly on AWS App runner.

You need to push your image on AWS ECR and then only you could deploy it on App Runner.

Step 1# Create Docker Image for Your Laravel 9 Project

So, you already learnt how to create a docker image for your Laravel project here.

Let’s move to the next step.

Step 2# Install AWS CLI on your machine

First, you need to install AWS CLI on your machine. A simple way to do that is to follow instructions given here.

I use a mac, so here are the instructions for mac:

If you have sudo permissions, you can install the AWS CLI for all users on the computer. We provide the steps in one easy to copy and paste group. See the descriptions of each line in the following steps.

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
$ sudo installer -pkg AWSCLIV2.pkg -target /

Download the file using the curl command. The -o option specifies the file name that the downloaded package is written to. In this example, the file is written to AWSCLIV2.pkg in the current folder.

$ curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"

Run the standard macOS installer program, specifying the downloaded .pkg file as the source. Use the -pkg parameter to specify the name of the package to install, and the -target / parameter for which drive to install the package to. The files are installed to /usr/local/aws-cli, and a symlink is automatically created in /usr/local/bin. You must include sudo on the command to grant write permissions to those folders.

$ sudo installer -pkg ./AWSCLIV2.pkg -target /

After installation is complete, debug logs are written to /var/log/install.log.

To verify that the shell can find and run the aws command in you $PATH, use the following commands.

$ which aws /usr/local/bin/aws

$ aws --version aws-cli/2.7.24 Python/3.8.8 Darwin/18.7.0 botocore/2.4.5

If the aws command cannot be found, you might need to restart your terminal or follow the troubleshooting in Troubleshooting AWS CLI errors.

Step 3# How to Set Up a Local User for AWS CLI

Go to the IAM section of AWS console on the web.

Remember:

  1. Access Key — Programmatic Access is checked when you enter the name of a new user.
  2. Add a user policy that gives full access to ECS. The name of the policy is AmazonECS_FullAccess and AmazonEC2ContainerRegistryFullAccess
  3. Note down access key ID and Secret access key, as we'll have to use these later.

Let’s go back to our good ol’ terminal. Execute the configuration command in the terminal and enter your access key, secret access key, and preferred region. Skip the default output format for now.

aws configure

Verify the configuration by executing aws configure list the command.

Verifying AWS CLI Configuration

Congratulations! We have successfully setup AWS CLI with our local terminal. Now it’s time to push our Docker image to Amazon ECR.

Step 4# How to Create a Repo in ECR

For simplicity, I suggest keeping the same name as your project.

aws ecr create-repository --repository-name <repo_name> --region <region_name>

If you are not sure about region_name, put us-east-1. This would create your repo in US EAST-1 region. Once finished, it will prompt a JSON object like response in the terminal.

For a safer side, check AWS console and see if a repository is created:

Remember:

  1. You won’t see any images under repository. Because we haven’t pushed any image yet.
  2. Note down URI of your repo. We'll have to use it soon.

Step 4# Push Your Docker Image to AWS ECR

Now, moving forward, we need to understand two things.

For each interaction with AWS resource, we need to authenticate ourselves and second we need to keep this information handy.

Now please open your Docker GUI on your machine and login into your account. If you do not login into your docker account, you will see lots of errors.

For Docker to push the image to ECR, first we have to authenticate our Docker credentials with AWS.

The get-login-password is the preferred method for authenticating to an Amazon ECR private registry when using the AWS CLI. Ensure that you have configured your AWS CLI to interact with AWS. For more information, see AWS CLI configuration basics in the AWS Command Line Interface User Guide.

When passing the Amazon ECR authorization token to the docker login command, use the value AWS for the username and specify the Amazon ECR registry URI you want to authenticate to. If authenticating to multiple registries, you must repeat the command for each registry.

Important : If you receive an error or the get-login-password command is unavailable, ensure you are using the latest version of the AWS CLI. For more information on installing or upgrading to the latest version of the AWS CLI, see Installing the AWS Command Line Interface in the AWS Command Line Interface User Guide.

aws ecr get-login-password --region region_name | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com

Remember, you need to use the same region_name that you used while creating a repo.

Now you can push your image to the Amazon ECR repository you created in the previous section. You use the docker CLI to push images, but there are a few prerequisites that must be satisfied for this to work properly:

  • The minimum version of docker is installed: 1.7
  • The Amazon ECR authorization token has been configured with docker login.
  • The Amazon ECR repository exists, and the user has access to push to the repository.

After those prerequisites are met, you can push your image to your newly created repository in the default registry for your account.

To tag and push an image to Amazon ECR

List the images you have stored locally to identify the image to tag and push.

docker images

Output:

REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
hello-world latest e9ffedc8c286 4 minutes ago 241MB

Tag the image to push to your repository.

docker tag hello-world:latest aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

Push the image.

docker push aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository

Output:

The push refers to a repository [aws_account_id.dkr.ecr.region.amazonaws.com/hello-repository] (len: 1)
e9ae3c220b23: Pushed
a6785352b25c: Pushed
0998bf8fb9e9: Pushed
0a85502c06c9: Pushed
latest: digest: sha256:215d7e4121b30157d8839e81c4e0912606fca105775bb0636EXAMPLE size: 6774

If you want to learn more about how to push and pull docker images to ECR, you can refer to this AWS article.

Step 5# Deploy to AWS App Runner

Congratulations, you are done with all the hard work.

Now, just got to your AWS dashboard and search for app runner.

After that, click on create a service.

Once done, click on container registry.

Then Select Amazon ECR, here you will need to browse the image you just pushed in step 4.

After selecting the image, click continue

On the configuration window. Name your service.

Then select the amount of vCPU’s you think you will need to run this service along with the memory.

I would recommend adding all the ENV variables here. A lot of security experts will suggest you not to add them here.

But remember this is only for a tutorial purpose. If you are spinning up an enterprise level service. You will need to set your environmental variables into the secure vault. But this article is not about that.

After that, click on create service.

It will take a while but once its status change to live.

You will be able to see your service deployed

And if you face issues with the heath check this stakeoverflow issue.

If you want to know how to connect your RDS MySQL database with this service. Do comment below, and I will create another tutorial.

--

--

Lonare
Lonare

Written by Lonare

Imagination is the key to unlock the world. I am trying to unlock mine.

Responses (1)