Terraform 8 of 10: Modules using AWS Cloud9

Background

This tutorial deals with Terraform Modules. AWS Cloud9 is the environment. If you recall in an earlier tutorial, it was mentioned that there were more efficient ways of creating a number of resources more efficiently. This tutorial may prove useful if you are interested in that.

1 of 10. Open the documentation for "Modules" and aws_instance

Resource: aws_instance

Modules

2 of 10. Open your AWS Cloud9 environment

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

3 of 10. Create directories and files for this tutorial

Make sure to start from your "environment" folder. If the mkdir/touch commands are unfamiliar to you (though I think I've used them in prior tutorials already), check the man pages links in the reference section below. The terraform-module-ec2 folder will be your main project folder. Note that there are two main.tf files: terraform-module-ec2/main.tf and terraform-module-ec2/modules/ec2_instance/main.tf.

pwd mkdir terraform-module-ec2 && cd terraform-module-ec2 touch main.tf mkdir -p modules/ec2_instance touch modules/ec2_instance/variables.tf modules/ec2_instance/outputs.tf modules/ec2_instance/main.tf

4 of 10. Modify "modules/ec2_instance/variables.tf" to define input variables.

Define input variables for the module. This should be familiar to you, from prior tutorial. WARNING: Make sure that you're modifying the correct file in the correct folder.

variable "instance_name" { description = "Name of the EC2 instance" type = string } variable "ami_id" { description = "AMI ID for the EC2 instance" type = string } variable "instance_type" { description = "EC2 instance type" type = string default = "t2.micro" } variable "instance_count" { description = "Number of EC2 instances to create" type = number }

5 of 10. Modify "modules/ec2_instance/outputs.tf" to create output values.

Here, you are defining the output variables.

output "instance_ids" { value = aws_instance.webserver[*].id } output "public_ips" { value = aws_instance.webserver[*].public_ip }

6 of 10. Modify "modules/ec2_instance/main.tf" to refer to the variables.

Also, note that count.index starts at 0, so, there is a +1, so that the first one will be 0+1 = 1, the second will be 1+1 = 2, and so on.

resource "aws_instance" "webserver" { count = var.instance_count ami = var.ami_id instance_type = var.instance_type tags = { Name = "${var.instance_name}-${count.index + 1}" } }

7 of 10. Modify the "main.tf" in the "terraform-module-ec2" folder to refer to module as well as define inputs and outputs.

Note: This is not the same as the other main.tf. The other main.tf was specific to the ec2_instance. This main.tf is for the entire project. This is where it ties everything together. Here, you refer to the ec2_instance module that you previously created, you input values for the variables, and you specify the outputs. Please note that AMIs are region-specific, so you should use an AMI that exists in the region you're using. (In my own case, I am using the us-east-1 region.)

provider "aws" { region = "us-east-1" } module "ec2_instances" { source = "./modules/ec2_instance" ami_id = "ami-079db87dc4c10ac91" # Amazon Linux 2023 AMI instance_count = 10 instance_name = "webserver" instance_type = "t2.micro" } output "instance_ids" { value = module.ec2_instances.instance_ids } output "public_ips" { value = module.ec2_instances.public_ips }

8 of 10. Initialize and plan

If everything works, it would create 10 instances and name them per the naming convention you provided earlier. In addition, the output values should specify the intance IDs and public IPs for the instances.

terraform fmt terraform init terraform plan

9 of 10. Change to only create two (2) instances.

Edit the main.tf, and change it to two (2) instances, instead of ten (10). Then, let's run the plan and apply. While it's fun to create 10 resources at once, for purposes of this lab, I am practicing cost avoidance. No need to launch 10 instances to prove a point, when launching 2 would also prove a point. You should see the two instances created in your console.

terraform plan terraform apply

10 of 10. Cleanup

Destroy the created resources. If you re-check your EC2 console, only your Cloud9 instance should be running.

terraform destroy

Reference

Resource: aws_instance

Modules

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

mkdir

mkdir

touch

Define input variables

Query data with outputs

The count meta-argument

Instance lifecycle

Build infrastructure

Destroy infrastructure

Comments

Popular posts from this blog

Orphaned No More: Adopting AWS Lambda

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov