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" } data "aws_vpc" "GetVPC" { filter { name = "tag:Name" values = ["CustomVPC"] } }
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" }
data "aws_instances" "ec2_list" { instance_state_names = ["running"] }
🔳 Resource
✦ aws_lb_target_group:- This resource group resources for use so that it can be associated with load balancers.
🔳 Arguments
✦ name:- This is an optional argument to define the name of the target group.
✦ port:- This is a mandatory argument to mention the port on which targets receive traffic unless overridden when registering a specific target.
✦ vpc_id:- This is a mandatory argument and refers to id of a VPC to which it would be associated.
✦ protocol:- This is a mandatory argument as our target type is “instance”. Protocol to use for routing traffic to the targets. Should be one of “TCP”, “TLS”, “UDP”, “TCP_UDP”, “HTTP” or “HTTPS”.
✦ target_type:- This is an optional argument with target types as an instance, IP, and lambda.
resource "aws_lb_target_group" "CustomTG" { name = "CustomTG" port = 80 protocol = "HTTP" vpc_id = data.aws_vpc.GetVPC.id target_type = "instance" }
🔳 Resource
✦ aws_lb_target_group_attachment:- This resource provides us the ability to register containers and instances with load balancers.
🔳 Arguments
✦ target_group_arn:- This is a mandatory argument to mention the target group ARN which would be associated with the target id.
✦ port:- This is a mandatory argument to mention the port on which targets receive traffic unless overridden when registering a specific target.
✦ target_id:- This is a mandatory argument to mention the target id as the Instance ID for an instance or the container ID for an ECS container.
resource "aws_lb_target_group_attachment" "CustomTGAttach" { count = "${length(data.aws_instances.ec2_list.ids)}" target_group_arn = aws_lb_target_group.CustomTG.arn target_id = "${data.aws_instances.ec2_list.ids[count.index]}" port = 80 }
👨🏻💻Launch Load Balancer And Its Listener👨🏻💻
Before Creating a Load Balancer lets create a data source variable to fetch a list of subnets
data "aws_subnet_ids" "GetSubnet_Ids" { vpc_id = data.aws_vpc.GetVPC.id filter { name = "tag:Type" values = ["Public"] } }
Configure Security Group For Load Balancer
The method acts as a virtual firewall to control your inbound and outbound traffic flowing to your EC2 instances inside a subnet.
🔳 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" "elb_sg" { name = "allow_http_elb" description = "Allow http inbound traffic for elb" 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 = 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-elb-security-group" } }
Create a Load Balancer and associate it with public subnets and the security group of the load balancer.
🔳 Resource
✦ aws_lb:- This resource is used to create a load balancer that helps us distribute our traffic.
🔳 Arguments
✦ name:- This is an optional argument to define the name of the Load Balancer.
✦ subnets:- This is an optional argument to mention which load balancer will be part of which subnets.
✦ security_groups:- This is an optional argument to mention which controls your inbound and outbound traffic flowing.
✦ tags:- One of the most important property used in all resources. Always make sure to attach tags for all your resources.
resource "aws_lb" "CustomELB" { name = "CustomELB" subnets = data.aws_subnet_ids.GetSubnet_Ids.ids security_groups = [aws_security_group.elb_sg.id] tags = { Name = "CustomELB" } }
Let’s now create a new load balancer listener which will be configured to accept HTTP client connections.
🔳 Resource
✦ aws_lb_listener:- This resource is used to create a load balancer listener which helps us to check for connection requests, using the protocol and port that you configure.
🔳 Arguments
✦ load_balancer_arn:- This is a mandatory argument to define arn of the Load Balancer by using arn attribute.
✦ port:- This is an optional argument to mention the port on which targets receive traffic.
✦ protocol:- This is an optional argument as our target type is “instance”. Protocol to use for routing traffic to the targets. Should be one of “TCP”, “TLS”, “UDP”, “TCP_UDP”, “HTTP” or “HTTPS”.
✦ default_action:- This is a mandatory argument to define the type of routing for this listener.
resource "aws_lb_listener" "http" { load_balancer_arn = aws_lb.CustomELB.arn port = "80" protocol = "HTTP" default_action { type = "forward" forward { target_group { arn = aws_lb_target_group.CustomTG.arn } stickiness { enabled = true duration = 28800 } } } }
🔳 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 "CustomTG" { value = aws_lb_target_group.CustomTG.id description = "This is Target Group id." } output "CustomELB" { value = aws_lb.CustomELB.id description = "This is load balancer ID." } output "elb_sg" { value = aws_security_group.elb_sg.id description = "This is Security Group 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 Target Group
⛔️ AWS Target Group Attachment
⛔️ Terraform Length Function
⛔️ AWS Load Balancer
⛔️ AWS Load Balancer Listener
🥁🥁 Conclusion 🥁🥁
In this blog, we have configured the below resources
✦ AWS Security Group for the Load Balancer.
✦ AWS Target Group and its attachment.
✦ AWS Load Balancer and its listener.
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 where we will be doing deep dive into AWS Launch Configuration & Autoscaling Group Using Terraform.
📢 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 ...
hello there and thank you for your info – I’ve definitely picked up something new from right here.
I did however expertise several technical issues using this website, as
I experienced to reload the website lots of times previous to I could get it to load properly.
I had been wondering if your hosting is OK? Not that I’m complaining, but slow loading instances times will very frequently
affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords.
Anyway I am adding this RSS to my e-mail and could look out for
a lot more of your respective intriguing content. Ensure
that you update this again soon.. Lista escape roomów
I was examining some of your posts on this website and I think this internet site is
real instructive! Keep on posting.?
Very interesting topic, regards for posting.Raise range
Examining the Art of Effective Communication
Interpersonal communication is key to success in every aspect of life. Whether it’s building bonds with others, discussing deals in business, or articulating ideas effectively, mastering the art of communication can lead to great outcomes.
In this post, we look into the diverse facets of effective communication. From oral communication to facial expressions cues, we delve into the approaches that promote concise and substantial interactions.
Successful communication involves not only articulating oneself clearly but also attentively hearing to others. We investigate the significance of engaged listening and how it boosts shared understanding and fosters improved connections.
Moreover, we talk about the role of understanding and emotional intelligence in efficient communication. Understanding feelings of others and keeping empathetic can lead to deeper relationships and solution of conflicts.
Additionally, we explore the impact of technological innovations on communication in the age of technology. While technological advancements has facilitated communication easier and more convenient, we furthermore address its potential pitfalls and the method by which to manage them.
In conclusion, mastering the skill of successful communication is crucial for success in various aspects of life. By recognizing its principles and implementing active listening, empathy, and adapting to technological advancements, individuals can build stronger connections and achieve their goals more effectively. [url=https://www.southwestteepeerental.com/copy-of-mood-boards]Exclusive event rentals serving Scottsdale and surrounding areas[/url]
There’s certainly a great deal to find out about this topic. I love all the points you have made.
I truly love your blog.. Very nice colors & theme. Did you develop this amazing site yourself? Please reply back as I’m hoping to create my own personal blog and would like to find out where you got this from or what the theme is called. Cheers!
I quite like looking through an article that can make people think. Also, thank you for permitting me to comment.
You’re so awesome! I do not suppose I’ve read anything like that before. So wonderful to discover someone with original thoughts on this subject. Seriously.. many thanks for starting this up. This website is something that’s needed on the internet, someone with some originality.
Good info. Lucky me I recently found your website by accident (stumbleupon). I’ve book marked it for later.
This is a great tip especially to those fresh to the blogosphere. Short but very precise information… Many thanks for sharing this one. A must read article.
Aw, this was an incredibly good post. Taking a few minutes and actual effort to generate a good article… but what can I say… I hesitate a whole lot and don’t manage to get nearly anything done.
There’s definately a great deal to know about this topic. I really like all of the points you made.
You ought to be a part of a contest for one of the most useful sites on the internet. I’m going to recommend this web site!
A motivating discussion is definitely worth comment. I believe that you ought to write more on this subject matter, it may not be a taboo subject but usually people do not discuss these subjects. To the next! Kind regards.
I enjoy looking through a post that will make people think. Also, thanks for allowing for me to comment.
Pretty! This has been an incredibly wonderful post. Many thanks for supplying these details.
Great web site you have got here.. It’s difficult to find high quality writing like yours these days. I truly appreciate individuals like you! Take care!!
I blog often and I really thank you for your information. This great article has really peaked my interest. I’m going to bookmark your website and keep checking for new information about once per week. I subscribed to your RSS feed as well.
Everything is very open with a precise description of the issues. It was really informative. Your site is very useful. Thanks for sharing!
You need to be a part of a contest for one of the most useful websites on the internet. I am going to highly recommend this blog!
Very good post! We are linking to this great article on our website. Keep up the good writing.
Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the site is very good.
Way cool! Some extremely valid points! I appreciate you penning this post and the rest of the website is also very good.
The next time I read a blog, I hope that it doesn’t fail me just as much as this particular one. After all, I know it was my choice to read, nonetheless I actually thought you would have something helpful to say. All I hear is a bunch of whining about something you can fix if you weren’t too busy searching for attention.
I blog frequently and I really thank you for your information. This article has really peaked my interest. I’m going to take a note of your website and keep checking for new information about once a week. I subscribed to your RSS feed as well.
Hi, I think your site may be having browser compatibility problems. Whenever I look at your blog in Safari, it looks fine however, when opening in I.E., it’s got some overlapping issues. I simply wanted to give you a quick heads up! Aside from that, wonderful blog!
When I initially commented I appear to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve four emails with the exact same comment. Perhaps there is a way you are able to remove me from that service? Thanks.
A motivating discussion is definitely worth comment. There’s no doubt that that you need to publish more about this subject, it may not be a taboo subject but usually people do not discuss these issues. To the next! Many thanks!
This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Appreciate it.
Good post. I learn something totally new and challenging on blogs I stumbleupon everyday. It’s always useful to read through articles from other authors and use something from their websites.
Aw, this was an extremely nice post. Finding the time and actual effort to make a top notch article… but what can I say… I hesitate a whole lot and don’t seem to get nearly anything done.
Good post. I learn something totally new and challenging on sites I stumbleupon everyday. It will always be exciting to read content from other writers and use a little something from other websites.
Howdy! This blog post couldn’t be written much better! Reading through this article reminds me of my previous roommate! He always kept preaching about this. I’ll forward this article to him. Pretty sure he’s going to have a very good read. Many thanks for sharing!
Very good info. Lucky me I discovered your blog by chance (stumbleupon). I have book marked it for later.
Spot on with this write-up, I truly believe that this website needs far more attention. I’ll probably be back again to read more, thanks for the info.
Your style is very unique in comparison to other folks I’ve read stuff from. Many thanks for posting when you have the opportunity, Guess I’ll just book mark this site.
Excellent site you have here.. It’s hard to find high quality writing like yours these days. I really appreciate individuals like you! Take care!!
I truly love your blog.. Excellent colors & theme. Did you make this site yourself? Please reply back as I’m hoping to create my own personal site and want to learn where you got this from or just what the theme is called. Appreciate it.
You ought to take part in a contest for one of the most useful blogs online. I will highly recommend this site!
Greetings! Very useful advice within this post! It is the little changes that produce the biggest changes. Many thanks for sharing!
Way cool! Some very valid points! I appreciate you penning this post plus the rest of the website is very good.
Next time I read a blog, I hope that it doesn’t disappoint me as much as this one. After all, I know it was my choice to read, however I really thought you would probably have something useful to talk about. All I hear is a bunch of complaining about something you can fix if you weren’t too busy searching for attention.
This excellent website truly has all the info I wanted concerning this subject and didn’t know who to ask.
Hi, I do think this is a great web site. I stumbledupon it 😉 I may return once again since i have saved as a favorite it. Money and freedom is the greatest way to change, may you be rich and continue to help others.
Great post! We will be linking to this great post on our website. Keep up the great writing.
This is a very good tip especially to those new to the blogosphere. Brief but very accurate information… Thanks for sharing this one. A must read post.
I needed to thank you for this fantastic read!! I definitely loved every little bit of it. I have you book marked to look at new things you post…
Very good post. I am experiencing a few of these issues as well..
Howdy! This blog post couldn’t be written much better! Going through this article reminds me of my previous roommate! He continually kept preaching about this. I will send this post to him. Pretty sure he will have a great read. I appreciate you for sharing!
Way cool! Some very valid points! I appreciate you writing this article plus the rest of the website is extremely good.
Everything is very open with a precise explanation of the issues. It was truly informative. Your website is very useful. Thanks for sharing.
The very next time I read a blog, I hope that it won’t fail me just as much as this particular one. After all, Yes, it was my choice to read through, however I actually believed you’d have something useful to say. All I hear is a bunch of complaining about something you could possibly fix if you weren’t too busy seeking attention.
Great post. I will be going through many of these issues as well..
Aw, this was a really good post. Taking a few minutes and actual effort to create a very good article… but what can I say… I procrastinate a whole lot and don’t seem to get nearly anything done.
Hi! I could have sworn I’ve been to this web site before but after looking at a few of the posts I realized it’s new to me. Nonetheless, I’m definitely pleased I found it and I’ll be book-marking it and checking back regularly.
This is a topic which is near to my heart… Cheers! Exactly where can I find the contact details for questions?
After checking out a few of the blog posts on your website, I seriously appreciate your way of blogging. I bookmarked it to my bookmark webpage list and will be checking back soon. Take a look at my web site as well and let me know how you feel.
I seriously love your site.. Excellent colors & theme. Did you make this site yourself? Please reply back as I’m looking to create my own site and want to find out where you got this from or just what the theme is named. Cheers!
I couldn’t refrain from commenting. Perfectly written.
Next time I read a blog, I hope that it doesn’t fail me as much as this particular one. After all, I know it was my choice to read through, but I truly thought you’d have something interesting to talk about. All I hear is a bunch of complaining about something that you could possibly fix if you weren’t too busy searching for attention.
I blog often and I really appreciate your information. Your article has really peaked my interest. I am going to book mark your blog and keep checking for new details about once a week. I subscribed to your Feed as well.
I’m amazed, I must say. Seldom do I come across a blog that’s equally educative and amusing, and without a doubt, you’ve hit the nail on the head. The issue is something that not enough folks are speaking intelligently about. Now i’m very happy I came across this in my hunt for something concerning this.
bookmarked!!, I love your website!
This is the right blog for anybody who wishes to understand this topic. You realize a whole lot its almost hard to argue with you (not that I really would want to…HaHa). You certainly put a fresh spin on a subject that has been discussed for decades. Excellent stuff, just great.
Right here is the right webpage for anyone who really wants to find out about this topic. You realize a whole lot its almost tough to argue with you (not that I really will need to…HaHa). You certainly put a brand new spin on a subject that’s been written about for a long time. Great stuff, just great.
I like looking through a post that will make people think. Also, thank you for allowing me to comment.
Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I will come back once again since I book-marked it. Money and freedom is the best way to change, may you be rich and continue to help others.
I’m amazed, I must say. Seldom do I encounter a blog that’s both educative and amusing, and let me tell you, you’ve hit the nail on the head. The problem is an issue that too few men and women are speaking intelligently about. Now i’m very happy I came across this in my hunt for something concerning this.
Having read this I thought it was really informative. I appreciate you taking the time and energy to put this information together. I once again find myself personally spending way too much time both reading and leaving comments. But so what, it was still worthwhile!
Greetings, I do believe your site may be having web browser compatibility problems. When I look at your site in Safari, it looks fine however, when opening in I.E., it has some overlapping issues. I just wanted to give you a quick heads up! Apart from that, excellent blog!
I’m amazed, I have to admit. Seldom do I encounter a blog that’s both educative and interesting, and let me tell you, you have hit the nail on the head. The problem is an issue that too few folks are speaking intelligently about. Now i’m very happy I stumbled across this during my search for something regarding this.
Hi! I could have sworn I’ve visited this blog before but after going through many of the posts I realized it’s new to me. Nonetheless, I’m definitely delighted I came across it and I’ll be book-marking it and checking back frequently.
This is a topic that’s close to my heart… Cheers! Exactly where can I find the contact details for questions?
Spot on with this write-up, I honestly feel this site needs a lot more attention. I’ll probably be back again to see more, thanks for the advice.
Very good post! We are linking to this great article on our site. Keep up the good writing.
When I originally commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on every time a comment is added I get 4 emails with the exact same comment. There has to be a means you are able to remove me from that service? Many thanks.
I was able to find good advice from your articles.
Hello, There’s no doubt that your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine however, if opening in IE, it’s got some overlapping issues. I just wanted to give you a quick heads up! Apart from that, excellent website!
I like reading a post that can make people think. Also, thanks for allowing me to comment.
Greetings! Very helpful advice in this particular post! It’s the little changes which will make the largest changes. Thanks for sharing!
Having read this I believed it was extremely enlightening. I appreciate you finding the time and effort to put this content together. I once again find myself personally spending way too much time both reading and commenting. But so what, it was still worth it!
Hi, I do think this is an excellent site. I stumbledupon it 😉 I am going to return yet again since I bookmarked it. Money and freedom is the best way to change, may you be rich and continue to help others.
Hi, I do believe your blog may be having internet browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping issues. I just wanted to give you a quick heads up! Aside from that, excellent website!
An outstanding share! I have just forwarded this onto a colleague who has been conducting a little homework on this. And he in fact bought me dinner due to the fact that I found it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to discuss this issue here on your web site.
Nice post. I learn something new and challenging on websites I stumbleupon everyday. It’s always useful to read articles from other writers and use a little something from their sites.
Everything is very open with a really clear description of the challenges. It was truly informative. Your website is extremely helpful. Many thanks for sharing!
You are so awesome! I do not think I’ve read through anything like that before. So great to discover someone with original thoughts on this topic. Seriously.. thank you for starting this up. This website is something that’s needed on the internet, someone with some originality.
bookmarked!!, I love your website!
An outstanding share! I have just forwarded this onto a coworker who was conducting a little research on this. And he in fact bought me breakfast simply because I stumbled upon it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending some time to discuss this matter here on your web site.
I needed to thank you for this great read!! I absolutely enjoyed every little bit of it. I have you saved as a favorite to look at new things you post…
Hi, I do think this is a great blog. I stumbledupon it 😉 I’m going to revisit yet again since i have book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to help others.
This page definitely has all of the information I needed concerning this subject and didn’t know who to ask.
I want to to thank you for this excellent read!! I absolutely loved every bit of it. I’ve got you saved as a favorite to check out new stuff you post…
An impressive share! I’ve just forwarded this onto a coworker who had been doing a little homework on this. And he in fact bought me lunch due to the fact that I stumbled upon it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending some time to discuss this subject here on your internet site.
Next time I read a blog, Hopefully it does not disappoint me just as much as this particular one. I mean, I know it was my choice to read, nonetheless I genuinely thought you’d have something helpful to talk about. All I hear is a bunch of whining about something you can fix if you weren’t too busy looking for attention.
I’m amazed, I have to admit. Rarely do I encounter a blog that’s both equally educative and amusing, and without a doubt, you’ve hit the nail on the head. The issue is something which too few people are speaking intelligently about. Now i’m very happy that I stumbled across this during my hunt for something regarding this.
Профессиональный сервисный центр по ремонту бытовой техники с выездом на дом.
Мы предлагаем: сервисные центры по ремонту техники в москве
Наши мастера оперативно устранят неисправности вашего устройства в сервисе или с выездом на дом!