Create and update AMI distribution configurations
This section covers creating and updating distribution configurations for an Image Builder AMI.
Contents
AMI distribution prerequisites
Some distribution settings have prerequisites, as follows:
Prerequisites for SSM output parameters
Before you create a new AMI distribution configuration that sets an Amazon Systems Manager Parameter Store parameter (SSM parameter), ensure that you've met the following prerequisites.
- Execution role
-
When you create a pipeline or use the create-image command in the Amazon CLI, you can only specify one Image Builder execution role. If you have defined an Image Builder workflow execution role, you would add any additional feature permissions to that role. Otherwise, you would create a new custom role that includes the required permissions.
Important
We recommend that you don't pass the AWSServiceRoleForImageBuilder service-linked role as your execution role. Instead, create a custom IAM role and attach the EC2ImageBuilderExecutionPolicy Amazon managed policy. This policy grants the same permissions that Image Builder needs to call Amazon Web Services services on your behalf. Using a custom role gives you full control over the permissions that Image Builder uses. It also keeps your service control policies (SCPs) and resource control policies (RCPs) in effect for operations that Image Builder performs on your behalf.
-
To store the output AMI ID in an SSM parameter during distribution, you must specify the
ssm:PutParameteraction in your Image Builder execution role, with the parameter listed as a resource. -
When you set the parameter data type to
AWS EC2 Imageto signal Systems Manager to validate the parameter value as an AMI ID, you must also add theec2:DescribeImagesaction.
-
- Opt-in Region
-
You must enable an opt-in Region (Region that is disabled by default) in the source account. This requirement applies when your distribution configuration targets that Region in
ssmParameterConfigurations, whether you use the Image Builder service-linked role or a custom execution role.Note
The AMI copy to an opt-in Region can succeed even when the source account hasn't enabled the Region. Only the SSM output parameter write fails.
Without this prerequisite, the build fails with an error similar to the following:
In region 'af-south-1' - 'STS Client Error: The provided execution role does not exist or does not have sufficient permissions.'To resolve this issue, do one of the following:
-
Enable the opt-in Regions in the source account. For instructions on enabling a Region, see Enable or disable Amazon Regions in your account in the Amazon Account Management Reference Guide.
-
Omit
ssmParameterConfigurationsfor the opt-in Regions. Instead, use an Amazon EventBridge rule to react to the EC2 Image Builder Image State Change event and write the parameters yourself.
-
Prerequisites for EC2 Fast Launch
Before you create a new distribution configuration for EC2 Fast Launch for Windows AMIs, ensure that you've met the following prerequisites.
-
If you provide a custom launch template when you configure EC2 Fast Launch, the service uses the VPC and other configuration settings that you've defined in the launch template. For more information, see Use a launch template when you set up EC2 Fast Launch.
-
If you don't use a custom launch template to configure your settings, you must attach the EC2FastLaunchFullAccess policy to the IAM role that Image Builder uses to create your image. When you create a pipeline or use the create-image command in the Amazon CLI, you can only specify one Image Builder execution role. If you have defined an Image Builder workflow execution role, you would add any additional feature permissions to that role. Otherwise, you would create a new custom role that includes the required permissions.
This feature requires additional permissions beyond those in the base execution policy, so you must use a custom role. We recommend that you create a custom role with the EC2ImageBuilderExecutionPolicy Amazon managed policy attached, and then add the EC2FastLaunchFullAccess policy to that role.
Then, when Image Builder copies your image, EC2 Fast Launch automatically creates an Amazon CloudFormation stack with the following resources in your Amazon Web Services account.
-
A virtual private cloud (VPC)
-
Private subnets across multiple Availability Zones
-
A launch template configured with Instance Metadata Service Version 2 (IMDSv2)
-
A security group with no inbound or outbound rules
-
Note
Image Builder doesn't support cross-account distribution for AMIs with EC2 Fast Launch pre-enabled. EC2 Fast Launch must be enabled from the destination account.
Create an AMI distribution configuration
Distribution configurations include the output AMI name, specific Region settings for encryption, launch permissions, and Amazon Web Services accounts, organizations, and organizational units (OUs) that can launch the output AMI, and license configurations.
A distribution configuration allows you to specify the name and description of
your output AMI, authorize other Amazon Web Services accounts to launch the AMI, copy the AMI to
other accounts, and replicate the AMI to other Amazon Regions. It also allows you to
export the AMI to Amazon Simple Storage Service (Amazon S3), or configure EC2 Fast Launch for output Windows
AMIs. To make an AMI public, set the launch permission authorized accounts to
all. See the examples for making an AMI public at EC2 ModifyImageAttribute.
Update an AMI distribution configuration
You can change your AMI distribution configuration. However, the changes you make do not apply to any resources that Image Builder has already distributed. For example, if you have distributed an AMI to a Region that you later remove from your distribution, the AMI that was already distributed remains in that Region until you remove it manually.
Example: Enable EC2 Fast Launch with a launch template for output AMIs
The following example shows how to use the create-distribution-configuration
To configure EC2 Fast Launch settings without a launch template, ensure that you've met all of the EC2 Fast Launch prerequisites before you create your distribution configuration.
-
Create a CLI input JSON file
Use a file editing tool to create a JSON file with keys as shown in the following example, plus values that are valid for your environment.
This example launches instances for all of its target resources simultaneously, because the maximum number of parallel launches is greater than the target resource count. This file is named
ami-dist-config-win-fast-launch.jsonin the command example shown in the next step.{ "name": "WinFastLaunchDistribution", "description": "An example of Windows AMI EC2 Fast Launch settings in the distribution configuration.", "distributions": [ { "region": "us-west-2", "amiDistributionConfiguration": { "name": "Name {{imagebuilder:buildDate}}", "description": "Includes Windows AMI EC2 Fast Launch settings.", "amiTags": { "KeyName": "Some Value" } }, "fastLaunchConfigurations": [{ "enabled": true, "snapshotConfiguration": { "targetResourceCount": 5 }, "maxParallelLaunches": 6, "launchTemplate": { "launchTemplateId": "lt-0ab1234c56d789012", "launchTemplateVersion": "1" } }], "launchTemplateConfigurations": [{ "launchTemplateId": "lt-0ab1234c56d789012", "setDefaultVersion": true }] }] }Note
You can specify the
launchTemplateNameinstead of thelaunchTemplateIdin thelaunchTemplatesection, but you can't specify both the name and Id. -
Run the following command, using the file you created as input.
aws imagebuilder create-distribution-configuration --cli-input-json file://ami-dist-config-win-fast-launch.jsonNote
-
You must include the
file://notation at the beginning of the JSON file path. -
The path for the JSON file should follow the appropriate convention for the base operating system where you are running the command. For example, Windows uses the backslash (\) to refer to the directory path, while Linux and macOS use the forward slash (/).
For more detailed information, see create-distribution-configuration
in the Amazon CLI Command Reference. -
Example: Create distribution settings for output VM disks from the Amazon CLI
The following example shows how to use the create-distribution-configuration command to create distribution settings that will export VM image disks to Amazon S3 with every image build.
-
Create a CLI input JSON file
You can streamline the create-distribution-configuration command that you use in the Amazon CLI. To do this, create a JSON file that contains all of the export configuration that you want to pass into the command.
Note
The naming convention for the data values in the JSON file follows the pattern that is specified for the Image Builder API operation request parameters. To review the API operation request parameters, see the CreateDistributionConfiguration command in the EC2 Image Builder API Reference.
To provide the data values as command line parameters, refer to the parameter names specified in the Amazon CLI Command Reference. to the create-distribution-configuration command as options.
Here is a summary of the parameters that we specify in the
s3ExportConfigurationJSON object for this example:-
roleName (string, required) – The name of the role that grants VM Import/Export permission to export images to your S3 bucket.
-
diskImageFormat (string, required) – Export the updated disk image to one of the following supported formats:
-
Virtual Hard Disk (VHD) – Compatible with Citrix Xen and Microsoft Hyper-V virtualization products.
-
Stream-optimized ESX Virtual Machine Disk (VMDK) – Compatible with VMware ESX and VMware vSphere versions 4, 5, and 6.
-
Raw – Raw format.
-
-
s3Bucket (string, required) – The S3 bucket in which to store the output disk images for your VM.
Save the file as
export-vm-disks.json. Use the file name in the create-distribution-configuration command.{ "name": "example-distribution-configuration-with-vm-export", "description": "example", "distributions": [ { "region": "us-west-2", "amiDistributionConfiguration": { "description": "example-with-vm-export" }, "s3ExportConfiguration": { "roleName": "vmimport", "diskImageFormat": "RAW", "s3Bucket": "vm-bucket-export" } }], "clientToken": "abc123def4567ab" } -
-
Run the following command, using the file you created as input.
aws imagebuilder create-distribution-configuration --cli-input-json file://export-vm-disks.jsonNote
-
You must include the
file://notation at the beginning of the JSON file path. -
The path for the JSON file should follow the appropriate convention for the base operating system where you are running the command. For example, Windows uses the backslash (\) to refer to the directory path, while Linux and macOS use the forward slash (/).
For more detailed information, see create-distribution-configuration
in the Amazon CLI Command Reference. -