AWS CLI使用elasticbeanstalk创建环境创建RDS

时间:2014-09-20 08:15:33

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

如何使用create-environment或其他子命令aws 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命名空间指定的任何内容似乎都会从配置中删除。

4 个答案:

答案 0 :(得分:7)

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

  1. 使用AWS控制台创建
  2. 使用eb cli
  3. 使用Resources section of ebextensions创建RDS资源
  4. 前两种方法最方便,因为它们为您完成了所有繁重的工作,但对于第三种方法,您必须做一些额外的工作。如果您不使用控制台或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数据库here上配置哪些属性。在此页面上,您还可以找到所需的属性以及可选的属性。

    如果上述内容对您不起作用,或者您有任何其他问题,请与我们联系。

答案 1 :(得分:1)

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

EB CLI使用未记录的API功能和botocore库('eb_botocore')的自定义版本来实现此功能。 :(

所以我继续分叉botocore,并合并到eb_botocore使用的API数据文件中:https://github.com/boto/botocore/pull/396

然后我在my modified botocoreaws-cli上都运行'python setup.py install'(两者都在master),而aws-cli现在在'aws elasticbeanstalk上接受--template-specification选项create-environment'命令。万岁!

使用示例:

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工具来看,这是一个非常重要的遗漏功能,所以希望他们能够解决这个问题。

答案 2 :(得分:1)

截至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"
        }
    }
    }
}

答案 3 :(得分:1)

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