Terraform for AWS Top-Level Blocks
Hi, this is Paul, and welcome to the #7 part of my Terraform guide. Today we will discuss Top-Level Blocks.
Top-Level Blocks
Terraform’s language utilizes a limited set of top-level block types, which are blocks that can appear independently in a TF configuration file. These are referred to as top-level blocks. Most Terraform features are implemented solely as top-level blocks. By mastering them, one can become proficient in Terraform. These blocks are categorized into three main types: fundamental blocks, variable blocks, and calling or referencing blocks.
Fundamental blocks include Terraform settings, providers, and resources. Variable blocks encompass input variables, output values, and local values. Finally, calling or referencing blocks involve data sources and modules. In the Terraform settings block, one defines Terraform settings. In the providers' block, one specifies the provider being used (e.g., AWS or Azure) along with its settings. Resources blocks are used to provision resources on the respective cloud. Parameterizing the entire configuration is achieved through input variables, output values, and local values. Calling or referencing blocks involves accessing data sources from the cloud provider or using modules to streamline resource creation. Mastery is achieved by sequentially learning about fundamental blocks, variable blocks, and calling or referencing blocks.
Examples:
Terraform Settings Block
terraform {
required_version = "~> 1.6"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.0"
}
}
backend "s3" {
bucket = "terraform-stacksimplify"
key = "dev2/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-dev-state-table"
}
}
Thank you for reading until the end. Before you go:
Provider Block
provider "aws" {
profile = "default" # AWS Credentials Profile configured on your local desktop terminal $HOME/.aws/credentials
region = "us-east-1"
}
Resource Block
resource "aws_instance" "ec2demo" {
ami = "ami-04d29b6f966df1537" # Amazon Linux
instance_type = var.instance_type
}
Input Variables Block
variable "instance_type" {
default = "t2.micro"
description = "EC2 Instance Type"
type = string
}
Output Values Block
output "ec2_instance_publicip" {
description = "EC2 Instance Public IP"
value = aws_instance.my-ec2-vm.public_ip
}
Local Values Block
Create S3 Bucket — with Input Variables & Local Values
locals {
bucket-name-prefix = "${var.app_name}-${var.environment_name}"
}
Data sources Block
Get the latest AMI ID for Amazon Linux2 OS
data "aws_ami" "amzlinux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
}
Modules Block AWS EC2 Instance Module
module "ec2_cluster" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 2.0"
name = "my-modules-demo"
#instance_count = 2
ami = data.aws_ami.amzlinux.id
instance_type = "t2.micro"
key_name = "terraform-key"
monitoring = true
vpc_security_group_ids = ["sg-08b25c5a5bf489ffa"] #Default VPC Security Group ID and replace
subnet_id = "subnet-4ee95470" # Get one public subnet id from default vpc and replace
user_data = file("apache-install.sh")
tags = {
Terraform = "true"
Environment = "test"
}
}
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn