Deploy C# App on AWS Part (2/3)

Ali Alhaddad
5 min readJan 22, 2024

We will continue from the previous tutorial, we will finish our dotnet project. Create a docker image, and push a docker image to aws ecr.

If you haven’t started the previous tutorial, I recommend checking out this one:

Photo by Fotis Fotopoulos on Unsplash

We will complete our REST API by incorporating update and delete methods into the SupabaseService.

For the update method, create a corresponding request with itemName, itemType, and importance properties. Similarly, design a corresponding response with id and success properties to handle the updating of records.

To handle the deletion of records, establish a delete response class with a success property.

Create corresponding update record request and response.
Create Delete Response

Update the SupabaseService by adding methods to handle record updates and deletions. Include UpdateRecordAsync for updating records with a request and ID, and DeleteRecordAsync for deleting records with an ID.

Next, enhance your ListController by implementing PUT and DELETE methods. These methods should utilize the UpdateRecordsAsync and DeleteRecordsAsync methods from the SupabaseService.

To update records in the List Controller, provide the ID and pass the necessary request.

Update endpoint

Delete records on the list controller using the ID.

Delete endpoint

Now, run your application and test your endpoints.

Verify your updated endpoint by changing a record with an ID of 4 and an itemName of “T-Bone Steak.”

You should see the record updated in your database.

Finally, test deleting the same record in the database with an ID of 4.

You should see your record with an ID of “4” deleted.

Congratulations! You now have a C# REST API. Let’s proceed to create a Docker image and push it to Amazon ECR.

We will incorporate Docker support into our project by adding the Dockerfile, not by adding a new item.

In the Dockerfile, replace the existing code with our customized Dockerfile.

Now, create a repository to store your Docker image for your API.

Next, create an IAM user responsible for pushing the image to ECR.

Go to IAM and add the necessary permissions.

  • Granting AmazonEC2ContainerRegistryFullAccess provides complete access to Amazon ECR.
  • To retrieve your credentials when logging in to AWS CLI, you will require SecretsManagerReadWrite, which grants full access to AWS Secrets Manager.

Generate an access key for an IAM user to use during login. Open Git Bash and navigate to the hidden .aws folder. If you have configured AWS CLI before, use the following command in VSCode to open your credentials file: code credentials.

Paste your credentials into the credentials file.

[todo-list-admin]
aws_access_key_id = <iam user access key>
aws_secret_access_key = <iam user secret access key>
output=json
region= <region your iam user is in(found in the top right corner in your aws console.)>

Otherwise, execute the aws configure command and input both the access key and secret access key when prompted.

“Create a build script for pushing a Docker image to a repository.

Open Git Bash and create a new PowerShell file with a .sh file extension.

touch build_and_push_ecr_image.sh

You can add it using Visual Studio as well.

We will break down each line of the script.

  • set -e This line instructs the script to exit and halt execution if any errors occur.
  • aws ecr get-login-password — region <aws region where repo is found> — profile <iam user profile> | docker login — username AWS — password-stdin <arn>.dkr.ecr.us-east-2.amazonaws.com/<repo name> Retrieve a temporary token from the AWS ECR registry to authenticate Docker on the ECR registry.
  • docker build -f ./Dockerfile -t <docker image name>:latest . The Docker command builds and tags an image using the current context.
  • docker tag <docker image name>:latest <arn>.dkr.ecr.us-east-2.amazonaws.com/<repo name>:latest Tag the locally built Docker image and the ECR repository image. This step is essential before pushing the image to ECR.
  • docker push <arn>.dkr.ecr.us-east-2.amazonaws.com/<repo name>:latest This command pushes the Docker image to the Amazon ECR registry.
  • Write-Host “AWS Ecr repo updated” Indicates that you have updated the repository in Amazon Elastic Container Registry (ECR).

We will name our local Docker image “todo-list-api,” matching the name of the ECR repository.

Name our IAM profile responsible for pushing Docker images as todo-list-api-admin.

The ARN is unique to our AWS account; each AWS account has a different ARN.

To execute the command in PowerShell, the execution policy needs to be changed to RemoteSigned.

  • Open powershell as administrator.
  • Run this command Set-ExecutionPolicy RemoteSigned and select A.
  • Now run the script by this command .\build_and_push_ecr_image.sh.

After executing the script, you should observe the updated ECR image in your repository under the “images” section, labeled with the tag “latest.”

Once you have pushed your Docker image to Amazon ECR, you simply need to define your task definitions and execute your task in ECR.

Here is the last part of the tutorial:

Here is our GitHub repository with the source code for reference:

--

--