Is anyone using AWS Batch with custom AMIs?

I have to use custom AMIs at work for compliance reasons and from the docs I can see that Batch supports this. My question is, when we install the ECS agent in the machine it is usually associated with an ECS cluster. What value should I provide for the ECS_CLUSTER environment key in the AMI I am creating for Batch? Will this be dynamically set when Batch creates resources?


Related discussions:

I’m not using custom AMIs yet, but I’m planning to so I’ve been researching this recently:

By default Batch uses a recent ECS AMI (currently 2017.03.e). If you use it as the base for your AMI it already comes with everything installed and ready to go. If you want to build the image from scratch and install everything yourself you need to do the same you would do to build an ECS AMI.

Batch sets its own user-data dynamically when launching a new managed compute environment. They set their own cluster name, configure the CloudWatch logs driver for Docker, etc. What userdata settings they use is not documented, so I expect it may change without warning, but it’s all just ECS configuration so far.

I can’t use ECS optimized AMIs and have to build one from scratch. I am familiar with creating custom AMIs for ECS. The only thing that is puzzling me is that, usually I know the cluster name and I can pass it to the ECS agent while starting it. From what I read from the docs, Batch requires the ECS agent to be run at startup and I need to know how to provide the cluster name to it.

The ECS agent in the optimized images uses a configuration file located in /etc/ecs/ecs.config. Per AWS documentation that’s the official way to configure it, and that’s what Batch does when you launch a Managed environment:

https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-agent-config.html
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/bootstrap_container_instance.html

This is the user-data that batch set on one of my clusters:

Content-Type: multipart/mixed; boundary=“==BOUNDARY==”
MIME-Version: 1.0

--==BOUNDARY==
MIME-Version: 1.0
Content-Type: text/x-shellscript; charset="us-ascii"
#!/bin/bash
echo ECS_CLUSTER={ENVIRONMENT NAME HERE}_Batch_{RANDOM UUID HERE}>>/etc/ecs/ecs.config
echo ECS_DISABLE_IMAGE_CLEANUP=false>>/etc/ecs/ecs.config
echo ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION=5m>>/etc/ecs/ecs.config
echo ECS_IMAGE_CLEANUP_INTERVAL=10m>>/etc/ecs/ecs.config
echo ECS_IMAGE_MINIMUM_CLEANUP_AGE=15m>>/etc/ecs/ecs.config
echo ECS_NUM_IMAGES_DELETE_PER_CYCLE=5>>/etc/ecs/ecs.config
--==BOUNDARY==

As you can see Batch sets a cluster name with a random ID, so you can’t “guess” it beforehand. If you can make the ECS agent you install read its configuration from that file it should work.

But if you want to configure the cluster name directly in the AMI you may want to use an unmanaged environment, where you tell Batch to use an already existing ECS cluster to run the jobs. You’ll have to scale it up and down yourself though.

Thanks very much! This is really helpful. So, when I create a custom AMI, I just have to ensure that the ecs-agent starts up by providing /etc/ecs/ecs.config as an environment file. I’ll try this.

then at least have a look at the ECS optimized AMI and get answers from there.

you should by now know how to launch an instance you can log in to in your preferred AZ, look at the details about that AMI and get the snapshot it uses, create a volume in the same AZ from that snapshot, attach that volume to the instance you launched, login, and using sudo, mount that device at your preferred mount point (or just /mnt), and look around in there to see how they do it.

if you are allowed to base your custom AMI off of the Linux AMI they create, then why can’t you base your custom AMI off of the ECS optimized AMI they create? just change some messages in various files like /etc/motd and create your shiny new custom AMI.

or are you going to be creating your own custom OS to go along with it?

This is documented at http://docs.aws.amazon.com/batch/latest/userguide/create-batch-ami.html

Please provide feedback on the docs if they aren’t clear about any aspects of this process:
https://docs.aws.amazon.com/forms/aws-doc-feedback?hidden_service_name=AWSBatch&topic_url=http://docs.aws.amazon.com/batch/latest/userguide/create-batch-ami.html&hidden_guide_name=User%20Guide&hidden_api_version=&hidden_file_name=create-batch-ami