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:
- 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.
- 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
- 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.
- 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.
- 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
