10

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

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似乎都会从配置中删除。

4

4 回答 4

8

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

  1. 使用 AWS 控制台创建
  2. 使用eb cli
  3. 使用ebextensions 的 Resources 部分创建 RDS 资源

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

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

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 数据库上配置哪些属性的更多信息。在此页面上,您还可以找到哪些属性是必需的,哪些属性是可选的。

让我知道以上是否对您不起作用或您有任何其他问题。

于 2014-09-21T20:58:13.273 回答
3

截至 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_* 环境变量传递给你的应用程序。

于 2017-12-12T22:51:59.327 回答
2

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

配置模板片段(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"
        }
    }
    }
}
于 2015-09-11T22:12:32.947 回答
1

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

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

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

然后,我在修改后的 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 地区的代码段)。

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

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

于 2014-12-04T01:11:44.387 回答