Easy Terraform GitHub Repository using Terraform

In a previous blog post, I wrote about using Terraform to provision a Terraform remote backend on Amazon Web Services (AWS) S3 and DynamoDB service. This blog post will show you the steps to create a GitHub repository using Terraform.

All the code snippets found in this post will also be in my GitHub repository here.

These blog posts (Terraform remote backend, GitHub repository) will form part of a series to give you a quick start to set up Terraform Continuous Integration/Continuous Delivery (CI/CD) using Terraform. I will explain more in an upcoming blog post.

Prerequisites

Similar to provisioning Terraform remote backend with Terraform, you need to install Terraform on your computer as a prerequisite. For more instructions on how to do so, you may refer to the Official Terraform Installation guide.

In addition to installing Terraform, you need to own a GitHub account. Create a new account if you do not have one.

Detailed Installation Steps

Clone the code from my public repository and switch to the GitHub repository feature branch. In your CLI, run:

git clone https://github.com/sgLancelot/terraform-remote-backend-on-aws.git
cd terraform-remote-backend-on-aws
git checkout origin/github-as-code-repo

Create a new Terraform variable file (terraform.tfvars) to store your GitHub access token.

touch terraform.tfvars

Generate a GitHub access token following the official steps here. Give the access token the scope to repo and delete_repo, like the screenshot below.

Copy the token generated here. REMEMBER to treat your access Token like your password and NEVER expose it by checking it into a public version control repository. Use environment variables instead.

Modify your terraform.tfvars file with the text editor of your choice, and add in the following line:

github_access_token = "<YOUR_GITHUB_ACCESS_TOKEN>"

I use Visual Studio Code in the screenshot below:

After adding your token, remember to save the file. The last installation step is to install all the providers that are declared in the Terraform code. Run:

terraform init

And the providers will be installed according to the version specified in provider.tf.

Provision the Resources

To provision the resources in main.tf (including the Amazon S3 bucket and DynamoDB for remote backend), run:

terraform apply

Review the changes in the plan and input yes if satisfied.

Closing

Remember to destroy the resources if you do not want to continue using them. To do so, run:

terraform destroy

Review the changes and key in yes if satisfied.

If you are having issues, please let me know via email tzhenkai@outlook.com.

Stay tuned for more on this Terraform CI/CD series.

Leave a comment

Your email address will not be published. Required fields are marked *