Follow these steps to create an AMI with pre-requisites:
- Refer to the Packer Installation Guide.
- For Amazon Linux 2023 (AWS CloudShell):
sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo sudo yum -y install packer
-
Create a Packer Configuration File: Create a file named
packer-config.pkr.hclwith the following content:packer { required_plugins { amazon = { version = ">= 1.3.3" source = "github.com/hashicorp/amazon" } } }
-
Run Packer Initialization: Use the following command to download and install the Amazon plugin:
packer init packer-config.pkr.hcl
Set your AWS credentials and region as environment variables:
export AWS_ACCESS_KEY_ID="your_Access_Key"
export AWS_SECRET_ACCESS_KEY="your_Secret_Key"
export AWS_DEFAULT_REGION="Your_Region"Clone the required repository to your local machine or AWS CloudShell.
- Ensure the target account number is added in the Admin Account-ECR with permissions to access image builds.
-
Create a Role:
- Create a role and attach a policy permitting ECR and EC2 actions.
-
Update the
packer-rg.jsonFile:- Replace the placeholder
<your_rolename>in the"iam_instance_profile"field under the builders section with your role name.
- Replace the placeholder
Run the following command to build the AMI:
packer build -var 'awsRegion=your_region' -var 'vpcId=your_VPCID' -var 'subnetId=your_SubnetID' packer-rg.json- Pass the following variables at runtime:
VPCIDSubnetIDAWSRegion
After the build completes successfully, note the AMI ID from the output.
- Ensure all variables and IAM roles are correctly configured before running the build.
- For further details, refer to the repository documentation or Packer's official documentation.