【问题标题】:How to reference Instance Profile in .ebextension如何在 .ebextension 中引用实例配置文件
【发布时间】:2015-04-12 14:31:43
【问题描述】:

我的弹性 beanstalk 应用程序中需要一个队列,因此我在 .ebextensions/app.conf 中使用这个 sn-p 创建队列和队列策略:

  Resources:
    BackgroundTaskQueue:
      Type: "AWS::SQS::Queue"
    AllowWorkerSQSPolicy:
      Type: "AWS::SQS::QueuePolicy"
      Properties:
        Queues:
          -
            Ref: "BackgroundTaskQueue"
        PolicyDocument:
          Version: "2008-10-17"
          Id: "PublicationPolicy"
          Statement:
            -
              Sid: "Allow-Create-Task"
              Effect: "Allow"
              Principal:
                AWS: "*"
              Action:
                - "sqs:SendMessage"
              Resource:
                Fn::GetAtt:
                  - "BackgroundTaskQueue"
                  - "Arn" 

很遗憾,我找不到在自动扩展组中引用我的 EC2 实例的实例配置文件的方法。 (目前队列向世界开放)我尝试了两种方法:

  1. 读取配置:

              Principal:
                AWS: 
                  Fn::GetOptionSetting:
                    OptionName: "IamInstanceProfile"
    

据我所知,OptionName 总是从 aws:elasticbeanstalk:customoption 命名空间中检索,但 IamInstanceProfile 是在 aws:autoscaling:launchconfiguration 命名空间中定义的。 -> 运气不好

  1. 从实际AWSEBAutoScalingLaunchConfiguration资源中读取:

              Principal:
                AWS: 
                  Fn::GetAtt:
                    - "AWSEBAutoScalingLaunchConfiguration"
                    - "IamInstanceProfile"
    

此方法失败,因为属性IamInstanceProfile 未公开。

有没有人想办法让这样的政策发挥作用? 有谁知道如何指示 GetOptionSetting 在不同的命名空间中查找? 有人找到了获取实例配置文件的方法吗?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-elastic-beanstalk


    【解决方案1】:

    您需要在 eb 环境之外设置实例配置文件。您可以使用“aws iam”命令创建策略、角色和实例配置文件 (http://docs.aws.amazon.com/cli/latest/reference/iam/index.html#cli-aws-iam),然后在选项设置中指定配置文件:

        namespace: aws:autoscaling:launchconfiguration
        option_name: IamInstanceProfile
        value: your-instance-profile-name
    

    如果您使用的是 eb_deployer,则有一种独立的方式:

    创建一个 CloudFormation 模板来定义您的资源堆栈,例如config/my-resources.json:

    {
      "Outputs": {
        "InstanceProfile": {
          "Description": "defines what ec2 instance can do with aws resources",
          "Value": { "Ref":  "InstanceProfile" }
        }
      },
    
      "Resources": {
        "Role": {
          "Type": "AWS::IAM::Role",
          "Properties": {
            "AssumeRolePolicyDocument": {
              "Statement": [{
                "Effect": "Allow",
                "Principal": {
                  "Service": ["ec2.amazonaws.com"]
                },
                "Action": ["sts:AssumeRole"]
              }]
            },
            "Path": "/",
            "Policies": [ {
              "PolicyName": "S3Access",
              "PolicyDocument": {
                "Statement": [
                  {
                    "Effect": "Allow",
                    "Action": [
                      "s3:Get*",
                      "s3:List*",
                      "s3:PutObject"
                    ],
                    "Resource": "*"
                  }
                ]
              }
            }, {
              "PolicyName": "SQSAccess",
              "PolicyDocument": {
                "Statement": [ {
                  "Effect": "Allow",
                  "Action": [
                    "sqs:ChangeMessageVisibility",
                    "sqs:DeleteMessage",
                    "sqs:ReceiveMessage",
                    "sqs:SendMessage"
                  ],
                  "Resource": "*"
                }]
              }
            }]
          }
        },
        "InstanceProfile": {
          "Type": "AWS::IAM::InstanceProfile",
          "Properties": {
            "Path": "/",
            "Roles": [ { "Ref": "Role" } ]
          }
        }
      }
    }
    

    在您的 eb_deployer.yml 中添加“资源”部分

      resources:
        template: config/my-resources.json
        capabilities:
          - CAPABILITY_IAM
        outputs:
          InstanceProfile:
            namespace: aws:autoscaling:launchconfiguration
            option_name: IamInstanceProfile
    

    在上面的示例中,我们定义了一个实例配置文件,其中包含启用对 S3 和 SQS 的特定访问的策略。然后将实例配置文件名称(模板的输出)映射到 Elastic Beanstalk 选项设置。

    看看这个:https://github.com/ThoughtWorksStudios/eb_deployer/wiki/Elastic-Beanstalk-Tips-and-Tricks#setup-instance-profile-for-your-ec2-instances

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-21
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 2016-10-01
      • 2021-09-26
      • 2015-05-14
      相关资源
      最近更新 更多