Deploy to AWS with GitHub Actions and AWS CDK
Tags: aws, devops, cdk, github
Published: 2023-02-06 07:51:56

![](https://cdn-images-1.medium.com/max/2000/0*AX6wGWj7Jz0Zmu2A.jpg)

![[https://www.ranthebuilder.cloud/](https://www.ranthebuilder.cloud/)](https://cdn-images-1.medium.com/max/2000/0*8GNeb3AgI2WAn85q.png)

**This blog was originally published on my website [“Ran The Builder”.](https://www.ranthebuilder.cloud/)**

This blog explains how you can use AWS CDK to deploy to your AWS account using GitHub Actions CI/CD pipeline.

Feel free to use the code snippets as you please.

Click [here](https://github.com/ran-isenberg/aws-lambda-handler-cookbook) and [here](https://github.com/ran-isenberg/aws-lambda-handler-cookbook/blob/main/.github/workflows/serverless-service.yml) to view a fully working GitHub AWS Serverless service template that uses the same snippet.

## Going Over The Job Steps

Let’s go over the CI/CD pipeline steps:

 1. Environment setup

 2. AWS account setup

 3. Run linters & unit tests

 4. Deploy stack

 5. Run E2E tests

 6. Destroy stack (for dev stack only)

## Environment Setup

### Python & Node Setup
{% gist https://gist.github.com/ran-isenberg/4ce60a5dff6c6474cafed5721a1db700.js %}
This snippet will setup Python 3.9 and Node v16 in the CI/CD runner.

### Setup AWS CDK
{% gist https://gist.github.com/ran-isenberg/5d230eb4e778fcb32c977ef414585eac.js %}
It’s recommended to use latest pip and AWS CDK version.

In line 4, you should install your Python dependencies with either pip, pipenv, or poetry, depending on your weapon of choice to manage Python dependencies.

These dependencies include development dependencies (pytest, linters, etc.) and service runtime dependencies.

## Setup AWS Secrets

![](https://cdn-images-1.medium.com/max/4936/1*82lRtrp7oc_iCQbkWOWAdQ.png)

You must set up GitHub’s repository secrets for this snippet to work.

Under Settings/Secrets/Actions, add ‘AWS_SECRET_KEY’ and ‘AWS_ACCESS_KEY’.

These secrets are used in a predefined IAM role you created for your CI/CD process.

This is a simple example; however, for security reasons, it is best to use an SSO solution (out of the scope of this guide).

The yaml config:
{% gist https://gist.github.com/ran-isenberg/b53a047b9fbd4d776e736808ab910ea3.js %}
In line 7, choose your AWS region of choice.

## Linters & Unit Tests

Right before deployment, it is recommended to run linters such as:

 1. pylint/flake8

 2. pre-commit checks

 3. yapf/black

 4. Code complexity checks (radon/xenon)

See the linters example and configuration at my GitHub template [here](https://github.com/ran-isenberg/aws-lambda-handler-cookbook/blob/main/.github/workflows/serverless-service.yml) and the makefile that runs the linters commands [here](https://github.com/ran-isenberg/aws-lambda-handler-cookbook/blob/main/Makefile).

Once the linters finish, run unit tests as a first service logic gate.

## Deploy Time
{% gist https://gist.github.com/ran-isenberg/1187c4dd5731c773b90ab66300c90f3d.js %}
In line3, you must set the correct path to your CDK ‘app.py’ file.

I usually put all the CDK project files in a ‘cdk’ folder instead of the root project path.

## E2E Tests

Once deployment is completed, you should run your E2E tests and make sure your service runs properly on AWS. You can use pytest to create REST API requests or other triggers to test your deployed service.

## Destroy Stack

This step is relevant only for local stack or pull requests where you want to destroy the stack in the end.
{% gist https://gist.github.com/ran-isenberg/eb2ecab8bec2e92f65077d41e5f7abcc.js %}
As in the deployment stage, make sure set the correct path to your CDK ‘app.py’ file.

## The Full Flow
{% gist https://gist.github.com/ran-isenberg/5d230eb4e778fcb32c977ef414585eac.js %}