Instance-Volume Attachment using Terraform

Instance-Volume Attachment using Terraform

A terraform code for all the below mentioned steps is being provided-

  • Create a key pair
  • Create a security group
  • Launch an instance using the above created key pair and security group.
  • Create an EBS volume of 10 GB.
  • The final step is to attach the above created EBS volume to the?instance you created in the previous steps.

Step 1

A key-pair "task-2" is successfully created.

No alt text provided for this image
Step 2
provider?"aws"?{
????region?=?"ap-south-1"
????profile?=?"default"
}


#?step ->?Create?a?Security?Group


resource?"aws_security_group"?"task2-sg"?{
??name?=?"task2-sg"
??ingress?{
????from_port???=?80
????to_port?????=?80
????protocol????=?"tcp"
????cidr_blocks?=?["0.0.0.0/0"]
??}


??egress?{
????from_port???=?0
????to_port?????=?0
????protocol????=?"-1"
????cidr_blocks?=?["0.0.0.0/0"]
??}
}
        
No alt text provided for this image
Step 3
#?step?->?Launch?an ec2?instance


resource?"aws_instance"?"task2"?{
????ami?=?"ami-010aff33ed5991201"
????key_name?=?"task-2"?
????instance_type?=?"t2.micro"
????vpc_security_group_ids?=?[aws_security_group.task2-sg.id]


????tags?=?{
????????Name?=?"ec2_taks2"
??????????}        
}        
No alt text provided for this image
Step 4
#?step?->?Create?an ebs?volume


resource?"aws_ebs_volume"?"volume"?{
????availability_zone?=?aws_instance.task2.availability_zone
????size?=?10
????tags?=?{
????????Name?=?"ec2_ebs_vol_task2"
?????????}        
}        
No alt text provided for this image
Step 5
#?step?->?Attaching?volume?to?instance


resource?"aws_volume_attachment"?"ebs_att_task2"?{
??device_name?=?"/dev/sdh"
??volume_id???=?aws_ebs_volume.volume.id
??instance_id?=?aws_instance.task2.id        
}        
No alt text provided for this image
We can also fetch the desired outputs if required
#?step?->?Outputs


output?"az"?{
????value?=?aws_instance.task2.availability_zone????
}


output?"public_ip"?{
????value?=?aws_instance.task2.public_ip????        
}        
No alt text provided for this image
Terraform init , Plan and finally Apply
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image
THANK YOU !
robert weaver

Senior Hybrid Linux Infraustructure Engineer

2 年

Many thanks for this , was a simple and good follow through of adding a aws EBS Volume to ec2 , via terraform.

回复
Shashwat S.

SDE @ National Informatics Centre | Micro-Frontend | Microservices | React.js, MongoDB, Express, Node.js | Java DSA | React Native | Jest.js & Mocha | AWS, Azure DevOps | CSE 2024

3 年

I've used Terraform too at GCP

要查看或添加评论,请登录

Sujagi Verma的更多文章

社区洞察

其他会员也浏览了