Launch template support - Amazon Batch
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Launch template support

Amazon Batch supports using Amazon EC2 launch templates with your EC2 compute environments. With launch templates, you can modify the default configuration of your Amazon Batch compute resources without needing to create customized AMIs.

Note

Launch templates aren't supported on Amazon Fargate resources.

You must create a launch template before you can associate it with a compute environment. You can create a launch template in the Amazon EC2 console. Or, you can use the Amazon CLI or an Amazon SDK. For example, the following JSON file represents a launch template that resizes the Docker data volume for the default Amazon Batch compute resource AMI and also sets it to be encrypted.

{ "LaunchTemplateName": "increase-container-volume-encrypt", "LaunchTemplateData": { "BlockDeviceMappings": [ { "DeviceName": "/dev/xvda", "Ebs": { "Encrypted": true, "VolumeSize": 100, "VolumeType": "gp2" } } ] } }

You can create the previous launch template by saving the JSON to a file that's called lt-data.json and running the following Amazon CLI command.

aws ec2 --region <region> create-launch-template --cli-input-json file://lt-data.json

For more information about launch templates, see Launching an Instance from a Launch Template in the Amazon EC2 User Guide for Linux Instances.

If you use a launch template to create your compute environment, you can move the following existing compute environment parameters to your launch template:

Note

Suppose that any of these parameters (except the Amazon EC2 tags) are specified both in the launch template and in the compute environment configuration. Then, the compute environment parameters take precedence. Amazon EC2 tags are merged between the launch template and the compute environment configuration. If there's a collision on the tag's key, the value in the compute environment configuration takes precedence.

  • Amazon EC2 key pair

  • Amazon EC2 AMI ID

  • Security group IDs

  • Amazon EC2 tags

The following launch template parameters are ignored by Amazon Batch:

  • Instance type (specify your desired instance types when you create your compute environment)

  • Instance role (specify your desired instance role when you create your compute environment)

  • Network interface subnets (specify your desired subnets when you create your compute environment)

  • Instance market options (Amazon Batch must control Spot Instance configuration)

  • Disable API termination (Amazon Batch must control instance lifecycle)

Amazon Batch only updates the launch template with a new launch template version during infrastructure updates. For more information, see Updating compute environments.

Amazon EC2 user data in launch templates

You can supply Amazon EC2 user data in your launch template that's run by cloud-init when your instances launch. Your user data can perform common configuration scenarios, including but not limited to the following:

Amazon EC2 user data in launch templates must be in the MIME multi-part archive format. This is because your user data is merged with other Amazon Batch user data that's required to configure your compute resources. You can combine multiple user data blocks together into a single MIME multi-part file. For example, you might want to combine a cloud boothook that configures the Docker daemon with a user data shell script that writes configuration information for the Amazon ECS container agent.

If you're using Amazon CloudFormation, the AWS::CloudFormation::Init type can be used with the cfn-init helper script to perform common configuration scenarios.

A MIME multi-part file consists of the following components:

  • The content type and part boundary declaration: Content-Type: multipart/mixed; boundary="==BOUNDARY=="

  • The MIME version declaration: MIME-Version: 1.0

  • One or more user data blocks that contain the following components:

    • The opening boundary that signals the beginning of a user data block: --==BOUNDARY==. You must keep the line before this boundary blank.

    • The content type declaration for the block: Content-Type: text/cloud-config; charset="us-ascii". For more information about content types, see the Cloud-Init documentation. You must keep the line after the content type declaration blank.

    • The content of the user data, such as a list of shell commands or cloud-init directives.

  • The closing boundary that signals the end of the MIME multi-part file: --==BOUNDARY==--. You must keep the line before the closing boundary blank.

The following are example MIME multi-part files that you can use to create your own.

Note

If you add user data to a launch template in the Amazon EC2 console, you can paste it in as plaintext. Or, you can upload it from a file. If you use the Amazon CLI or an Amazon SDK, you must first base64 encode the user data and submit that string as the value of the UserData parameter when you call CreateLaunchTemplate, as shown in this JSON file.

{ "LaunchTemplateName": "base64-user-data", "LaunchTemplateData": { "UserData": "ewogICAgIkxhdW5jaFRlbXBsYXRlTmFtZSI6ICJpbmNyZWFzZS1jb250YWluZXItdm9sdW..." } }

Example: Mount an existing Amazon EFS file system

This example MIME multi-part file configures the compute resource to install the amazon-efs-utils package and mount an existing Amazon EFS file system at /mnt/efs.

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==MYBOUNDARY==" --==MYBOUNDARY== Content-Type: text/cloud-config; charset="us-ascii" packages: - amazon-efs-utils runcmd: - file_system_id_01=fs-abcdef123 - efs_directory=/mnt/efs - mkdir -p ${efs_directory} - echo "${file_system_id_01}:/ ${efs_directory} efs tls,_netdev" >> /etc/fstab - mount -a -t efs defaults --==MYBOUNDARY==--

Example: Override default Amazon ECS container agent configuration

This example MIME multi-part file overrides the default Docker image cleanup settings for a compute resource.

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==MYBOUNDARY==" --==MYBOUNDARY== Content-Type: text/x-shellscript; charset="us-ascii" #!/bin/bash echo ECS_IMAGE_CLEANUP_INTERVAL=60m >> /etc/ecs/ecs.config echo ECS_IMAGE_MINIMUM_CLEANUP_AGE=60m >> /etc/ecs/ecs.config --==MYBOUNDARY==--

Example: Mount an existing Amazon FSx for Lustre file system

This example MIME multi-part file configures the compute resource to install the lustre2.10 package from the Extras Library and mount an existing FSx for Lustre file system at /scratch and a mount name of fsx. This example is for Amazon Linux 2. For installation instructions for other Linux distributions, see Installing the Lustre Client in the Amazon FSx for Lustre User Guide. For more information, see Mounting your Amazon FSx file system automatically in the Amazon FSx for Lustre User Guide.

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==MYBOUNDARY==" --==MYBOUNDARY== Content-Type: text/cloud-config; charset="us-ascii" runcmd: - file_system_id_01=fs-0abcdef1234567890 - region=us-east-2 - fsx_directory=/scratch - amazon-linux-extras install -y lustre2.10 - mkdir -p ${fsx_directory} - mount -t lustre ${file_system_id_01}.fsx.${region}.amazonaws.com@tcp:fsx ${fsx_directory} --==MYBOUNDARY==--

In the volumes and mountPoints members of the container properties the mount points must be mapped into the container.

{ "volumes": [ { "host": { "sourcePath": "/scratch" }, "name": "Scratch" } ], "mountPoints": [ { "containerPath": "/scratch", "sourceVolume": "Scratch" } ], }