How do I copy data from AWS S3 to EC2 in a CloudFormation template?
我创建了一个启动 AutoScaling 组的 CloudFormation 模板。在启动期间,允许
模板中的用户数据如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | "UserData": { "Fn::Base64": { "Fn::Join": [ "\ ", [ "#!/bin/bash", "yum update -y", "yum install -y httpd24 php56", "service httpd start", "chkconfig httpd on", "groupadd DMO", "usermod -a -G DMO ec2-user", "chgrp -R DMO /var/www", "chmod 2775 /var/www", "find /var/www -type d -exec chmod 2775 {} +", "find /var/www -type f -exec chmod 0664 {} +" ] ] } } |
要坚持您已经在做的事情,您可以从您的 userdata 脚本中运行 AWS CLI:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | "UserData": { "Fn::Base64": { "Fn::Join": [ "\ ", [ "#!/bin/bash", "yum update -y", "yum install -y httpd24 php56", "service httpd start", "chkconfig httpd on", "groupadd DMO", "usermod -a -G DMO ec2-user", "chgrp -R DMO /var/www", "chmod 2775 /var/www", "aws s3 cp s3://MYBUCKET/MYFILE.zip /tmp", "unzip -d /var/www /tmp/MYFILE.zip", "rm /tmp/MYFILE.zip", "find /var/www -type d -exec chmod 2775 {} +", "find /var/www -type f -exec chmod 0664 {} +" ] ] } } |
为此,您的 EC2 实例配置文件必须授予从 S3 读取文件的权限。
另一种方法是使用 AWS::CloudFormation::Init:它是一个预定义的元数据键,您可以附加到
这里有教程