Hello folks!
As I’ve said before it’s the even day, so we are going to implement these stuff. As the first one is only the concept, we can skip that but when we learn Cloud Formation (AWS) Tool, we compare these two more clearly.
Note!
I hope you’ve covered the previous part where we’ve done practical implementation and also applied init
, validate
, plan
, approve
, destroy
and few other flags like -auto-approve
, etc. These all comes into the infrastructure lifecycle part. If you’ve not covered it, I recommend you to go and check that out and start this. Else you are not gonna understand this, okay?
For today’s article, we will learn the implementation of the Local Exec, Remote Exec and Variables.
Let us begin,
Local Exec:
We are going to use the same code which we’ve used in the last practical implementation (in which we’ve created a VPC and an EC2 Instance). But the change here is that we are going to add a new module for executing the Local Exec.
resource "aws_instance" "inst_local" {
ami = "ami-03f4878755434977f"
instance_type = "t2.micro"
tags = local.tags
provisioner "local-exec" {
command = "echo ${self.private_ip} >> private_ips.txt"
}
}
Here, if we observe that we get to see a new block within the instance resource block we’ve included the information of it.
- We just mention the commands that we need to run on the execution of that block and get it’s private IP Address.
Variables
Just like in programming languages, we define these to store the data. These variables can store any particular data type like string, we’ll discuss about this in-depth in upcoming articles. Till then, make yourself familiar with this.
- We define these in a variable block, and access using the keyword
var
and it’sname
# Define variables
variable "region" {
type = string
default = "ap-south-1"
}
variable "instance_type" {
type = string
default = "t2.micro"
}
# Configure AWS provider
provider "aws" {
region = var.region
}
# Create an EC2 instance using variables
resource "aws_instance" "my_instance" {
ami = "ami-03f4878755434977f"
instance_type = var.instance_type
}
Output Code:
Basically, we use this block to get any output from the execution of the configuration file.
The code just looks like…
output "instance_ip_addr" {
value = aws_instance.inst_local.private_ip
}
You can paste this after defining the instance resource block.
- And by following the lifecycle commands, you can get this output.
Locals Values
Just like in variables, we use this
local
Here we can also give them tags and use them for referring tag names as well.
# Create an EC2 instance using variables
locals {
instance_name = "com.payments.prod"
tags = {
environment = "Production"
}
}
#Creating the AWS Instance using Locals
resource "aws_instance" "inst_local" {
ami = "ami-03f4878755434977f"
instance_type = "t2.micro"
tags = local.tags
provisioner "local-exec" {
command = "echo ${self.private_ip} >> private_ips.txt"
}
I think that's all for today's implementation. These are just basics, in the upcoming article of practical, we are going to create a single-tier-aarchitecture using Terraform on AWS.
And in tomorrow's article we are going to learn some basic important concepts which helps us in creating our project-1 (Single-Tier-Architecture)
Please don't wait to ask any questions in the comment and see you tomorrow!