Building an AWS EC2 Instance and Security Group with Terraform

In this guide, we’ll walk through the steps to create an AWS EC2 instance and a security group using Terraform. Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure in a declarative way. By the end of this guide, you’ll have a fully functional EC2 instance with a security group that allows SSH access.

This is Part 1 of the series. In Part 2, we’ll cover how to create a VPC and place the EC2 instance within it.

Here’s what we’ll cover in this part:

Define the AWS Provider

To specify the cloud provider and the region where resources will be created, define the AWS provider in the main.tf file. The provider block specifies AWS as the cloud provider, and the region argument sets the AWS region to us-east-1.

provider "aws" { region = "us-east-1" }

Define the EC2 Instance and Security Group

Next, define the resources in the main.tf file. This includes creating an EC2 instance and a security group that allows SSH access.

Create the EC2 Instance

Use the aws_instance resource block to define an EC2 instance. The ami argument specifies the Amazon Machine Image (AMI) ID, which determines the operating system and software for the instance. In this example, an Ubuntu AMI (ami-04b4f1a9cf54c11d0) is used. The instance_type argument defines the instance type, such as t2.micro, a small, low-cost instance. The key_name argument specifies the key pair used for SSH access. The security_groups argument associates the instance with the security group named instance-sg. The tags block assigns metadata to the instance, such as its name.

resource "aws_instance" "instance1" { ami = "ami-04b4f1a9cf54c11d0" instance_type = "t2.micro" key_name = "jenkins" security_groups = ["instance-sg"] tags = { Name = "Terraform-first-instance" } }

Create the Security Group

Use the aws_security_group resource block to define a security group. The name and description arguments provide a name and description for the security group. The ingress block defines an inbound rule that allows SSH access (port 22) from any IP address (0.0.0.0/0). The egress block allows all outbound traffic. The tags block assigns metadata to the security group, such as its name.

resource "aws_security_group" "instance-sg" { name = "instance-sg" description = "SSH Access allowed" ingress { description = "SSH access" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "security-group" } }

Apply the Terraform Configuration

Once the configuration files are ready, follow these steps to deploy the resources:

  1. Initialize Terraform: Run the following command to initialize the working directory and download the necessary provider plugins:
    terraform init
  2. Validate the Configuration: Validate the syntax and configuration using:
    terraform validate
  3. Preview Changes: Use the terraform plan command to see what resources will be created:
    terraform plan
  4. Apply the Configuration: Finally, apply the configuration to create the resources:
    terraform apply
    Confirm the action by typing yes when prompted.

Verify the Resources

After the terraform apply command completes, verify the resources in the AWS Management Console:

Conclusion

By following these steps, you’ve successfully created an AWS EC2 instance and a security group using Terraform. This setup allows you to manage your infrastructure as code, making it easier to version, share, and replicate your configurations. In Part 2, we’ll cover how to create a VPC and place the EC2 instance within it.