Automating CI/CD on AWS CodePipeline (using TypeScript and AWS CDK) Part-1

Julia R
6 min readAug 18, 2023

--

This series mainly talks about CI/CD Pipeline on AWS CodePipeline. More can be found in Github.

Requirements:

  • Previous articles on AWS S3 hosting Static Website (article 4 & 10)
  • TypeScript
  • AWS CDK
  • AWS S3
  • AWS ECS
  • AWS CodeCommit
  • AWS CodeBuild
  • AWS CodeDeploy
  • AWS CodePipeline

The 1st article (part 1) discusses how to manually build a CI/CD pipeline for deploying Static Website in S3.

The 2nd article (part 2) discusses how to integrate and automate the building process for CI/CD + Static Website using AWS CDK and TypeScript.

The 3rd article (part 3) shares how to automate Terraform Codes deployment in CI/CD system.

The 4th article (part 4) shares a more complex case when CI/CD deploys ECS (Container).

If you are not interested in the concepts of Code Pipeline, Terraform and CDK, please skip this section.

  1. Content (Codes):

Code Pipeline is a pipeline for codes. It helps us focus more on application development. Any change in the source code will trigger the pipeline to build/test the codes and deploy a new version. The attribute of ‘continuous integration and continuous delivery/continuous deployment’ means a fast, efficient and automatic way to deploy the applications. However, the application can’t live without its structure/environment. We still need to build the cloud structure with the services (like Beanstalk, S3, ALB, ECS, VPC, etc) and automate the building process using Terraform or other IaC (Infrastructure as Code) like AWS CDK.

2. Structure:

As I shared previously in my articles, the Terraform is a powerful tool that helps us build the cloud structure in an automated way.

Terraform has its own language HCL (HarshiCorp Configuration Language). Although it includes a number of built-in functions to transfer and combine values, you may still find that Terraform loops are not as simple as programming languages. I’d rather see HCL as a configuration language. It was not invented to become an actual programming language. It is used primarily by DevOps teams to automate various infrastructure tasks. We can create many commonly used AWS resources with Terraform, and Terraform continues to expand its coverage of AWS services. One of the advantage of using Terraform is that it supports creating resources from various cloud providers.

AWS CDK is a great option to define the cloud infrastructure in codes as well. AWS CDK uses libraries to create the resources. Developers can choose their preferred languages like TypeScript, Python, Java, etc to compile the file which includes the constructs, and use command line to deploy or synth the resources according to the file. ‘cdk deploy’ is similar to ‘terraform apply’. AWS CDK works for all AWS Services, and it synthesizes the CloudFormation templates. But AWS CDK only supports AWS resources, of course.

In August of 2022, AWS announced with HashiCorp that CDK for Terraform was available. It is possible for developers to define and provision infrastructure using their favourite programming languages (like TypeScript, Python, Java, C#, and Go) from cloud providers like AWS, Azure, GCP, etc.

Code Pipeline mainly helps automate the deployment of the codes. Terraform mainly helps automate the deployment of the cloud structures. It does not mean Terraform or CDK can only declare cloud resources. Terraform and CDK can deploy infrastructure and application run time codes together. In my previous article on AWS Lambda with Terraform, the Lambda Function is uploaded and deployed by Terraform at one move. There is another example, before we create containers with AWS ECS using Terraform (shared in my previous article), we need to build and push a container image. This image may have many different versions. The operations to build/push images can be completed by Terraform as well. It seems that Terraform and CDK are versatile. In fact, I would prefer to use CI/CD system if the versions for Lambda or ECS are many. CI/CD is classified as “Continuous Deployment”. The Code Pipeline can detect the change in the source code and continuously deploy the updated versions. It will save us a lot of ‘terraform apply’ or ‘cdk deploy’.

Since Terraform Codes are also ‘codes’. So we can even include Terraform in the CI/CD system to automate the deployment of Terraform codes.

Let’s manually build the CI/CD pipeline.

#1 CodeCommit

1) to open terminal on MAC

2) to create code repo in AWS CodeCommit (Please remember to replace the repository name with yours)

$ aws codecommit create-repository — repository-name here_is_the_name — repository-description “here_is_the_description” — tags Project=here_is_the_value

3) to get a list of all existing repositories

$ aws codecommit list-repositories

4) To get information about specific repository

$ aws codecommit get-repository — repository-name here_is_the_name

5) to copy the Repo URL from console:(log in as non-root user)

( https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/here_is_the_name )

6) to create a local repo on Mac (OPTIONAL)

$ git clone https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/here_is_the_name

7) the terminal will ask for username/password for codecommit

The info can be found in IAM Console → User Credentials → HTTPS Git credentials for AWS CodeCommit

— — now repo is ready for commit

8) to create a new branch

$ git checkout -b CICD

9) to add remote origin

$ git remote add CICD https://git-codecommit.ca-central-1.amazonaws.com/v1/repos/here_is_the_name

10) to add local files into staging area

$ git add .

11) to commit code into local repo

$ git commit -m “here_is_the_comments”

12) to push code into remote repo

$ git push -u CICD CICD

13) to ignore local files when committing

$ git add .gitignore

$ git config — global core.excludesfile ~/.gitignore_global

14) to delete the repo

$ aws codecommit delete-repository — repository-name here_is_the_name

15) to delete a specific file

— repository-name here_is_the_name \

— branch-name CICD \

— file-path here_is_the_path

— parent-commit-id here_is_the_id

(Commit-id can be found in the AWS CodeCommit Console)

#2 Code Pipeline

Before we build the Code Pipeline, we can create a s3 bucket enabled website hosting. Or we can use a more complex structure as I did in the previous articles on AWS. The complicated structure is a complete version for my website including services like Route 53, WAF, Cloudfront, S3, VPC, ECS, RDS, Lambda Function, Secrets Manager, ACM, ALB, etc.

Now let’s build a code pipeline.

1st to setup Code Source Stage

1) In AWS CodeCommit Console, choose to create a pipeline.

2) choose the IAM role for pipeline, as the pipeline later will talk to S3, CodeCommit for tasks(actions).

3) choose the source to store your input artifacts for your pipeline, the choices are multiple, choose CodeCommit, the repo is the one you upload your website files.

2nd to setup Build Stage

1) if we host static website in s3 bucket, there is no code compilation. We can choose to skip the build stage. This is a simple case when we can directly go to the deploy stage.

2) Otherwise, choose AWS CodeBuild or Jenkins

3rd to setup Deploy Stage

1) Can choose deploy your codes in various choices such as the application and its environments in Beanstalk

2) In this case, we choose the S3 bucket for website and the bucket for artifact. Please remember to check ‘Extract file before deploy’.

** After the Code Pipeline was created successfully, stages set in the CodePipeline will rerun once the pipeline detects any change in the source (AWS CodeCommit in this case). You can also click the ‘Release Change’ in the Code Pipeline Console to manually re-deploy the website. Because of the Code Pipeline, we can focus more on Web development and leave the rest to the AWS.

The Pipeline can be added with more stages. Between the stages of ‘deploy to dev’ and ‘deploy to prod’, an action of ‘ManualApproval’ is inserted. It tells the pipeline to deploy to the production environment only the Release Manager approves.

Up until now, the CI/CD pipeline has been built. Whenever the files for the website are updated in CodeCommit, the pipeline is triggered and works on deploying the new content of the website.

If you are interested the HTML, Scripts for the website, please refer to my previous article.

--

--