【问题标题】:AWS CLI create RDS with elasticbeanstalk create-environmentAWS CLI 使用 elasticbeanstalk 创建环境创建 RDS
【发布时间】:2014-11-14 19:18:15
【问题描述】:

如何使用create-environmentaws elasticbeanstalk 的另一个子命令创建RDS 实例?我尝试了几种参数组合均无济于事。下面是一个例子。

APP_NAME="randall-railsapp"
aws s3api create-bucket --bucket "$APP_NAME"
APP_VERSION="$(git describe --always)"
APP_FILE="deploy-$APP_NAME-$APP_VERSION.zip"
git archive -o "$APP_FILE" HEAD
aws s3 cp "$APP_FILE" "s3://$APP_NAME/$APP_FILE"

aws --region us-east-1 elasticbeanstalk create-application-version \
--auto-create-application \
--application-name "$APP_NAME" \
--version-label "$APP_VERSION" \
--source-bundle S3Bucket="$APP_NAME",S3Key="$APP_FILE"

aws --region us-east-1 elasticbeanstalk create-environment \
--application-name "$APP_NAME" \
--version-label "$APP_VERSION" \
--environment-name "$APP_NAME-env" \
--description "randall's rails app environment" \
--solution-stack-name "64bit Amazon Linux 2014.03 v1.0.0 running Ruby 2.1 (Puma)" \
--cname-prefix "$APP_NAME-test" \
--option-settings file://test.json

还有test.json的内容:

[
{
    "OptionName": "EC2KeyName",
    "Namespace": "aws:autoscaling:launchconfiguration",
    "Value": "a-key-is-here"
},
{
    "OptionName": "EnvironmentType",
    "Namespace": "aws:elasticbeanstalk:environment",
    "Value": "SingleInstance"
},
{
    "OptionName": "SECRET_KEY_BASE",
    "Namespace": "aws:elasticbeanstalk:application:environment",
    "Value": "HAHAHAHAHAHA"
},
{
    "OptionName": "DBPassword",
    "Namespace": "aws:rds:dbinstance",
    "Value": "hunter2"
},
{
    "OptionName": "DBUser",
    "Namespace": "aws:rds:dbinstance",
    "Value": "random"
},
{
    "OptionName": "DBEngineVersion",
    "Namespace": "aws:rds:dbinstance",
    "Value": "9.3"
},
{
    "OptionName": "DBEngine",
    "Namespace": "aws:rds:dbinstance",
    "Value": "postgres"
}
]

有人知道为什么会失败吗?我使用 aws:rds:dbinstance 命名空间指定的任何内容似乎都已从配置中删除。

