# Defining Cached Services

### Create a Terraform Configuration

The first step to creating a cached service is to create an infrastructure as code representation of the service. Make sure you define terraform outputs for any values that will need to be used in templates in the future -- otherwise they will not be accessible! For example, if you would like to define a MySQL AWS RDS cached service, you can use the following terraform snippet:

```
resource "aws_db_instance" "default" {
  allocated_storage    = 20
  engine               = "postgresql"
  engine_version       = "12.2"
  instance_class       = "db.t3.micro"
  name                 = var.database_name
  username             = var.database_user
  password             = var.database_password
  skip_final_snapshot  = true
}

output "db_name" {
  value = aws_db_instance.default.name
}
```

####

#### Snapshots

If your DBMS supports snapshotting, you can specify snapshots in your IaC templates to get pre-seeded databases.

### Add the Configuration to Gallery

Now, you can go to the cached services tab, name your cached service, and select the cloud providers you need to use in your terraform file. You'll use the cached service name as an identifier to access cached service resources in your template IaC scripts.

![](/files/8fj6OjG1tsr0oNsl1c8Q)

&#x20;Now link your terraform definition with Gallery. You can either use a terraform snippet or a repository.

![](/files/lMBpFwXAJ6H4Yzn3fXHv)

Once you create your cached service, instances of your services will start spinning up (up to the minimum replicas you initially indicated).&#x20;

#### Scaling

You can adjust the number of active cached service instances from the cached service page. Just hit edit, adjust the minimum number of instances and hit save to scale up or down the number of idle cached services that are running at any time.

![](/files/V93cm9fU8GihdvbOXtr3)

### Use in Templates

You can now use your cached services in templates. Take a look at [Using Cached Services](/build-system/terraform/using-cached-services.md) for more information.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.usegallery.com/cached-services/defining-cached-services.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
