Skip to content

The ec2.tf File

Filename Location Group Project/Repository
ec2.tf ./ec2.tf infrastructure terraform

Why?

We'll be deploying two EC2 instances for our application. Without such instances we cannot host our Golang based application.

Our instances can start off very small for the time being. In time they might grow in size.

Breakdown

All we're doing is defining two EC2 Instances. Let's look at one and break it down:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
resource "aws_instance" "meow_1" {
  instance_type = "t2.micro"
  key_name      = aws_key_pair.httpcats.key_name
  ami           = local.ubuntu_ami
  subnet_id     = aws_subnet.httpcats-http-az-a.id

  vpc_security_group_ids = [
    aws_security_group.webserver.id,
  ]

  tags = merge({
    Name = "httpcats-meow-1"
  }, local.common_tags)
}

We're configuring a few attributes here:

The instance_type attribute is being set to t2.micro. This value might be a perfect candidate for a variable {}. What do you think?

The key_name is set to the Key Pair we defined previously. This tells AWS to configure the instance with our key so we can SSH into the host. If the host was using Windows Server, the Key Pair would be used to encrypt the Administrator user's password.

We're using our ubuntu_ami local variable in the ami attribute, telling AWS what OS we want to create the instance with.

We've previously created subnets in our VPC and now we're using them in our EC2 instance definition via subnet_id.

And finally we define the vpc_security_group_ids attribute and give it a list of Security Groups.

Now we can define a second EC2 instance, practically identical to the first, and we're done:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
resource "aws_instance" "meow_2" {
  instance_type = "t2.micro"
  key_name      = aws_key_pair.httpcats.key_name
  ami           = local.ubuntu_ami
  subnet_id     = aws_subnet.httpcats-http-az-b.id

  vpc_security_group_ids = [
    aws_security_group.webserver.id,
  ]

  tags = merge({
    Name = "httpcats-meow-2"
  }, local.common_tags)
}

Notice that the subnet_id is different. So is the Name key in the tags attribute.

The Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
resource "aws_instance" "meow_1" {
  instance_type = "t2.micro"
  key_name      = aws_key_pair.httpcats.key_name
  ami           = local.ubuntu_ami
  subnet_id     = aws_subnet.httpcats-http-az-a.id

  vpc_security_group_ids = [
    aws_security_group.webserver.id,
  ]

  tags = merge({
    Name = "httpcats-meow-1"
  }, local.common_tags)
}

resource "aws_instance" "meow_2" {
  instance_type = "t2.micro"
  key_name      = aws_key_pair.httpcats.key_name
  ami           = local.ubuntu_ami
  subnet_id     = aws_subnet.httpcats-http-az-b.id

  vpc_security_group_ids = [
    aws_security_group.webserver.id,
  ]

  tags = merge({
    Name = "httpcats-meow-2"
  }, local.common_tags)
}

Terraform Documentation

Type Documentation
aws_instance Terraform AWS Provider

Committing the Code

  1. Set your working directory to the infrastructure/terraform repository
  2. Save the file as ec2.tf and use git add ec2.tf to add it to the Git staging area
  3. Use git commit -am 'creating the compute resources for our application' to commit the file to our repository
  4. Push the code to GitLab.com: git push

Last update: August 25, 2021