> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/use-continuous-delivery/cd-building-blocks/services/create-services.md).

# Create services

Learn how to create CD services.

Services represent your microservices and other workloads. Each service contains a **Service Definition** that defines your deployment artifacts, manifests or specifications, configuration files, and service-specific variables.

## What you will learn from this topic

* How to [create a service](#create-a-service) within a pipeline, outside a pipeline, or at the account or organization level
* Understand [runtime inputs and expressions with services](#using-runtime-inputs-and-expressions-with-services) and [why service inputs are not configurable](#why-are-service-inputs-not-configurable)
* How to [select settings when the service is an expression](#selecting-settings-when-the-service-is-an-expression)
* How to [use trigger payload data](#use-trigger-payload-data-to-configure-the-service-at-runtime) or [pipeline variables with trigger payload data](#use-pipeline-variables-and-trigger-payload-data-to-configure-the-service-at-runtime) to configure the service at runtime

## Create a service <a href="#create-a-service" id="create-a-service"></a>

You can create services from:

* Within a pipeline
* Outside a pipeline
* An account
* An Organization

If you are new to Harness, review [Harness key concepts](https://developer.harness.io/docs/platform/get-started/overview) and [create your first CD pipeline](https://developer.harness.io/continuous-delivery/troubleshooting-and-resources/tutorials/kubernetes-container-deployments/manifest.md).

{% tabs %}
{% tab title="Within a pipeline" %}
To create a service from inside of a pipeline, select the **Services** tab of a new CD stage, then select **New Service**.

![New Service option on the Services tab of a CD stage](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-4399772f1cee1b4d4bba28d05a6360cb6f357c4e%2Fservices-and-environments-overview-10.png?alt=media)

Once the service and its service definition are saved, you can select it in any pipeline.

When you select the service in a pipeline, you can select **Edit Service** to edit its **Service Definition**.

When you create the new service you define its **Service Definition**. For example, a Kubernetes **Service Definition** with a Kubernetes manifest and Docker artifact.
{% endtab %}

{% tab title="Outside a pipeline" %}
To create a service from outside of a pipeline, you use **Services** in the navigation pane.

![Services page in the navigation pane with the New Service button](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-ed792ee1e3ad42a48f298006fbe3e16f6e7cedde%2Fservices-and-environments-overview-09.png?alt=media)
{% endtab %}

{% tab title="From organization or account" %}
You can create a service at an account or organization level from the Pipeline Studio, APIs, or Terraform.

{% tabs %}
{% tab title="Pipeline Studio" %}
To create a service at an account or organization level, go to **Organization Resources** -> **Services**.

Creating an account level service enables you to manage the service globally across the organizations and projects within the account.

An account level service can only reference connectors for the manifests and artifacts within the account. These services are global and cannot have dependencies at a lower hierarchy level.

Shared services can also be created and managed at account or organization levels.

![Create service option under Organization Resources at the account level](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-927dbc3d308b79f19530642797e7f4112cd61309%2Fcreate-services-from-account-level.png?alt=media)

{% hint style="info" %}
When using an account level deployment stage template, you can reference an account level service only. Similarly, for organization level stage templates, you can reference organization level services only.

However, when using a deployment stage in a pipeline that has service configured as a runtime input, you can pick services from project, organization, or account levels to pass them as runtime inputs based on your RBAC.

Go to [add a stage template](https://developer.harness.io/docs/platform/templates/add-a-stage-template) for more information.
{% endhint %}

Expand the section below to see a sample account level service YAML.

<details>

<summary>Account level service YAML</summary>

```yaml
service:
  name: nginx
  identifier: nginx
  tags: {}
  serviceDefinition:
    spec:
      manifests:
        - manifest:
            identifier: nginx-base
            type: K8sManifest
            spec:
              store:
                type: Github
                spec:
                  connectorRef: account.Harness_K8sManifest
                  gitFetchType: Branch
                  paths:
                    - cdng/
                  repoName: <+input>
                  branch: main
              skipResourceVersioning: false
      artifacts:
        primary:
          primaryArtifactRef: <+input>
          sources:
            - spec:
                connectorRef: account.Harness_DockerHub
                imagePath: library/nginx
                tag: <+input>
                digest: <+input>
              identifier: harness dockerhub
              type: DockerRegistry
    type: Kubernetes
```

</details>

Expand the section below to see a sample organization level service YAML.

<details>

<summary>Organization level service YAML</summary>

```yaml
service:
  name: redis
  identifier: redis
  tags: {}
  serviceDefinition:
    spec:
      manifests:
        - manifest:
            identifier: redis
            type: HelmChart
            spec:
              store:
                type: Http
                spec:
                  connectorRef: org.bitnami
              chartName: redis
              chartVersion: ""
              subChartName: ""
              helmVersion: V3
              skipResourceVersioning: false
              enableDeclarativeRollback: false
        - manifest:
            identifier: Redis Values
            type: Values
            spec:
              store:
                type: Github
                spec:
                  connectorRef: account.<GITHUB_CONNECTOR_ID>
                  gitFetchType: Branch
                  paths:
                    - redis/values.yaml
                  repoName: <REPOSITORY_NAME>
                  branch: main
      variables:
        - name: namespace
          type: String
          description: "namespace for the redis service"
          value: redis
    type: Kubernetes
  description: sample redis service
```

</details>
{% endtab %}

{% tab title="API" %}
For information about creating a service API, go to [create a service](https://apidocs.harness.io/tag/Services#operation/createServiceV2).

The `orgIdentifier` and `projectIdentifier` field definitions are optional, and depend on where you want to create the service. For example, if you create a service at an account level, you will not need org or project identifiers in the post API call payload.
{% endtab %}

{% tab title="Terraform" %}
For information about creating a Harness platform service, go to [harness\_platform\_service (Resource)](https://registry.terraform.io/providers/harness/harness/latest/docs/resources/platform_service).

The `org_id` and `project_id` field definitions are optional, and depend on where you want to create the service. For example, if you create a service at an account level, you will not need org or project identifiers.

Expand the section below to see a sample platform service in Terraform.

<details>

<summary>Harness platform service</summary>

```hcl
resource "harness_platform_service" "example" {
  identifier  = "identifier"
  name        = "name"
  description = "test"
  org_id      = "org_id"
  project_id  = "project_id"

  ## SERVICE V2 UPDATE
  ## This resource now accepts a YAML that can define the service definition for a given Service
  ## It is not mandatory for Service creation
  ## It is mandatory for Service use in a pipeline

  yaml = <<-EOT
                service:
                  name: name
                  identifier: identifier
                  serviceDefinition:
                    spec:
                      manifests:
                        - manifest:
                            identifier: manifest1
                            type: K8sManifest
                            spec:
                              store:
                                type: Github
                                spec:
                                  connectorRef: <+input>
                                  gitFetchType: Branch
                                  paths:
                                    - files1
                                  repoName: <+input>
                                  branch: master
                              skipResourceVersioning: false
                      configFiles:
                        - configFile:
                            identifier: configFile1
                            spec:
                              store:
                                type: Harness
                                spec:
                                  files:
                                    - <+org.description>
                      variables:
                        - name: var1
                          type: String
                          value: val1
                        - name: var2
                          type: String
                          value: val2
                    type: Kubernetes
                  gitOpsEnabled: false
              EOT
}
```

</details>
{% endtab %}
{% endtabs %}
{% endtab %}
{% endtabs %}

### Runtime inputs and expressions with services <a href="#using-runtime-inputs-and-expressions-with-services" id="using-runtime-inputs-and-expressions-with-services"></a>

Services are often configured using runtime inputs or expressions, so you can change service settings for different deployment scenarios at pipeline runtime.

{% hint style="info" %}
For more information on runtime inputs and expressions, go to [Fixed Values, Runtime Inputs, and Expressions](https://developer.harness.io/docs/platform/variables-and-expressions/runtime-inputs).
{% endhint %}

Often, the service option in a pipeline is also set as a runtime input, so you can select which service to use when you deploy the pipeline.

Also, the pipeline that deploys the service might be triggered in response to changes in a service's artifact or manifest.

Note the following when using services with or as runtime inputs:

* Service runtime inputs are not configurable in the pipeline that deploys the service.
* Service runtime inputs are not configurable in the trigger.

### Service input configuration limits <a href="#why-are-service-inputs-not-configurable" id="why-are-service-inputs-not-configurable"></a>

Many Harness settings and options are dependent on the specific values set in a service. When a value is set as runtime input, Harness cannot identify what configurations to render in the user interface.

### Select settings when the service is an expression <a href="#selecting-settings-when-the-service-is-an-expression" id="selecting-settings-when-the-service-is-an-expression"></a>

If you use an expression in a service setting, Harness must be able to resolve the expressions when you run the pipeline using the service.

There are a few options you can use to support expressions when a service is set as a runtime input in a pipeline, or when the service itself uses runtime inputs. These options are covered in the following sections.

### Use trigger payload data to configure the service at runtime <a href="#use-trigger-payload-data-to-configure-the-service-at-runtime" id="use-trigger-payload-data-to-configure-the-service-at-runtime"></a>

You can pass in this data when the pipeline execution is triggered using a custom cURL trigger.

For more information, go to [Passing data in Custom triggers](https://developer.harness.io/docs/platform/triggers/custom-trigger-passing-data).

The following is the YAML for a service in a pipeline that uses trigger payload data for the service reference, primary artifact, and tag:

```yaml
spec:
  service:
    serviceRef: <+trigger.payload.serviceId>
    serviceInputs:
      serviceDefinition:
        type: TAS
        spec:
          artifacts:
            primary:
              primaryArtifactRef: <+trigger.payload.artifactId>
              sources:
                - identifier: <+trigger.payload.artifactId>
                  type: Nexus2Registry
                  spec:
                    tag: <+trigger.payload.tag>
```

To supply the data, provide the payload key-value pairs in the cURL command you use to execute the pipeline:

```bash
curl -X POST -H 'Content-Type: application/json' --url 'webhook_url' -d '{"serviceId": "Kubernetes", "artifactId": "nginx", "tag": "abc"}'
```

### Use pipeline variables and trigger payload data to configure the service at runtime <a href="#use-pipeline-variables-and-trigger-payload-data-to-configure-the-service-at-runtime" id="use-pipeline-variables-and-trigger-payload-data-to-configure-the-service-at-runtime"></a>

You can create pipeline variables and map those to service settings. When you run the pipeline, you can provide values for the variables to define the service.

For example, here are pipeline variables for a service's artifact and tags.

![Pipeline variables for a service artifact and tag](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-1fdbab83dccc6e347e8138f55fbacd1f18c54aa0%2F62e0417c8075a66b582c4f77873cc04c228a985c2f4bc7ca2994e2768ade208b.png?alt=media)

Next, you map the pipeline variables to the services' artifact settings using expressions:

![Pipeline variables mapped to the service artifact settings using expressions](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-ae9d0c608ddd0c0d9f055b58d7a42e22097bb732%2F039bb495a010a86dd319421b1d1e4c50296c818933b3173376e83c9378ad0b9e.png?alt=media)

You can also map a pipeline variable for the service name as an expression:

![Pipeline variable mapped to the service name as an expression](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-b8252de3c5dee7669fc2407fc433e8af6be81174%2Fbf5d06238223ce766372f48a1ec1cc356413c542064d2806ebe9b9d48716e161.png?alt=media)

When you run the pipeline, you are prompted to provide values for the pipeline variables.

![Run pipeline form prompting for pipeline variable values](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-63bd71e6222edc1116aeb5ce0ae6d83e1b2b1c02%2Fd29655cbf1b0a6f129c0b1c02ad492fb2499b7fe0a14a7463811fae43209ff44.png?alt=media)

You can also map these same pipeline variables to trigger payload data expressions.

![Pipeline variables mapped to trigger payload data expressions](https://3694223630-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fy1JhZ4oKIppwY7d5AhPj%2Fuploads%2Fgit-blob-deaabeb0140a9410eccac0d331a00a936c49148c%2F7e52eb23cfcb0461224b9ed278b6ad699945ec80e4b15c768010b2835f072b0d.png?alt=media)

When you initiate the trigger using the cURL command, the key-value pairs in the command are used for the pipeline variables. Finally, those pipeline variable values are used to define the service.

## Next steps <a href="#next-steps" id="next-steps"></a>

* [Add inline service files using file store](https://developer.harness.io/continuous-delivery/use-continuous-delivery/cd-building-blocks/services/add-inline-manifests-using-file-store.md)
* [Add a custom artifact source for CD](https://developer.harness.io/continuous-delivery/use-continuous-delivery/cd-building-blocks/services/add-a-custom-artifact-source-for-cd.md)
* [Propagate CD services](https://developer.harness.io/continuous-delivery/use-continuous-delivery/cd-building-blocks/services/propagate-and-override-cd-services.md)

{% @harness-feedback/feedback %}
