Skip to content

Commit 725521a

Browse files
authored
Merge pull request #1229 from itowlson/service-chaining
Service chaining
2 parents 867d95c + f299973 commit 725521a

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

content/spin/v2/http-outbound.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ url = "https://github.com/fermyon/developer/blob/main/content/spin/v2/http-outbo
88
---
99
- [Using HTTP From Applications](#using-http-from-applications)
1010
- [Granting HTTP Permissions to Components](#granting-http-permissions-to-components)
11-
- [Making HTTP Requests Within an Application](#making-http-requests-within-an-application)
11+
- [Making HTTP Requests Within an Application](#making-http-requests-within-an-application)
12+
- [Local Service Chaining](#local-service-chaining)
13+
- [Intra-Application HTTP Requests by Route](#intra-application-http-requests-by-route)
1214

1315
Spin provides an interface for you to make outgoing HTTP requests.
1416

@@ -177,13 +179,40 @@ allowed_outbound_hosts = [ "https://*.example.com" ]
177179

178180
For development-time convenience, you can also pass the string `"https://*:*"` in the `allowed_outbound_hosts` collection. This allows the Wasm module to make HTTP requests to _any_ host and on any port. However, once you've determined which hosts your code needs, you should remove this string and list the hosts instead. Other Spin implementations may restrict host access and disallow components that ask to connect to anything and everything!
179181

180-
### Making HTTP Requests Within an Application
182+
## Making HTTP Requests Within an Application
181183

182-
In an HTTP component, you can make requests to a path to make HTTP requests **within the current Spin application**. For example, if you make an outbound HTTP request to /api/customers/, Spin replaces self with whatever host the application is running on. It also replaces the URL scheme (`http` or `https`) with the scheme of the current HTTP request. For example, if the application is running in the cloud, Spin changes `/api` to `https://.../api`.
184+
If your Spin application functions as a set of microservices, you'll often want to make requests directly from one component to another within the same application. It's best not to use a full URL for this, because that's not portable across different deployment environments - the URL in the cloud is different from the one in your development environment. Instead, Spin provides two ways to make inter-component requests:
183185

184-
Using paths means that the application doesn't need to know the URL where it's deployed, or whether it's running locally versus in the cloud.
186+
* By component ID ("local service chaining")
187+
* By route
185188

186-
> This doesn't work in Redis components because Spin uses the incoming HTTP request to determine the current host.
189+
Both of these work only from HTTP components. That is, if you want to make an intra-application request from, say, a Redis trigger, you must use a full URL.
190+
191+
### Local Service Chaining
192+
193+
To make an HTTP request to another component in your application, use the special `<component-id>.spin.internal` host name. For example, an outbound HTTP request to `authz.spin.internal` will be handled by the `authz` component.
194+
195+
In this way of doing self-requests, the request is passed in memory without ever leaving the Spin host process. This is extremely fast, as the two components are wired almost directly together, but may reduce deployment flexibility depending on the nature of the microservices. Also, components that are the target of service chaining requests may see URLs in both routed and chained forms: therefore, if they parse the URL (for example, extracting a resource identifier from the path), they must ensure both forms are correctly handled.
196+
197+
You must still grant permission by including the relevant `spin.internal` hosts in `allowed_outbound_hosts`:
198+
199+
```toml
200+
allowed_outbound_hosts = ["http://authz.spin.internal", "https://reporting.spin.internal"]
201+
```
202+
203+
To allow local chaining to _any_ component in your application, use a subdomain wildcard:
204+
205+
```toml
206+
allowed_outbound_hosts = ["*://*.spin.internal"]
207+
```
208+
209+
> Local service chaining is not currently supported on Fermyon Cloud.
210+
211+
### Intra-Application HTTP Requests by Route
212+
213+
To make an HTTP request to another route with your application, you can pass just the route as the URL. For example, if you make an outbound HTTP request to `/api/customers/`, Spin prepends the route with whatever host the application is running on. It also replaces the URL scheme (`http` or `https`) with the scheme of the current HTTP request. For example, if the application is running in the cloud, Spin changes `/api` to `https://.../api`.
214+
215+
In this way of doing self-requests, the request undergoes normal HTTP processing once Spin has prepended the host. For example, in a cloud deployment, the request passes through the network, and potentially back in through a load balancer or other gateway. The benefit of this is that it allows load to be distributed across the environment, but it may count against your use of bandwidth.
187216

188217
You must still grant permission by including `self` in `allowed_outbound_hosts`:
189218

0 commit comments

Comments
 (0)