Marcin Żelasko

DevOps

Apr 01, 2020 in Development

Creating your first EC2 instance with Terraform

Thanks to the HashiCorp software company and their cloud-provisioning tool, you can manage your infrastructures easily and much more efficiently!

In Terraform, all the configuration is written in a language called HCL — HashiCorp Configuration Language. 

Now, let’s get a look at how to run a simple EC2 instance on AWS with the help of Terraform!

Prerequisites

  • AWS account with a user that has the programmatic access —> you can create your free AWS account here.
  • Terraform installed on your machine.

And that's it! Now that we have all the ingredients, let's go to the actual recipe!

Building your first EC2 using Terraform

We need to create a file named ec2.tf and add some stuff here. You can check some of the basic templates at my repository.

  • PROVIDER
    • ACCESS_KEY
    • SECRET_KEY
    • REGION

These are credentials that AWS gives you after creating a user account with the programmatic access.

  • RESOURCE
    • In this block, you have to specify which resource you want to create - EC2 instance called 'example'
    • AMI - At this point, you have to decide which image to use on the instance. You can find a specific image here.
    • INSTANCE_TYPE - We're going to use 't2.micro' as it's available on the AWS Free Tier account.

Now, we have to run command „terraform init” in a directory in which we created ec2.tf file, to initialize provider plugins, AWS in our case.

Once the initialization is complete, we're going to run 'terraform plan' to see what Terraform will do before we decide to deploy EC2.

If everything seems right, we can create our first EC2 instance. Let’s run 'terraform apply'.

Now if we go to AWS Console to EC2 section:

Aaand... we did it! We created our first EC2 instance!

Don’t forget to destroy unused resources after you finish playing around with your EC2. It can be done with the 'terraform destroy' command.

We decided to show you how the basic work with Terraform looks like, because for us, it’s an incredibly useful tool that helps plan our infrastructures and see them more clearly. We can also recommend you to check the variety of Terraform’s available features!

Do you need more information about this topic?

Schedule a call with our developers

Let's talk!

Back to top