Automating Amazon Linux Instance Creation using Terraform and Pre-Installed AMI | Step-by-Step Guide
Veltris
MENU
  • Company
      • AboutusAbout Us
      • __CSR_mainCSR
      • LeadershipLeadership
      • __Life at Veltris-mainLife at Veltris
      • ChannelPartnerships
      • Contact usContact Us
      • veltrisPressroom
      • EventsEvents
  • AI Services
    • Digital-CloudAI-Augmented Engineering
    • Data-AIAI Readiness & Data
      Modernization
    • __Connectivity & Industry X.0_1 (1)AI Monetization
    • __Product Lifecyle & Managed Services_1Product Lifecycle &
      Managed Services
  • Industries
    • HealthcareHealthcare
      • __Health TechHealthTech & MedTech Platforms
      • __Dental CareDental & Multi-Unit Retail Health
      • __Pharmacy Benefit Management (PBM)Pharmacy Benefit Management (PBM)
    • Hi TechTechnology
      • __Technology Products & PlatformsSoftware / XTech / AI Platforms
      • __Communications Service ProvidersSecure Networking & Infrastructure
    • Diversified-IndustrialsIndustrials
      • __Autonomous Vehicles and LogisticsLogistics & Mobility Tech
      • __Engineering and ConstructionEngineering & Construction Tech
  • Insights
    • BlogsBlogs
    • White papersWhitepapers
    • Glossary-svgAI Guides and Innovations
    • CasestudiesClient Case Studies
  • Careers
  • Vertical AI Platform
    (VeltrisOne™)
Get My AI Readiness Score
Veltris
Get My AI Readiness Score
  • Company
    • About Us
    • Leadership
    • Partnerships
    • Pressroom
    • Events
    • CSR
    • Life at Veltris
  • Services
    • AI-Augmented Engineering
    • AI Readiness
    • AI Monetization
    • Product Lifecycle & Managed Services
  • Industries
    • Healthcare
      • Healthtech Platforms
      • Dental & Multi-Unit Retail Health
      • Pharmacy Benefit Management (PBM)
    • Technology
      • Software / XTech / AI Platforms
      • Secure Networking & Infrastructure
    • Industrials
      • Logistics & Mobility Tech
      • Engineering & Construction Tech
  • Insights
    • Blogs
    • Whitepapers
    • AI Guides and Innovations
    • Client Case Studies
  • Careers
  • Get My AI Readiness Score
Blogs

Automating Amazon Linux Instance Creation using Terraform and Pre-Installed AMI – Part 1

April 12, 2023 | Team Veltris
  • Blogs
  • Digital & Cloud Engineering
  • Technology, Communications & Media

Automated Amazon Linux Instance Creation with Terraform and AMI Creation is a process of creating Amazon Linux instances with pre-installed packages using Terraform and then converting it into an Amazon Machine Image (AMI) for future deployments. 

This process helps to automate the instance creation process, saves time, and reduces the manual effort involved in creating and configuring instances. Additionally, using Terraform allows version control and easy maintenance of infrastructure as code. The created AMI can be used to launch instances with the required packages, making the deployment process faster and more efficient.

AWS Prerequisites

  • Terraform should be installed in local machine
  • AWS CLI installation
  • Create a folder for the terraform project in any directory in local machine

Terraform installation in local machine

  • Open Terminal on your Mac.
  • Use the following command to download the latest Terraform version:
    • curl -O https://releases.hashicorp.com/terraform/1.4.2/terraform_1.4.2_darwin_amd64.zip

Note: You can check for the latest Terraform version on the HashiCorp releases page and replace the version number in the above command accordingly.

  • Unzip the downloaded file using the following command:
    • unzip terraform_1.4.2_darwin_amd64.zip
  • Move the unzipped file to the /usr/local/bin directory using the following command:
    • sudo mv terraform /usr/local/bin/
  • Check if Terraform is installed and available in your PATH by running the following command:
    • terraform –version

This should display the installed version of Terraform.

AWS CLI installation in local machine

  • Open Terminal on your Mac.
  • Use the following command to install the AWS CLI using Homebrew:
    • brew install awscli

Note: If you don’t have Homebrew installed, you can install it using the command:

  • /bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  • Verify that the installation was successful by running the following command:
    • aws –version

