【问题标题】:AWS::CloudFormation::Init fails to create files from S3 Bucket although the retrieval via aws "s3 cp s3" is successful尽管通过 aws "s3 cp s3" 检索成功,但 AWS::CloudFormation::Init 无法从 S3 Bucket 创建文件
【发布时间】:2019-12-09 16:23:22
【问题描述】:

在 EC2 实例初始化期间,我试图从 S3 存储桶中检索文件。 没有失败(语法)。但是缺少一些东西。 根目录下没有创建initial_setup.sh文件 有这么多文章都说相同(或者至少根据我作为新手的谦虚理解)

Parameters:
 MyVPC: { Type: String, Default: vpc-000xxx }
 myNstdKeyName: { Type: String, Default: xxx-key-test }
 myNstdBucket: { Type: String, Default: myBucket }
 myNstdEC2HostSubnet: { Type: String, Default: subnet-0xxxxx }
 myNstdImageId: { Type: 'AWS::EC2::Image::Id', Default: 'ami-0xxxx' }

Resources:
#Allow incoming SSH and all types of outgoing traffic
 SSHSecGrp4Pub:
  Type: AWS::EC2::SecurityGroup
  Properties:
   GroupDescription: Security group to allow SSH connection in public subnet
   VpcId: !Ref MyVPC
   SecurityGroupIngress: [ { IpProtocol: tcp, FromPort: 22, ToPort: 22, CidrIp: 0.0.0.0/0  } ]
   SecurityGroupEgress: [ { IpProtocol: -1, CidrIp: 0.0.0.0/0, FromPort: 1, ToPort: 65535 } ]
#Assume Role  
 SAPEC2Role:
  Type: AWS::IAM::Role
  Properties:
   AssumeRolePolicyDocument:
   RoleName: EC2AWSAccess
   AssumeRolePolicyDocument:
    Statement: [ { Effect: Allow, Principal: { Service: ec2.amazonaws.com } , Action: [ 'sts:AssumeRole' ] }  ]
#Policy for the above role
 S3RolePolicy:
  Type: AWS::IAM::Policy
  Properties:
   PolicyName: "S3DownloadPolicy"
   Roles: [ !Ref SAPEC2Role ]
   PolicyDocument:
    Statement:
     - Effect: Allow
       Action: [ 's3:GetObject' ]
       Resource: !Sub "arn:aws:s3:::${myNstdBucket}/*"
#Profile for EC2 Instance       
 SAPEC2Profile:
  Type: AWS::IAM::InstanceProfile 
  Properties: { InstanceProfileName: SAPEC2Profile,  Roles: [ !Ref SAPEC2Role ] }
#My EC2 Instance  
 EC2:
  Type: AWS::EC2::Instance
  Metadata:
   AWS::CloudFormation::Authentication: 
    S3Access: 
     type: "S3"
     roleName: { Ref: "SAPEC2Role" }
     buckets: [ !Ref myNstdBucket ]
   AWS::CloudFormation::Init:
    config:
     files:
      /root/initial_setup.sh: {
       source: !Sub "https://${myNstdBucket}.s3.eu-central-1.amazonaws.com/initial_setup.sh", 
       mode: "000777", 
       owner: root, 
       group: root, 
       authentication: "S3Access" 
       }
     commands:
       myStarter:
         command: "/bin/bash  /root/initial_setup.sh"  
  Properties:
   SubnetId: !Ref myNstdEC2HostSubnet
   ImageId: !Ref myNstdImageId
   InstanceType: t2.micro
   KeyName: !Ref myNstdKeyName
   IamInstanceProfile: !Ref SAPEC2Profile
   SecurityGroupIds: [ !Ref SSHSecGrp4Pub ]
   UserData:
    Fn::Base64: !Sub |
     #!/bin/bash 
     echo "my test file" > /root/testfile.txt

实例初始化后,我使用 aws cp s3://mybucket/initial_setup.sh 进行尝试 它可以工作,但我必须使用 dos2unix。

另一种方法是将它放在 UserData 中。但这也应该适用于命令。 (^)

也有人here 遇到了几乎相同的情况,但有人提到:

“为了使任何命令正常工作,我们需要在 Userdata 中提供一个 shell 环境,否则它无法创建任何文件”

我也将它添加为安全组之后的最后一行。

   UserData:
    Fn::Base64: !Sub |
     #!/bin/bash 
     echo "my test file" > /root/testfile.txt

所以 /root/testfile.txt 被创建,其中包含指定的文本。

但是存储桶中所需的文件没有显示出来。

【问题讨论】:

    标签: amazon-s3 aws-cloudformation-custom-resource


    【解决方案1】:

    我有一个误解,即仅通过说明元数据内容,它也会被执行。但现在我已经完成了一半。 这是 UserData 下缺少的部分

    UserData:
     Fn::Base64:  !Sub |
      cd /tmp
      wget https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz
      gzip -df aws-cfn-bootstrap-latest.tar.gz
      tar -xvf aws-cfn-bootstrap-latest.tar
      chmod -R 755 /tmp/aws-cfn-bootstrap-1.4
      pip install --upgrade pip
      pip install --upgrade setuptools
      pip install awscli --ignore-installed six &> /dev/null
      export PYTHONPATH=/tmp/aws-cfn-bootstrap-1.4
      /tmp/aws-cfn-bootstrap-1.4/bin/cfn-init -v  --stack ${AWS::StackName} --resource EC2 --region ${AWS::Region}
    

    实际上是最后一行有所不同。之前的行用于设置配置,因为使用的图像是非 aws 图像。所以它并没有带来开箱即用的功能。

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2019-12-22
      相关资源
      最近更新 更多