Lets get kick-started with creation of Compute Instances using Terraform. Note that you all ready have a project (that is linked with a billing account), a service account and keys of using that in JSON format. Spinning up an instance is very simple here. List out the configuration of the Instance that needs to be created and give them to TF in HCL language. Lets do a quick example.
Configurtions of Desired Instance are as follows:
> Instance in South-Asia-1 (zone b)
> Machine type is “f1-micro” which is a free-tier one
> Image / OS should be Linux Debian 9
> Let the Subnet to be default
> Make sure that it get updated as soon as you create it
Now, Nothing can stop you from spinning it up but, how do you think Terraform would know where to do this? We haven’t told it Which provider it should create (GCP, AWS, Azure etc..), It doesn’t know, who are you (which can be found in key of service account.
To Begin
Create a file say, main.tf . Inside that, let’s declare the provider, give it the location to find keys, inform the project-ID and the region to work in. This is done like this -
Make sure that any information you provide, you give it in String format (enlosed between double inverted commas) If your file lies in the same directory/folder you need not specify the address, or else you need to.
Resources to build
Entering the interesting module. In the same file, declare as shown below -
If I had to describe this, this is how it goes (chronologically)
- We declare a resource which is Google Compute Instance and name it default for future reference
- We give a name my-instance to this instance, which will be reflected in our GCP console
- Machine type is specified (in this case it is f1-micro). List of all machine types is linked below
- Zone in which this has to be created is mentioned here
- Tags are given in form of list, which can be used to link this instance with other resources such as Firewall rules and other.
- Startup script is also provided, which containes the set of Command Line Instructions that are performed when the Instance begins. These are commands that are executes as soon as Instance begins, here, It is updating the system. There can be multi-line instructions seperated by ; or a bash script file which can be locted using file(address of script.sh)
WHAT YOU CAN CHANGE:
Everybody knows, we can change names of the Instance here, (slow claps). We cannot create replicas here, There is no option of just increasing the replica count. (you get it when we use instance templates)
You have an option of changing the Subnetwork and apply Firewall rules on those subnets to restrict the access. This becomes more fun and complex to operate.
DO THE DRILL
Perform "terraform init" and "terraform plan" to know what's going to be built!
and VOILA..!
Comments
Post a Comment