Intro to Terraform: Provision EC2 Instance and Install Jenkins

Kevin Luu
Level Up Coding
Published in
7 min readOct 21, 2020

--

How to easily provision an EC2 Instance and execute remote commands to install Jenkins

Photo by Lance Anderson on Unsplash

Intro

Terraform is a tool that allows you to quickly build and provision your Infrastructure as Code and is compatible with many providers such as AWS, Google Cloud, Digital Ocean, and more.

In this article, I will walk you through how we can easily and quickly leverage Terraform to provision an EC2 instance on AWS running Ubuntu and install Jenkins.

Prerequisites

Provision EC2 Instance

At the time of this writing, I am running Terraform v0.13.4. Let’s start off by creating a folder for our working directory and create a main.tf file.

➜  ~ mkdir terraform-intro
➜ ~ cd terraform-intro
➜ terraform-into touch main.tf

Open main.tf in your favorite text editor and paste in the following.

The provider block defines which provider to use, in our case aws. Inside the block, we define the associated aws profile and region. I recommend using the closest region to you for this tutorial.

The data block allows us to grab a specific parameter from aws. We want to grab the most recent aws_ami Ubuntu 20 image.

The resource block defines what resource to provision. We tell Terraform to provision an aws_instance resource. The second parameter jenkins is only used when referring to this resource from other parts of the Terraform code.

Inside this resource block, we specify the ami, instance_type, and tags. For ami, we reference the id of the…

--

--