Menu
AWS OpsWorks
User Guide (API Version 2013-02-18)

Connecting an Application to a Database Server

You can associate an Amazon RDS database server with an app when you create the app or later by editing the app. Your application can then use the database connection information—user name, password, ...—to connect to the database server. When you deploy an app, AWS OpsWorks Stacks provides this information to applications in two ways:

  • For Linux stacks, AWS OpsWorks Stacks creates a file on each of the built-in application server instances containing the connection data that the application can use to connect to the database server.

  • AWS OpsWorks Stacks includes the connection information in the stack configuration and deployment attributes that are installed on each instance.

    You can implement a custom recipe to extract the connection information from these attributes and put it in a file in your preferred format. For more information, see Passing Data to Applications.

Important

For Linux stacks, if you want to associate an Amazon RDS service layer with your app, you must add the appropriate driver package to the associated app server layer, as follows:

  1. Click Layers in the navigation pane and open the app server's Recipes tab.

  2. Click Edit and add the appropriate driver package to OS Packages. For example, you should specify mysql if the layer contains Amazon Linux instances and mysql-client if the layer contains Ubuntu instances.

  3. Save the changes and redeploy the app.

Using a Custom Recipe

You can implement a custom recipe that extracts the connection data from the app's deploy attributes and saves it in a form that the application can read, such as a YAML file.

You attach a database server to an app when you create the app or later by editing the app. When you deploy the app, AWS OpsWorks Stacks installs a stack configuration and deployment attributes on each instance that include the database connection information. Your app can then retrieve the appropriate attributes. The details depend on whether you are using a Linux or Windows stack.

Connecting to a Database Server for a Linux Stack

For Linux stacks, the stack configuration and deployment attributes' deploy namespace includes an attribute for each deployed app, named with the app's short name. When you attach a database server to an app, AWS OpsWorks Stacks populates the app's [:database] attribute with the connection information, and installs it on the stack's instances for each subsequent deployment. The attribute values are either user-provided or generated by AWS OpsWorks Stacks.

Note

AWS OpsWorks Stacks allows you to attach a database server to multiple apps, but each app can have only one attached database server. If you want to connect an application to more than one database server, attach one of the servers to the app, and use the information in the app's deploy attributes to connect to that server. Use custom JSON to pass the connection information for the other database servers to the application. For more information, see Passing Data to Applications.

An application can use the connection information from the instance's deploy attributes to connect to a database. However, applications cannot access that information directly—only recipes can access the deploy attributes. You can address this issue by implementing a custom recipe that extracts the connection information from the deploy attributes and puts it in a file that can be read by the application.

Connecting to a Database Server for a Windows Stack

With Windows stacks, you can use the aws_opsworks_rds_db_instance search index to retrieve database connection information. The following example recipe shows how to retrieve the RDS instance's ID, which is assigned to the [:db_instance_identifier] attribute.

Copy
dbserver = search(:aws_opsworks_rds_db_instance, "*:*").first Chef::Log.info("**********The DB instance ID is: '#{dbserver[:db_instance_identifier]}'**********")

The search code returns a list of RDS instances that are registered with the stack. This example simply retrieves the first RDS instance's instance ID and puts it in the Chef log. You can easily modify this code to write the connection information to a file in an appropriate format.

For a complete list of database connection attributes, see Amazon RDS Data Bag (aws_opsworks_rds_db_instance).

On this page: