Use Amazon EC2 launch templates with AWS Batch
AWS Batch supports using Amazon EC2 launch templates with your EC2 compute environments. With launch templates, you can modify the default configuration of your AWS Batch compute resources without needing to create customized AMIs.
Note
Launch templates aren't supported on AWS 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 AWS CLI or an AWS SDK. For example, the following JSON file represents a launch template that resizes the Docker data volume for the default AWS 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 AWS 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.
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 AWS 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 (AWS Batch must control Spot Instance configuration)
-
Disable API termination (AWS Batch must control instance lifecycle)
AWS 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
Amazon EC2 user data in launch templates must be in the MIME multi-part
archive
If you're using AWS 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:
. For more information about content types, see the Cloud-Init documentationtext/cloud-config
; charset="us-ascii". 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.
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 AWS CLI or an AWS 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...
" } }