# Logstash connecting to both QA and PROD?

**URL:** <https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987>\
**Category:** Logstash\
**Created:** [March 26, 2019, 5:59pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987 "2019-03-26T17:59:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![titan1978](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@titan1978](https://discuss.elastic.co/u/titan1978)\
**Post date:** [March 26, 2019, 5:59pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/1 "2019-03-26T17:59:55Z")

</div>

When Logstash bootstraps in my container, the logstash output is indicating it is registering BOTH my qa and prod endpoints. I have a series of if-else checks to connect to one or the other. But it doesn't look like logstash is honoring this and is registering both qa and prod endpoints. Can anyone suggest what is going on? This is the essence of my logastash config (Running in Docker) script:

```
output {
    if [Environment == "qa"] {
        elasticsearch {
           hosts => ["https://my-qa-endpoint:443/"]
        }
     } else if [Environment == "prod"] {
        elasticsearch {
           hosts => ["https://my-prod-endpoint:443/"]
        }   
      }
}

```

Docker Inspect shows the environment correctly being set:

```
        "Env": [
            "Environment=qa",
             "LANG=en_US.UTF-8",
              ...
         ]

```

Logs:

> [2019-03-26T17:41:29,638][INFO][logstash.outputs.elasticsearch] Running health check to see if an Elasticsearch connection is working {:healthcheck\_url=\>[https://elastic:xxxxxx@my-qa-endpoint:443/](https://elastic:xxxxxx@my-qa-endpoint:443/), :path=\>"/"}  
> [2019-03-26T17:41:29,913][WARN][logstash.outputs.elasticsearch] Restored connection to ES instance {:url=\>"[https://elastic:xxxxxx@my-qa-endpoint:443/](https://elastic:xxxxxx@my-qa-endpoint:443/)"}  
> [2019-03-26T17:41:29,966][INFO][logstash.outputs.elasticsearch] ES Output version determined {:es\_version=\>6}  
> [2019-03-26T17:41:30,061][INFO][logstash.outputs.elasticsearch] Elasticsearch pool URLs updated {:changes=\>{:removed=\>, :added=\>[[https://elastic:xxxxxx@my-prod-endpoint:443/](https://elastic:xxxxxx@my-prod-endpoint:443/)]}}  
> [2019-03-26T17:41:30,062][INFO][logstash.outputs.elasticsearch] Running health check to see if an Elasticsearch connection is working {:healthcheck\_url=\>[https://elastic:xxxxxx@my-prod-endpoint:443/](https://elastic:xxxxxx@my-prod-endpoint:443/), :path=\>"/"} [2019-03-26T17:41:50,109][WARN][logstash.outputs.elasticsearch] Attempted to resurrect connection to dead ES instance, but got an error. {:url=\>"[https://elastic:xxxxxx@my-prod-endpoint:443/](https://elastic:xxxxxx@my-prod-endpoint:443/)", :error\_type=\>LogStash::Outputs::Elasticsearch::HttpClient::Pool::HostUnreachableError, :error=\>"Elasticsearch Unreachable: [[https://elastic:xxxxxx@my-prod-endpoint](https://elastic:xxxxxx@my-prod-endpoint)::443/][Manticore::ConnectTimeout] connect timed out"}

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 26, 2019, 6:11pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/2 "2019-03-26T18:11:50Z")

</div>

> [@titan1978](#):
>
> if [Environment == "qa"]

Not sure what you are trying to do there.

No matter. The elasticsearch output establishes a connection to ES on startup. It does not wait for an event that needs to be routed to ES.

---

<div class="post-metadata">

**Author:** ![titan1978](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@titan1978](https://discuss.elastic.co/u/titan1978)\
**Post date:** [March 26, 2019, 6:13pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/3 "2019-03-26T18:13:39Z")

</div>

> [@Badger](#):
>
> Not sure what you are trying to do there

We have two different endpoints - one to QA and one to PROD. Since we're using a docker container and the container is immutable - the only variables we have to play with is the "Environment" using which we either send traffic to QA elastic endpoint when the container is spun up in our QA environment or the PROD elastic endpoint when the container is spun up in our PROD environment.

> [@Badger](#):
>
> No matter. The elasticsearch output establishes a connection to ES on startup. It does not wait for an event that needs to be routed to ES.

Does this mean the the if-else conditions are pointless? How does logstash know to connect to QA or PROD endpoints at startup? is it just scanning the logstash.conf from top to bottom and plucking both elasticsearch endpoints and registering them?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 26, 2019, 6:28pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/4 "2019-03-26T18:28:40Z")

</div>

> [@titan1978](#):
>
> is it just scanning the logstash.conf from top to bottom and plucking both elasticsearch endpoints and registering them?

In essence, yes.

Because it connects at startup, you cannot use a sprintf reference to a field on an event. In other words, this does not work...

```
mutate { add_field => { "[@metadata][environment]" => "${environment}" } }
if [@metadata][environment] == "QA" {
    mutate { add_field => { "[@metadata][instance]" => "https://qa-es.example.com:9200" } }
    else if ...
}
output { elasticsearch { hosts => "%{[@metadata][instance]}" } }

```

It's going to try to connect to both.

---

<div class="post-metadata">

**Author:** ![titan1978](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@titan1978](https://discuss.elastic.co/u/titan1978)\
**Post date:** [March 26, 2019, 6:38pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/5 "2019-03-26T18:38:11Z")

</div>

Ouch. This puts me in a bind. Here's another question then - lets say it DOES connect to both on startup (thats correct I hope - it will try to connect to both?) and the PROD connection fails in the QA environment due to security group rules (and vice versa ) - will the event traffic go to the connection that is successful in each environment?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 26, 2019, 6:49pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/6 "2019-03-26T18:49:00Z")

</div>

No, you will have to route events conditionally. Otherwise (as I understand it) in the QA environment the failed connection to production will result in events backing up. This will eventually fill the in-memory queue and the input will stop reading new events. So you will need something like

```
filter {
    mutate { add_field => { "[@metadata][environment]" => "${environment}" } }
}
output {
     if [@metadata][environment] == QA {
         elasticsearch {
             hosts => ["https://my-qa-endpoint:443/"]
         }
 } else {
        elasticsearch {
             hosts => ["https://my-prod-endpoint:443/"]
        }   
  }

```

You cannot reference ${environment} in a conditional in the filter section, that is why I do the add\_field.

---

<div class="post-metadata">

**Author:** ![titan1978](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@titan1978](https://discuss.elastic.co/u/titan1978)\
**Post date:** [March 26, 2019, 6:53pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/7 "2019-03-26T18:53:20Z")

</div>

> [@Badger](#):
>
> add\_field =\> { "[@metadata][environment]" =\> "${environment}" }

Interesting. Just to sum this up - the events will route conditionally BUT at startup Logstash will try to connect to both QA and PROD in QA environment, just that during Runtime, no events will be sent to PROD?

Also a nuance - are these equivalent?  
`if [@metadata][environment] == QA {`  
and  
`if [@metadata][environment] == "QA" {`

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 26, 2019, 8:18pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/8 "2019-03-26T20:18:41Z")

</div>

Your summary is correct.

Whoops. I would expect a syntax error at startup for

```
if [@metadata][environment] == QA {

```

The double quotes are required.

---

<div class="post-metadata">

**Author:** ![titan1978](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@titan1978](https://discuss.elastic.co/u/titan1978)\
**Post date:** [March 26, 2019, 8:23pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/9 "2019-03-26T20:23:40Z")

</div>

So I just created a new image with the suggested. IT looks like when elastic tries to post an event, its only trying to post to the PROD (nothing is going to QA even though in the beginning logstash is indicating its connecting to QA successfully but PROD connections keep failing). In the logs I clearly see an Error posting to the PROD endpoint and QA doesn't show this event as well.

I'm wondering - if I CONSTRUCT the endpoints as part of the mutate and use those in the elasticsearch section of the conf - would that solve this? (Thinking of ways to fool bootstrapping to think there's only one endpoint)

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 26, 2019, 10:01pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/10 "2019-03-26T22:01:11Z")

</div>

That suggests to me that the wrong value is in [@metadata][environment].

You cannot construct the endpoint using a mutate, because the connections to endpoints are done at startup, before any events have passed through the pipeline to be mutated. That was what I was trying to say earlier when I mentioned sprintf references.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 23, 2019, 10:01pm UTC](https://discuss.elastic.co/t/logstash-connecting-to-both-qa-and-prod/173987/11 "2019-04-23T22:01:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
