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 let’s 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.
#-------------------------Data Block to fetch vpc id--------------------- data "aws_vpc" "GetVPC" { filter { name = "tag:Name" values = ["CustomVPC"] } } #-------------------------Variable for RDS Configuration--------------------- variable "rds_instance_identifier" { type = string default = "terraform-mysql" } variable "db_name" { type = string default = "terraform_test_db" } variable "db_password" { type = string default = "" } variable "db_user" { type = string default = "terraform" } #-------------------------Data Block to fetch subnet ids--------------------- data "aws_subnet_ids" "GetSubnet_Ids" { vpc_id = data.aws_vpc.GetVPC.id filter { name = "tag:Type" values = ["Public"] } }
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" }
👨🏻💻Configure Security Group For RDS👨🏻💻
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" "rds" { name = "rds_security_group" description = "Security group for RDS" vpc_id = data.aws_vpc.GetVPC.id # Keep the instance private by only allowing traffic from the web server. ingress { from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } # Allow all outbound traffic. egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "rds-security-group" } }
👨🏻💻Deploy Database Subnet Group👨🏻💻
🔳 Resource
✦ aws_db_subnet_group:- This resource provides an RDS database subnet group.
🔳 Arguments
✦ name:- This is an optional argument to provide a name to the DB Subnet group instance, if not provided terraform is auto assigns a name.
✦ subnet_ids:- This is a mandatory argument to mention the list of subnet ids under which the RDS instance will be launched.
resource "aws_db_subnet_group" "rds_db_subnet_group" { name = "rds-subnet-group" subnet_ids = data.aws_subnet_ids.GetSubnet_Ids.ids }
👨🏻💻Deploy RDS Instance👨🏻💻
🔳 Resource
✦ aws_db_instance:- This resource is used to launch the RDS instance resource.
🔳 Arguments
✦ identifier:- This is an optional argument to provide a name to the rds instance, if not provided terraform is auto assigns a name.
✦ allocated_storage:- This is a mandatory argument to mention storage allocation in gigabytes.
✦ engine:- This is a mandatory argument to mention the database engine to be used.
✦ engine_version:- This is an optional argument to the specific engine version to be used.
✦ instance_class:- This is a mandatory argument to mention the instance type of RDS instance to be launched.
✦ name :- This is an optional argument to provide a database name to be created after the RDS instance is launched.
✦ username :- This is a mandatory argument to mention the username for the master DB user.
✦ password :- This is a mandatory argument to set a password for the master DB user. Note:- that this may show up in logs, and it will be stored in the state file.
✦ db_subnet_group_name :- This is an optional argument to mention where the DB instance will be created in the VPC associated with the DB subnet group. If unspecified will be created in the default VPC.
✦ vpc_security_group_ids :- This is an optional argument to provide a list of VPC security groups.
✦ skip_final_snapshot:- This option argument to determine whether a final DB snapshot is to be created before the DB instance is deleted. If specified as true, no database snapshot is created. If specified as false, a database snapshot is created before the DB instance is deleted, using the value from final_snapshot_identifier.
✦ final_snapshot_identifier:- This option argument to determine the name of your final Database snapshot when this database instance is deleted. Must be provided if skip_final_snapshot is set to false. The value must begin with a letter, only contain alphanumeric characters and hyphens, and not end with a hyphen or contain two consecutive hyphens.
resource "aws_db_instance" "rds_instance" { identifier = "${var.rds_instance_identifier}" allocated_storage = 5 engine = "mysql" engine_version = "5.6.35" instance_class = "db.t2.micro" name = "${var.db_name}" username = "${var.db_name}" password = "${var.db_password}" db_subnet_group_name = "${aws_db_subnet_group.rds_db_subnet_group.id}" vpc_security_group_ids = ["${aws_security_group.rds.id}"] skip_final_snapshot = true final_snapshot_identifier = "Ignore" }
👨🏻💻Configure parameter group for RDS Instance👨🏻💻
🔳 Resource
✦ aws_db_parameter_group:- This resource creates an RDS parameter group which is a collection of engine configuration values that you set for your RDS database instance.
🔳 Arguments
✦ name:- This is an optional argument to mention the name of the parameter group if not provided terraform will auto-generate a name for it.
✦ description:- This is an optional argument to define descriptive details about the parameter group.
✦ family:-This is a mandatory argument to define family for the parameter group.
Parameter blocks support the following arguments: ✦ name:- This is a mandatory argument to define the name of the database parameter.
✦ value:- This is a mandatory argument to define the value of the database parameter defined in the name.
✦ apply_method:- This option argument to determine whether the parameter should be immediately applicable or after reboot.
resource "aws_db_parameter_group" "rds_para_grp" { name = "rds-param-group" description = "Parameter group for mysql5.6" family = "mysql5.6" parameter { name = "character_set_server" value = "utf8" } parameter { name = "character_set_client" value = "utf8" } }
🔳 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 "aws_db_subnet_group" { value = aws_db_subnet_group.rds_db_subnet_group.id description = "This is DB Subnet Group id." } output "aws_db_instance" { value = aws_db_instance.rds_instance.id description = "This is RDS instance ID." } output "aws_db_parameter_group" { value = aws_db_parameter_group.rds_para_grp.id description = "This is RDS parameter 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 Subnet Group
⛔️ AWS Database Instance
⛔️ AWS DB Parameter Group
🥁🥁 Conclusion 🥁🥁
In this blog, we have configured the below resources
✦ AWS DB subnet group.
✦ AWS Security Group for the RDS Instance.
✦ AWS RDS Instance.
✦ AWS DB Parameter 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 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 ...
You actually make it appear really easy together with your presentation but I
in finding this topic to be really something that I think I
would never understand. It sort of feels too complex and very huge for me.
I am looking forward in your subsequent submit, I will
try to get the hold of it! Najlepsze escape roomy
I was reading some of your content on this internet site and
I think this internet site is rattling instructive! Keep on posting.?
I like this site it’s a master piece! Glad I found this ohttps://69v.topn google.Raise your business
I used to be able to find good information from your articles.
Your style is unique in comparison to other folks I’ve read stuff from. Many thanks for posting when you have the opportunity, Guess I will just bookmark this site.
Hello there! I could have sworn I’ve been to your blog before but after looking at a few of the articles I realized it’s new to me. Anyways, I’m definitely delighted I came across it and I’ll be book-marking it and checking back often!
You have made some good points there. I checked on the net for more info about the issue and found most people will go along with your views on this web site.
Good information. Lucky me I found your website by accident (stumbleupon). I have saved as a favorite for later.
bookmarked!!, I love your web site!
Very good write-up. I certainly appreciate this site. Stick with it!
You need to be a part of a contest for one of the finest blogs online. I am going to recommend this site!
An intriguing discussion is definitely worth comment. There’s no doubt that that you should publish more on this subject, it might not be a taboo matter but usually people do not discuss such topics. To the next! Kind regards!
Greetings! Very useful advice within this post! It’s the little changes that produce the biggest changes. Thanks a lot for sharing!
After looking over a number of the articles on your site, I truly appreciate your way of writing a blog. I saved it to my bookmark webpage list and will be checking back soon. Please visit my web site too and let me know your opinion.
I seriously love your blog.. Very nice colors & theme. Did you create this website yourself? Please reply back as I’m attempting to create my own personal website and would love to know where you got this from or just what the theme is named. Many thanks!
This website was… how do you say it? Relevant!! Finally I’ve found something which helped me. Thanks!
This blog was… how do I say it? Relevant!! Finally I have found something which helped me. Many thanks!
Very good info. Lucky me I ran across your site by chance (stumbleupon). I’ve book-marked it for later.
The very 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 through, but I actually thought you would probably have something useful to talk about. All I hear is a bunch of moaning about something that you could possibly fix if you weren’t too busy looking for attention.
Hi! I simply want to offer you a big thumbs up for your excellent information you have got right here on this post. I am returning to your website for more soon.
Everything is very open with a very clear description of the challenges. It was definitely informative. Your site is very useful. Thank you for sharing.
Good day! I could have sworn I’ve visited this site before but after going through a few of the articles I realized it’s new to me. Regardless, I’m certainly delighted I came across it and I’ll be bookmarking it and checking back frequently!
I’d like to thank you for the efforts you have put in penning this site. I’m hoping to check out the same high-grade content from you later on as well. In fact, your creative writing abilities has inspired me to get my own blog now 😉
Good post. I learn something totally new and challenging on blogs I stumbleupon on a daily basis. It will always be useful to read articles from other writers and practice something from their sites.
I want to to thank you for this very good read!! I definitely enjoyed every bit of it. I have you book-marked to check out new things you post…
Hello there! I could have sworn I’ve been to your blog before but after browsing through a few of the posts I realized it’s new to me. Regardless, I’m definitely delighted I found it and I’ll be book-marking it and checking back regularly!
Greetings! Very helpful advice within this post! It’s the little changes which will make the most important changes. Thanks a lot for sharing!
I must thank you for the efforts you have put in writing this blog. I really hope to see the same high-grade blog posts from you in the future as well. In fact, your creative writing abilities has encouraged me to get my own, personal site now 😉
This is a great tip especially to those new to the blogosphere. Short but very precise information… Thank you for sharing this one. A must read post!
bookmarked!!, I love your site!
After exploring a few of the articles on your site, I honestly like your way of writing a blog. I saved it to my bookmark webpage list and will be checking back soon. Please visit my web site too and tell me how you feel.
This is a very good tip especially to those new to the blogosphere. Short but very accurate information… Appreciate your sharing this one. A must read post.
Your style is very unique in comparison to other folks I have read stuff from. I appreciate you for posting when you have the opportunity, Guess I’ll just book mark this blog.
Good information. Lucky me I discovered your website by accident (stumbleupon). I’ve saved as a favorite for later!
Way cool! Some very valid points! I appreciate you writing this post and also the rest of the website is really good.
Way cool! Some very valid points! I appreciate you penning this write-up and also the rest of the site is very good.
Having read this I believed it was rather enlightening. I appreciate you spending some time and energy to put this content together. I once again find myself spending a lot of time both reading and leaving comments. But so what, it was still worthwhile!
Greetings! Very helpful advice in this particular article! It’s the little changes that will make the largest changes. Many thanks for sharing!
After looking over a number of the blog articles on your web page, I honestly appreciate your way of blogging. I saved it to my bookmark webpage list and will be checking back soon. Please visit my web site as well and let me know what you think.
Can I simply just say what a comfort to find an individual who really understands what they are talking about on the net. You actually know how to bring a problem to light and make it important. A lot more people should look at this and understand this side of the story. I can’t believe you aren’t more popular given that you definitely have the gift.
Aw, this was an incredibly nice post. Spending some time and actual effort to create a top notch article… but what can I say… I put things off a lot and don’t manage to get nearly anything done.
bookmarked!!, I really like your blog.
Spot on with this write-up, I actually feel this site needs a lot more attention. I’ll probably be back again to read through more, thanks for the information!
Way cool! Some very valid points! I appreciate you penning this write-up and also the rest of the website is extremely good.
Everything is very open with a really clear clarification of the challenges. It was definitely informative. Your site is useful. Many thanks for sharing.
I could not resist commenting. Exceptionally well written!
I really like reading through a post that can make people think. Also, thank you for allowing for me to comment.
Next time I read a blog, Hopefully it won’t fail me as much as this one. I mean, Yes, it was my choice to read through, however I really believed you would probably have something helpful to say. All I hear is a bunch of complaining about something you could possibly fix if you were not too busy looking for attention.
Hey there! I simply wish to give you a big thumbs up for your excellent info you have right here on this post. I will be coming back to your web site for more soon.
Very good article. I am experiencing many of these issues as well..
Way cool! Some extremely valid points! I appreciate you penning this write-up and also the rest of the site is very good.
This is a topic which is close to my heart… Take care! Exactly where are your contact details though?
You should take part in a contest for one of the greatest blogs on the web. I will recommend this website!
After exploring a handful of the blog posts on your site, I really like your way of blogging. I book marked it to my bookmark site list and will be checking back soon. Take a look at my website as well and tell me your opinion.
That is a good tip particularly to those new to the blogosphere. Short but very accurate information… Thanks for sharing this one. A must read article!
I really like it when people come together and share ideas. Great website, stick with it!
An interesting discussion is worth comment. There’s no doubt that that you ought to write more about this issue, it may not be a taboo subject but typically people do not speak about such subjects. To the next! Many thanks!
It’s difficult to find experienced people for this subject, however, you seem like you know what you’re talking about! Thanks
I want to to thank you for this very good read!! I definitely loved every bit of it. I have got you book marked to check out new things you post…
This blog was… how do I say it? Relevant!! Finally I have found something that helped me. Thanks a lot.
Aw, this was a very good post. Finding the time and actual effort to create a great article… but what can I say… I put things off a whole lot and don’t seem to get anything done.
Hi there! I could have sworn I’ve been to this site before but after looking at some of the articles I realized it’s new to me. Anyways, I’m certainly delighted I stumbled upon it and I’ll be bookmarking it and checking back regularly.
Aw, this was a really nice post. Taking the time and actual effort to produce a superb article… but what can I say… I procrastinate a lot and don’t manage to get nearly anything done.
I couldn’t refrain from commenting. Very well written!
I really like reading a post that can make people think. Also, many thanks for allowing for me to comment.
A motivating discussion is definitely worth comment. I do believe that you need to write more about this subject matter, it might not be a taboo subject but usually folks don’t speak about such topics. To the next! Kind regards.
I absolutely love your site.. Pleasant colors & theme. Did you develop this website yourself? Please reply back as I’m wanting to create my very own blog and would love to find out where you got this from or just what the theme is called. Appreciate it.
I like this site very much, Its a very nice position to read and
get info. Euro travel guide
This is a topic that is close to my heart… Many thanks! Where are your contact details though?
You’ve made some decent points there. I looked on the net to learn more about the issue and found most people will go along with your views on this site.
I really love your website.. Pleasant colors & theme. Did you create this website yourself? Please reply back as I’m hoping to create my very own blog and would like to know where you got this from or what the theme is called. Kudos.
Very good write-up. I certainly appreciate this site. Thanks!
Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I may return once again since i have book-marked it. Money and freedom is the greatest way to change, may you be rich and continue to help other people.
I couldn’t resist commenting. Perfectly written!
An interesting discussion is worth comment. I do think that you ought to publish more on this issue, it might not be a taboo subject but generally folks don’t talk about such issues. To the next! Best wishes!
Oh my goodness! Incredible article dude! Many thanks, However I am encountering difficulties with your RSS. I don’t know the reason why I can’t subscribe to it. Is there anybody having the same RSS problems? Anyone that knows the solution will you kindly respond? Thanx!!
Hi, I do believe this is an excellent blog. I stumbledupon it 😉 I will revisit yet again since i have bookmarked it. Money and freedom is the best way to change, may you be rich and continue to help other people.
Hello, There’s no doubt that your web site may be having browser compatibility issues. When I take a look at your site in Safari, it looks fine however when opening in Internet Explorer, it has some overlapping issues. I simply wanted to give you a quick heads up! Other than that, great blog.
I was able to find good info from your blog articles.
You have made some good points there. I looked on the web for more information about the issue and found most people will go along with your views on this web site.
Good day! I could have sworn I’ve been to this website before but after looking at some of the articles I realized it’s new to me. Anyhow, I’m definitely happy I came across it and I’ll be book-marking it and checking back regularly.
This page certainly has all the information I wanted concerning this subject and didn’t know who to ask.
Hello! I could have sworn I’ve been to this site before but after looking at a few of the articles I realized it’s new to me. Regardless, I’m definitely pleased I discovered it and I’ll be bookmarking it and checking back often!
Way cool! Some extremely valid points! I appreciate you writing this write-up plus the rest of the site is also very good.
Howdy! I just wish to give you a big thumbs up for your great info you’ve got right here on this post. I’ll be returning to your web site for more soon.
Great post! We will be linking to this particularly great post on our website. Keep up the great writing.
Everything is very open with a precise explanation of the issues. It was really informative. Your website is extremely helpful. Thank you for sharing!
May I simply say what a relief to uncover somebody that really understands what they are discussing online. You actually know how to bring a problem to light and make it important. More people need to look at this and understand this side of the story. I was surprised that you are not more popular since you definitely have the gift.
I was very pleased to uncover this site. I need to to thank you for ones time for this wonderful read!! I definitely savored every little bit of it and I have you bookmarked to look at new things in your blog.
I’m impressed, I have to admit. Rarely do I come across a blog that’s equally educative and entertaining, and without a doubt, you have hit the nail on the head. The issue is an issue that too few folks are speaking intelligently about. Now i’m very happy that I stumbled across this during my hunt for something relating to this.
Excellent post! We are linking to this particularly great content on our site. Keep up the great writing.
This site was… how do I say it? Relevant!! Finally I have found something that helped me. Thank you.
Good article. I will be going through a few of these issues as well..
You ought to be a part of a contest for one of the greatest websites on the web. I will highly recommend this website!
Great blog you have got here.. It’s hard to find high quality writing like yours these days. I truly appreciate individuals like you! Take care!!
Hi, I do believe this is a great site. I stumbledupon it 😉 I will return once again since i have saved as a favorite it. Money and freedom is the best way to change, may you be rich and continue to guide others.
Hi, I do believe your website could possibly be having browser compatibility problems. Whenever I take a look at your site in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping issues. I simply wanted to give you a quick heads up! Aside from that, fantastic site.
Aw, this was a very good post. Spending some time and actual effort to produce a great article… but what can I say… I hesitate a whole lot and don’t seem to get anything done.
Nice post. I learn something totally new and challenging on sites I stumbleupon on a daily basis. It will always be interesting to read content from other authors and use a little something from their websites.
This is the perfect site for everyone who wants to find out about this topic. You realize a whole lot its almost hard to argue with you (not that I really will need to…HaHa). You certainly put a new spin on a subject which has been written about for years. Wonderful stuff, just wonderful.
Greetings! I know this is kind of off topic but
I was wondering which blog platform are you using for this site?
I’m getting fed up of WordPress because I’ve had problems with
hackers and I’m looking at options for another platform.
I would be great if you could point me in the direction of a good platform.
Good day! I could have sworn I’ve been to this web site before but after browsing through a few of the articles I realized it’s new to me. Anyways, I’m definitely delighted I found it and I’ll be book-marking it and checking back regularly!
Saved as a favorite, I like your site.
I need to to thank you for this excellent read!! I definitely loved every little bit of it. I have got you book-marked to look at new stuff you post…
Pretty! This was an extremely wonderful post. Many thanks for supplying this info.
May I just say what a relief to find somebody that really knows what they’re discussing over the internet. You actually know how to bring a problem to light and make it important. More people must check this out and understand this side of your story. I was surprised that you are not more popular given that you definitely possess the gift.
Your style is very unique compared to other folks I’ve read stuff from. Many thanks for posting when you’ve got the opportunity, Guess I will just bookmark this site.
Excellent post. I absolutely love this site. Stick with it!
Pretty! This has been a really wonderful article. Thanks for providing this information.
Next time I read a blog, I hope that it does not disappoint me just as much as this particular one. After all, I know it was my choice to read through, but I genuinely thought you’d have something helpful to talk about. All I hear is a bunch of crying about something that you could possibly fix if you were not too busy searching for attention.
g27mgm
Hi there! I could have sworn I’ve visited this blog before but after looking at a few of the posts I realized it’s new to me. Anyhow, I’m definitely delighted I discovered it and I’ll be book-marking it and checking back regularly.
Very good article. I will be going through many of these issues as well..
Your style is really unique compared to other folks I have read stuff from. Thank you for posting when you have the opportunity, Guess I’ll just book mark this site.
This is some awesome thinking. Would you be interested to learn more? Come to see my website at Article Sphere for content about SEO.
Good post. I learn something new and challenging on sites I stumbleupon on a daily basis. It will always be exciting to read through content from other authors and use something from their sites.