【问题标题】:How to access contents of an s3 bucket through cloudformation template?如何通过 cloudformation 模板访问 s3 存储桶的内容?
【发布时间】:2019-02-02 12:33:36
【问题描述】:

我目前已经设置了一个包含单个文件的 S3 存储桶。我还有一个 cloudformation 模板,它启动了一个具有 IAM 角色的 ec2 实例,我相信它允许访问这个 S3 存储桶。我究竟如何在我的 ec2 实例中访问此文件?我希望当堆栈完成部署时该文件出现在实例上。

【问题讨论】:

标签: amazon-web-services amazon-s3 amazon-ec2 amazon-cloudformation


【解决方案1】:

您需要将角色附加到您的实例。这是一个例子

AWSTemplateFormatVersion: '2010-09-09'
Description: Attach IAM Role to an EC2
Resources:
  Test:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType:
        Ref: InstanceType
      IamInstanceProfile:
        Ref: ListS3BucketsInstanceProfile
  ListS3BucketsInstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties:
      Path: "/"
      Roles:
      - Ref: ListS3BucketsRole
  ListS3BucketsPolicy:
    Type: AWS::IAM::Policy
    Properties:
      PolicyName: ListS3BucketsPolicy
      PolicyDocument:
        Statement:
        - Effect: Allow
          Action:
          - s3:List*
          Resource: "*"
      Roles:
      - Ref: ListS3BucketsRole
  ListS3BucketsRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - ec2.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"

ListS3BucketsInstanceProfile 担任角色:ListS3BucketsRole
ListS3BucketsPolicy 附加到 ListS3BucketsRole,这允许该角色列出所有 s3 对象。

有了这个,您的 EC2 实例可以列出 S3 上的文件

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 2020-10-17
    • 2021-05-06
    • 2015-04-30
    • 1970-01-01
    • 2023-04-11
    • 2020-05-05
    • 2017-06-08
    • 2020-06-24
    相关资源
    最近更新 更多