Single Repo: AWS CDK, Route53, APIGateway, Lambda, Cloudfront, Flask & Nuxt

Tech Rap
7 min readMay 23, 2021


TL;DR Step-by-step tutorial building a full-stack application using AWS-CDK, Flask & Nuxt using a unified code repository for all components: frontend, backend, and infrastructure.


Every business is looking to accelerate product-driven businesses, many would say it is the most stable determinant of success.

For the last five years, as a CTO of a cloud-based startup, we worked with several cloud infrastructure deployment frameworks including Terraform, Cloudformation, and while we also programmed in different cloud application development frameworks like Django, Flask, Asyncio.

Having two work streams of cloud infrastructure and cloud application is the most common HR structure within R&D teams today; however, it is an antithesis of shift left methodology, not just from the security perspective. Furthermore, it creates misunderstanding in the role & responsibilities between operations and engineering.

The most common outcome of this misunderstanding is that most organizations have two code bases — one for the application and another for the infrastructure.

This paper will share my first attempt to merge both application code and infrastructure code into a unified pipeline utilizing AWS CDK.

Shift left the deployment pipeline


The AWS Cloud Development Kit (AWS CDK) is an open-source software development framework to define your cloud application resources using familiar programming languages.

It helps R&D teams deploy cloud applications and infrastructure faster and with confidence, using a unified, shared pipeline and codebase.

AWS CDK is available in four different languages: Typescript, Python, .NET and Java. They publish documentation and examples in Typescript, however, I decided to use Python as it’s my preferred language.

Today alternatives to AWS CDK are CloudFormation or Terraform, forcing developers to learn these technologies in order to take ownership of the cloud infrastructure. For example, developers need to learn Arcane templating skills for CloudFormation or the Hashicorp Configuration Language (HCL) for Terraform.

As a CTO, I was unable to find candidates with the skill set to develop applications and deploy them in production.

Having the same language for applications and infrastructure with AWS CDK allows my developers to quickly start making changes to the infrastructure with a familiar language helping to reduce the DevOps team’s dependency dramatically.


To demonstrate a real-world environment, I used several technologies to build a full-stack environment, including backend and frontend components. Although all the technologies are combined into the same code-base, let’s take a look at each layer separately. It will help us create a baseline for what we are already familiar with: the infrastructure and application layers.

Application Layer

Nuxt — A free and open-source web application framework based on Vue.js which functions both as in-browser single page application (SPA) views as well as server-rendered web.

Flask — A micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries except Flask itself.

Infrastructure Layer

Route53 — AWS managed Domain Name System (DNS) service

Lambda — AWS managed serverless compute service that lets you run code without provisioning or managing servers. I specifically used Docker Image to pack the python code and its dependencies.

Cloudfront — AWS managed a content delivery network service.

S3 — AWS managed object-based storage service.


This tutorial requires the installation of the AWS CDK library. The CDK is written in Typescript and requires node and npm to be installed.

  1. Install AWS CDK
npm install -g aws-cdk

2. Clone my example project

git clone

3. Create & activate virtual env

python3 -m venv awscdk
. awscdk/bin/activate
pip install -r requirements.txt

Code Walkthrough

Directory tree

A quick look at AWS CDK project will highlight how easy it is:

  • The term ‘AWSGEN’ references to file that generated automatically by AWSCDK
├── # AWSGEN
├── backend # The backend code of the project
│ ├──
│ ├── Dockerfile
│ └── requirements.txt
├── cdk.json # AWSGEN
├── cdk.out # AWSGEN
├── frontend # The frontend code of the project
│ ├── package.json
│ └── pages
│ ├── about.vue
│ └── index.vue
├── package-lock.json
├── requirements.txt
├── # AWSGEN
├── source.bat # AWSGEN
└── spa_flask_aws_cdk # AWSGEN
├── spa_flask_aws_cdk.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── requires.txt
│ ├── SOURCES.txt
│ └── top_level.txt
6 directories, 20 files

The most important file in our project is; it contains all the Infrastructure as a code that we will use to build our infrastructure.

DNS & Certificates

Let’s start with making sure our users can browse the application using a domain name. I assume that there is already an existing root hosted zone — ROOT_ZONE; in our case, it will be

We will create an already existing hosted zone as a reference in my AWS account, either by another CloudFormation Stack or by another CDK App.

