Streamline and Automate EC2 Instance Management with Terraform and Resource Groups
A resource group is a collection of AWS resources that are all in the same AWS Region, and that match the criteria specified in the group’s query. For a list of the services that support AWS Resource Groups and a brief description of what each service allows you to do with a resource group, see AWS services that work with AWS Resource Groups.
As we know that Terraform is an infrastructure as a code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
Now we are trying to create two EC2 instances with VPC. First we have to create a provider.tf with the region we’re using to set up our infrastructure.
VPC and EC2 instance:
When setting up a new VPC to deploy EC2 instances, we usually follow these basic steps.
- Create a vpc
- Create subnets for different parts of the infrastructure
- Attach an internet gateway to the VPC
- Create a route table for a public subnet
- Create security groups to allow specific traffic
- Create ec2 instances on the subnets
Create a vpc: Create a vpc.tf which enables you to build a virtual network in the AWS cloud. We can create AWS resources, such as Amazon EC2 instances, into the subnets of your VPC. some tags are also added to vpc to identify which resource group it belongs to.
Next create igw.tf which creates an internet gateway and attaches it to the vpc id. Now we need a route table to handle routing to one or more of the subnets. In the below we can see a new route table on the vpc. We can also specify the routes to route internet traffic through the gateway. So the route table and internet gateway are set up on The VPC, now we just need to associate any public subnets with the route table.
Next create a security-group.tf it acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance and outbound rules control the outgoing traffic from your instance.
Sample instance.tf which creates two instances with AMI ,instance type and below you can see tags.
Created local.tf and variable.tf which defines some custom-tags.. as mentioned below.
Resource groups: Resource Groups is the service that lets you manage and automate tasks on large numbers of resources at one time.
What are resource groups?
You can use resource groups to organize your AWS resources. AWS Resource Groups is the service that lets you manage and automate tasks on large numbers of resources at one time
Created resource-group.tf with name test-group and some json query added to filter which resource we required and also to filter tags.
finally output.tf these Outputs are also necessary to share data. In this file you can see data source is used to get information about resources external to Terraform, and use them to set up your Terraform resources.
Run the terraform commands:
- terraform init : Setup a new terraform project for this file.
- terraform plan : To preview the actions as it’s defined in the .tf file.
- terraform apply : Setup the infrastructure as it’s defined in the .tf file.
- terraform destroy: Tear down everything that terraform created.
Now we are able to see the resources which are created in the AWS console.
resource-group with tag filter
group of resources with tag based
For more information you can check the link below.
Resource Groups are used to organize our AWS resources like to manage and automate tasks on large numbers of resources at one time.
The above one is the best example of how we are grouping ec2 instances with tag base and Automating deployments with Terraform is a great way of creating reliable and easy to customize configurations for many use cases.