Search code examples
amazon-web-servicesaws-cloudformation-custom-resource

Error "Index: 0, Size: 0" while using AWSUtility::CloudFormation::CommandRunner in cloud formation template


We are trying to run few bash commands in cloud formation template in AWS.

We followed the following blog of AWS to run CommandRunner utilty in cloud formation template. https://aws.amazon.com/blogs/mt/running-bash-commands-in-aws-cloudformation-templates/

Resources:
    CommandRunner:
        Type: AWSUtility::CloudFormation::CommandRunner
        Properties: 
            Command: 'echo HelloWorld > /command-output.txt'

Outputs:
  Op1:
    Description: Test output
    Value:
      Fn::GetAtt: CommandRunner.Output

Facing following error while running command utility in cloud formation template.

Resource handler returned message: "Index: 0, Size: 0" (RequestToken: c99903a7-****-0c58-010c-4fa03699fcfd, HandlerErrorCode: InternalFailure)

Stack ID(s) / Stack ARN: arn:aws:cloudformation:eu-west-2:*****:stack/comandrunner-test-iops/ **

The error message is not descriptive enough to identify the root cause.


Solution

  • When contacted AWS Supported team, they gave the following response.

    The Index: 0, Size: 0 error usually happens because you may not have a default VPC in the eu-west-2 region (P.S.: I am not able to verify this since I do not have access to your account).

    In general, CommandRunner uses the default VPC to run the command, but when it doesn’t exist this error is thrown. You can still run the CommandRunner without a default VPC, by specifying the SubnetId property.

    I also found an active issue pulled in on the github repository to improve the error handling for this error when it occurs on CloudFormation.

    After their response, we added Subnet id and security group id to the template.

    Give private subnet id.

    Parameters:
      ReqSubnetId:
        Type: String
        Description: Please enter Subnet id so that command runner can execute
        Default: subnet-0sampleid28474
        
      ReqSecurityGroupId:
        Type: String
        Description: Please enter security group id so that command runner can execute
        Default: sg-03samplesecuritygroupid
    
    
    Resources:
        CommandRunner:
            Type: AWSUtility::CloudFormation::CommandRunner
            Properties: 
                Command: 'echo HelloWorld > /command-output.txt'
                SubnetId: !Ref ReqSubnetId
                SecurityGroupId: !Ref ReqSecurityGroupId
                
    
    Outputs:
      Op1:
        Description: Test output
        Value:
          Fn::GetAtt: CommandRunner.Output
    

    Options supported for Type: AWSUtility::CloudFormation::CommandRunner are

    Type: AWSUtility::CloudFormation::CommandRunner
    Properties:
        Command: String
        Role: String
        LogGroup: String
        SubnetId: String
        KeyId: String
        SecurityGroupId: String
    

    Reference: https://github.com/aws-cloudformation/aws-cloudformation-resource-providers-awsutilities-commandrunner/tree/master/docs#yaml