HomeTutorsContact
Terraform - Infrastructure as Code
Terraform Tutorial for Beginners
Anton Putra
January 03, 2021
1 min

Install Terraform

  • Install Terraform using binary.
  • You can find all the available providers here.

  • In this lesson we will only use AWS Provider.

Authentication Methods

  • Create provider.tf file

  • Static credentials

  • Environment variables
  • Shared credentials/configuration file
  • EC2 Instance Metadata Service - Preferred approach over any other when running in EC2

  • Add aws provider version constraint

  • Create Terraform IAM group and attach AmazonEC2FullAccess policy

  • Create terraform user and place it to Terraform group

  • Create aws terraform profile aws configure --profile terraform

  • Create main.tf file

  • Run terraform init to initialize terraform working directory and download and install aws provider

  • Run terraform fmt to format the code

  • Run terraform validate to validate terraform configuration

  • Run terraform apply to create AWS infrastructure

  • Run terraform show to inspect state

  • Run terraform state list to get list of managed by Terraform resources

Change Infrastructure

  • Update ami of the ec2 instance and run terraform apply

  • Add lifecycle method, update ami and run terraform apply

  • Add EC2 tag service: ubuntu and run terraform apply

  • Add ignore_changes method and run terraform apply

Destroy Infrastructure

  • Run terraform destroy to destroy AWS infrastructure

Define Input Variables

  • Create variables.tf with following content
  • Remove default value

  • Replace ami hacoded value to var.ami

  • Set ami variable via cli flag terraform apply -var 'ami=62534'

  • Set ami variable from file terraform.tfvars

  • From environment variables TF_VAR_ami

Output Variables

  • Create aws_eip resource
  • Create output.tf file
  • Destroy infra terraform destroy

Store Remote State in S3 with DynamoDB

  • Create S3 bucket antonputra-tfstate to store state with bucket versioning enabled

  • To enable locking we need to create DynamoDB tfstate table with partition key LockID of type string

  • Define backend block

  • Run terraform init

  • Create TerraformS3Policy

  • Create TerraformDynamoDBPolicy IAM policy
  • Attach these two policies to the Terraform IAM group

Tags

#AWS#Terraform

Anton Putra

DevOps Engineer

Big Data | DevOps engineer with hands-on experience in building large, scalable batch and real-time applications with Apache Spark, Hive, Flink on top of Kubernetes; designing and developing CI/CD pipelines.

Expertise

Kubernetes
Terraform
AWS
GCP

Social Media

youtubefacebooklinkedinwebsite

Related Posts

Run Terraform from CircleCI?
How to Run Terraform from CircleCI?
March 14, 2021
1 min
© 2021, All Rights Reserved.

Quick Links

About UsContact Us

Social Media