【问题讨论】:

    标签: amazon-web-services amazon-elastic-beanstalk amazon-rds aws-cli


    【解决方案1】:

    仅设置 aws:rds:dbinstance 选项不会创建 RDS 数据库。 目前,您可以使用以下技术之一创建 RDS 实例:

    1. 使用 AWS 控制台创建
    2. 使用eb cli
    3. 使用Resources section of ebextensions 创建 RDS 资源

    前两种方法最方便,因为它们为您完成了所有繁重的工作,但对于第三种方法,您必须做一些额外的工作。如果您不使用控制台或 eb CLI,则可以使用第三种方法。

    您可以使用以下 ebextension sn-p 为您的 beanstalk 环境创建 RDS 资源。在应用源的.ebextensions 目录中创建一个名为01-rds.config 的文件。

    Resources:
        AWSEBRDSDatabase:
            Type: AWS::RDS::DBInstance
            Properties:
                AllocatedStorage: 5
                DBInstanceClass: db.t2.micro
                DBName: myawesomeapp
                Engine: postgres
                EngineVersion: 9.3
                MasterUsername: myAwesomeUsername
                MasterUserPassword: myCrazyPassword
    

    此文件为 YAML 格式,因此缩进很重要。如果您愿意,也可以使用 JSON。 这些不是选项设置,因此您不能将其作为--option-settings test.json 传递。您只需将此文件与您的应用源捆绑在一起即可。

    详细了解您可以在 RDS 数据库here 上配置哪些属性。在此页面上,您还可以找到哪些属性是必需的,哪些属性是可选的。

    如果上述方法不适合您或您有任何其他问题,请告诉我。

    【讨论】:

    • 那么01-rds.configEB命令行工具是怎么做的?
    • 你也试过这个吗?当我尝试它时,我的环境会立即终止,我似乎无法从中获取任何日志。
    • 甜我想通了。我现在正在经历的欣喜若狂的喜悦真是太棒了。你们好棒。我对您的答案进行了小修改,更正了 01-rds.config 的参数名称,我发现 eb 命令行工具就是这样做的。
    • 我现在在获取环境变量时遇到问题。
    • 具体有哪些问题?您可以使用包含选项设置的配置文件在此处查看如何设置 CUSTOM_ENV。 stackoverflow.com/a/14491294/161628 因为它是一个选项设置,您可以在.ebextensions 中指定它或通过命令行选项--option-settings 传递它
    【解决方案2】:

    截至 2017 年 12 月,我们使用以下 ebextensions

    $ cat .ebextensions/rds.config
    Resources:
        AWSEBRDSDBSecurityGroup:
            Type: AWS::RDS::DBSecurityGroup
            Properties:
                EC2VpcId:
                    Fn::GetOptionSetting:
                        OptionName: "VpcId"
                GroupDescription: RDS DB VPC Security Group
                DBSecurityGroupIngress:
                    - EC2SecurityGroupId:
                        Ref: AWSEBSecurityGroup
    
        AWSEBRDSDBSubnetGroup:
            Type: AWS::RDS::DBSubnetGroup
            Properties:
                DBSubnetGroupDescription: RDS DB Subnet Group
                SubnetIds:
                    Fn::Split:
                        - ","
                        - Fn::GetOptionSetting:
                            OptionName: DBSubnets
    
        AWSEBRDSDatabase:
            Type: AWS::RDS::DBInstance
            DeletionPolicy: Delete
            Properties:
                PubliclyAccessible: true
                MultiAZ: false
                Engine: mysql
                EngineVersion: 5.7
                BackupRetentionPeriod: 0
                DBName: test
                MasterUsername: toor
                MasterUserPassword: 123456789
                AllocatedStorage: 10
                DBInstanceClass: db.t2.micro
                DBSecurityGroups:
                    - Ref: AWSEBRDSDBSecurityGroup
                DBSubnetGroupName:
                    Ref: AWSEBRDSDBSubnetGroup
    
    Outputs:
        RDSId:
            Description: "RDS instance identifier"
            Value:
                Ref: "AWSEBRDSDatabase"
    
        RDSEndpointAddress:
            Description: "RDS endpoint address"
            Value:
                Fn::GetAtt: ["AWSEBRDSDatabase", "Endpoint.Address"]
    
        RDSEndpointPort:
            Description: "RDS endpoint port"
            Value:
                Fn::GetAtt: ["AWSEBRDSDatabase", "Endpoint.Port"]
    
        AWSEBRDSDatabaseProperties:
            Description: Properties associated with the RDS database instance
            Value:
                Fn::Join:
                    - ","
                    - - Ref: AWSEBRDSDatabase
                      - Fn::GetAtt: ["AWSEBRDSDatabase", "Endpoint.Address"]
                      - Fn::GetAtt: ["AWSEBRDSDatabase", "Endpoint.Port"]
    

    有了这样的自定义选项

    $ cat .ebextensions/custom-options.config
    option_settings:
        "aws:elasticbeanstalk:customoption":
            DBSubnets: subnet-1234567,subnet-7654321
            VpcId: vpc-1234567
    

    唯一的事情 - 您必须将 RDS_* 环境变量显式传递给您的应用程序。

    【讨论】:

      【解决方案3】:

      截至 2015 年 9 月,其他答案在我的环境中不起作用。经过反复试验,以下方法对我有用:

      配置模板 sn-p (YAML):

        aws:rds:dbinstance:
          DBAllocatedStorage: '5'
          DBDeletionPolicy: Delete
          DBEngine: postgres
          DBEngineVersion: 9.3.9
          DBInstanceClass: db.t2.micro
          DBPassword: PASSWORD_HERE
          DBUser: USERNAME_HERE
          MultiAZDatabase: false
      

      .ebextensions/rds.config 文件(JSON):

      {
          "Parameters": {
          "AWSEBDBUser": {
              "NoEcho": "true",
              "Description": "The name of master user for the client DB Instance.",
              "Default": "ebroot",
              "Type": "String",
              "ConstraintDescription": "must begin with a letter and contain only alphanumeric characters"
          },
          "AWSEBDBPassword": {
              "NoEcho": "true",
              "Description": "The master password for the DB instance.",
              "Type": "String",
              "ConstraintDescription": "must contain only alphanumeric characters"
          },
          "AWSEBDBName": {
              "NoEcho": "true",
              "Description": "The DB Name of the RDS instance",
              "Default": "ebdb",
              "Type": "String",
              "ConstraintDescription": "must contain only alphanumeric characters"
          }
          },
          "Resources": {
          "AWSEBAutoScalingGroup": {
              "Metadata": {
              "AWS::ElasticBeanstalk::Ext": {
                  "_ParameterTriggers": {
                  "_TriggerConfigDeployment": {
                      "CmpFn::Insert": {
                      "values": [
                          {
                          "CmpFn::Ref": "Parameter.AWSEBDBUser"
                          },
                          {
                          "CmpFn::Ref": "Parameter.AWSEBDBPassword"
                          },
                          {
                          "CmpFn::Ref": "Parameter.AWSEBDBName"
                          }
                      ]
                      }
                  }
                  },
                  "_ContainerConfigFileContent": {
                  "plugins": {
                      "rds": {
                      "Description": "RDS Environment variables",
                      "env": {
                          "RDS_USERNAME": {
                          "Ref": {
                              "CmpFn::Ref": "Parameter.AWSEBDBUser"
                          }
                          },
                          "RDS_PASSWORD": {
                          "Ref": {
                              "CmpFn::Ref": "Parameter.AWSEBDBPassword"
                          }
                          },
                          "RDS_DB_NAME": {
                          "Ref": {
                              "CmpFn::Ref": "Parameter.AWSEBDBName"
                          }
                          },
                          "RDS_HOSTNAME": {
                          "Fn::GetAtt": [
                              "AWSEBRDSDatabase",
                              "Endpoint.Address"
                          ]
                          },
                          "RDS_PORT": {
                          "Fn::GetAtt": [
                              "AWSEBRDSDatabase",
                              "Endpoint.Port"
                          ]
                          }
                      }
                      }
                  }
                  }
              }
              }
          },
          "AWSEBRDSDatabase": {
              "Type": "AWS::RDS::DBInstance",
              "DeletionPolicy": "Delete",
              "Properties": {
              "DBName": {
                  "Ref": {
                  "CmpFn::Ref": "Parameter.AWSEBDBName"
                  }
              },
              "AllocatedStorage": "5",
              "DBInstanceClass": "db.t2.micro",
              "Engine": "postgres",
              "DBSecurityGroups": [
                  {
                  "Ref": "AWSEBRDSDBSecurityGroup"
                  }
              ],
              "MasterUsername": {
                  "Ref": {
                  "CmpFn::Ref": "Parameter.AWSEBDBUser"
                  }
              },
              "MasterUserPassword": {
                  "Ref": {
                  "CmpFn::Ref": "Parameter.AWSEBDBPassword"
                  }
              },
              "MultiAZ": false
              }
          },
          "AWSEBRDSDBSecurityGroup": {
              "Type": "AWS::RDS::DBSecurityGroup",
              "Properties": {
              "DBSecurityGroupIngress": {
                  "EC2SecurityGroupName": {
                  "Ref": "AWSEBSecurityGroup"
                  }
              },
              "GroupDescription": "Enable database access to Beanstalk application"
              }
          }
          }
      }
      

      【讨论】:

      • 更新,自 2016 年 1 月起,上述方法不再有效。
      【解决方案4】:

      我遇到了同样的问题,无法通过 .ebextensions 使其工作,而且我不喜欢 EB CLI 工具。

      EB CLI 使用未记录的 API 功能和自定义版本的 botocore 库 ('eb_botocore') 来完成这项工作。 :(

      于是我继续 fork botocore,并合并到 eb_botocore 使用的 API 数据文件中:https://github.com/boto/botocore/pull/396

      然后我在 my modified botocoreaws-cli(均在主服务器)上运行了“python setup.py install”,并且 aws-cli 现在接受“aws elasticbeanstalk create-environment”上的 --template-specification 选项命令。万岁!

      示例用法:

      aws elasticbeanstalk create-environment\
        ...various options...\
        --option-settings file://option-settings.json
        --template-specification file://rds.us-west-2.json
      

      rds.us-west-2.json 在哪里:

      {
        "TemplateSnippets": [{
          "SnippetName": "RdsExtensionEB",
          "Order": 10000,
          "SourceUrl":
      "https://s3.amazonaws.com/elasticbeanstalk-env-resources-us-west-2/eb_snippets/rds/rds.json"
          }]
      }
      

      (看来您必须选择特定于您的 EB 地区的 sn-p)。

      并且 option-settings.json 包含与问题中列出的类似的 RDS 相关设置(DBEngine、DBInstanceClass、DBAllocatedStorage、DBPassword)。

      效果很好。我希望 AWS CLI 团队将来允许我们在官方工具中使用此功能。我猜这不是一个微不足道的更改,或者他们已经完成了,但它是 Elastic Beanstalk API 和 AWS CLI 工具中一个相当大的遗漏功能,所以希望他们能够破解它。

      【讨论】:

      • 只是一个更新,截至 2015 年 9 月,此答案不是最新的,CLI 现在已经完全不同了。
      猜你喜欢
      • 2015-08-20
      • 2015-09-15
      • 2019-03-21
      • 2018-11-22
      • 2018-03-18
      • 2019-09-27
      • 2020-12-12
      • 1970-01-01
      • 2016-11-01
      相关资源
      最近更新 更多