Python App Deploy to AWS App Runner using GitHub Actions
Tags: aws, python, docker, github
Published: 2022-04-15 15:46:10


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.

In this scenario, we going to deploy a simple python application via AWS App Runner. We use the Github action CI/CD tool.

First of all, go through the Architectural diagram.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nhq6fompob55ekxv5x9d.png)

You can follow these steps.

## Creating IAM Role

* In the AWS management console search App Runner service and go- 
  to service. After clicking **Create role** button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w4uuh6kluxxi074ivv9p.jpg)

* Next Trusted entity type as Custom trust policy and paste 
  following policy.
```
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "build.apprunner.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbf6hdfrwy6029nwnhqg.jpg)
  After that click, the **Next** button.

* Next select policy as AWS Managed 
  "AWSAppRunnerServicePolicyForECRAccess" policy and click next 
  button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bd1yg07otcldm3zw85c.jpg)

* Give the Name and Description(Optional) for the Role and 
  click **Create role** button.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/az2q5qksoa745o10n0ib.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynyk11c76kj25ar7hdu3.jpg)

## Create a GitHub repository and push the following files. You can get sample files using the following repository.

> Repository [URL](https://github.com/sanju2/pythonapplication)
  Github get-started guide [guide](https://www.w3schools.com/git/git_remote_getstarted.asp?remote=github)

Use the following folder structure.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g3sgh9aosdpg5mo36p4q.jpg)

## Next, add Secrets inside GitHub repository settings.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z073blktp9rwk8v16gub.jpg)

Create the following repository secrets.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zuroejippvbh7xvrf7j9.jpg)

1. You can easily create **AWS AWS_ACCESS_KEY_ID** & **AWS_SECRET_ACCESS_KEY** using creating new IAM role. Don't create access keys for the root user.
2. **AWS_REGION** is your AWS region. Ex - us-east-2
3. **ROLE_ARN** means the previous step created IAM role ARN. You can easily copy it go to the app-runner-sr role.

## Create ECR Private repository.

In the AWS management console find AWS App Runner. After that create ECR private repository like the following.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q6ckpt5dspuadz7rzuxu.jpg)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4jmny5bvx9n867xv5wet.jpg)

## Next, go to the local repository or remote repository and change files, and push it. 

After inside the Github repository and select the action section. you can see the pipeline is running & after a few minutes it becomes completed.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7hies8c4ye4aob4dtrfg.jpg)

AWS App Runner dashboard you can see your application is up and running.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vwy0itnr2vt5zdutj3ib.jpg)

Finally, in the Service overview section Copy the Default domain and paste the web browser. You can see your running application.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0o6pzkk508kn1he0x8vb.jpg)

AWS App Runner References - https://docs.aws.amazon.com/apprunner/latest/dg/what-is-apprunner.html

Thanks for reading the Article.