1

在我的 cloudformation 模板下方:

我已经添加了所有的资源代码,请原谅缩进问题(复制粘贴的东西),我向你保证模板正在运行。

  ---
AWSTemplateFormatVersion: '2010-09-09'
Description: Sets up your AWS Batch Environment for running  workflows

Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      - Label:
          default: Compute Environment Config
        Parameters:
          - ComputeEnvironmentName
          - VpcId
          - SubnetIds
          - MinvCpus
          - MaxvCpus
          - DesiredvCpus
      - Label:
          default: Job Definition
        Parameters:
          - JobDefinitionName
          - DockerImage
          - Vcpus
          - Memory
          - Command
          - RetryNumber
      - Label:
          default: Job Queue
        Parameters:
          - JobQueueName


Parameters:
  VpcId:
    Type: 'AWS::EC2::VPC::Id'
    Description: >-
      VpcId of where the whole batch should be deployed. The VPC should have
      2 private subnets.
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Id>
    Description: Subnets you want your batch compute environment to launch in. Recommend private subnets
  MinvCpus:
    Type: String
    Description: Minimum number of CPUs in the compute environment. Default 0.
    Default: 0
    AllowedPattern: "[0-9]+"
  DesiredvCpus:
    Type: String
    Description: Desired number of CPUs in the compute environment to launch with. Default 0.
    Default: 0
    AllowedPattern: "[0-9]+"
  MaxvCpus:
    Type: String
    Description: Maximum number of CPUs in the compute environment. Should be >= than MinCpus
    Default: 256
    AllowedPattern: "[0-9]+"
  RetryNumber:
    Type: String
    Default: "1"
    Description: Number of retries for each AWS Batch job. Integer required.
    MaxLength: 1
    AllowedPattern: "[1-9]"
    ConstraintDescription: Value between 1 and 9

  DockerImage:
    Type: String
    Description: Docker image used to run your jobs
  Vcpus:
    Type: Number
    Description: vCPUs available to Jobs. Default is usually fine
    Default: 2
  Memory:
    Type: Number
    Description: Memory (in MB) available to Jobs. Default is usually fine
    Default: 2000
  JobQueueName:
    Type: String
    Description: Enter job queue Name
  JobDefinitionName:
    Type: String
    Description: Enter JobDefinition Name for the batch
  ComputeEnvironmentName:
    Type: String
    Description: Enter name of the Compute Environment
  VPCCidr:
    Type: String
    Description: 'Cidr Block of the VPC, allows for ssh access internally.'
    Default: '10.0.0.0/8'
    MinLength: "9"
    MaxLength: "18"
    AllowedPattern: "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})"
    ConstraintDescription: "Must be valid CIDR notation (i.e. x.x.x.x/x)."
  Command:
    Type: CommaDelimitedList
    Description: The command that is passed to the container
  CreateNewRepository:
    Default: false
    Description: >-
      Set this to true if you want to create a new Repository, else
      it will not create a new one
    Type: String
    AllowedValues:
      - true
      - false   
  RepositoryName:
    Type: String
    Description: Enter name of the new Repository.

Conditions:
  CreateRepository: !Equals
    - !Ref CreateNewRepository
    - true
  isCommandPresent: !Not [!Equals [!Ref CreateNewRepository, '']]

Resources:
  JobDefinition:
    Type: AWS::Batch::JobDefinition
    Properties:
      Type: container
      JobDefinitionName: !Ref JobDefinitionName
      ContainerProperties:
        Image: !Ref DockerImage
        Vcpus: !Ref Vcpus
        Memory: !Ref Memory
        Command: !Ref Command
        ReadonlyRootFilesystem: true
        Privileged: true
      RetryStrategy:
        Attempts: !Ref RetryNumber
  JobQueue:
   Type: AWS::Batch::JobQueue
   Properties:
    ComputeEnvironmentOrder:
      - Order: 1
        ComputeEnvironment: !Ref MyComputeEnv
    State: ENABLED
    Priority: 10
    JobQueueName: !Ref JobQueueName

  myVPCSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties: 
      GroupDescription: Security group for batch process.
      SecurityGroupEgress:
        - CidrIp: 0.0.0.0/0
          IpProtocol: '-1'
      SecurityGroupIngress:
        - CidrIp: !Ref VPCCidr
          IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
      VpcId: !Ref VpcId
  MyComputeEnv:
   Type: AWS::Batch::ComputeEnvironment
   Properties:
    Type: MANAGED
    ServiceRole: !GetAtt awsBatchServiceRole.Arn
    ComputeEnvironmentName: !Ref ComputeEnvironmentName
    ComputeResources:
      MinvCpus: !Ref MinvCpus
      MaxvCpus: !Ref MaxvCpus
      DesiredvCpus: !Ref DesiredvCpus
      SecurityGroupIds: [!GetAtt myVPCSecurityGroup.GroupId]
      Type: EC2
      Subnets: !Ref SubnetIds
      InstanceRole: !GetAtt InstanceProfile.Arn
      InstanceTypes:
        - optimal
    State: ENABLED
  awsBatchServiceRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: 2012-10-17
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - "batch.amazonaws.com"
              Action:
                - "sts:AssumeRole"
        ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole
  ecsInstanceRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: InstanceRole
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - "ec2.amazonaws.com"
            Action:
              - "sts:AssumeRole"
      ManagedPolicyArns:
      - "arn:aws:iam::aws:policy/AmazonEC2FullAccess"
      - "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
  InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      InstanceProfileName: InstanceProfile
      Roles:
      - !Ref ecsInstanceRole
  MyRepository: 
    Type: AWS::ECR::Repository
    Condition: CreateRepository
    Properties: 
      RepositoryName: !Ref RepositoryName
      RepositoryPolicyText: 
        Version: "2012-10-17"
        Statement: 
          - 
            Sid: AllowPushPull
            Effect: Allow
            Principal: "*"
            Action: 
              - "ecr:*"

我收到此错误:

Operation failed, ComputeEnvironment went INVALID with error: CLIENT_ERROR - The security group 'sg-d9b85d91' does not exist

我不知道代码有什么问题,但奇怪的是,myVPCSecurityGroup 创建的 SecurityGroupIds 是 sg-2869f263,但 ComputeEnvironment 正在尝试查找 sg-d9b85d91。

4

2 回答 2

0

在这里暗中刺伤只是为我的手机工作,但我认为这是因为您的计算机环境可能没有 V PC

于 2018-06-26T13:18:30.083 回答
0

Disabling the Compute Environment in the UI and enabling it back again fixed the issue.

于 2021-05-19T18:53:30.680 回答