Create custom AMI In AWS Using Terraform: A Step-By-Step Guide
Step 1: Launch An Instance
- Now connect with git bash terminal by using SSH Client
Step 2: Install Terraform
- Now install terraform from official site of hashicorp or follow below commands
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
Step 3: Create A File And Write Terraform Script
- Create a file with .tf extension in that file write a script by using
vi <filename.tf>
Provider configuration
- This section specifies the AWS provider and sets the region to “us-east-1”. The provider block configures the authentication details and default settings for interacting with AWS.
provider "aws" {
region = "us-east-1" # Specify the AWS region
}
Variable Section
- This section defines a variable named “instance_id” that represents the ID of the existing EC2 instance.
variable "instance_id" {
description = "The ID of the existing EC2 instance to use as the base for the custom AMI"
}
IAM Role Creation
- This section creates an IAM (Identity and Access Management) role named “ami-builder-role” with a trust policy that allows EC2 instances to assume the role. IAM roles are used to grant permissions to entities in AWS.
resource "aws_iam_role" "instance_role" {
name = "ami-builder-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Principal = {
Service = "ec2.amazonaws.com"
}
Action = "sts:AssumeRole"
}]
})
}
IAM Policy Attachment
- This section attaches an IAM policy (AmazonEC2FullAccess) to the IAM role created earlier
resource "aws_iam_policy_attachment" "instance_policy_attachment" {
name = "ami-builder-policy"
roles = [aws_iam_role.instance_role.name]
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2FullAccess" # Adjust policy as needed
}
EC2 Instance Creation
resource "aws_instance" "example_instance" {
ami = "ami-12345678" # Specify the base AMI ID
instance_type = "t2.micro" # Specify the instance type
iam_instance_profile = aws_iam_instance_profile.instance_profile.name
key_name = "your-key-pair" # Specify your key pair
associate_public_ip_address = true # Adjust as needed
}
Custom AMI Creation
resource "aws_ami_from_instance" "example_ami" {
name = "custom-ami"
source_instance_id = var.instance_id
}
Output
- This section defines an output variable named “custom_ami_id” that retrieves the ID of the created custom AMI. Outputs provide visibility into the results of Terraform operations.
output "custom_ami_id" {
value = aws_ami_from_instance.example_ami.ami_id
}
Step 4: Now Initialize Terraform And Execute Terraform Commands
- Now initialize terraform by using following command
terraform init
- Now execute terraform execution commands by using following commands
terraform fmt
terraform validate
terraform plan
- Now execute terraform apply command by using following command
terraform apply --auto-approve
The following screenshot shows that we successfully created a custom AMI in AWS using Terraform.
How To Create Custom AMI In AWS Using Terraform
In the present cloud-centric software development landscape, the capacity to efficiently manage infrastructure resources is vital. Making custom Amazon Machine Images (AMIs) is a pivotal part of this process, permitting organizations to normalize their application environments and streamline sending work processes. Terraform, an open-source infrastructure as-code tool, gives a strong system to defining and managing infrastructure assets in cloud environments, including AWS (Amazon Web Services).
This guide centers around utilizing Terraform to make custom AMIs in AWS, enabling DevOps groups to really computerize and scale their foundation arrangements. By using Terraforms revelatory syntax and AWS supplier capacities, groups can classify their infrastructure prerequisites and accomplish consistency, reliability, and proficiency in their deployment processes.
All through this guide, we’ll dive into the complexities of utilizing Terraform to make custom AMIs, covering fundamental terminology, wording, and practical execution steps. From installation and setup to configuration asset determinations and managing conditions, perusers will acquire a thorough comprehension of how to tackle Terraforms capacities for AMI creation in AWS.