To do so, I will use one of the CDK Constructs fromXXX() methods — from_hosted_zone_attributes. Creating the reference by using hosted_zone_id and zone_name.

IMPORTANT — unlike all classes we will create during the next steps — creating a reference by using formXXX() method will not create a component, it is just a reference.

# File: spa_flask_aws_cdk_stack.pyROOT_ZONE = 'saasment'# Create interface to already existing hosted zone
root_hosted_zone = route53.HostedZone.from_hosted_zone_attributes(self,

Once we have the initial starting point, we can create our fully independent stack, starting with:

  1. Creating a new hosted zone ‘’
  2. Creating an NS record in the root hosted zone to point our new hosted zone
  3. Creating & validating certificates in ACM for both frontend ‘’ and backend ‘’ domains
# File: spa_flask_aws_cdk_stack.pyZONE = ''
# Create a new zone
hosted_zone = route53.HostedZone(self,
# Create NS record in the root zone, it will point to our new zone
# Create & validate SSL certificates for frontend & backend domains
front_certificate = acm.Certificate(self,
backend_certificate = acm.Certificate(self,

Static Frontend

One of the most exciting features of AWS CDK is the option to use standard development/build/deployment patterns.

In this case, I utilized ‘BundlingDockerImage’ pattern to deploy into S3 Bucket static HTML pages generated from the Nuxt project (located at ‘./frontend’). As you can see there, I don’t create any docker registry to support the S3deployment — it is fully transparent to us.

Having the build and deployment of the application layer inside the infrastructure highlights an essential advantage of using AWS CDK, it helps us have immutable and idempotent components across all our microservices, infrastructures, and applications.

Now we will create a CloudFront distribution that will fetch the content from the S3 static pages deployment and DNS record with the CloudFront as a target.

Our frontend infro + app layers
# File: Create bucket for static pagesstatic_bucket = s3.Bucket(self,
# Upload nuxt static pages to S3
build_cmd=['bash', '-c', ' && '.join(['ls -la ', 'npm install', 'npm run build', 'npm run generate', 'cp -r /asset-input/dist/* /asset-output/'])]
s3deployment = s3deploy.BucketDeployment(self,
# Init view policy
view_policy = cloudfront.ViewerProtocolPolicy.REDIRECT_TO_HTTPS
# Init error responses
error_403 = cloudfront.ErrorResponse(http_status=403, response_http_status=200, response_page_path='/index.html')
error_404 = cloudfront.ErrorResponse(http_status=404, response_http_status=200, response_page_path='/index.html')distribution = cloudfront.Distribution(self,
'origin': origins.S3Origin(static_bucket),
'viewer_protocol_policy': view_policy
error_responses=[error_403, error_404])
# Init alias
front_target = route53.RecordTarget.from_alias(targets.CloudFrontTarget(distribution))
# Create A & AAA records to point cloudfront and serves frontend
frond_r53_record = route53.AaaaRecord(self,

Serverless Backend

Flask is THE most popular Python web application development framework because of its simplicity, extensibility, and community. For the same reasons, AWS Lambda is THE most popular function as a service framework.

Bundling both to support our backend enables us to create low cost, pay-per-execution, and instant scale architecture.

Similar to the frontend S3 deployment, I am using an existing pattern, in this case — DockerImageFunction. Instead of uploading a ZIP file to my lambda function and losing time on structuring all the dependencies, I can use Dockerfile to build it in a standalone environment.

In the backend, instead of using Cloudfront, I will use APIGateway to forward the HTTP request to the flask application.

Our backend infra + app layers
# File: Pack backend code as docker
backend = lambda_.DockerImageFunction(self,
# Create API Gateway instance
apigwdomain = apigateway.DomainNameOptions(
# Trigger lambda by APIGateway
apigw = apigateway.LambdaRestApi(self,
backend_target = route53.RecordTarget.from_alias(targets.ApiGateway(apigw))backend_alias = route53.AaaaRecord(self,


Probably the most famous leapfrog is the mobile revolution, which put phones in the hands of millions of people while allowing developing nations to skip directly to mobile phones without the need to invest in landline infrastructure.

From my perspective, Cloudformation and Terraform are the landlines infrastructure of the Cloud Infrastructure technology. Any new business looking to build an application or any legacy business that will migrate its on-prem infrastructure to the cloud using AWS CDK is probably the right way.

Link to Gitlab repository —



Tech Rap

You live at least once

Recommended from Medium


See more recommendations