如何使用具有弹性beanstalk的amazon Web服务将秘密文件推送到EC2 Ruby on Rails应用程序?

nik*_*ikc 12 ruby ruby-on-rails amazon-s3 amazon-ec2 amazon-web-services

如何使用具有弹性beanstalk的amazon Web服务将秘密文件推送到EC2 Ruby on Rails应用程序?

我将文件添加到git存储库,然后我推送到github,但我想将我的秘密文件保存在git存储库之外.我正在使用以下部署到aws:

git aws.push
Run Code Online (Sandbox Code Playgroud)

以下文件位于.gitignore中:

/config/database.yml
/config/initializers/omniauth.rb
/config/initializers/secret_token.rb
Run Code Online (Sandbox Code Playgroud)

在此链接之后,我尝试将S3文件添加到我的部署中:http: //docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html

引用该链接:

示例代码段

以下示例从Amazon S3存储桶下载zip文件并将其解压缩到/ etc/myapp中:

sources:  
    /etc/myapp: http://s3.amazonaws.com/mybucket/myobject 
Run Code Online (Sandbox Code Playgroud)

按照这些说明,我将文件上传到S3存储桶,并将以下内容添加到.ebextensions目录中的private.config文件中:

sources:
  /var/app/current/: https://s3.amazonaws.com/mybucket/config.tar.gz
Run Code Online (Sandbox Code Playgroud)

该config.tar.gz文件将提取到:

/config/database.yml
/config/initializers/omniauth.rb
/config/initializers/secret_token.rb
Run Code Online (Sandbox Code Playgroud)

但是,部署应用程序时,不会复制或提取S3主机上的config.tar.gz文件.我仍然收到无法找到database.yml的错误,并且EC2日志没有配置文件的记录,这里是错误消息:

Error message:
  No such file or directory - /var/app/current/config/database.yml
Exception class:
  Errno::ENOENT
Application root:
  /var/app/current
Run Code Online (Sandbox Code Playgroud)

Pas*_*que 1

可以(而且很容易)将敏感文件存储在 S3 中并自动将它们复制到您的 Beanstalk 实例。

当您创建 Beanstalk 应用程序时,会自动创建一个 S3 存储桶。该存储桶用于存储应用程序版本、日志、元数据等。

aws-elasticbeanstalk-ec2-role分配给您的 Beanstalk 环境的默认值具有对此存储桶的读取访问权限。

因此,您需要做的就是将敏感文件放入该存储桶中(位于存储桶的根目录或您想要的任何目录结构中),然后创建一个.ebextension配置文件以将它们复制到您的 EC2 实例。

这是一个例子:

# .ebextensions/sensitive_files.config

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Auth:
          type: "s3"
          buckets: ["elasticbeanstalk-us-east-1-XXX"] # Replace with your bucket name
          roleName: 
            "Fn::GetOptionSetting": 
              Namespace: "aws:autoscaling:launchconfiguration"
              OptionName: "IamInstanceProfile"
              DefaultValue: "aws-elasticbeanstalk-ec2-role" # This is the default role created for you when creating a new Beanstalk environment. Change it if you are using a custom role

files:
  /etc/pki/tls/certs/server.key: # This is where the file will be copied on the EC2 instances
    mode: "000400" # Apply restrictive permissions to the file
    owner: root # Or nodejs, or whatever suits your needs
    group: root # Or nodejs, or whatever suits your needs
    authentication: "S3Auth"
    source: https://s3-us-west-2.amazonaws.com/elasticbeanstalk-us-east-1-XXX/server.key # URL to the file in S3
Run Code Online (Sandbox Code Playgroud)

此处记录:http ://docs.aws.amazon.com/elasticbeanstalk/latest/dg/https-storingprivatekeys.html