Skip to content

Cloudformation template validation error

0

Hi, I am trying to use cloudformation ,but I got an error message below when validating template with awscli.

$aws cloudformation validate-template --template-body file://awsbatch.yml

An error occurred (ValidationError) when calling the ValidateTemplate operation: Template format error: Unresolved resource dependencies [] in the Resources block of the template

Below is my template. It seems that something wrong in aws batch job definition's part. Could you tell me what is wrong in the template?

AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Sample template to illustrate use of existing S3 bucket as an event source for a Lambda function
Parameters:
  ################入力箇所
  ###############
  BucketName:
    Type: String
    Description: "バケット"
    Default: "Test"
  EmployeeID:
    Type: String
    Description: "社員番号"
    Default: "123456"
  ProjectName:
    Type: String
    Description: "プロジェクト名"
    Default: "TestProject"
  MemSize:
    Type: Number
    Description: memoryサイズ(単位MB)
    Default: 1024
  ImageURL:
    Type: String
    Description: "ECRのrepositoryのpath"
  TimeOutSec:
    Type: Number
    Description: "timeouta(単位:秒)"
    Default: 1800
  UlimitsName:
    Type: String
    Default: "nofile"
  UlimitsSoft:
    Type: Number
    Default: 65535
  UlimitsHard:
    Type: Number
    Default: 65535
  ######################################
  #######################################
  ###
  ExecutionRoleArn:
    Type: String
    Description: "arn"
    Default: "arn:aws:iam::1234:role/awsbatch-ECSTaskExecutionRole"
  VCPU:
    Type: Number
  Memory:
    Type: Number
    Default: 1024
  GPU:
    Type: Number
    Default: 1
  ShmSize:
    Type: Number
    Description: "shared memory mega byte(MB)"
    Default: 4096
  CostSharingKey:
    Type: String
    Default: "Cost Sharing"
  CostSharingValue:
    Type: String
    Default: "awsbatch"
  CostDetailKey:
    Type: String
    Default: "Cost Detail"
  #cost sharingva lue
#####
#####
Resources:
  ECSTaskRole:
    Type: 'AWS::IAM::Role'
    Properties:
      RoleName: !Sub 'awsbatch-${EmployeeID}-${ProjectName}'
      Tags:
        - Key: !Ref CostSharingKey
          Value: !Ref CostSharingValue

      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ecs-tasks.amazonaws.com
                #lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      
      Path: /
      Policies:
        - PolicyName: !Sub 'awsbatch-${EmployeeID}-${ProjectName}'
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - "s3:ListBucket"
                Resource: !Sub 'arn:aws:s3:::${BucketName}' 
                Condition:
                  StringEquals:
                    "s3:prefix": !Sub "${EmployeeID}/${ProjectName}"
              - Effect: Allow
                Action:
                  - 's3:PutObject'
                  - 's3:AbortMultipartUpload'
                  - 's3:DeleteObject'
                Resource: !Sub 'arn:aws:s3:::${BucketName}/${EmployeeID}/${ProjectName}/output/'
              - Effect: Allow
                Action:
                  - 's3:GetObject'
                Resource: !Sub 'arn:aws:s3:::${BucketName}/${EmployeeID}/${ProjectName}/*'
  JobDefinition:
    Type: AWS::Batch::JobDefinition
    Properties:
      TimeOut:
        AttemptDurationSeconds: !Ref TimeOutSec
      Tags:
        - Key: !Ref CostSharingKey
          Value: !Ref CostSharingValue
        - Key: !Ref CostDetailKey
          Value: !Sub "${EmployeeID}-${ProjectName}"
      Type: container
      JobDefinitionName: !Sub "awsbatch-${EmployeeID}"
      #platformは明示しなければEC2なので省略
      PropagateTags: True      
      ExecutionRoleArn: !Ref ExecutionRoleArn
      TaskRoleArn: !Ref ECSTaskRole
      ecsProperties:
        TaskProperties: 
          - Containers: 
            - Command: 
              - echo "hoge"
              - echo "hoge2"
            image : !Ref ImageURL
            LinuxParameters: 
              SharedMemorySize: !Ref ShmSize
            LogConfiguration:
               LogDriver: "awslogs"
            ResourceRequirements: 
              - Type: VCPU
                Value: !Ref
              - Type: MEMORY
                Value: !Ref
              - Type: GPU
                Value: !Ref GPU
            User: root
            Ulimit:
              HardLimit: !Ref UlimitsHard
              Name: !Ref UlimitsName
              SoftLimit: !Ref UlimitsSoft

asked 2 years ago247 views

