Launch an EC2 instance using Terraform

Launch an EC2 instance using Terraform

Terraform File + Commands -> EC2 instance

·

5 min read

Intro

In this blog, I'll tell you how to create an AWS EC2 instance using Terraform. You'll also learn how to delete the resources using Terraform.

Prerequisite

There's nothing much required to get started, but you should have -

Terraform

Terraform is an open-source infrastructure as code software tool that enables you to safely and predictably create, change, and improve infrastructure.

We will use this tool to build a simple infrastructure. It will help you get started on your Terraform learning journey. Let's begin, shall we?

Create a terraform user in AWS

We have to provide credentials in the terraform file to launch the instance in our AWS account. For this, we will create an IAM user named terraform in our AWS account and attach the AdministratorAccess policy to the user.

Follow the below steps to do so:

  1. Navigate to Services -> IAM -> Access management -> Users. Click on the Add users button.

  2. Select Attach existing policies directly. Attach the AdministratorAccess policy to this user.

  3. Click on Create user. Congrats! terraform user created. Save the access key ID and secret access key to use later.

Configure Terraform file

Create a terraform file. The extension for a terraform file is .tf. I have named the file launchEC2.tf.

Configure the file to launch an ec2 instance. Before moving on, I want to tell you that some arguments are optional, and some are required. For example, mentioning the availability zone is optional, but the AMI ID is required. If the optional arguments aren't specified, they will be configured using the default AWS settings.

Let's get started. Follow the below steps:

  1. Select provider

    • It is an AWS instance. Therefore we will specify aws as the provider.

      provider "aws"{  
      }
      
  2. Select a region

    • Select a region of your choice. I am choosing the ap-south-1 region.

      provider "aws"{  
          region = "ap-south-1" #Mumbai region
      }
      
  3. Provide credentials

    • You may use various approaches to log in to AWS. We'll be storing credentials in the file itself. Not a secure way. But we'll do it anyways because it's a pretty straightforward approach.

    • We'll use the access and secret key of the terraform user we created previously. I am using an alt text here, but you should mention the authentic keys.

      provider "aws"{
          region = "ap-south-1"
          access_key = "abc" #Provide access key of "terraform" user
          secret_key = "xyz" #Provide secret key of "terraform" user
      }
      
  4. Create Resource

    • Now, you have to specify what AWS resource you would like to create and what name should be given to it. We want to create an AWS instance named test-ec2-instance.

      provider "aws"{
          region = "ap-south-1"
          access_key = "abc"
          secret_key = "xyz"
      }
      resource "aws_instance" "test-ec2-instance"{
      }
      
  5. Configure image ID

    • Specify the amazon machine image(AMI) id of the instance. I am using Amazon Linux 2 Kernel 5.10 AMI 2.0.20221210.1 x86_64 HVM gp2 image, so I have to mention the AMI ID corresponding to this AMI.

    • The one thing that you should know is that AMI ID is region specific. This means AMI ID will be different in every region. As you can see below, the ID for Amazon Linux 2 Kernel 5.10 AMI 2.0.20221210.1 x86_64 HVM gp2 AMI is ami-0cca134ec43cf708f in the Osaka region, and ami-009a12cc1b7171e6e in the Mumbai region.

      provider "aws"{
          region = "ap-south-1"
          access_key = "abc"
          secret_key = "xyz"
      }
      
      resource "aws_instance" "test-ec2-instance"{
          ami = "ami-0cca134ec43cf708f" #Image id for the ap-south-1 region
      }
      
  6. Select an instance type that meets your computing, memory, networking, or storage needs. I will use the type t2.micro.

    provider "aws"{
        region = "ap-south-1"
        access_key = "abc"
        secret_key = "xyz"
    }
    
    resource "aws_instance" "test-ec2-instance"{
        ami = "ami-0cca134ec43cf708f" #Image id for the ap-south-1 region
        instance_type = "t2.micro"
    }
    

Launch

  1. Change the current directory to where the launchEC2.tf file resides.

  2. Execute terraform init command. This command will initialize the working directory containing Terraform configuration files and install any required plugins. It reads your configuration and identifies that you will be working with the provider of AWS. Now it will go ahead and start downloading the AWS related terraform plugins. As you can see below, it is installing AWS-related plugins.

    terraform init
    

    Plugin installation isn't the only thing the terraform init command does. To learn more about it, check out this well-written and informative blog on spacelift by Jack Roper about this command.

  3. Execute the terraform plan command to see the infrastructure plan. You can make changes if required.

    terraform plan
    

    Everything looks good! It's time to create the infrastructure.

  4. Execute terraform apply command to create the infrastructure.

    • Run the command:
        terraform apply
      
    • Enter yes
    • Instance created.
    • You can also verify that the instance is running from the AWS management console. Congratulations! We did it! :)

Deleting the instance

To delete the instance that we just created, execute the following command. Just mention the resource, i.e., aws_instance, and its name, i.e., test-ec2-instance, in the terraform destroy command.

  • Execute the command below:

    terraform destroy -target=aws_instance.test-ec2-instance
    

  • Enter yes.

  • The instance got deleted!

See! How amazing this infrastructure as a code tool Terraform is! Isn't it?

Practice

Now that you've learned how to launch an ec2 instance, I want you to do the same but with more configurations. Use different ways to provide credentials, create a custom VPC or select a specific availability zone.

These are just basic ideas. Use the registry to learn about more arguments. Feel free to add these to your terraform file.

Conclusion

In this blog, we successfully launched an ec2 instance on AWS using just a single file and two commands. We learned how to destroy an ec2 instance. We also learned that different regions have different AMI IDs.

Happy learning! :)