Automation of Application (Jenkins) Deployment & Configuration on AWS using Terraform and userdata

This is the first of a 3 blog series talking about automation and the various IaC options available to configure and deploy applications on AWS.

In today’s fast-paced world where speed, consistency, and accuracy are paramount, DevOps has revolutionized the way organizations design, develop, and maintain their IT infrastructure. Automation of infrastructure has replaced the tedious, manual process of managing IT infrastructure. It also helps to avoid errors and is helping engineers automatically launch cloud environments quickly and without mistakes.

In this three-part blog series, we will walk you through the options we use to automate IaC and how you can use them as well.

In this first blog of the series, we will learn about Terraform and its use with userdata. The concept is to build infrastructure by automating it with Terraform as Iac(Infrastructure as a code). We’ll use this to install Jenkins and its plugin, as well as run a simple pipeline script by automation. Terraform is our go-to tool for managing the whole infrastructure lifecycle with infrastructure as code.

Infrastructure As Code

Iac is all about managing and provisioning infrastructure through code instead of through manual processes. The purpose of Iac is to simplify the process of large-scale management of infrastructure. Iac basically means managing your IT infrastructure using configuration files.

Why IAC?

  • Cost reduction
  • Increase in speed of deployments
  • Reduce errors
  • Improve infrastructure consistency
  • Eliminate configuration drift

Alternative Methods instead of Terraform:

  • CloudFormation
  • Ansible
  • Puppet
  • Chef
  • SaltStack

Why Terraform instead of CloudFormation and Ansible?

Terraform offers to deploy Infrastructure as a Code, helps in readability and lift and shift deployments. Ansible is a configuration management tool for automating system configuration and management; Terraform for the purpose of orchestration and Ansible for configuration management. Terraform is declarative and upon giving an end instruction, can carry out all steps to give the final output, whereas each step has to be dictated in Ansible.

So, If you are looking to provision services on multiple cloud platforms, Terraform is your go-to option. While Terraform supports all cloud vendors like AWS, GCP, Azure, and many others, CloudFormation is confined only to AWS.

To install terraform refer to this link:

Architectural Diagram


Terraform Block

The terraform {} block contains Terraform settings, including the required providers Terraform will use to provision your infrastructure. For each provider, the source attribute defines an optional hostname, a namespace, and the provider type. Terraform installs providers from the Terraform Registry by default.


The provider block configures the specified provider, in this case, AWS. A provider is a plugin that Terraform uses to create and manage your resources.


Resource blocks have two strings before the block: the resource type and the resource name. In this example, the resource type is aws_instance and the name is “web server”. The prefix of the type maps to the name of the provider. In the example configuration, Terraform manages the aws_instance resource with the AWS provider. Together, the resource type and resource name form a unique ID for the resource. For example, the ID for the EC2 instance is aws_instance.web_server.

Useful terraform commands:

  • Terraform init
  • Terraform plan
  • Terraform apply

Initialize the directory 

When you create a new configuration — or check out an existing configuration from version control — you need to initialize the directory with Terraform init.Initializing, a configuration directory downloads and installs the providers defined in the configuration, which in this case is the AWS provider.


Creating Infrastructure

Apply the configuration now with the terraform apply command. Terraform will print an output similar to what is shown below. We have truncated some of the output to save space.


Walkthrough of what we did :

We created an Ec2 instance in VPC with one subnet.


Adding security group rules:


Features of Ec2:


The shell script is given as userdata with installation commands for git, Jenkins, and other configurations. We can edit shell script commands to install and configure various applications.


Challenges faced while performing Commands


  • The git might cause an error while cloning the repository.
  • Check the credentials while executing a command. This may cause an error.
  • Check the AMI ID for the specific OS Type & region.
  • In order to avoid errors, run the commands manually and check whether every command is working, then execute the whole script.
  • We can find errors occurring in user data in cloud-init.log in /var/log and debug it.

Creation of an EC2 Instance:

Creating an infrastructure code in file with the specifications needed by executing the code with Terraform and  then Ec2 instance will be launched


Jenkins Login:

After launching an EC2 instance by using userdata, we can log in to Jenkins directly bypassing the username and password in a groovy script file.The setup wizard page is disabled by changing the version to “2.0” in jenkins.UpgradeWizard.state file.


Pipeline Creation:

Once the code is executed a new job is created and built.



If you have any questions or suggestions, please reach out to us at

Written By:  Deepika R, Dinesh Kumar, Rajeev MS, and Umashankar N

Published On: June 30, 2022

Sharing is caring!

In Blog
Subscribe to our Newsletter1CloudHub