This repository has been archived, and no additional maintenance, development, bug fixes, or security fixes should be expected. No issues or pull requests will be addressed on this repository going forward. This repository remains visible for historical reference, but we do not support or recommend usage of the contents of this repository. If you fork this repository, please be mindful to respect the licensing and copyright attribution of its contents, as archiving the repository does not change the ownership or permissions of the contents.
Hello App Runner - Node.js 🚀
This is the companion repository for the tutorial "Enabling continuous workflows for AWS App Runner service with persistency using AWS Copilot CLI" on the AWS Containers blog.

This repository contains a sample Node.js app and a simplified version of the tutorial blog post, to let you deploy and run it on AWS App Runner with Amazon DynamoDB. The sample app will be built and deployed via a configurable pipeline created in AWS CodePipeline.
You'll get an architecture like in the diagram below.

In step 1 and 2, we’re going to set up and deploy minimal required resources such as App Runner and DynamoDB to run the sample application. After that, we’ll have a configurable pipeline with AWS CodePipeline in step 3, then try "push-to-deploy" in step 4 to see all elements work together as expected. Beware that the tutorial may incur some charges especially for App Runner usage and DynamoDB table.
Note that we use CodePipeline instead of App Runner’s built-in managed pipeline to show "how to deploy from Dockerfile" and "how to add tests in release pipeline" in this tutorial. If you’d like to try "deploy from source code" after the tutorial, follow here to use App Runner’s managed pipeline which supports Node.js and Python today.
Building and deploying Next.js application on App Runner using Copilot CLI
Prerequisites
- Copilot v1.7.1 or later
- Docker Desktop (or Docker Engine on Linux environment)
- AWS CLI v1 or v2
jq
- A forked GitHub repository
- You will need your own forked repository to try "push-to-deploy" in the step 4 of this tutorial, so ensure you're using a forked repo, which you're allowed to push.
0. Clone forked repo
$ git clone https://github.com/<Your GitHub ID>/hello-app-runner-nodejs
$ cd hello-app-runner-nodejs
1. Create Copilot application
If the Copilot binary is missing or is outdated, follow the Copilot doc to install/update.
$ copilot --version
copilot version: v1.7.1 # Ensure you have v1.7.1 or later
$ copilot init
# NOTE:
## Choose "n" (No) if Copilot asked "Would you like to use one of your existing applications?"
## Then answer the Copilot's questions as follows
What would you like to name your application?: my-app
Which workload type best represents your architecture?: Request-Driven Web Service (App Runner)
What do you want to name this Request-Driven Web Service?: my-svc
Which Dockerfile would you like to use for my-svc?: ./Dockerfile
Show terminal outputs
Application name: my-app
Service name: my-svc
Dockerfile: ./Dockerfile
Ok great, we'll set up a Request-Driven Web Service named my-svc in application my-app listening on port 80.
✔ Created the infrastructure to manage services and jobs under application my-app.
✔ Wrote the manifest for service my-svc at copilot/my-svc/manifest.yml
Your manifest contains configurations like your container size and port (:80).
✔ Created ECR repositories for service my-svc.
All right, you're all set for local development.
1-1. Provision "test" environment
At this point you'll be asked if you want to proceed to provision "test" environment. Choose "y" (Yes) to proceed.
Note that Copilot first creates an environment to hold shared infrastructure between services in a Copilot application. Although App Runner does not require a VPC or an Amazon Elastic Container Service (Amazon ECS) cluster, Copilot creates these free-of-charge resources by default. This makes it possible to for you have seamless workflows within the same environment across App Runner or Amazon ECS, whichever you chose for the compute.
# NOTE:
## Since this process is a one-time operation for each environment to create all the shared resources,
## it will take several minutes to complete.
Would you like to deploy a test environment?: y
Show terminal outputs
Deploy: Yes
✔ Linking account <Your AWS account ID> and region <Your AWS region> to application my-app.
✔ Proposing infrastructure changes for the my-app-test environment.
- Creating the infrastructure for the my-app-test environment. [create complete] [77.3s]
- An IAM Role for AWS CloudFormation to manage resources [create complete] [19.1s]
- An ECS cluster to group your services [create complete] [10.1s]
- Enable long ARN formats for the authenticated AWS principal [create complete] [0.0s]
- An IAM Role to describe resources in your environment [create complete] [16.9s]
- A security group to allow your containers to talk to each other [create complete] [3.3s]
- An Internet Gateway to connect to the public internet [create complete] [17.8s]
- Private subnet 1 for resources with no internet access [create complete] [15.0s]
- Private subnet 2 for resources with no internet access [create complete] [15.0s]
- Public subnet 1 for resources that can access the internet [create complete] [15.0s]
- Public subnet 2 for resources that can access the internet [create complete] [15.0s]
- A Virtual Private Cloud to control networking of your AWS resources [create complete] [17.8s]
✔ Created environment test in region us-west-2 under application my-app.
Environment test is already on the latest version v1.4.0, skip upgrade.
[+] Building 57.9s (12/12) FINISHED
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 40B
=> [internal] load .dockerignore
=> => transferring context: 34B
=> [internal] load metadata for docker.io/library/node:12
=> [auth] library/node:pull token for registry-1.docker.io
=> [1/6] FROM docker.io/library/node:12@sha256:e38a48c584fde720a43b1bacfea1178e0900936cb8d0141630e5e35693ee6d08
=> [internal] load build context
=> => transferring context: 39.09MB
=> CACHED [2/6] WORKDIR /app
=> CACHED [3/6] COPY package*.json ./
=> CACHED [4/6] RUN npm install
=> [5/6] COPY . .
=> [6/6] RUN npm run build
=> exporting to image
=> => exporting layers
=> => writing image sha256:d7a37ddb71b53cc87ae3822f0e9e417f0b2e9fa23e3618879a08d334211c6de4
=> => naming to <your AWS account ID>.dkr.ecr.<your AWS region>.amazonaws.com/my-app/my-svc
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Login Succeeded
Using default tag: latest
The push refers to repository [<your AWS account ID>.dkr.ecr.<your AWS region>.amazonaws.com/my-app/my-svc]
5e43bcce0ae2: Pushed
6858c871c249: Pushed
31aa9a1a0394: Pushed
9e30aae11a95: Pushed
f922dd0feff4: Pushed
390b35f36fa5: Pushed
f26dcd4a0423: Pushed
9271d99f30cd: Pushed
b257e69d416f: Pushed
1e9c28d06610: Pushed
cddb98d77163: Pushed
ed0a3d9cbcc7: Pushed
8c8e652ecd8f: Pushed
2f4ee6a2e1b5: Pushed
latest: digest: sha256:3a73cbda7313d2a248e0cab08fb2f91eeb1e94f6d9da30a9dfb77063c4459f8e size: 3266
✔ Proposing infrastructure changes for stack my-app-test-my-svc
- Creating the infrastructure for stack my-app-test-my-svc [create complete] [343.5s]
- An IAM Role for App Runner to use on your behalf to pull your image from ECR [create complete] [18.6s]
- An IAM role to control permissions for the containers in your service [create complete] [16.3s]
- An App Runner service to run and manage your containers [create complete] [315.2s]
✔ Deployed my-svc, you can access it at https://<random string>.<your AWS region>.awsapprunner.com.
1-2. Check deployed app in web browser
Open the endpoint of your App Runner service in your preferred web browser. You can find the endpoint url at the end of the previous command's output (it's in the form of https://..awsapprunner.com).
Once you opened it, you'll see the app failed to load data. This is because there is no DynamoDB table provisioned at this point.

