amazon-web-services - 为什么我在 AWS Cloudformation 上收到 "YAMLException: bad indentation of a mapping entry"?

标签 amazon-web-services amazon-ec2 aws-cloudformation aws-cloudformation-custom-resource

我开始学习/使用 AWS 和 Cloudformation,我有这个:

Type: 'AWS::EC2::Instance'
Properties:
 SecurityGroupIds:
    - !Ref InstanceSecurityGroup
 SubnetId: 
     !Ref "PublicSubnet1"
 IamInstanceProfile:
     !Ref RootInstanceProfile
 ImageId: ami-02cb52d7ba9887a93
 InstanceType: t3.micro
 UserData:
   Fn::Base64: !Sub |
    #!/bin/bash
    # Install Apache Web Server and PHP
      yum install -y httpd mysql
      amazon-linux-extras install -y php7.2
    # Download Lab files
      wget https://us-west-2-tcprod.s3.amazonaws.com/courses/ILT-TF-100-ARCHIT/v6.5.0/lab-2-webapp/scripts/inventory-app.zip
      unzip inventory-app.zip -d /var/www/html/
    # Download and install the AWS SDK for PHP
      wget https://github.com/aws/aws-sdk-php/releases/download/3.62.3/aws.zip
      unzip aws -d /var/www/html
    # Turn on web server
      chkconfig httpd on
      service httpd start

当我尝试创建堆栈时,我在 UserData 处收到识别错误:

有什么建议或提示吗?

非常感谢您的帮助

最佳答案

该错误表明您的 UserData 中存在缩进问题,这从您的代码中可以清楚地看出。应该是(您也不需要 Sub):

Type: 'AWS::EC2::Instance'
Properties:
 SecurityGroupIds:
    - !Ref InstanceSecurityGroup
 SubnetId: 
     !Ref "PublicSubnet1"
 IamInstanceProfile:
     !Ref RootInstanceProfile
 ImageId: ami-02cb52d7ba9887a93
 InstanceType: t3.micro
 UserData:
   Fn::Base64: |
      #!/bin/bash
      # Install Apache Web Server and PHP
      yum install -y httpd mysql
      amazon-linux-extras install -y php7.2
      # Download Lab files
      wget https://us-west-2-tcprod.s3.amazonaws.com/courses/ILT-TF-100-ARCHIT/v6.5.0/lab-2-webapp/scripts/inventory-app.zip
      unzip inventory-app.zip -d /var/www/html/
      # Download and install the AWS SDK for PHP
      wget https://github.com/aws/aws-sdk-php/releases/download/3.62.3/aws.zip
      unzip aws -d /var/www/html
      # Turn on web server
      chkconfig httpd on
      service httpd start

关于amazon-web-services - 为什么我在 AWS Cloudformation 上收到 "YAMLException: bad indentation of a mapping entry"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65685467/

相关文章:

amazon-web-services - 在Amazon SES中区分退回和投诉

amazon-web-services - 如何在 AWS IAAS 中执行端口镜像?

linux - 在亚马逊云服务器上设置 FTP

json - CloudFormation - 如何使用 Sub 内在函数?

amazon-web-services - 使用内联策略创建 IAM 角色的 cloudformation 模板

amazon-web-services - AWS ECS - 无法在 cloudformation 模板中指定服务名称

amazon-web-services - 如何为在 Cloudformation 中创建的 Lambda 设置 Cloudwatch 日志

amazon-web-services - AWS Secrets Manager 找不到指定的 key

ssh - 集成 Terraform 和 Serverspec

amazon-web-services - AWS Elastic Beanstalk - 启动 SWF 后台工作线程