Day 26–101 Days of DevOps — Introduction to Terraform — Part 2
Welcome to Day 26 of 101 Days of DevOps. Today we are going to extend the terraform code we write on Day25 https://www.101daysofdevops.com/courses/101-days-of-devops/lessons/day-25/.
To view the complete course, please check the below url.
For more info, register via the below link
YouTube Channel link
On Day 25, we write the bare minimum code to understand the various terraform construct; today, let extend that code further. So far, our code looks like this.
provider "aws" {
region = "us-west-2"
}resource "aws_instance" "mytest" {ami = "ami-0bc06212a56393ee1"instance_type = "t2.micro"}
So far, we have hardcoded the value of ami and instance_type. Let’s variablize it. To do that, we will create new file name variables.tf(file name is not important, but it's a standard format followed in terraform community).
variable "region" {
type = string
default = "us-west-2"
}
variable "ami" {
type = string
default = "ami-083ac7c7ecf9bb9b0"
}
variable "instance_type" {
type = string
default = "t2.micro"
}
Our new code will look like this.
provider "aws" {
region = var.region
}
resource "aws_instance" "myexample" {
ami = var.ami
instance_type = var.instance_type
}
Similarly, when the terraform resource is getting created, we need the output of various attributes. We can do that with the help of outputs.tf. In this case, we are printing public IP and instance id.
output "public_ip" {
value = aws_instance.myexample.public_ip
}
output "instance_id" {
value = aws_instance.myexample.id
}
If you look at the above code, we are still hardcoding the value of AMI.
- We can use data resources (similar to what we used for Availability Zone in the VPC section) to query and filter AWS and get the latest AMI based on the region, as the AMI id is different in a different region.
data "aws_ami" "centos" {
most_recent = true
owners = ["679593333241"]
filter {
name = "name"
values = ["CentOS Linux 7 x86_64 HVM EBS *"]
}
filter {
name = "architecture"
values = ["x86_64"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
}
NOTE: The use of data resources is not ideal, and in every used case, e.g., In the case of Production, we might want to use a specific version of CentOS.
- The above code will help us to get the latest Centos AMI, the code is self-explanatory, but one important parameter we used is owners
owners
– Limit search to specific AMI owners. Valid items are the numeric account ID,amazon
orself
.most_recent
– If more than one result is returned, use the most recent AMI. This is to get the latest Centos AMI as per our use case.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/ami
- To use data resources in our code, we need to use interpolation, which means terraform will interpolate that code especially.
resource "aws_instance" "myexample" {
ami = data.aws_ami.centos.id
instance_type = var.instance_type
}
If you like to dig deeper into the AWS concept, please feel free to check my book.
Looking forward to you guys joining this journey and spend a minimum of an hour every day for the next 101 days on DevOps work and post your progress using any of the below mediums.
- Website: https://101daysofdevops.com/
- Linkedin: https://www.linkedin.com/in/prashant-lakhera-696119b/
- Twitter: @100daysofdevops OR @lakhera2015
- Facebook: https://www.facebook.com/groups/795382630808645/
- Medium: https://medium.com/@devopslearning
- GitHub: https://github.com/100daysofdevops/100daysofdevops
- YouTube Channel: https://www.youtube.com/user/laprashant/videos
- Slack: https://join.slack.com/t/100daysofdevops/shared_invite/zt-au03logz-YfDUp_FJF4rAUeDEbgWmsg
- Reddit: r/101DaysofDevops
- Meetup: https://www.meetup.com/100daysofdevops/