You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AWS CDK(Python)中为Batch计算环境指定自定义EC2镜像ID

在AWS CDK(Python)中配置Batch GPU计算环境的特定EC2镜像

要在CDK中为Batch GPU计算环境指定EC2镜像,分两种场景处理,以下是具体实现方案:

使用AWS官方GPU优化ECS AMI

AWS提供了预配置好NVIDIA驱动、ECS容器代理的GPU优化AMI,直接调用EcsMachineImage.from_ecs_optimized并开启gpu=True即可,无需手动指定AMI ID,示例代码:

from aws_cdk import aws_batch as batch, aws_ec2 as ec2, Stack
from constructs import Construct

class BatchGpuStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # 复用现有VPC或创建新VPC
        vpc = ec2.Vpc(self, "BatchGpuVpc", max_azs=2)

        # 创建GPU计算环境
        gpu_compute_env = batch.ComputeEnvironment(self, "GpuComputeEnv",
            compute_resources=batch.ComputeResources(
                type=batch.ComputeResourceType.EC2,
                vpc=vpc,
                # 指定支持GPU的实例类型
                instance_types=[
                    ec2.InstanceType("g4dn.xlarge"),
                    ec2.InstanceType("p3.2xlarge")
                ],
                # 启用GPU优化的ECS官方AMI
                machine_image=batch.EcsMachineImage.from_ecs_optimized(
                    operating_system=batch.OperatingSystemType.LINUX,
                    gpu=True
                ),
                minv_cpus=0,
                maxv_cpus=32,
                subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS)
            )
        )

指定自定义GPU AMI

如果需要使用自己构建的GPU优化AMI,可通过EcsMachineImage.from_ami_id传入自定义AMI ID,注意该AMI必须已安装NVIDIA驱动、Docker和ECS容器代理:

from aws_cdk import aws_batch as batch, aws_ec2 as ec2, Stack
from constructs import Construct

class CustomGpuBatchStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        vpc = ec2.Vpc(self, "CustomGpuVpc")

        # 定义跨区域的自定义GPU AMI映射
        custom_gpu_ami_map = {
            "us-east-1": "ami-0abcdef1234567890",
            "eu-west-1": "ami-0fedcba9876543210"
        }
        custom_machine_image = ec2.MachineImage.generic_linux(ami_map=custom_gpu_ami_map)

        # 创建使用自定义AMI的计算环境
        custom_gpu_env = batch.ComputeEnvironment(self, "CustomGpuComputeEnv",
            compute_resources=batch.ComputeResources(
                type=batch.ComputeResourceType.EC2,
                vpc=vpc,
                instance_types=[ec2.InstanceType("g5.xlarge")],
                machine_image=batch.EcsMachineImage.from_ami_id(
                    ami_id=custom_machine_image.get_image(self).image_id,
                    operating_system=batch.OperatingSystemType.LINUX
                ),
                minv_cpus=0,
                maxv_cpus=16
            )
        )

关键注意事项

  • 必须选择支持GPU的EC2实例类型(如g4dn、g5、p3系列),否则GPU任务无法运行
  • 自定义AMI需确保NVIDIA驱动、ECS代理、Docker环境配置正确,否则Batch任务会启动失败
  • 计算环境的IAM角色需具备EC2实例创建、ECS集群管理、CloudWatch日志推送等必要权限

内容的提问来源于stack exchange,提问作者Luiz Tauffer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 08:43:31