Day3 - Terraform 5-06-2021
data reference/data manupulation
data resource in terrraform
In programming language, you define value and print
x=5
y="Hi"
print(x)
print(y)
resource is very important. EC2 is one of the resource. Please visit terraform doc for cloud specific resources such as aws ec2.
how to define variable in terraform?
You write in block with keywork. yesterday we use keyword as resource. lets try today with variable.
variable "x" {
type = string # value of string is string
default = "Terraform Training" # define the default value.
}
# keywork
keywork "name" {
}
here we will use keywork as output
output "myvalue" {
value = x
}
lets put together
$ cat ec2.tf
variable "x" {
type = string # value of string is string
default = "Terraform Training" # define the default value.
}
output "myvalue" {
value = x
}
lets go for an example
> terraform apply
we got an error: Invalie reference
what this mean is 'what you are typing, i have no idea
lets modify and return
value = "x" # put in double quote
terraform apply
type yes when prompted
you see outpyt myvalue=x
x is printed as it is.
you have to define x as a variable
so
value "var.x"
run it again,
we see myvalue = "var.x"
if you put inside double quote, it will print as it is.
x is a reference, need to look for value, so we have to use curly braces.
value = "${var.x}"
$ cat ec2.tf
variable "x" {
type = string # value of string is string
default = "Terraform Training" # define the default value.
}
output "myvalue" {
value = "${var.x}"
}
run it again
lets modifey
value = "Hi ${var.x} its cool"
this is called string interpolation
if you want to print any variable, you have to define where is it defined.
you have to specify the keywork - var.
variable you created are user defined variable and var variable will look for the data that is defined to x.
------------------------------
whats the plan
launch os : name of the service ec2
sub service instance
in terraform we call instance as resource.
lets see we want to attach a disk to the instance, we can get extra storage from aws, the service name is ebs.
ebs -> volume -> storage -> hard disk (block device)
ebs is sub service to ec2.
in AWS, you create instance at a region
lets go to ec2 -> ebs -> volume
when we create volume
- it will ask the size of the volume
- it will ask you availibity zone where you want to deploy.
1. launch OS - ec2
2. Create a volume (dish)
3. Attach volume to ec2.
The biggest problem is that you need to create harddisk at the same region where you have created ec2 instance.
-> when you launch os, it automatically assign what az (by default), it will launch.
> mkdir ec2; cd ec2
lets reference yesterday's code
> notepad terraform.tf
provider "aws" {
region = "ap-south-1"
access_key = ""
secret_key = ""
}
resource "ec2 instance" "os1" { # os1 is resource name
ami ="ami-01938d8s003ds88ss"
instance_type = "t2.small"
tags = {
Name = "My first OS New"
}
}
> terraform apply
------------------
do go doc,
ec2 -> aws instances -> arguement reference
------------------
using yesterdays' code, we use accessa_key and secret_key.
static credential is not recommended.
use
> aws configre # to configure profile # gogole aws create profile
> aws configure list-profile
provider "aws" {
region = "ap-south-1"
profile = myprofile"
}
resouce "aws_instance" "os1" {
ami = "ami...."
instance_type = "t2.micro"
tags = {
Name = "My OS - II"
}
}
os1 is a varible where it keeps all the info about all the variable.
note: if you create code on new folder, you have to initialize so that it will download the plug-in needed,
> terraform init
> terrafrom plan
> terraform apply
how do we get the region name so that we can attach the volume at the same region.
all variable info is stored at 'aws_instance.os1' variable.
we can use output function to print the value.
This is how we printed the value.
$ cat ec2.tf
variable "x" {
type = string # value of string is string
default = "Terraform Training" # define the default value.
}
output "myvalue" {
value = "${var.x}"
}
so we can do it
output "os1" {
value = aws_instance.os1
}
lets put together
provider "aws" {
region = "ap-south-1"
profile = myprofile"
}
resouce "aws_instance" "os1" {
ami = "ami...."
instance_type = "t2.micro"
tags = {
Name = "My OS - II"
}
}
output "os1" {
value = aws_instance.os1
}
> tf apply
you will see it print out everything
review the output.
you will se ami
availibity zone
public_ip
you can see all the variable, value here without going to aws.
you can login to aws console and ec2. verify ip, az and more info..
they are same.
print the public IP
edit the code and add the entry below
output "my_public_ip_is" {
# output "my_availibilibity_is" {
# availibility_zone
#value = aws_instance.os1.availbility_zone
value = aws_instance.os1.public_ip
}
> terraform apply
go to document
click on ec2
- Resources
- Data sources
click on data source
click on aws_instance
look for argument reference
now, we have to create a volume and attach.
but we have to create on specific az.
go to docs,
go to ec2 -> resource and search for ebs volume
aws_ebs_volume
click and look at the example
resource "aws_ens_volume" "st1" {
vaailability_zone = "us-west-2a" # review the region
size = 10
tags = {
Name = New Hradidisk"
}
}
we can't hardcode the value, use variable
resource "aws_ens_volume" "st1" {
vaailability_zone = "aws_instance.os1.availibity_zone" # review the region
size = 10
tags = {
Name = New Hradidisk"
}
}
put together and run
> tf apply
go to aws ebs, you will see the volumes
-------------------
resouce "aws_instance" "os1" {
ami = "ami...."
instance_type = "t2.micro"
tags = {
Name = "My OS - II"
}
}
output "my_az_is" {
value = aws_instance.os1.availability_zone
}
resource "Aws_ebs_volume" "st1" {
availbility_zone = aws_instance.os1.availability_zone
size = 10
tags = {
Name = New Hradidisk"
}
}
output "o2" {
value = aws_ebs_volume.st1.id
}
now, attach
your os and hard disk need to be on same az, same region.
one more resource we going to add
go to docs, look for resource which helps to attach the volume
aws_volume_attachment
review the example
go to arguement reference
you have to specify the device name
# Step3
resource "aws_volume_attachment" "ens_att" {
device_name = "/dev/sdh"
volume_id = aws_ens_volume.st1.id
instance_id = aws_instance.os1.id
}
> tf apply
dynamically create os, volume and attach..
now, go to your aws console -> go to the instance, you will see the instance with added disk..
destroy
destroy attachment, volume and shutdown instance..and destroy
once click creates (tf apply)
one click destroys entire infrastructure (tf destroy)
Thursday, May 6, 2021
day 3 - Terraform - data reference - data manupulation
Subscribe to:
Post Comments (Atom)
Git branch show detached HEAD
Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03) 00 2. Run re-set hard $ git reset --hard 3. ...
-
snmpconfig command allow you to managge snmpv1/v3 agent configuration on SAN switch. Event trap level is mapped with event severity level....
-
Firmware upgrade on HPE SuperDom Flex 280 - prerequisites tasks a. Set up repo b. Upload firmware to your webserver 1. For foundation so...
-
Disabling the Telnet protocol on Brocade SAN switches By default, telnet is enabled on Brocade SAN switches. As part of security hardening o...
No comments:
Post a Comment