IaC (Terraform)

This section will introduction you to create/destroy the infrastructure of this workshop by using Terraform

1. Setup

1.1. Setup Terraform

Download terraform by this link. After that, verify your terraform cli with:

terraform --version

1.2. Setup Github Token

Go to github account settings with this link. Then create Fine-grained personal access tokens with full permissions. After that copy the token to somewhere we can paste it in next step.

1.3. Setup AWS CLI and credentials

Install AWS CLI at this link. Then verify it with

aws --version

Using this document to setup AWS Credentials to our AWS CLI with this link

2. Running

This is our terraform source code tree, i split each service into modules for use later.

.
├── main.tf
├── modules
│   ├── cloudfront
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── github
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   ├── iam-role
│   │   ├── main.tf
│   │   ├── outputs.tf
│   │   └── variables.tf
│   └── s3
│       ├── main.tf
│       ├── outputs.tf
│       └── variables.tf
├── outputs.tf
├── providers.tf
├── terraform.tfvars.example (You must copy and rename this file to "terraform.tfvars")
└── variables.tf

2.1. Copy IaC to other directory

First, you must clone or download my source code at this link. After that you would copy 2 folders terraform and next-app to other directory (2 of them must be same directory).

any-directory
├── terraform/
├── next-app/

Next, in terraform directory we need copy the file terraform.tfvars.example to terraform.tfvars, lets check about 2 variables: github_token is the token we create in the step 1.2 and github_owner is your github username (case sensitive).

2.2. Init terraform

Go to terraform directory

cd terraform

Init Provider

terraform init

Check current state and variables

terraform plan

If not errors occurs, we can go next.

2.3. Creating resources

terraform apply -auto-approve

If this command runs well, we can see the final Cloudfront URL output at terminal and successfully create the infrastructure!

2.4. Deleting resources

terraform destroy -auto-approve