Terraform Providers
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to install Terraform in AWS EC2 instance and also created our 1st AWS resource using Terraform.
https://cloudaffaire.com/how-to-install-terraform-in-aws-ec2/
In this blog post, we will discuss terraform providers.
What is Terraform provider?
Providers are responsible in Terraform for managing the lifecycle of a resource: create, read, update, delete. Most providers require some sort of configuration to provide authentication information, endpoint URLs, etc. Terraform has multiple providers like AWS, Azure, GCP and VMWARE etc.
When a provider is used for the 1st time, you must execute terraform init to initialize the provider. Terraform init command downloads the provider-specific packages which are used to manage resource lifecycle.
Provider Syntax:
1 2 3 4 5 |
## Syntax provider " version = " alias = " } |
Provider demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
##------------------------- ## Terraform: Provider ## ##------------------------- ## Create a directory and get inside it mkdir terraform && cd terraform ## Create main.tf vi main.tf --------------- provider "aws" { access_key = " secret_key = " alias = "mumbai" #non default provider aws.mumbai version = "2.5" #AWS provider version 2.5 will be downloaded region = "ap-south-1" } provider "aws" { access_key = " secret_key = " region = "us-east-1" } provider "google" { #multiple types of provider can be used at the same time region = "us-central1" } resource "aws_vpc" "myvpc" { provider = "aws.mumbai" #non default provider mumbai has been called cidr_block = "10.0.0.0/16" } --------------- :wq ## Format code terraform fmt ## Initialized terraform (downloads provider [AWS] and [Google] packages) terraform init ## Check files (terraform downloaded the AWS and Google provider packages in .terraform directory) ls -a ls -lh .terraform/plugins/linux_amd64/ ## Check providers terraform providers ## Create resource terraform apply ## Destroy resource terraform destroy ## Cleanup cd .. && rm -rf terraform ############################################ ## AWS provider specific configurations ## ############################################ ## The AWS provider offers a flexible means of providing credentials for authentication. ## The following methods are supported, in this order, and explained below: ## 1. Static credentials ## 2. Environment variables ## 3. Shared credentials file ## 4. EC2 Role ## AWS Provider using Static credentials ## provider "aws" { access_key = " secret_key = " region = " } ## AWS Provider using Environment variables ## ## Export AWS access key, secret key and region export AWS_ACCESS_KEY_ID=" export AWS_SECRET_ACCESS_KEY=" export AWS_DEFAULT_REGION=" ## Provider block provider "aws" {} ## AWS Provider using Shared Credential File ## provider "aws" { region = " shared_credentials_file = "/Users/tf_user/.aws/creds" profile = "aws_profilename" } ## AWS Provider using IAM role ## provider "aws" { assume_role { role_arn = "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME" session_name = "optional" external_id = "optional" } } |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform resources.
To get more details on terraform, please refer below terraform documentation
https://www.terraform.io/docs/index.html