使用 cloudformation 模板创建 s3 存储桶时出现 ValidationError

Getting ValidationError while creating s3 bucket using cloudformation template

我是 cloudformation 模板的新手。使用 codepipeline 我正在尝试创建一个 s3 存储桶。这是 cloudformation 模板:

---
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" 

我在使用 codepipeline 执行 Lint 时遇到验证错误:

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

我该如何解决这个问题?

缩进在 YAML 中很重要,您的 SpaLoggingBucket 块似乎与 S3Bucketxls 等其他资源不一致,导致 CloudFormation 无法将其正确检测为资源。

将它向后移动一个缩进级别对我有用:

---
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。使用 JSON 的好处之一是不会 运行 发生这样的事故。