This should display the installed version of the AWS CLI.

  • If you need to configure the AWS CLI with your AWS access key and secret access key, you can run the following command:
    • aws configure

This will prompt you to enter your AWS access key ID, secret access key, default region name, and default output format. 

You can also follow another approach to setting AWS credentials in Terraform code.

Terraform Project Structure

Project

├── main.tf         —-> Contains main Terraform configuration code

├── variables.tf    —-> Contains Terraform variable definitions

├── terraform.tfvars —-> Contains actual values for the variables defined in variables.tf

├── output.tf       —-> Contains output definitions

├── aws-user-data.sh —-> Contains user data script used for EC2 instance configuration

Here we split the whole Terraform configuration code into multiple files for organizational purposes. Each file serves a specific purpose in the infrastructure definition, and this modular approach makes the code easier to read, maintain, and scale over time.

Create these different files in your terraform project [main.tf, variables.tf, terraform.tfvars, output.tf, aws-user-data.sh]

Here is the explanation for the purpose of each file mentioned above:

  1. main.tf: This file contains the main Terraform configuration code for your infrastructure. It defines the resources that you want to create, their properties, and any dependencies between them. This is where you will typically spend most of your time writing code.
  1. variables.tf: This file defines the input variables that your Terraform configuration needs to run. These variables are typically used to customize the behavior of your infrastructure resources. For example, you might use a variable to specify the default availability zone, instance type, or the default CIDR block for VPC
  1. terraform.tfvars: This file contains the values of the input variables defined in variables.tf. You can use this file to override the default values of these variables when you run terraform apply.
  1. output.tf: This file defines the output values that your Terraform configuration generates. These values can be used by other parts of your infrastructure or by external tools to interact with your resources. For example, you might use an output value to retrieve the IP address or the instance ID of an instance that you created.
  1. aws-user-data.sh: This file contains a shell script that is executed on the EC2 instances when they are launched. You can use this script to perform any custom configuration tasks that are required for your application to run. For example, you might use it to install software or set environment variables.

In the upcoming second part of this series, we will be covering advanced topics such as associating an Elastic IP to the EC2 instance, creating a Bash script to install Docker and Jenkins, defining output values in Terraform, generating an AMI from the instance, and discussing the benefits of using an AMI.

Author Information

Srija Anaparthi is a DevOps Engineer with a passion for exploring the latest cloud technologies. An Azure and AWS enthusiast sharing insights and experiences through thought-provoking blogs

FOUND THIS USEFUL? SHARE WITH

Related Articles

Beyond the Bolt-On: Why Enterprises Need AI-Native Security for an Autonomous Future
AI-Optimized Streaming: Delivering Better Quality at Lower Cost
The Automation Gap: Why Network Orchestration Now Determines Enterprise Velocity
Beyond the Perimeter: Why Zero Trust is the New Growth Architecture
From Monolith to Modular: Why Composable Architecture Is the Safer Path for PBM Modernization
The Cloud-First Imperative: Why PBM Trust and Compliance Now Depend on It
Veltris
Company
  • About Us
  • Leadership
  • Partnerships
  • Pressroom
  • Events
  • CSR
  • Life at Veltris
  • Careers
  • Contact Us
Services
  • AI-Augmented Engineering
  • AI Readiness & Data Modernization
  • AI Monetization
  • Product Lifecycle & Managed Services
Industries
  • Healthcare
  • Technology
  • Industrials
Insights
  • Blogs
  • Whitepapers
  • AI Guides and Innovations
  • Client Case Studies
Copyright © 2026 Veltris. All Rights Reserved.
Cookie Policy | Disclaimer | Privacy Policy | Sitemap
Veltris
  • About Us
  • Leadership
  • Pressroom
  • Events
  • CSR
  • Life at Veltris
  • Careers
  • Contact Us
  • AI-Augmented Engineering
  • AI Readiness & Data Modernization
  • AI Monetization
  • Product Lifecycle & Managed Services
  • Healthcare
  • Technology
  • Industrials
  • Blogs
  • Whitepapers
  • AI Guides and Innovations
  • Client Case Studies
Copyright © 2026 Veltris. All Rights Reserved.
Cookie Policy | Disclaimer | Privacy Policy | Sitemap