This repo is a companion repo to the Automate Terraform with GitHub Actions.
-
Authentication
- run
aws configure
from your terminal configure your user with the aws credentials
- run
-
Building you Infrastructure on AWS
- Create the *** terraform-and-aws *** folder
- cd into the folder and run the command,
wget https://raw.githubusercontent.com/linuxacademy/content-terraform-2021/main/main_aws.tf -O main.tf
to download the terraform config file into a new file named main.tf - run
terraform init
to initialize terraform. - run
terraform fmt
to ensure your config file is well formatted - check the validity by running
terraform validate
- run
terraform apply
to provision the infrastructure - run
terraform show
to show the current state file orterraform state list
to show all resources you are managing
-
Defining Input variables
- run
echo "# variable file" >> variables.tf
to create the variable file - copy and paste the variable block into your variables file and save
- go to the main.tf and replace the Name in the tag block with
var.instance_name
- then run
terraform apply -var "instance_name=Chimezdev-cloudguru"
to make changes
- run
-
Using Output variables to query data
- run
wget https://raw.githubusercontent.com/linuxacademy/content-terraform-2021/main/outputs_aws.tf -O outputs.tf
while in your working dir. to download the file into a file named outputs.tf - when the apply command completes you should see some outputs returned in the terminal, or run
terraform output
to see the ouputs displayed. - run
terraform destroy
to deprovision your infrastructure
- run
-
Storing Remote State
- run
vim main.tf
to edit the file using vim or any other text editor you prefer - add the following backend block to the terraform block
backend "remote" { organization = "cloudguru-cicd-demo" workspaces { name= "cicd-project" } }
- run
terraform login
and enter yes at the prompt to open terraform cloud on your browser. - click on create api token and copy the token and paste into your terminal.
- now run
terraform init
to initialize the remote backend - you can now remove the state file store locally by running
rm terraform.tfstate
- head back to your browser and visit your terraform cloud page and add your aws credentials as environment variables.
- when done go to the terminal and run
terraform apply
- run