{"id":75544,"date":"2025-09-05T18:39:02","date_gmt":"2025-09-05T13:09:02","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=75544"},"modified":"2025-09-17T11:48:48","modified_gmt":"2025-09-17T06:18:48","slug":"ec2-image-builder-in-action-from-ami-creation-to-cross-account-sharing","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/ec2-image-builder-in-action-from-ami-creation-to-cross-account-sharing\/","title":{"rendered":"EC2 Image Builder in Action: From AMI Creation to Cross-Account Sharing"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>EC2 Image Builder is an AWS service that automates creating, updating, and deploying your Amazon Machine Images (AMIs).<\/p>\n<p>EC2 Image Builder creates a pipeline for Linux or Windows Server images for use with Amazon EC2. The pipeline manages all stages, including image creation, maintenance, validation, sharing, and deployment.<\/p>\n<p>Image Builder is free to use, except for the cost of the AWS resources needed to create, store, and share the images.<\/p>\n<p>Refer to this blog to know more about EC2 Image Builder &#8211; <a href=\"https:\/\/www.tothenew.com\/blog\/packer-alternative-ec2-image-builder\/\"><strong>Packer Alternative: Migrating to AWS EC2 Image Builder for Automated AMI Pipelines<\/strong><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Scenarios in Action<\/h2>\n<p><strong>1. Using EC2 Image builder service and its sub-resources to build an EC2 Image Pipeline to automate AMI creation and AMI sharing to the destination AWS Account via CFN template only.<\/strong><\/p>\n<p>Here, we\u2019ll install a CloudWatch Agent and the Testing Component Script, which will validate if the CW agent is running or not. If the CloudWatch agent status is not in the running stage, break the AMI build process.<\/p>\n<div id=\"attachment_75543\" style=\"width: 701px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75543\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-75543\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Blog2.drawio.png\" alt=\"EC2 Image Builder\" width=\"691\" height=\"411\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Blog2.drawio.png 691w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Blog2.drawio-300x178.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Blog2.drawio-624x371.png 624w\" sizes=\"(max-width: 691px) 100vw, 691px\" \/><p id=\"caption-attachment-75543\" class=\"wp-caption-text\">EC2 Image Builder<\/p><\/div>\n<h3>Prerequisites before deploying the EC2 Image Builder CFN Stack<\/h3>\n<p>IAM Role &#8211; Permission of the S3 bucket and SNS Notification. Here, Image Builder download scripts from the S3 bucket and sends a notification to SNS.<\/p>\n<p>VPC and subnet (private only) &#8211; EC2 launch via Image builder privately, and access to the internet is required to download the packages.<\/p>\n<p>KMS key &#8211; To encrypt the AWS EBS volume, the best practice is to follow.<\/p>\n<p><strong>Note<\/strong> &#8211;\u00a0The above resources mentioned in the prerequisite are not part of the CFN template. They are passed as a parameter in the template.<\/p>\n<h3>Deploy the CFN template to create resources<\/h3>\n<p>CFN Template GitHub URL &#8211; <a href=\"https:\/\/github.com\/Kushagrattn\/AWS_EC2_Image_Builder_Hands_On\/blob\/9869df773537dba26fa0d5d4b98ac225200ffad2\/CloudFormationTemplate\/Ec2-image-builder-stack.yaml\">Click Here<\/a><br \/>\nComponent Scripts &#8211; <a href=\"https:\/\/github.com\/Kushagrattn\/AWS_EC2_Image_Builder_Hands_On.git\">Click Here<\/a><\/p>\n<p>Once the CFN template is deployed, the EC2 Image Pipeline is created, and it clubs all the resources within the pipeline to build in CI\/CD pipeline. It builds, validates and on successful share the AMI to other accounts.<\/p>\n<p><strong>Stack Output &#8211;<\/strong><\/p>\n<div id=\"attachment_75545\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75545\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-75545 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-1024x425.png\" alt=\"CFN Stack Output\" width=\"625\" height=\"259\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-1024x425.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-300x124.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-768x319.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-1536x637.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-2048x850.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.31.12\u202fAM-624x259.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75545\" class=\"wp-caption-text\">CFN Stack Output<\/p><\/div>\n<p><strong>EC2 Image Pipeline &#8211;<\/strong><\/p>\n<div id=\"attachment_75546\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75546\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-75546\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-1024x410.png\" alt=\"EC2 Image Pipeline\" width=\"625\" height=\"250\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-1024x410.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-300x120.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-768x308.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-1536x615.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-2048x820.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.20.29\u202fAM-624x250.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75546\" class=\"wp-caption-text\">EC2 Image Pipeline<\/p><\/div>\n<p><strong>Distribution of the AMI &#8211;<\/strong><\/p>\n<div id=\"attachment_75547\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75547\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-75547\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-1024x406.png\" alt=\"AMI Distribution\" width=\"625\" height=\"248\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-1024x406.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-300x119.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-768x305.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-1536x610.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-2048x813.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.44.41\u202fAM-624x248.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75547\" class=\"wp-caption-text\">AMI Distribution<\/p><\/div>\n<div id=\"attachment_75548\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75548\" decoding=\"async\" loading=\"lazy\" class=\"wp-image-75548 size-large\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-1024x591.png\" alt=\"AMI Distribution\" width=\"625\" height=\"361\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-1024x591.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-300x173.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-768x443.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-1536x887.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-2048x1183.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.48.06\u202fAM-624x360.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75548\" class=\"wp-caption-text\">AMI Distribution<\/p><\/div>\n<p>It\u2019s important to do unit testing on our end to validate if the agents are up and running. Launch an EC2 Instance via a custom AMI and check the CW agent status using the command below.<\/p>\n<p><strong>Command:<\/strong>\u00a0sudo systemctl status amazon-cloudwatch-agent<\/p>\n<div id=\"attachment_75550\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75550\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-75550\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM-1024x237.png\" alt=\"CloudWatch Agent\" width=\"625\" height=\"145\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM-1024x237.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM-300x69.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM-768x178.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM-1536x356.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM-624x145.png 624w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-12.38.16\u202fAM.png 1874w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75550\" class=\"wp-caption-text\">CloudWatch Agent<\/p><\/div>\n<p>&nbsp;<\/p>\n<p><strong>2. To validate the package&#8217;s installation via the EC2 image builder itself using a custom component script<\/strong><\/p>\n<ul>\n<li>Here we are configuring and installing the CloudWatch agent, but before creating an AMI, there must be a testing process to validate if the agent is running or not.<\/li>\n<li>If the agent status is not running, break the build pipeline because we don\u2019t want to share a broken AMI with a misconfigured agent setup.<\/li>\n<li>In Scenario 01, it successfully configures the agent. Let\u2019s try to break the build process on failure.<\/li>\n<li>In the Script InstallLinuxComponent.sh script, after the installation, let\u2019s STOP the CloudWatch agent.<\/li>\n<li>Add a command at the end of a script &#8211; sudo systemctl stop amazon-cloudwatch-agent<\/li>\n<li>Upload the updated script to the S3 bucket path.<\/li>\n<li>Execute the EC2 Image Pipeline<\/li>\n<\/ul>\n<div id=\"attachment_75551\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75551\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-75551\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-1024x541.png\" alt=\"Build Workflow\" width=\"625\" height=\"330\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-1024x541.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-300x159.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-768x406.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-1536x812.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-2048x1083.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.22.07\u202fAM-624x330.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75551\" class=\"wp-caption-text\">Build Workflow<\/p><\/div>\n<p>Here is the flow: when the validation script checks for the CloudWatch agent service status, it&#8217;s not running, which returns the exit code. The exit code can be any number except 0, due to which the Build workflow failed because the agent is not UP and Running and breaks the build process.<\/p>\n<div id=\"attachment_75552\" style=\"width: 635px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75552\" decoding=\"async\" loading=\"lazy\" class=\"size-large wp-image-75552\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-1024x530.png\" alt=\"Workflow ID logs\" width=\"625\" height=\"323\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-1024x530.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-300x155.png 300w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-768x397.png 768w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-1536x795.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-2048x1060.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/Screenshot-2025-09-05-at-1.28.16\u202fAM-624x323.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><p id=\"caption-attachment-75552\" class=\"wp-caption-text\">Workflow ID logs<\/p><\/div>\n<p>&nbsp;<\/p>\n<p><strong>3. SNS Integration in EC2 Image Builder to receive pass\/success status of the AMI.<\/strong><\/p>\n<p>Once AMI is created, Image Pipeline sends a huge Raw JSON, which can be filtered via a lambda, and the lambda can send the output to a new SNS as an input to receive a pass\/fail status.<\/p>\n<div id=\"attachment_75553\" style=\"width: 340px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-75553\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-75553\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2025\/09\/SNS-lambda-sns.drawio.png\" alt=\"AMI Notification\" width=\"330\" height=\"263\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2025\/09\/SNS-lambda-sns.drawio.png 330w, \/blog\/wp-ttn-blog\/uploads\/2025\/09\/SNS-lambda-sns.drawio-300x239.png 300w\" sizes=\"(max-width: 330px) 100vw, 330px\" \/><p id=\"caption-attachment-75553\" class=\"wp-caption-text\">AMI Notification<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>Once the AMI is in an available or fail state, it triggers the SNS configured in the Infrastructure configurations in EC2 Image builder via a CFN template.<br \/>\nConfigure AWS Lambda and a custom Python script to filter the Raw JSON sent by the pipeline to deliver filtered and formatted key: values to the stakeholders\/team.<\/p>\n<h2>Key Notes<\/h2>\n<ul>\n<li>The Pipeline fetches the latest AMI, always using the x.x.x semantic versioning. It\u2019ll always pick the latest Major, Minor, and Patch version whenever the EC2 image pipeline gets triggered.<\/li>\n<li>It requires no other AWS service dependency to trigger the pipeline. It has a cron expression scheduler built in.<\/li>\n<li>It can share AMIs across accounts, SSM parameters, update the launch template and so on.<\/li>\n<li>The components in the recipe are executed in the sequence order in which they are defined.<\/li>\n<\/ul>\n<h2>Wrapping Up<\/h2>\n<p>While learning and implementing EC2 Image Builder, I realised that we can achieve much more with this service than I initially thought. Don\u2019t stop here\u2014explore new scenarios and possibilities with it.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction EC2 Image Builder is an AWS service that automates creating, updating, and deploying your Amazon Machine Images (AMIs). EC2 Image Builder creates a pipeline for Linux or Windows Server images for use with Amazon EC2. The pipeline manages all stages, including image creation, maintenance, validation, sharing, and deployment. Image Builder is free to use, [&hellip;]<\/p>\n","protected":false},"author":1658,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":39},"categories":[2348],"tags":[248,4252,8012,8070,7943,7895],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/75544"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1658"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=75544"}],"version-history":[{"count":6,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/75544\/revisions"}],"predecessor-version":[{"id":76357,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/75544\/revisions\/76357"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=75544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=75544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=75544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}