Welcome back to the series of Deploying On AWS Cloud Using Terraform 👨🏻💻. In this entire series, we will focus on our core concepts of Terraform by launching important basic services from scratch which will take your infra-as-code journey from beginner to advanced. This series would start from beginner to advance with real life Usecases and Youtube Tutorials.
If you are a beginner for Terraform and want to start your journey towards infra-as-code developer as part of your devops role buckle up 🚴♂️ and lets get started and understand core Terraform concepts by implementing it…🎬
🔎Basic Terraform Configurations🔍
As part of basic configuration we are going to setup 3 terraform files
1. Providers File:- Terraform relies on plugins called “providers” to interact with cloud providers, SaaS providers, and other APIs.
Providers are distributed separately from Terraform itself, and each provider has its own release cadence and version numbers.
The Terraform Registry is the main directory of publicly available Terraform providers, and hosts providers for most major infrastructure platforms. Each provider has its own documentation, describing its resource types and their arguments.
We would be using AWS Provider for our terraform series. Make sure to refer Terraform AWS documentation for up-to-date information.
Provider documentation in the Registry is versioned; you can use the version menu in the header to change which version you’re viewing.
provider "aws" { region = "var.AWS_REGION" shared_credentials_file = "" }
2. Variables File:- Terraform variables lets us customize aspects of Terraform modules without altering the module’s own source code. This allows us to share modules across different Terraform configurations, reusing same data at multiple places.
When you declare variables in the root terraform module of your configuration, you can set their values using CLI options and environment variables. When you declare them in child modules, the calling module should pass values in the module block.
variable "AWS_REGION" { default = "us-east-1" } #-------------------------Fetch VPC ID--------------------------------- data "aws_vpc" "GetVPC" { filter { name = "tag:Name" values = ["CustomVPC"] } } #-------------------------Variables For Autoscaling--------------------- variable "instance_type" { type = string default = "t2.micro" } variable "autoscaling_group_min_size" { type = number default = 2 } variable "autoscaling_group_max_size" { type = number default = 3 } variable "aws_key_pair" { type = string default =} #-------------------------Fetch Public Subnets List---------------------- data "aws_subnet_ids" "GetSubnet_Ids" { vpc_id = data.aws_vpc.GetVPC.id filter { name = "tag:Type" values = ["Public"] } } data "aws_subnet" "GetSubnet" { count = "${length(data.aws_subnet_ids.GetSubnet_Ids.ids)}" id = "${tolist(data.aws_subnet_ids.GetSubnet_Ids.ids)[count.index]}" } #-------------------------Fetch Target Group ARN---------------------- data "aws_lb_target_group" "elb_tg" { arn = var.elb_tg_arn }
3. Versions File:- It’s always a best practice to maintain a version file where you specific version based on which your stack is testing and live on production.
terraform { required_version = ">= 0.12" }
🎨 Diagrammatic Representation 🎨
Configure Security Group For Launch Configuration
The method acts as a virtual firewall to control your inbound and outbound traffic flowing in and out.
🔳 Resource
✦ aws_security_group:- This resource is define traffic inbound and outbound rules on the subnet level.
🔳 Arguments
✦ name:- This is an optional argument to define the name of the security group.
✦ description:- This is an optional argument to mention details about the security group that we are creating.
✦ vpc_id:- This is a mandatory argument and refers to the id of a VPC to which it would be associated.
✦ tags:- One of the most important property used in all resources. Always make sure to attach tags for all your resources.
EGRESS & INGRESS are processed in attribute-as-blocks mode.
resource "aws_security_group" "asg_sg" { name = "ASG_Allow_Traffic" description = "Allow all inbound traffic for asg" vpc_id = data.aws_vpc.GetVPC.id ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 8 to_port = 0 protocol = "icmp" cidr_blocks = ["0.0.0.0/0"] } 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"] } tags = { Name = "terraform-asg-security-group" } }
👨🏻💻Deploy Launch Config and AutoScaling group👨🏻💻
Before creating the Autoscaling group let’s first define the launch configuration for EC2 instances that will be used by autoscaling later.
🔳 Resource
✦ aws_launch_configuration:- This resource creates a launch configuration for EC2 instances that we are going to deploy as part of our autoscaling group.
🔳 Arguments
✦ name_prefix:- This is an optional argument to create a unique name beginning with the specified prefix.
✦ image_id:- This is an mandatory argument to mention image_id id based on which EC2 instance will be launched.
✦ instance_type:- This is a mandatory argument to mention instance type for the EC2 instances like t2.small, t2.micro etc.
✦ key_name:- This is an optional argument to enable ssh connection to your EC2 instance.
✦ security_groups:- This is an optional argument to mention which controls your inbound and outbound traffic flowing to your EC2 instances inside a subnet.
✦ user_data:- This is an optional argument to provide commands or scripts to be executed during the launch of the EC2 instance.
✦ Lifecycle:- Lifecycle is a nested block that can appear within a resource block.
create_before_destroy:- when Terraform must change a resource argument that cannot be updated in place due to remote API limitations, Terraform will instead destroy the existing object and then create a new replacement object with the newly configured arguments.
resource "aws_launch_configuration" "launch_config_dev" { name_prefix = "webteir_dev" image_id = "ami-0742b4e673072066f" instance_type = "${var.instance_type}" key_name = "${var.aws_key_pair}" security_groups = ["${aws_security_group.asg_sg.id}"] associate_public_ip_address = true user_data = <Deployed EC2 Using ASG" | sudo tee /var/www/html/index.html EOF lifecycle { create_before_destroy = true } }
🔳 Resource
✦ aws_autoscaling_group:- This resource group resources for use so that it can be associated with load balancers.
🔳 Arguments
✦ launch_configuration:- This is an optional argument to mention name of the launch configuration to be used.
✦ min_size:- This is a mandatory argument to define the minimum size of the Autoscaling group.
✦ max_size:-This is a mandatory argument to define the maximum size of the Autoscaling group.
✦ target_group_arns:- This is an optional argument to define the target group arn to which EC2 can register.
✦ vpc_zone_identifier:- This is an optional argument to define a list of subnet IDs to launch resources in.
✦ tags:- One of the most important property used in all resources. Always make sure to attach tags for all your resources.
resource "aws_autoscaling_group" "autoscaling_group_dev" { launch_configuration = "${aws_launch_configuration.launch_config_dev.id}" min_size = "${var.autoscaling_group_min_size}" max_size = "${var.autoscaling_group_max_size}" target_group_arns = ["${data.aws_lb_target_group.elb_tg.arn}"] vpc_zone_identifier = "${data.aws_subnet.GetSubnet.*.id}" tag { key = "Name" value = "autoscaling-group-dev" propagate_at_launch = true } }
🔳 Output File
Output values make information about your infrastructure available on the command line, and can expose information for other Terraform configurations to use. Output values are similar to return values in programming languages.
output "asg_sg" { value = aws_security_group.asg_sg.id description = "This is Security Group for autoscaling launch configuration." } output "aws_launch_configuration" { value = aws_launch_configuration.launch_config_dev.id description = "This is ASG Launch Configuration ID." } output "autoscaling_group_dev" { value = aws_autoscaling_group.autoscaling_group_dev.id description = "This is ASG ID." }
🔊To view the entire GitHub code click here
1️⃣ The terraform fmt command is used to rewrite Terraform configuration files to a canonical format and style👨💻.
terraform fmt
2️⃣ Initialize the working directory by running the command below. The initialization includes installing the plugins and providers necessary to work with resources. 👨💻
terraform init
3️⃣ Create an execution plan based on your Terraform configurations. 👨💻
terraform plan
4️⃣ Execute the execution plan that the terraform plan command proposed. 👨💻
terraform apply --auto-approve
👁🗨👁🗨 YouTube Tutorial 📽
❗️❗️Important Documentation❗️❗️
⛔️ Hashicorp Terraform
⛔️ AWS CLI
⛔️ Hashicorp Terraform Extension Guide
⛔️ Terraform Autocomplete Extension Guide
⛔️ AWS Security Group
⛔️ AWS Launch Configuration
⛔️ AWS Autoscaling Group
⛔️ Lifecycle Meta-Argument
🥁🥁 Conclusion 🥁🥁
In this blog, we have configured the below resources
✦ AWS Security Group for the ASG Launch Configuration.
✦ AWS Launch Configuration.
✦ AWS Autoscaling Group.
I have also referenced what arguments and documentation we are going to use so that while you are writing the code it would be easy for you to understand terraform official documentation. Stay with me for the next blog.
📢 Stay tuned for my next blog…..
So, did you find my content helpful? If you did or like my other content, feel free to buy me a coffee. Thanks.
Author - Dheeraj Choudhary
RELATED ARTICLES
Automate S3 Data ETL Pipelines With AWS Glue Using Terraform
Discover how to automate your S3 data ETL pipelines using AWS Glue and Terraform in this step-by-step tutorial. Learn to efficiently manage and process your data, leveraging the power of AWS Glue for seamless data transformation. Follow along as we demonstrate how to set up Terraform scripts, configure AWS Glue, and automate data workflows.
Automating AWS Infrastructure with Terraform Functions
IntroductionManaging cloud infrastructure can be complex and time-consuming. Terraform, an open-source Infrastructure as Code (IaC) tool, si ...
Hi there, just became alert to your blog through Google,
and found that it’s truly informative. I’m gonna
watch out for brussels. I will be grateful if you continue this in future.
Numerous people will be benefited from your writing.
Cheers! Escape rooms hub
Very interesting topic, appreciate it for putting up..
Really great info can be found on site.Blog monetyze
Excellent blog you have here.. It’s hard to find good quality writing like yours these days. I really appreciate individuals like you! Take care!!
Very good info. Lucky me I came across your site by chance (stumbleupon). I have book marked it for later!
I seriously love your website.. Great colors & theme. Did you create this site yourself? Please reply back as I’m looking to create my own blog and would like to know where you got this from or exactly what the theme is named. Kudos.
Way cool! Some extremely valid points! I appreciate you writing this article plus the rest of the website is really good.
Excellent blog you have here.. It’s difficult to find excellent writing like yours these days. I truly appreciate individuals like you! Take care!!
Pretty! This was an extremely wonderful post. Many thanks for supplying this info.
This site really has all the info I needed concerning this subject and didn’t know who to ask.
Great information. Lucky me I found your website by chance (stumbleupon). I’ve bookmarked it for later.
This website truly has all the information I needed about this subject and didn’t know who to ask.
Spot on with this write-up, I really believe that this amazing site needs far more attention. I’ll probably be back again to read more, thanks for the info!
Having read this I believed it was really informative. I appreciate you taking the time and energy to put this content together. I once again find myself spending way too much time both reading and commenting. But so what, it was still worthwhile.
bookmarked!!, I like your site.
Very good write-up. I certainly appreciate this site. Keep writing!
Spot on with this write-up, I truly believe this web site needs much more attention. I’ll probably be returning to see more, thanks for the info!
Aw, this was an exceptionally good post. Spending some time and actual effort to produce a great article… but what can I say… I hesitate a whole lot and never seem to get anything done.
I’m very happy to find this web site. I need to to thank you for ones time just for this wonderful read!! I definitely liked every part of it and I have you bookmarked to check out new things on your site.
Your posts in this blog really shine! Glad to gain some new insights, which I happen to also cover on my page. Feel free to visit my webpage UY5 about Thai-Massage and any tip from you will be much apreciated.
Greetings! Very useful advice in this particular post! It’s the little changes that produce the most important changes. Thanks for sharing!
Hi! I just want to offer you a big thumbs up for the great info you have got right here on this post. I am returning to your site for more soon.
Very good article. I am facing some of these issues as well..
Howdy! I could have sworn I’ve been to this web site before but after looking at many of the posts I realized it’s new to me. Nonetheless, I’m certainly pleased I found it and I’ll be bookmarking it and checking back frequently.
Hi there, There’s no doubt that your web site could be having browser compatibility issues. When I look at your site in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I merely wanted to provide you with a quick heads up! Aside from that, fantastic website.
Having read this I thought it was extremely informative. I appreciate you spending some time and energy to put this article together. I once again find myself personally spending a significant amount of time both reading and commenting. But so what, it was still worth it.
You’ve made some good points there. I looked on the web to find out more about the issue and found most people will go along with your views on this site.
This site was… how do I say it? Relevant!! Finally I’ve found something that helped me. Thanks a lot.
After checking out a handful of the blog articles on your blog, I seriously like your technique of blogging. I book marked it to my bookmark webpage list and will be checking back soon. Please check out my web site too and let me know your opinion.
An outstanding share! I’ve just forwarded this onto a colleague who has been doing a little homework on this. And he in fact bought me dinner due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending some time to talk about this subject here on your site.
You made some really good points there. I checked on the web to find out more about the issue and found most individuals will go along with your views on this web site.
This website was… how do I say it? Relevant!! Finally I have found something which helped me. Many thanks.
This page certainly has all of the information and facts I needed concerning this subject and didn’t know who to ask.
Hi, I do think this is a great blog. I stumbledupon it 😉 I will come back once again since i have book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.
I could not refrain from commenting. Perfectly written!
Way cool! Some extremely valid points! I appreciate you writing this post plus the rest of the website is extremely good.
Everything is very open with a precise clarification of the challenges. It was truly informative. Your website is very helpful. Thanks for sharing!
I love it when people come together and share thoughts. Great site, stick with it!
I would like to thank you for the efforts you’ve put in writing this blog. I’m hoping to check out the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has inspired me to get my very own website now 😉
An impressive share! I’ve just forwarded this onto a colleague who was conducting a little research on this. And he in fact ordered me lunch because I discovered it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending time to discuss this issue here on your web site.
I blog frequently and I genuinely appreciate your information. This great article has really peaked my interest. I’m going to book mark your site and keep checking for new information about once a week. I subscribed to your RSS feed too.
Greetings! Very useful advice in this particular post! It’s the little changes that will make the largest changes. Thanks for sharing!
Great article! We will be linking to this particularly great post on our website. Keep up the good writing.
I was very pleased to uncover this site. I wanted to thank you for ones time just for this fantastic read!! I definitely really liked every part of it and i also have you book marked to see new stuff on your site.
A fascinating discussion is worth comment. I do believe that you need to publish more about this subject, it might not be a taboo subject but typically people do not discuss these subjects. To the next! Many thanks.
I seriously love your site.. Very nice colors & theme. Did you create this web site yourself? Please reply back as I’m attempting to create my own personal website and want to know where you got this from or what the theme is called. Kudos.
It’s hard to come by experienced people for this topic, but you sound like you know what you’re talking about! Thanks
Howdy! This post could not be written any better! Reading through this post reminds me of my previous roommate! He constantly kept talking about this. I’ll send this post to him. Pretty sure he’s going to have a good read. I appreciate you for sharing!
Great info. Lucky me I recently found your blog by accident (stumbleupon). I’ve bookmarked it for later!
Very good post! We will be linking to this particularly great content on our website. Keep up the great writing.
An impressive share! I’ve just forwarded this onto a coworker who was conducting a little homework on this. And he actually bought me lunch because I stumbled upon it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanx for spending some time to discuss this issue here on your website.
I blog often and I really thank you for your content. The article has really peaked my interest. I’m going to bookmark your site and keep checking for new information about once a week. I subscribed to your Feed too.
Greetings! Very helpful advice within this post! It is the little changes which will make the largest changes. Thanks a lot for sharing!
Good article. I am experiencing some of these issues as well..
After I initially left a comment I appear to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get four emails with the exact same comment. Perhaps there is an easy method you can remove me from that service? Many thanks.
Aw, this was an incredibly good post. Taking the time and actual effort to make a good article… but what can I say… I hesitate a lot and never manage to get anything done.
Having read this I thought it was very enlightening. I appreciate you spending some time and energy to put this article together. I once again find myself spending way too much time both reading and leaving comments. But so what, it was still worth it!
I wanted to thank you for this fantastic read!! I absolutely loved every bit of it. I’ve got you bookmarked to look at new stuff you post…
Oh my goodness! Impressive article dude! Many thanks, However I am encountering difficulties with your RSS. I don’t understand the reason why I can’t subscribe to it. Is there anybody getting similar RSS issues? Anyone that knows the solution can you kindly respond? Thanks.
Aw, this was a really good post. Taking a few minutes and actual effort to produce a really good article… but what can I say… I hesitate a whole lot and don’t seem to get anything done.
To the dheeraj3choudhary.com admin, Your posts are always a great source of knowledge.
This website was… how do I say it? Relevant!! Finally I’ve found something which helped me. Kudos!
This web site certainly has all of the information I needed about this subject and didn’t know who to ask.
Everything is very open with a clear description of the challenges. It was definitely informative. Your site is very helpful. Many thanks for sharing.
Hi, I do think this is an excellent site. I stumbledupon it 😉 I’m going to revisit yet again since I bookmarked it. Money and freedom is the best way to change, may you be rich and continue to guide other people.
You made some really good points there. I looked on the web to learn more about the issue and found most people will go along with your views on this web site.
Hi dheeraj3choudhary.com owner, Good work!
May I simply just say what a comfort to find somebody who genuinely understands what they’re discussing online. You actually understand how to bring a problem to light and make it important. A lot more people need to check this out and understand this side of the story. I can’t believe you’re not more popular given that you definitely possess the gift.
I was pretty pleased to discover this site. I wanted to thank you for your time for this particularly wonderful read!! I definitely enjoyed every bit of it and i also have you bookmarked to see new things on your web site.
Everything is very open with a really clear explanation of the issues. It was truly informative. Your site is very useful. Many thanks for sharing.
Everything is very open with a precise description of the issues. It was truly informative. Your site is very helpful. Many thanks for sharing.
Having read this I believed it was very enlightening. I appreciate you spending some time and energy to put this information together. I once again find myself spending way too much time both reading and leaving comments. But so what, it was still worth it!