TTS Converter App — Challenge2 — GitHub actions workflows

Mariem SOUSSI
4 min readMar 30, 2024

--

In Text-To-Speech Converter — Challenge1, we took the first step toward automating the deployment of the application by defining its infrastructure as code using terraform and initiating its deployment through Terraform commands (terraform init, terraform plan, and terraform apply) manually via the terminal.

While manual deployment serves its purpose initially, it becomes difficult as the application evolves. Each time we need to make changes, we find ourselves launching a series of commands manually, resulting in time lost, especially as the number of changes increases.

Now, to enhance the automation of the deployment process and facilitate future app improvements, we aim to further automate deployment and updates of AWS resources. This involves automatically triggering deployment and updates whenever changes are made to the Terraform or lambda code and pushed to our GitHub repository. Achieving this objective requires leveraging CI/CD pipelines, and for this purpose, we have chosen GitHub Actions.

In the following sections, I’ll provide a brief introduction to GitHub Actions, present the challenge we’re facing, give you some steps to assist you in overcoming it, and provide a link to the solution.

GitHub Actions

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that empowers you to automate various tasks within a GitHub repository. This includes building, running tests, linting code, automatically commenting on pull requests and issues, and deployment.

To achieve this, you define GitHub Actions workflows using YAML files. These workflows consist of one or more jobs triggered by events occurring in your repository, such as pushes, pull requests, or other events. Each job can contain one or more steps, and each step consists of one or more actions. These actions can either be scripts that you define or sourced from the GitHub Marketplace.

Additionally, you can specify when to trigger the workflow and establish dependencies between workflows. GitHub Actions provides a comprehensive solution that enhances the efficiency of projects across different domains.

GitHub Actions and Terraform can work together to create powerful, automated workflows, which we will explore in this challenge.

Challenge 2

Automating TTS Converter App Deployment with GitHub Actions

Steps to help you solve the challenge

1. Clone the repo of the first challenge

Begin by cloning the repository from the first challenge. This will serve as the foundation for implementing automation in Challenge 2.

If you encounter the error “Fatal: Remote Origin Already Exists” after cloning the repository, you can find the solution here.

2. Create an S3 bucket for remote backend

When you deploy your infra using terraform, a file named Terraform.tfstate is created to track the infrastructure created by the Terraform code. This state file can be created locally, which is the default option, or remotely.

GitHub Actions workflows utilize runners, which are virtual machines provided by GitHub. Each job gets assigned a runner, and a new one is assigned for each run. To facilitate this process, we utilize a remote state that GitHub runners can access to determine which resources are deployed on AWS and their current state.

To enable remote state management for our Terraform configurations, we need to create an S3 bucket to store the Terraform state files securely. It can be created manually or using a separate Terraform configuration which should not be part of the project’s Terraform configs. Once the bucket is created, update the Terraform configurations accordingly to utilize this remote backend.

3. Set AWS credentials as environment secrets in your github repo

Setting AWS credentials as environment secrets in your GitHub repository involves securely storing your AWS access keys (Access Key ID and Secret Access Key). These credentials can then be referenced in your GitHub Actions workflows and accessed by the workflows when needed.

4. Add the folder .github/workflows to your project

Add the .github/workflows directory to your project. This is where GitHub Actions searches for workflows to execute.

5. Define your pipelines 😊

With distinct types of changes — AWS resource updates and Lambda code modifications — I’ve opted for two pipelines: infra.yaml for managing AWS resources and code.yaml for handling Lambda code changes.

Lambda code pipeline

code.yaml is triggered by any change in the Lambda code folder. Currently, it’s responsible for zipping the Lambda code.

If you encounter the error “Unable to import module ‘lambda_function’: No module named ‘lambda_function” when testing the lambda function after zipping it with the pipeline, you can find the solution here.

Infra pipeline

infra.yaml is triggered by changes in the Terraform code (on push) or after a successful run of the code pipeline (on workflow_run). Additionally, it can be manually triggered (on workflow_dispatch) with two options: apply or destroy AWS resources.

Manual trigger of the infra pipeline

6. Test your pipelines

Validate that each pipeline executes as expected

7. Bravo ! 🎉🎉🎉

Congratulations on successfully implementing your CI/CD pipelines for automating the deployment of the TTS Converter AWS services and Lambda code!

Solution of challenge 2

For a detailed implementation of the solution, you can find it in my github repository.

Stay tuned for the next challenge as we continue to explore further improvements 😉

--

--