简体   繁体   English

使用云形成将文件从 S3 存储桶复制到 EC2 主目录

[英]Copy file(s) from S3 bucket to EC2 home using cloud formation

I am trying to copy a file from S3 bucket to EC2 home directory.我正在尝试将文件从 S3 存储桶复制到 EC2 主目录。 I wrote a script using help from this SOF Answer我使用此SOF Answer的帮助编写了一个脚本

However in my case the script is getting executed without any errors but I am unable to see the file getting copied in my instance.但是,在我的情况下,脚本正在执行而没有任何错误,但我无法在我的实例中看到文件被复制。 Can someone help me figure out what am I missing.有人可以帮我弄清楚我错过了什么。

Cloud formation script:云形成脚本:

Properties:
  ImageId: !FindInMap [Region2AMI, !Ref 'AWS::Region', 'AMI']
  InstanceType: t2.micro
  SecurityGroups:
    - !Ref WebserverSecurityGroup
  Tags:
    - Key: Name
      Value: Amazon Linux w/ nginx included -2
  KeyName: !Ref KeyName
  UserData:
    'Fn::Base64': !Sub |
      #!/bin/bash -x
      yum update -y aws-cfn-bootstrap
      sudo yum install git -y
      sudo yum update -y
      sudo yum install nginx -y
      sudo service nginx enable
      sudo service nginx start
      mkdir /home/ec2-user/s3-dist
      aws s3 cp s3://ai-dashboard-bucket/dist.zip /tmp
      unzip -d /home/ec2-user/s3-dist /tmp/dist.zip
      /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}  --configSets InstallAndConfig

Edit: I checked the logs file and this is error I am getting.编辑:我检查了日志文件,这是我得到的错误。 I checked the logs and found the following error我检查了日志,发现以下错误

aws s3 sync s3://ai-dashboard-bucket/dist.zip /home/ec2-user fatal error: Unable to locate credentials aws s3 sync s3://ai-dashboard-bucket/dist.zip /home/ec2-user 致命错误:无法找到凭据

How do I pass credentials?我如何通过凭据?

Thanks for the help.谢谢您的帮助。

Looks like the IAM instance profile doesn't have the permissions to access objects in ai-dashboard-bucket .看起来 IAM 实例配置文件没有访问ai-dashboard-bucket中对象的权限。

Try adding an IAM Instance profile to your EC2 instance尝试将 IAM 实例配置文件添加到您的 EC2 实例

Resources:
  InstanceRole:
    Type: AWS::IAM::Role
    Properties: 
      AssumeRolePolicyDocument:
        Version: "2012-10-17"
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ec2.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      Policies: 
        - PolicyName: root
          PolicyDocument:
            Version: "2012-10-17"
            Statement:
              - Effect: Allow
                Action: 's3:Get*'
                Resource: !Sub 'arn:${AWS::Partition}:s3:::ai-dashboard-bucket/*'

  InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Roles: 
        - !Ref InstanceRole
  Ec2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: !FindInMap [Region2AMI, !Ref 'AWS::Region', 'AMI']
      InstanceType: t2.micro
      IamInstanceProfile: !GetAtt InstanceRole.Arn  # Using the role created above
      SecurityGroups:
        - !Ref WebserverSecurityGroup
      Tags:
        - Key: Name
          Value: Amazon Linux w/ nginx included -2
      KeyName: !Ref KeyName
      UserData:
        'Fn::Base64': !Sub |
          #!/bin/bash -x
          yum update -y aws-cfn-bootstrap
          sudo yum install git -y
          sudo yum update -y
          sudo yum install nginx -y
          sudo service nginx enable
          sudo service nginx start
          mkdir /home/ec2-user/s3-dist
          aws s3 cp s3://ai-dashboard-bucket/dist.zip /tmp
          unzip -d /home/ec2-user/s3-dist /tmp/dist.zip
          /opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --region ${AWS::Region}  --configSets InstallAndConfig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AWS Lambda 函数将文件从 S3 存储桶复制到 EC2 Windows 实例 - How to copy a file from S3 bucket to an EC2 Windows instance using AWS Lambda function 如何使用 lambda function 将文件从 s3 存储桶复制到 ec2 实例? - How do I copy a file from s3 bucket to ec2 instance using lambda function? 文件从 ec2 复制到 aws 中的 s3 存储桶端口 - files copy from ec2 to s3 bucket port in aws 如何使用 cloudformation 模板中的用户数据将文件从 s3 存储桶复制到 ec2 实例中 - How can I copy a file from s3 bucket into an ec2 instance using userdata inside a cloudformation template 使用 Lambda 将文件从 S3 存储桶复制到 EC2 Windows 实例 - Copy files from S3 bucket to EC2 Windows Instance using Lambda 使用 python 将文件从 s3 存储桶复制到 ec2 实例 - Copy files from s3 bucket to ec2 instance using python 如何使用 AWS Lambda 函数将文件从 AWS S3 存储桶复制到 EC2 linux 机器 - How to copy files from AWS S3 bucket to EC2 linux machine using AWS Lambda Functions 如何在不使用云形成模板中的互联网访问的情况下将 ec2 连接到 s3 - how to connect ec2 to s3 without using internet access in cloud formation template 自动将文件从 S3 复制到 EC2 - Copy a File From S3 to EC2 Automatically 如何将Windows EC2实例复制到AWS中的S3存储桶? - How to copy Windows EC2 instances to S3 bucket in AWS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM