Search code examples
amazon-web-servicesamazon-s3aws-cloudformationaws-cloudformation-custom-resource

Getting ValidationError while creating s3 bucket using cloudformation template


I am new to cloudformation template. Using codepipeline I am trying to create one s3 bucket. This is the cloudformation template:

---
AWSTemplateFormatVersion: 2010-09-09
Description: Template to create buckets and copy ymls to S3.

Parameters:
  SPABucket:
    Description: S3 bucket name for backend lambda functions
    Type: String
    Default: reference-data-migration-bucket-2021   
  
Resources:
  S3Bucketxls:
    Type: AWS::S3::Bucket
    Properties:
        BucketName: !Sub ${SPABucket}-${AWS::AccountId}-${AWS::Region}
        PublicAccessBlockConfiguration:
          BlockPublicPolicy: true
          IgnorePublicAcls: true
          RestrictPublicBuckets: true
        VersioningConfiguration:
          Status: Enabled
        AccessControl: Private
        LoggingConfiguration:
          DestinationBucketName: !Ref SpaLoggingBucket
          LogFilePrefix: S3Bucketxls
        BucketEncryption: 
          ServerSideEncryptionConfiguration: 
          - ServerSideEncryptionByDefault:
              SSEAlgorithm: 'AES256'          
        Tags:
        - Key: "sample"
          Value: "test"

    SpaLoggingBucket:
      Type: AWS::S3::Bucket
      Properties:       
        AccessControl: Private
        Tags:
        - Key: "sample"
          Value: "test"
        

Outputs:
  S3Bucketxlsx:
    Description: The CodeDeploy role for a particular business service being deployed
    Value: !GetAtt S3Bucketxls.Arn
    Export:
      Name: !Sub "${AWS::StackName}-S3BucketxlsArn" 

I am getting validation error while doing Lint using codepipeline:

An error occurred (ValidationError) when calling the ValidateTemplate operation: Invalid template resource property 'SpaLoggingBucket'

[Container] 2022/02/24 16:30:14 Command did not exit successfully aws cloudformation validate-template --template-body file://${TMPLNAME} exit status 254 [Container] 2022/02/24 16:30:14 Phase complete: BUILD State: FAILED [Container] 2022/02/24 16:30:14 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: aws cloudformation validate-template --template-body file://${TMPLNAME}. Reason: exit status 254

How can I fix this?


Solution

  • Indentation is important in YAML & it seems that your SpaLoggingBucket block is out of line with other resources like S3Bucketxls, making CloudFormation not detect it correctly as a resource.

    Moving it one indentation level back works for me:

    ---
    AWSTemplateFormatVersion: 2010-09-09
    Description: Template to create buckets and copy ymls to S3.
    
    Parameters:
      SPABucket:
        Description: S3 bucket name for backend lambda functions
        Type: String
        Default: reference-data-migration-bucket-2021   
      
    Resources:
      S3Bucketxls:
        Type: AWS::S3::Bucket
        Properties:
            BucketName: !Sub ${SPABucket}-${AWS::AccountId}-${AWS::Region}
            PublicAccessBlockConfiguration:
              BlockPublicPolicy: true
              IgnorePublicAcls: true
              RestrictPublicBuckets: true
            VersioningConfiguration:
              Status: Enabled
            AccessControl: Private
            LoggingConfiguration:
              DestinationBucketName: !Ref SpaLoggingBucket
              LogFilePrefix: S3Bucketxls
            BucketEncryption: 
              ServerSideEncryptionConfiguration: 
              - ServerSideEncryptionByDefault:
                  SSEAlgorithm: 'AES256'          
            Tags:
            - Key: "sample"
              Value: "test"
    
      SpaLoggingBucket:
        Type: AWS::S3::Bucket
        Properties:       
          AccessControl: Private
          Tags:
          - Key: "sample"
            Value: "test"
            
    
    Outputs:
      S3Bucketxlsx:
        Description: The CodeDeploy role for a particular business service being deployed
        Value: !GetAtt S3Bucketxls.Arn
        Export:
          Name: !Sub "${AWS::StackName}-S3BucketxlsArn" 
    

    P.S. one of the benefits of using JSON instead is not running into accidents like this.