TERRAFORM 3-EC2 INSTANCES BEHIND AN ELASTIC APPLICATION LOAD BALANCER (ALB),S AND CONFIGURE WITH BASH USERDATA
- Create a VPC
- Create 3 Public Subnets
- Create 3 Internet Gateways
- Public Route Tables
- Security Group
- Three EC2 Instances
- Create Target Group and ALB
- Create provider.tf to mention cloud provider details.
- Define all required variables in the variable.tf file.
- Create the vpc_subnet.tf file to set up VPC, Subnets, Internet gateways, Route Tables.
- Create the security.tf file to allow for SSH, HTTP and HTTPS connections.
- Create an ec2.tf file.
- Create the data.tf file.
- View the alb.tf file. The target group makes our deployed servers the target of our A.L.B.
- Create a bash userdata file.
- The Output.tf file helps us obtain useful data including our instances' public IP addresses and the DNS of our ALB.
After creating all required tf files, we initiate terraform to download every necessary module and providers. Before doing that we can format all tf files to make them more readable using
terraform fmt
.
terraform validate
: This command is used to validate the codes which are written. If there is any syntax mismatching, then it will throw an error to get this fixed, else, you'd get a prompt that says,Success! The configuration is valid.
terraform init
: This command initializes the providers and/or modules mentioned in your script(s). Which means by running this cmd, terraform simply downloads all APIs relevant to your specific cloud provider and modules to ensure successful execution of further operations.
terraform plan
: afterterraform init
runs successfully,terraform plan
displays the details of how terraform will proceed with executing all the commands stated in our script(s) to provision our desired resources in the right order.
terraform apply
: withterraform apply
we finally deploy our script(s) to provision all the resources we have stated in our actual cloud environment (which in this case is AWS).Finally, paste the
alb_id
from the output on your browser, and refresh multiple times to view the ALB balancing the load across the three different ec2-instances. See snapshots .ACKNOWLEDGEMENT: How to Deploy a Set of EC2 Instances behind an ALB Using Terraform by Plain English