0

我开始学习/使用 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 收到一个识别错误:

有什么建议或提示吗?

非常感谢你的帮助

4

2 回答 2

0

该错误表示您的代码中有缩进问题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
于 2021-01-12T22:21:31.353 回答
0

您可以将此作为在 cloudformation 模板中写入用户数据的示例。

使用cfn-linter文档生成模板,文档中也有很多很好的示例。

Github 搜索总是会产生工作示例。

linter 可以作为插件安装在VS Code中,或者您可以从上述命令行中运行它。

于 2021-01-12T14:11:50.400 回答