Deploying a Containerized Web App with AWS Copilot | by Edward Krueger | Towards Data Science

By: Edward Krueger and Dylan Rossi
This article will show you how to deploy a containerized web application to AWS using AWS Copilot. Copilot greatly simplifies deployment to AWS and provides a unified framework to provision architecture, build images and deploy containers as either public or private services. Additionally, Copilot lets us create either a load-balanced service or a requests-driven service according to our needs without requiring the user to change the application, configuration, or architecture.
Will walk through the steps to create an Identity Access Management account, more commonly known as an IAM on AWS. Once this account is created, we will connect to both the AWS and Copilot CLI on a local machine.
Note: You will need docker running to have this work successfully.
To create an IAM account on AWS, you must first have already signed up for an AWS account. If you have not already done so, do so here.
Once you have your account created, navigate your way to the AWS Management Console as the Root User.
There will be a dropdown labeled “All Services.” Click this, and it will open a massive list of all the different services that AWS has to offer. IAM will be located under the Security, Identity, & Compliance section on the top right.
When you arrive at the IAM dashboard, there will be an extensive array of things you can click.
You will first want to create a “User Group” with administrator access.
To do so, click on “User Groups.”
If you already have created a user group in the past, they will be listed here. We are going to assume you do not have any created and make one together.
Start by clicking the “Create Group” button.
You will then be asked to give the new user group a name. You will call it “administrators.”
For now, you can skip adding users to the group, as you will do that after creating the group.
Scroll down a bit further, and you will be brought to the “Attach Permissions Policies” section. Inside the text box, type “admin” and then hit enter. This will filter the results a bit more and allow us to find the “AdministratorAccess” policy name a bit quicker. Go ahead and check the box next to this policy.
Finally, click “Create Group.”
You have now successfully created the “administrators” user group.
Now that we have our administrators group created, we need to create a new user and add them to the group.
Head back to the main IAM page and click on the “Users” link under IAM resources.
When the page loads, you will click the “Add User” button.
For the username, we will be calling it “copilot” since that is what we will be connecting this IAM account to.
We then want to check the box next to “Programmatic Access” as we will require this user to use the AWS CLI.
Once set, click permissions to continue to the next page.
You can add our new user to our freshly created “administrators” user group on this page. Do this by checking the box next to the “administrators” user group.
Once the box is checked, you can click to the next page, tags. Tags are just some additional information you can add to the user for quick lookup or information gathering. We don’t care about adding any tags at the moment, but you can do so if you like.
After you are satisfied with the tags you added (or didn’t add), head to the next page to review the new user information. If everything looks good, click “create user” to finish creating our new user.
You should receive a green message stating that the user was successfully created and some information about the user that we will need later — specifically, the access key ID and the Secret access key. Either stay on this page or save this information for later use.
After successfully creating the user, we can now install the AWS CLI and connect the IAM account we just created.
To install the AWS CLI, open up Terminal (for Unix machines) or git-bash (any other command-line tool) for Windows.
From there, run one of these commands depending on your Operating System:
curl “https://awscli.amazonaws.com/AWSCLIV2.pkg" -o “AWSCLIV2.pkg”sudo installer -pkg AWSCLIV2.pkg -target /
curl “https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o “awscliv2.zip”unzip awscliv2.zipsudo ./aws/install
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi
It will then ask for the password of your computer and install it.
Once completed, you have successfully installed the AWS CLI to your local machine.
Now that you have the AWS CLI installed on our machine, you can now push an example web application using Copilot.
To get us started, I created a repository with a containerized web application already coded out. You can download the repository here.
Once cloned to your machine, go ahead and open this directory in terminal/git-bash.
While inside of the spam-detector repository in terminal. You are then able to prepare this web application for deployment using Copilot.
This is done with the simple command
copilot init
As it runs, you will be prompted to answer some questions about the project:
Would you like to use one of your existing applications — No (If you have never used Copilot before, you may not see this prompt)
What would you like to name your application — spam-detector-app (can call it whatever you want)
Which workload type best represents your architecture? — Load Balance Web Service or Request Driven Web Service if you want it public-facing.
What do you want to name this load balanced web app? — spam-detector-app (or whatever you like)
Which dockerfile would you like to use for spam-detector-app? — ./Dockerfile.copilot
After all these questions have been answered, it will then start to deploy your application!
Once this instance has been deployed, you will get a prompt asking if you would like to deploy a test environment.
All a test environment is, is another instance of the application that uses fewer resources. This is useful if you will be doing a lot of testing and changing your project before deploying it fully.
This will then start to build our environment and then the docker container. Once these are created successfully, Copilot will then push the docker container to ECR, boot the application with ECS, and give us a URL to test the application.
Now that the build is completed and deployed, we can head to the URL we were returned in the terminal. You can either copy/paste this URL into your browser or cmd-click the URL to open it automatically.
The application should load and have some text on the window that says:
“The app is up”
To test that the POST request works as well, we will be using the free application, Postman.
If you do not have Postman installed, you can download it here.
With Postman open, you will take the URL we recieved and paste it into the “Enter request URL” text box. Then you will add “/predict” to the end of the URL as that is the route we have created to handle POST requests. You will also want to make sure that the type of request is set to POST.
Once the URL is correctly set and POST is selected, click on the “body” tab.
The “body” tab will contain the data we want to send to our POST endpoint.
The type of data we want to send is JSON. So select “raw” from the given selections and then change the type from “Text” to “JSON” using the dropdown menu.
You can then populate the JSON below. We will simply be sending a single key/value pair.
Finally, you can click send and wait for a response from our endpoint.
If you followed along correctly, you should have received back the result “ham”, thus confirming that our POST endpoint is working as expected.
Copilot also makes shutting down your deployment relatively easy. Instead of going into AWS and shutting down each service individually, you can shut everything down at once via the AWS CLI.
So heading back to your terminal inside the same app directory, enter the following command to shut down our deployment:
copilot app delete
You will be prompted to confirm that you wish to delete the app, simply type “y” and then enter to confirm.
After a few minutes your app will be successfully shut down!
As you can see, AWS’ Copilot allows the deployment and shutting down of a containerized web app to be as simple as typing a few commands into your terminal.
If you would like to see a video version of this process, check out my YouTube Channel for this and more.