Step 3: Create a launch template and attach it to a compute environment
When the instance starts, the user-data script automatically installs
Fluent Bit, retrieves the instance ID from IMDSv2, syncs configuration
from Amazon S3, and starts the Fluent Bit service. For more information
about launch templates, see Use Amazon EC2 launch templates with AWS Batch. For more information
about IMDS configuration, see Instance Metadata Service (IMDS) configuration.
For a sample user-data script, see the companion GitHub repository. Use the
launch-template-userdata-minimal.txt file for your launch template.
This file excludes comments to reduce user-data size. The
launch-template-userdata.txt file is a commented version for reference.
Replace the following placeholders in the template before use:
Prerequisites
- AWS Console
-
Create the launch template:
Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
In the navigation pane, choose Launch
Templates.
Choose Create launch
template.
For Launch template name, enter a
name such as batch-host-logs.
Expand Advanced details.
For Metadata version, choose
V2 only (token required).
For Metadata response hop limit,
enter 2.
Scroll to User data and paste the user-data
script from the companion GitHub repository. Replace
<BUCKET_NAME> with your bucket name.
Replace <FLUENT_BIT_VERSION> with
your desired Fluent Bit version, or leave blank for the latest
version.
Choose Create launch template.
Create the compute environment:
Open the AWS Batch console at
https://console.aws.amazon.com/batch/.
In the navigation pane, choose Compute
environments.
Choose Create.
For Compute environment configuration,
choose Amazon Elastic Compute Cloud (Amazon EC2).
For Orchestration type,
choose Managed.
For Name,
enter a name such as my-host-logs-ce.
For Instance role,
choose the instance role you updated or created in Step 2, such as ecsInstanceRole.
Choose Next.
Under Instance configuration, expand
Launch templates and select the template you
created.
Complete the remaining fields and choose Create
compute environment.
- AWS CLI
-
Create the launch template. Save the user-data script from the
companion GitHub repository to a file and encode it with
base64:
aws ec2 create-launch-template \
--launch-template-name batch-host-logs \
--launch-template-data '{
"MetadataOptions": {
"HttpTokens": "required",
"HttpPutResponseHopLimit": 2
},
"UserData": "BASE64_ENCODED_USER_DATA"
}'
Create the compute environment with the launch template:
aws batch create-compute-environment \
--compute-environment-name my-host-logs-ce \
--type MANAGED \
--compute-resources '{
"type": "EC2",
"minvCpus": 0,
"maxvCpus": 256,
"instanceTypes": ["optimal"],
"subnets": ["subnet-12345678"],
"securityGroupIds": ["sg-12345678"],
"instanceRole": "arn:aws:iam::111122223333:instance-profile/ecsInstanceRole",
"launchTemplate": {
"launchTemplateName": "batch-host-logs",
"version": "$Latest"
}
}'
The metadata response hop limit is set to 2 in the launch template
because applications running in job containers require an extra network hop to
reach the IMDSv2 endpoint. Without this setting, IMDSv2 requests from containers
fail.