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

Image for post

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.

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

	data "aws_ami" "ubuntu" {
	  most_recent = true

	  filter {
	    name   = "name"
	    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
	  }

	  filter {
	    name   = "virtualization-type"
	    values = ["hvm"]
	  }

	  owners = ["099720109477"]
	}

	resource "aws_instance" "jenkins" {
	  ami           = data.aws_ami.ubuntu.id
	  instance_type = "t2.micro"

	  tags = {
	    "Name"      = "Jenkins_Server"
	    "Terraform" = "true"
	  }
	}

#terraform #infrastructure #ec2 #aws #infrastructure-as-code

Intro to Terraform: Provision EC2 Instance and Install Jenkins
8.15 GEEK