1 Answer
0

Hello.

Make your CloudFormation template as follows.
The cause of the error was that the variable referenced by "!Ref" was not set.
https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/aws-resource-batch-jobdefinition.html

AWSTemplateFormatVersion: 2010-09-09
Description: >-
  Sample template to illustrate use of existing S3 bucket as an event source for a Lambda function
Parameters:
  ################入力箇所
  ###############
  BucketName:
    Type: String
    Description: "バケット"
    Default: "Test"
  EmployeeID:
    Type: String
    Description: "社員番号"
    Default: "123456"
  ProjectName:
    Type: String
    Description: "プロジェクト名"
    Default: "TestProject"
  MemSize:
    Type: Number
    Description: memoryサイズ(単位MB)
    Default: 1024
  ImageURL:
    Type: String
    Description: "ECRのrepositoryのpath"
  TimeOutSec:
    Type: Number
    Description: "timeouta(単位:秒)"
    Default: 1800
  UlimitsName:
    Type: String
    Default: "nofile"
  UlimitsSoft:
    Type: Number
    Default: 65535
  UlimitsHard:
    Type: Number
    Default: 65535
  ######################################
  #######################################
  ###
  ExecutionRoleArn:
    Type: String
    Description: "arn"
    Default: "arn:aws:iam::1234:role/awsbatch-ECSTaskExecutionRole"
  VCPU:
    Type: Number
  Memory:
    Type: Number
    Default: 1024
  GPU:
    Type: Number
    Default: 1
  ShmSize:
    Type: Number
    Description: "shared memory mega byte(MB)"
    Default: 4096
  CostSharingKey:
    Type: String
    Default: "Cost Sharing"
  CostSharingValue:
    Type: String
    Default: "awsbatch"
  CostDetailKey:
    Type: String
    Default: "Cost Detail"
  #cost sharingva lue
#####
#####
Resources:
  ECSTaskRole:
    Type: 'AWS::IAM::Role'
    Properties:
      RoleName: !Sub 'awsbatch-${EmployeeID}-${ProjectName}'
      Tags:
        - Key: !Ref CostSharingKey
          Value: !Ref CostSharingValue

      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ecs-tasks.amazonaws.com
                #lambda.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      
      Path: /
      Policies:
        - PolicyName: !Sub 'awsbatch-${EmployeeID}-${ProjectName}'
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Action:
                  - "s3:ListBucket"
                Resource: !Sub 'arn:aws:s3:::${BucketName}' 
                Condition:
                  StringEquals:
                    "s3:prefix": !Sub "${EmployeeID}/${ProjectName}"
              - Effect: Allow
                Action:
                  - 's3:PutObject'
                  - 's3:AbortMultipartUpload'
                  - 's3:DeleteObject'
                Resource: !Sub 'arn:aws:s3:::${BucketName}/${EmployeeID}/${ProjectName}/output/'
              - Effect: Allow
                Action:
                  - 's3:GetObject'
                Resource: !Sub 'arn:aws:s3:::${BucketName}/${EmployeeID}/${ProjectName}/*'
  JobDefinition:
    Type: AWS::Batch::JobDefinition
    Properties:
      TimeOut:
        AttemptDurationSeconds: !Ref TimeOutSec
      Tags:
        - Key: !Ref CostSharingKey
          Value: !Ref CostSharingValue
        - Key: !Ref CostDetailKey
          Value: !Sub "${EmployeeID}-${ProjectName}"
      Type: container
      JobDefinitionName: !Sub "awsbatch-${EmployeeID}"
      #platformは明示しなければEC2なので省略
      PropagateTags: True      
      ExecutionRoleArn: !Ref ExecutionRoleArn
      TaskRoleArn: !GetAtt ECSTaskRole.Arn # <-- ARNを取得
      EcsProperties: # <-- EcsPropertiesに修正 (Eは大文字です)
        TaskProperties: 
          - Containers: 
            - Command: 
              - echo "hoge"
              - echo "hoge2"
            image : !Ref ImageURL
            LinuxParameters: 
              SharedMemorySize: !Ref ShmSize
            LogConfiguration:
               LogDriver: "awslogs"
            ResourceRequirements: 
              - Type: VCPU
                Value: !Ref VCPU # <-- 追加
              - Type: MEMORY
                Value: !Ref Memory # <-- 追加
              - Type: GPU
                Value: !Ref GPU
            User: root
            Ulimit:
              HardLimit: !Ref UlimitsHard
              Name: !Ref UlimitsName
              SoftLimit: !Ref UlimitsSoft
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Relevant content