powered by open knowledge
During the last exercise, we used a managed service called AppRunner to easily deploy our backend service and make it scalable. The AppRunner service has relieved us of the task of setting up and connecting a number of internal AWS components, saving us a lot of time and effort.
In this exercise, we will get to know an additional feature of that the AppRunner service: The ability to automatically detect changes to the deployed backend service and to trigger a redeployment.
So, let us change our backend service! We will expand our backend with a permanent NoSQL data storage (DynamoDB) instead of using an in-memory storage.
During this exercise you will learn how to:
- Log into the AWS Cloud via AWS management console
- Create a DynamoDB table
- Update backend service and related docker image
- Update docker image in ECR
- Change configuration of AppRunner
Note: This step is only necessary if you are not already logged in.
First of all we have to connect to the AWS Cloud:
- Go to https://console.aws.amazon.com/console/home
- Select "IAM user sign in" if not already preselected
- Use the user information provided to you to sign in
- Account ID: [WORKSHOP ACCOUNT ID]
- IAM username: [YOUR ANIMAL]
- Password: [YOUR ANIMAL PWD]
After successfully logged in you should see the AWS CLoud main dashboard.
Note: Make sure the region in the upper right corner of the browser window is set to "Europe (Frankfurt)" aka eu-central-1.
To create a DynamoDB table we have to call the DynamoDB dashboard first. There are several ways to do so:
- use global quick search and lookup for "DynamoDB"
- select DynamoDB service from service overview via "Database"
- select DynamoDB service from "recently visited" (if available)
Use the DynamoDB dashboard to create and configure a new DynamoDB table:
- Click "Create table" (left border of the dashboard). This will lead you to the "Creat table" page of DynamoDB.
- Fill in the following values (and leave everything as is):
- Table name: use the prefix of your animal, e.g. dog-dynamo-table.
- Partition key: use a partition key called "pk" with type string
- Sort key: use a sort key called "sk" with type string
- Click "Create table" button
You should see the DynamoDB dashboard tables overview when everything done right.
Next we have to update our backend service to be able to access the DynamoDB table we
have just created. The backend service itself is already able to access DynamoDB tables
in general (see TopicRepository class in app/src/main/java for details). But of course we
have to configure which table to use:
- Open
application.propertiesin app/src/main/resources. - Replace
YOUR_DYNAMO_DB_TABLEwith your DynamoDB table name.
After we have changed the configuration, we can rebuild the backend service:
-
Open a new terminal in Codespaces using the terminal tab.
-
Make sure you are in the
appfolder using thepwdcommand. -
Build a new version of the backend service using maven:
mvn clean package
As you may remember from the last exercise, we first have to connect to AWS via AWS cli to be able to push the docker image with our backend service to the Elastic Container Registry later on.
Note: Codespaces remembers your AWS connection. If you see your login information
already filled in just confirm by pressing [ENTER].
-
open a new terminal in Codespaces using the terminal tab
-
Configure your AWS connection using the
aws configurecommand that will ask you step by step for all relevant information to sign in to AWS via cli:$ aws configure AWS Access Key ID [None]: [YOUR AWS ACCESS KEY] AWS Secret Access Key [None]: [YOUR SECRET ACCESS KEY] Default region name [None]: eu-central-1 Default output format [None]: json
-
Make an AWS CLI call to test connection, e.g. asking for the caller identity (aka YOU):
$ aws sts get-caller-identity
The output should look something like ...
{ "UserId": "AIDASVQKHPCR2LKF6UHNT", "Account": "183631313059", "Arn": "arn:aws:iam::183631313059:user/dog" }
Now, we are able to create a new docker image containing the updated version of our backend service and use AWS cli to push it to our elastic container registry:
- Go to https://console.aws.amazon.com/console/home
- Log into your AWS account via web console using your credentials (if not already signed in).
- Choose AWS ECR service via global search or service overview (Container).
- Choose your ECR service
- Click "View push commands" and follow the instructions using the Codespace terminal
- authenticate your Docker client to your registry
- build docker image
- tag your docker image to be able to push it to the repository
- push docker image to ECR
- Check the ECR for the new image with tag "latest"
- Goto the AWS web console
- Choose ECR service and select your ECR
You should see (at least) two images inside your repository because we updated an already existing container image. One of them should have the image tag "latest" and a timestamp (pushed at) corresponding to the current time.
When we have created our AppRunner service we set up the deployment mechanism to "automatic". This results in the container image that has just been pushed to ECR being automatically redeployed. To check this, just take a look at AppRunner service dashboard:
- Go to https://console.aws.amazon.com/console/home
- Log into your AWS account via web console using your credentials (if not already signed in).
- Choose AppRunner service via global search or service overview (Compute).
- Choose your AppRunner service instance
- Scroll down the App Runner event logs.
There should be several log entries indicating a redeployment. Don't worry if you see a message like
"Your application stopped or failed to start. See logs for more information. Container exit code: 1"
This is because we do not have connected our managed backend service with the dynamoDb table so far!
Ok, let's fix this and connect our backend service with our dynamoDB table. To do so we make use of the AppRunner configuration feature that allows to configure the managed services via environment variables:
- Choose your AppRunner service instance (as explained above)
- Goto "Configuration" tab of your service
- Click "Edit" button to change configuration
- Scroll down to "Configure service"
- Click "Add environment variable"
- Environment variable name: DYNAMODB_TABLE
- Environment variable value: name of your DynamoDB table
- Click "Save changes" button
Switch back to "Logs" tab of your AppRunner service instance and observe the new log entries related to the service update (this may take several minutes).
There are two possibilities to check if everything worked fine:
Version 1: Open the DyanmoDB dashboard. Select your table and click "Explore table items". If you "Run" a new scan, you will see the table items.
Version 2. Call one of the backend service APIs via AppRunner service from a browser of your choice, e.g. ...
http://[APP_RUNNER_SERVICE_ADDRESS]/usersAfter successfully being deployed it is time to finish this exercise and connect our frontend to the AppRunner service:
-
Goto AppRunner service page and select your AppRunner instance
-
Copy the AppRunner service default domain
-
Go to the typescript file showcases.ts that can be found in ./frontend/src of your frontend project.
-
Replace the fake URL baseUrl: http://todo.invalid of the entry "3 - PaaS" with the valid URL of the backend. The result should look like.
export const SHOWCASES: ShowcaseConfig = { ... "3 – PaaS": { baseUrl: "http://[APP_RUNNER_SERVICE_ADDRESS]", }, ... }
To test the AppRunner service and the connection from our frontend to it - including the DynamoDB table access - open the ok-forum app in a browser of your choice (URL see above) and select the showcase "3 – PaaS" in the dropdown. Check if the ok-forum app works properly by clicking through the forums categories, topics and discussions. Add some topics and check the DynamoDB table for corresponding changes.
Note: The AWS management console may display DynamoDB changes with a (huge) delay. So don't worry if you can not find your new entries immediately. Also check the log events of your AppRunner service instead.
With the help of the AppRunner service we managed to redesign (add a DynamoDB table) and redeploy our backend service within minutes instead of hours. The only thing we had to do was to update the backend service itself and its related container image and push the image to the elastic container registry (ECR) afterwards.
Next we will make use of serverless functions to react to cloud events.
NO SEVER NO STRESS, SERVERLESS ...