3.Terraform : Provision VPC using Terraform | Terraform Manifest file to Create VPC and EC2 Instance

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
creating VPC is one of the important aspect if you are working with AWS Cloud if you are watching this video hopefully you know how to create VPC in the AWS console level hey guys welcome back to Galaxy Technologies my name is Ravi Shankar in this video we are going to see how to create VPC using terraform and this is a continuation of our previous video in the previous video we have created an ec2 instance in the default VPC let me quickly show you the Manifest file which we have created in our previous lecture so this is the ec2.tf file which we have created we have provided the provider block as well as resource block as part of resource block we are creating an ec2 instance and this is the Ami and this Ami is a Amazon Linux Ami and it is part of AP South one and we have user key pair and we created a T2 micro but it will be get created in the default VPC but I would like to create a VPC along with the ec2 instance if you wish to do you need to create below resources so to create an VPC we need to create these resources so the resources are first one is VPC we are going to use 10.10 series for the vpcc IDR then subnet as we are launching only one subnet that is going to be a public subnet then internet gateway to give internet access to our instance of course we need to create an internet gateway then route table along with the route table Association then Security Group at last and ec2 instance these are the resources we are going to create as part of our VPC manifest file so to create a VPC of course we need a provider nothing change over here and resource anyway we need to create an ec2 instance as well so let it be like this we'll modify once we have created the VPC and I am going to rename this file as VPC with ec2 so we are creating VPC with ec2 and as part of this one first thing is we need to create a VPC right let's go and check the syntax how we can create VPC in terraform go back here VPC resource VPC resource for terraform and you can see AWS VPC let me open this one and uh example usage you can create a simple VPC by using this one okay even you can give the tags and the tenancy however this would be sufficient let me copy this one over here you can see here the resource type is AWS VPC and a resource name I mean to say VPC name is main let me change it to demo VPC and cidr it is giving 10.0 but we are going to use 10.10 okay so I can say that this will help us to create a VPC next thing is we need to create a subnet so let me give the comments as well create VPC next thing create subnet next let's go on search for subnet resource so subnet resource for terraform so let's open this one you can see here AWS subnet this is what you need to use and example uses so resource that is AWS subnet is the resource name main is the value I mean to say label and you can see here VPC ID so this is the VPC ID under which VPC you should create the subnet and cidr also we need to specify under tag what is the name of this resource so let me copy this snippet copy it and go here and copy and I'm going to name it as a demo subnet and uh VPC ID so you are going to take it from here so how to define this VPC ID it is quite simple usually to define a VPC ID it is the resource type dot resource name so let me copy this okay and remove double quotes and give dot over here and ends with the ID okay this is how you can Define the VPC ID now what does it do whenever it requests the VPC ID under which VPC ID it should create a subnet it go to the AWS VPC dot demo VPC this is the one and ID because this is a dynamic value right means once we have created VPC then only we need to create subnet so as part of the script we are just creating a VPC so we don't know the name of VPC at this moment so this is kind of a defining a dynamic variable in your terraform script next thing is cidr block we are using 10 Series right so it should be 10 series and even I can name it as a demo subnet okay now we have created subnet next thing is create create internet gateway so let's go and search for that resource one quick announcement from Galaxy Technologies we have changed our website from Galaxy Dot into Galaxy tech.com but Galaxy Dot in also works and also we are giving live trainings so you can opt for devops practitioner training Azure devops python data science devops real-time project interview preparation cyber security cell scripting so these courses you can opt for the live trainings if you are interested you can connect it over here with the talk with our expert our team will help you with the all the live trainings thank you let's jump back to our discussion internet gateway resource you can see AWS internet gateway and this is the example usage resource type is AWS internet gateway under name is GW they have given but let me copy it and we'll change it according to our naming convention so AWS internet gateway and let me give demo igw okay and even here also demo igw and the VPC ID because we are attaching this one to the VPC so we should know the VPC ID so it is same again I can either copy it from here or else I have already did it that this is how I can retrieve the VPC ID dynamically so I just replace it okay next thing we need to create route table let's go and search for the route table route table resource for terraform so resource type is AWS resource table so let me grab the example yep you can see here resource AWS resource table is the resource type and this is the name and it is giving the route nothing but from where you can accept the traffic so from this you can accept the traffic into the internet gateway and this is another route for the IPv6 anyway we are not using IPv6 we can ignore it let me copy this and so we are just removing IPv6 we don't need and we want to accept traffic from anywhere okay that's the reason I can give internet IP internet IP range and the next thing I am going to name it as a demo route table RT I am going to give then VPC ID again same VPC ID I can use it and the next thing internet gateway ID so you should replace this one with the internet gateway ID so let's grab that as well go up so this is the resource type and this is the resource name let me copy this and just replace this one anyway at end we need ID so remove it and in between again you need to specify a DOT and delete it that's it now I can say that internet gateway also created and I'm going to name it as a demo route table sorry this is a route table now we need to create subnet Association as well okay next thing is subnet Association so we need to add our subnet to the route table for that let's go here and search for subnet Association and let me open this and here you can find AWS route table Association you can see the example okay so we can copy anyone and go here and copy it and AWS routable Association I will name it as a demo route table Association okay just I'm giving demo route table Association as the resource name and this is resource type next thing we need to give the subnet which should be associated in this so let's go and get the subnet so we have created subnet over here this is the subnet with the subnet type and sorry resource type and resource name so we just need to replace this one and ends with ID okay similar way we need to give the route table as well so we have created route table just now right okay let's copy till here and just replace it and add dot over here that's it I can say that we have created required resources at last we need to create a security group so let's go back and create a security group Security Group resource for terraform and this is the AWS Security Group and you can see here you example uses you need to create a resource and the resource type is AWS Security Group and resource name and this is the name of security group and description and VPC ID we should give the VPC where you need to create this Security Group and Ingress Ingress nothing but inbound rules so usually we need SSH and other application ports okay we'll change this one aggress nothing but outbound rules okay we will modify this one because uh address we don't need to change but increase we need to change it so let's copy over here and just I'm going to change this one as a demo SG or demo VPC ESG I will name it and and same thing I am going to give it over here and we appreciated this is the same as above AWS VPC and our VPC name is demo vpc.id so just replace it and description it is optional again this is also I'm just removing it and I forget to add comments okay create a security group next thing is we need Port 20 so 2 Port also 20 it is TCP so next cidr block it should be from anywhere so 0.0.0.0 and even this one this is the IPv6 cidr block we can copy this one okay now I can say that sorry it is 22 right now I can say that we have created a security group at last we need to create an ec2 instance that is already defined okay so this is the ec2 instance right even though you mentioned in any sequence terraform can understand which resource should create when okay so no worry about that one so Ami there is no change key pair no change instance type no change but we need to give the VPC for the ec2 instance right so VPC nothing but this is the VPC ID okay so this is the VP said but we don't need to mention the VPC ID in this ec2 instance okay you just need to tell that under which subnet you should create it and also what is the security group it should use let's grab that one so let's search for ec2 resource for terraform and you can see here AWS instance we already knew about this one last time we have used it example usage you can see here basic example using Ami lookup which will help us to dynamically take the Ami but we are using the hard-coded Ami that's okay but if you want to use Dynamic one then you should use data sources it is again altogether a different discussion so in our case now we are looking for the how we can Define the subnet and Security Group for that you can go with the argument references and here you can search for subnet subnet okay so this is to create a VPC but we are not using this and if you go here yep you can see here subnet ID it is optional defends the VPC subnet ID to launch in so in which VPC it should launch it will decide okay now let's go and Define our subnet ID so for that subnet ID and value you can see here it is automatically propagated now you can give the subnet ID this is the subnet right AWS subnet we have given name as a demo subnet you can click over here under dot ID okay so this is how you can Define or else another way is you can just go here and grab the subnet ID so this is the subnet ID okay you just replace this one and uh create a DOT over here and Dot ID okay this is also give same because it identified we have given subnet ID so it identified that resource even though you just type AWS subnet okay so AWS subnet ID dot ID all right next thing is we need to define the security group under which Security Group we should create for that also let's go and search for Security Group you can see here to define a security group you should use the argument called VPC Security Group IDs okay but anyway you should provide this one as a list you can see here list so even that one also you can Define VPC and it is propagating because we have installed a plugins related to the terraform over here okay that's the reason but anyway let's go back over here and VPC let me remove it and VPC Security Group IDs and you should provide it as a string nothing but you should provide it over here and let me get the VPC ID or else you just remove it okay and you look further and you just go and grab it over here so Security Group AWS Security Group ID okay demo vpcsg this is the one so just copy it over here AWS Security Group dot ID dot sorry dot ID okay so this is how you can Define now I can say that my file is looks good and you can see here it is showing somewhere some error okay here it is specifying yeah so we need to specify this in the double quotes cidr range that is the error now there is no errors let me save this file and let's execute the terraform init okay it looks good and in case let's show you even if you delete it and if you run the terraform validate okay it clearly show you an error that okay there is some issue let's see okay we didn't save this file let me save this and you can see there is an error now let me run it and you can see here it is saying that there is a error in the cidrn okay so you don't forget to save the file each time whenever you do updates now again if I run it it shows that our manifest file looks good next thing terraform plan we are checking what and all resources it creates you can see here it is going to create seven resources what and all resources it is going to create let me go up you can see here it is going to create a demo server the next thing is demo igw it is going to create demo route table route table Association okay demo vpcsg and VPC as well so all the resources subnets VPC okay all these resources are going to get create Now to create it let's apply the terraform apply command and we can give yes in the command line itself that is minus minus Auto approve if you give it is going to create resource before that let me show you my AWS console and here there is no running instances or else let me remove it and you can see here there are three systems that is ansible Jenkins master and slip apart from this we should get one demo server also if you see vpcs we have two vpcs rtp03 VPC but we should get the demo VPC let's execute this one so it's creating the resources let's go here and Let me refresh it you can see here it's created a new VPC that is range of 10.10 okay that is the series and it is not a default VPC okay so along with VPC it also creates the subnets in our VPC so subnet not yet created and even you can just look after further resources of your VPC by checking over here this is default one this is which we just created you can see here demo subnet and Route table also you can see demo routable has been created and the internet gateway so demo igw has been created next thing if I go to instances okay if everything is created it should create the resource let's go here okay it is showing some error that unsupported your required instance type T2 micro is not supported in your registered availability Zone AP South 1C please try to request not specifying the availability zone or choose the AP South one year 1B okay I didn't request on AP South okay availability Zone I didn't request it took the AP South one let me add the availability Zone as well availability Zone equal to AP South 1A okay so this is the one I am giving you should mention it in the double quotes okay now let's try to execute it because whatever is created it will be recorded in our TF State file so whatever resources is here to create those resources it is going to create okay because it stopped while creating an ec2 instance so it is going to create only ec2 instance you can see here one resource it is getting added so AP South one a it is saying that our subnet has been created under AP South 1C region so in one series and T2 micro is not supported so let me go with the t2 small okay for this example and let me change it to the 1C and save this file and let's execute it once again or else we can go with the terraform plan and it should create only one resource yeah and terraform apply okay this is also not supported crazy thing let's do one thing let's recreate the Subnet in the other availability zone for that you just remove this okay and go to subnet and specify the availability Zone AP sorry double quotes AP South 1A okay so we are creating the Subnet in AP South 1A now let's try to run terraform plan so you can see here it is destroying two resources and adding three resources so what and all it is destroying subnet as well as subnet Association okay so let's execute it terraform plan autom ating T2 small instance that's okay okay it has been completed let's go and check it out in our console Let me refresh it yes you can see here one in running ec2 instance and if you see the timestamp it's just created 7th 20 that is the time at this moment okay now we can connect to this system Let me refresh it and take the public IP once it is up and taking the public IP even that also you can display over here by using the output resource block okay that is again a different discussion this one right we are not getting the public IP because we miss it to allocate the public IP that you can do in the subnet level as well so if you go to the this is security group right subnet yeah here there will be a public IP so so if you see here map a public IP and launch optional specify true to indicate the instance launch into the subnet should be assigned a public IP address okay so this option if we give then you will get a public IP we haven't specified it over here that's the reason you don't see a public IP over here anyway you can try it out as an assignment and you let me know the comments okay whether this option is worked for you or not so far we have created a terraform manifest file to create a VPC and easy to write I will show you one simple method how you can able to achieve the same thing in easy way that is where chart GPT comes into the picture so let's open the chart GPT so chart dot open AI you can start a new chart so let me type that terraform manifest file to create VPC along with ec2 instance okay this is the request I can do and you can see what Miracle can do by a chart GPT so we have given the provider block we have given the resource that is VPC and we have given the subnet block so next we have given the security group as well and you can see here it is opening port number 22 and this is where we had done the mistake and the next thing AWS AWS instance type and uh this is a VPC security groups it is adding security groups under tax and does it added subnet it is also not launching the public IP over here but that's okay you can see here how simple it is to get the same code with help of charge GPT so better you can use the chart GPT as your assistant or helper okay that's all for this video if you want to see how to launch a kubernetes cluster by using terraform please let me know in the comments of this video according to that I will prepare my next video If many people are not interested for the topic I can skip that that's all for this video thank you so much and see you in the next interesting video
Info
Channel: Valaxy Technologies
Views: 12,119
Rating: undefined out of 5
Keywords: aws, devops, valaxy technologies, security, automation, serverless, linux, valaxy, valaxy devops, devops online training, valaxy devops training, valaxy devops videos, terraform, provision vpc using terraform on aws, create vpc and ec2 using terraform, terraform manifest files, terraform for beginners, terraform from valaxy, ar shankar terraform course, vpc resource in terraform, subenet resource in terraform, terraform using chatgpt, steps to create vpc using terraform
Id: wx7L6snkrTU
Channel Id: undefined
Length: 24min 6sec (1446 seconds)
Published: Wed Mar 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.