{"id":16489,"date":"2014-12-26T12:59:06","date_gmt":"2014-12-26T07:29:06","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16489"},"modified":"2016-12-19T14:54:48","modified_gmt":"2016-12-19T09:24:48","slug":"launching-infrastrucure-using-terraform-infrastructure-as-code","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/launching-infrastrucure-using-terraform-infrastructure-as-code\/","title":{"rendered":"Launching Infrastrucure using Terraform (Infrastructure as Code)"},"content":{"rendered":"<p>Terraform is a tool for creating, managing and versioning the infrastructure effectively. Terraform provides a common configuration to launch infrastructure of physical servers, DNS, Email Servers. With terraform, we will be launching servers using simple file based configuration ( JSON based ) and maintain versioning for each launch on any version control software.<\/p>\n<p>This tool aims to take the concept of &#8220;Infrastructure as Code&#8221;<\/p>\n<p>This can be used with various providers, including AWS, Digital Ocean, Heroku etc. In this article,we will be provisioning the servers using AWS.<\/p>\n<p>This section walks you through the step by step guide for launching infrastructure using terraform on AWS.<\/p>\n<p>Prerequisite:<\/p>\n<p><em>1) Create an IAM user with full Administrative Access. ( Since we are going to launch and destroy server&#8217;s)<br \/>\n<\/em><br \/>\n<em>2) Create a key pair on AWS Management Console or via CLI which will be used for connecting to the instance during the launch<br \/>\n<\/em><\/p>\n<p><strong>Step 1) Install Terraform <\/strong><\/p>\n<p><strong>Step 2) Configure Variables <\/strong><\/p>\n<p><strong>Step 3) Launch Infrastructure<\/strong><\/p>\n<p><strong>Step 4) Push the code to git ( Maintain Versions of Insfrastructure )<\/strong><\/p>\n<p>Let&#8217;s break down the steps \ud83d\ude42<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">1) Install Terraform<\/h3>\n<p>You can download it here<br \/>\nor<\/p>\n<p>[js]<br \/>\n$ cd \/opt<br \/>\n$ wget<br \/>\n$ unzip terraform_0.3.5_linux_amd64.zip<br \/>\n$ export PATH=$PATH:\/opt\/terraForm\/<br \/>\n[\/js]<\/p>\n<p>Terraform is downloaded and installed. Just run to check whether it is installed or not<\/p>\n<p>[js] $ terraform [\/js]<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terra1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-16490\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terra1-300x177.png\" alt=\"terra1\" width=\"500\" height=\"177\" \/><\/a><\/p>\n<p>You will get the above output.<\/p>\n<p>Terraform configuration are simple text file in JSON format and has .tf extensions<\/p>\n<p>We&#8217;ll create three .tf files<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">variables.tf<\/h3>\n<p>( we&#8217;ll define variables here )<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">main.tf<\/h3>\n<p>( will contained details about infrastructure)<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">output.tf<\/h3>\n<p>(Expected Output)<\/p>\n<p>We will create one <strong>terraform.tfvars<\/strong> files which will contain secret informations (access_key\/secret_key) and put this in <strong>.gitignore<\/strong><\/p>\n<p>We&#8217;ll define variables inside variables.tf file<br \/>\n<strong><br \/>\n<\/strong><\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">variables.tf<\/h3>\n<p>[js]<br \/>\nvariable &quot;access_key&quot;{}<br \/>\nvariable &quot;secret_key&quot;{}<br \/>\nvariable &quot;key_name&quot;{}<br \/>\nvariable &quot;key_path&quot;{}<\/p>\n<p>variable &quot;aws_region&quot;{<br \/>\ndescription=&quot;Region to Launch Server&#8217;s&quot;<br \/>\ndefault=&quot;us-east-1&quot;<br \/>\n}<\/p>\n<p>variable &quot;aws_amis&quot;{<br \/>\ndefault = {<br \/>\nus-east-1=&quot;ami-9eaa1cf6&quot;<br \/>\n   }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>As you can see we are using variables name aws_region and aws_amis with given value.<\/p>\n<p>access_key and secret_key is placed in <strong>terraform.tfvars<\/strong> files in below format<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">terraform.tfvars<\/h3>\n<p>[js]<br \/>\naccess_key=AKIAI****************<br \/>\nsecret_key=wJalrXUtnFEMI\/********\/bPxRfiCYzEXAMPLEKE<br \/>\n[\/js]<\/p>\n<p>We have created a key-pair on Management console named &#8220;terraform&#8221; and downloaded terraform.pem<\/p>\n<p>We&#8217;ll define provider\/resources inside main.tf file.<br \/>\n<strong><br \/>\n<\/strong><\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">main.tf<\/h3>\n<p>[js]<br \/>\nprovider &quot;aws&quot;{<br \/>\n    region=&quot;${var.aws_region}&quot;<br \/>\n    access_key=&quot;${var.access_key}&quot;<br \/>\n    secret_key=&quot;${var.secret_key}&quot;<br \/>\n}<\/p>\n<p>resource &quot;aws_security_group&quot; &quot;default&quot;{<br \/>\n    name=&quot;terraform-sg&quot;<br \/>\n    description=&quot;Created by terraform&quot;<\/p>\n<p>    ingress{<br \/>\n        from_port = 22<br \/>\n        to_port = 22<br \/>\n        protocol= &quot;tcp&quot;<br \/>\n        cidr_blocks = [&quot;0.0.0.0\/0&quot;]<br \/>\n    }<br \/>\n    ingress{<br \/>\n        from_port = 80<br \/>\n        to_port = 80<br \/>\n        protocol = &quot;tcp&quot;<br \/>\n        cidr_blocks = [&quot;0.0.0.0\/0&quot;]<br \/>\n    }<\/p>\n<p>}<br \/>\nresource &quot;aws_instance&quot; &quot;web&quot;{<\/p>\n<p>    connection={<br \/>\n        user=&quot;ubuntu&quot;<br \/>\n        key_file=&quot;${var.key_path}&quot;<br \/>\n    }<br \/>\n    instance_type=&quot;t2.micro&quot;<br \/>\n    ami=&quot;${lookup(var.aws_amis, var.aws_region)}&quot;<br \/>\n    key_name = &quot;${var.key_name}&quot;<br \/>\n    security_groups= [&quot;${aws_security_group.default.name}&quot;]<br \/>\n    tags{<br \/>\n        Name=&quot;Terraform-EC2&quot;<br \/>\n    }<br \/>\n    provisioner &quot;remote-exec&quot;{<\/p>\n<p>        inline=[<br \/>\n            &quot;sudo apt-get -y update&quot;,<br \/>\n            &quot;sudo apt-get -y install nginx&quot;,<br \/>\n            &quot;sudo apt-get -y install php5-fpm&quot;,<br \/>\n            &quot;sudo service nginx restart&quot;<br \/>\n        ]<br \/>\n     }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>You can see in <strong>main.tf<\/strong> we have few sections.<\/p>\n<p>1) <strong>provider<\/strong> : defines the provider, In our case its &#8220;AWS&#8221;<br \/>\n2) <strong>resource<\/strong> : block defines a resource that exists within the infrastructure. A resource might be a physical component such as an EC2 instance,<br \/>\n3) <strong>provisioner<\/strong>: User data scripts.<\/p>\n<p>Our configuration is complete. Now we&#8217;ll check which how terraform builds the resource that we have declared in our configuration files.<\/p>\n<p>for that we have to execute plan command<\/p>\n<p>[js] $ terraform plan [\/js]<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terraform2.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-16491\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terraform2-300x159.png\" alt=\"terraform2\" width=\"500\" height=\"159\" \/><\/a><\/p>\n<p>This Output show&#8217;s that, Terraform will launch a t2.micro EC2 Server with ami-9eaa1cf6.<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">output.tf<\/h3>\n<p>&nbsp;<br \/>\nIn output.tf we will be defining the output variables that&#8217;ll be displayed after successful launch.<\/p>\n<p>[js]<br \/>\noutput &quot;address&quot;{<br \/>\n    value=&quot;${aws_instance.web.public_dns}&quot;<br \/>\n }<br \/>\n[\/js]<\/p>\n<h3 style=\"text-align: justify; color: #ff9900;\">Step 4) Launch Infrastructure<\/h3>\n<p>Since we haven&#8217;t received any error message while terraform plan<br \/>\nNow we&#8217;ll apply the changes.<\/p>\n<p>[js]<br \/>\n$ terraform apply -var &#8216;key_name=terraform&#8217; -var &#8216;key_path=~\/terraform\/terraform.pem&#8217;<br \/>\n[\/js]<\/p>\n<p>Note: We are passing pemfile name and path as an argument ( Recommended ), instead of declaring on using path_value inside variables.tf.<\/p>\n<p>You will get this output if everything goes well.<br \/>\n<a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terrraf3.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-16492\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terrraf3-300x111.png\" alt=\"terrraf3\" width=\"500\" height=\"111\" \/><\/a><\/p>\n<p>Now on Web Console, You can see a instance is in launching state<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terraf4.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-16493\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/12\/terraf4-300x31.png\" alt=\"terraf4\" width=\"500\" height=\"60\" \/><\/a><\/p>\n<p>Push all your files to git to maintain versions of your infrastructure configuration. Make sure to add terraform.tfvars to your .gitignore if you are placing secret info on this file<\/p>\n<p>Leave a comment if you have any questions regarding this setup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Terraform is a tool for creating, managing and versioning the infrastructure effectively. Terraform provides a common configuration to launch infrastructure of physical servers, DNS, Email Servers. With terraform, we will be launching servers using simple file based configuration ( JSON based ) and maintain versioning for each launch on any version control software. This tool [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1174,1],"tags":[572,248,1586,1587,1585],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16489"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16489"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16489\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}