Okay then, let's set up a DynamoDB table in the next step.
2. Set up DynamoDB table
$ copilot storage init
What type of storage would you like to associate with my-svc?: DynamoDB (NoSQL)
What would you like to name this DynamoDB Table?: Items
What would you like to name the partition key of this DynamoDB?: ItemId
What datatype is this key?: String
Would you like to add a sort key to this table?: N
Show terminal outputs
Only found one workload, defaulting to: my-svc
Storage type: DynamoDB
Storage resource name: Items
Partition key: ItemId
Partition key datatype: String
Sort key? No
✔ Wrote CloudFormation template for DynamoDB Table Items at copilot/my-svc/addons/Items.yml
Recommended follow-up actions:
- Update my-svc's code to leverage the injected environment variable ITEMS_NAME.
For example, in JavaScript you can write `const storageName = process.env.ITEMS_NAME`.
- Run `copilot deploy --name my-svc` to deploy your storage resources.
Before proceeding, it's helpful to understand that the command copilot storage init does not actually provision AWS resources, and it only creates configuration files in your local workspace, more specifically ./copilot/my-svc/addons/Items.yml in this case. So you will provision it using the copilot deploy command in the next step.
You may also realized at the end of the terminal output above, that your application will be able to refer the DynamoDB table name via an environment variable named ITEMS_NAME that Copilot will inject into your App Runner service automatically.
Then, let's provision the dependent resources (a DynamoDB table and an IAM role in this case) and update your service to use the new environment variable "ITEMS_NAME."
$ copilot deploy --name my-svc
Show terminal outputs
Only found one environment, defaulting to: test
Environment test is already on the latest version v1.4.0, skip upgrade.
[+] Building 58.6s (12/12) FINISHED
=> [internal] load build definition from Dockerfile
=> => transferring dockerfile: 40B
=> [internal] load .dockerignore
=> => transferring context: 78B
=> [internal] load metadata for docker.io/library/node:12
=> [auth] library/node:pull token for registry-1.docker.io
=> [1/6] FROM docker.io/library/node:12@sha256:e38a48c584fde720a43b1bacfea1178e0900936cb8d0141630e5e35693ee6d08
=> [internal] load build context
=> => transferring context: 1.21MB
=> CACHED [2/6] WORKDIR /app
=> CACHED [3/6] COPY package*.json ./
=> CACHED [4/6] RUN npm install
=> [5/6] COPY . .
=> [6/6] RUN npm run build
=> exporting to image
=> => exporting layers
=> => writing image sha256:fa2bde89ceef586fb009e3ea5c1fb746e3c570752015641098a8a25b399dc535
=> => naming to <your AWS account ID>.dkr.ecr.<your AWS region>.amazonaws.com/my-app/my-svc
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Login Succeeded
Using default tag: latest
The push refers to repository [<your AWS account ID>.dkr.ecr.<your AWS region>.amazonaws.com/my-app/my-svc]
33783c942afd: Pushed
782e979b18c5: Pushed
31aa9a1a0394: Layer already exists
9e30aae11a95: Layer already exists
f922dd0feff4: Layer already exists
390b35f36fa5: Layer already exists
f26dcd4a0423: Layer already exists
9271d99f30cd: Layer already exists
b257e69d416f: Layer already exists
1e9c28d06610: Layer already exists
cddb98d77163: Layer already exists
ed0a3d9cbcc7: Layer already exists
8c8e652ecd8f: Layer already exists
2f4ee6a2e1b5: Layer already exists
latest: digest: sha256:d4ca0725c7404bdd22f3dd81399ce5dad8503bf550d8b5f3063161e26a7f2aed size: 3266
✔ Proposing infrastructure changes for stack my-app-test-my-svc
- Updating the infrastructure for stack my-app-test-my-svc [update complete] [351.9s]
- An Addons CloudFormation Stack for your additional AWS resources [create complete] [60.8s]
- An IAM ManagedPolicy for your service to access the Items db [create complete] [15.7s]
- An Amazon DynamoDB table for Items [create complete] [31.9s]
- An IAM role to control permissions for the containers in your service [update complete] [15.6s]
- An App Runner service to run and manage your containers [update complete] [261.7s]
✔ Deployed my-svc, you can access it at https://<random string>.<your AWS region>.awsapprunner.com.
2-2. Seed initial data into the DynamoDB table
The provisioned DynamoDB table is still empty of course, so let’s seed it by executing the following command.
If you don't have npm in your workspace, then just execute ./seed/seed.sh instead. Also note that the script requires the jq command to run successfully.
$ npm run seed
Show terminal outputs