You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/aws/services/api-gateway.md
+74-57Lines changed: 74 additions & 57 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,5 @@
1
1
---
2
2
title: "API Gateway"
3
-
linkTitle: "API Gateway"
4
3
description: Get started with API Gateway on LocalStack
5
4
tags: ["Free", "Base"]
6
5
persistence: supported
@@ -15,7 +14,7 @@ API Gateway supports standard HTTP methods such as `GET`, `POST`, `PUT`, `PATCH`
15
14
LocalStack supports API Gateway V1 (REST API) in the Free plan, and API Gateway V2 (HTTP, Management and WebSocket API) in the Base plan.
16
15
LocalStack allows you to use the API Gateway APIs to create, deploy, and manage APIs on your local machine to invoke those exposed API endpoints.
17
16
18
-
The supported APIs are available on the API coverage page for [API Gateway V1]({{< ref "coverage_apigateway" >}}) & [API Gateway V2]({{< ref "coverage_apigatewayv2" >}}), which provides information on the extent of API Gateway's integration with LocalStack.
17
+
The supported APIs are available on the API coverage page for [API Gateway V1]() & [API Gateway V2](), which provides information on the extent of API Gateway's integration with LocalStack.
19
18
20
19
## Getting started
21
20
@@ -50,16 +49,16 @@ The above code defines a function named `apiHandler` that returns a response wit
50
49
Zip the file and upload it to LocalStack using the `awslocal` CLI.
51
50
Run the following command:
52
51
53
-
{{< command >}}
54
-
$ zip function.zip lambda.js
55
-
$ awslocal lambda create-function \
52
+
```bash
53
+
zip function.zip lambda.js
54
+
awslocal lambda create-function \
56
55
--function-name apigw-lambda \
57
56
--runtime nodejs16.x \
58
57
--handler lambda.apiHandler \
59
58
--memory-size 128 \
60
59
--zip-file fileb://function.zip \
61
60
--role arn:aws:iam::111111111111:role/apigw
62
-
{{< /command >}}
61
+
```
63
62
64
63
This creates a new Lambda function named `apigw-lambda` with the code you specified.
65
64
@@ -68,9 +67,9 @@ This creates a new Lambda function named `apigw-lambda` with the code you specif
68
67
We will use the API Gateway's [`CreateRestApi`](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateRestApi.html) API to create a new REST API.
This creates a new REST API named `API Gateway Lambda integration`.
76
75
The above command returns the following response:
@@ -97,9 +96,9 @@ You'll need this ID for the next step.
97
96
98
97
Use the REST API ID generated in the previous step to fetch the resources for the API, using the [`GetResources`](https://docs.aws.amazon.com/apigateway/latest/api/API_GetResources.html) API:
@@ -172,16 +171,16 @@ The above command returns the following response:
172
171
173
172
Now, create a new integration for the method using the [`PutIntegration`](https://docs.aws.amazon.com/apigateway/latest/api/API_PutIntegration.html) API.
The above command integrates the `GET` method with the Lambda function created in the first step.
187
186
We can now proceed with the deployment before invoking the API.
@@ -190,37 +189,46 @@ We can now proceed with the deployment before invoking the API.
190
189
191
190
Create a new deployment for the API using the [`CreateDeployment`](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateDeployment.html) API:
192
191
193
-
{{< command >}}
194
-
$ awslocal apigateway create-deployment \
192
+
```bash
193
+
awslocal apigateway create-deployment \
195
194
--rest-api-id <REST_API_ID> \
196
195
--stage-name dev
197
-
{{< /command >}}
196
+
```
198
197
199
198
Your API is now ready to be invoked.
200
199
You can use [curl](https://curl.se/) or any HTTP REST client to invoke the API endpoint:
201
200
202
-
{{< command >}}
203
-
$ curl -X GET http://<REST_API_ID>.execute-api.localhost.localstack.cloud:4566/dev/test
201
+
```bash
202
+
curl -X GET http://<REST_API_ID>.execute-api.localhost.localstack.cloud:4566/dev/test
203
+
```
204
+
205
+
The response would be:
204
206
207
+
```json
205
208
{"message":"Hello World"}
206
-
{{< /command >}}
209
+
```
210
+
211
+
You can also use our [alternative URL format](#alternative-url-format) in case of DNS issues:
212
+
213
+
```bash
214
+
curl -X GET http://localhost:4566/_aws/execute-api/<REST_API_ID>/dev/test
215
+
```
207
216
208
-
You can also use our [alternative URL format]({{< ref "#alternative-url-format" >}}) in case of DNS issues:
209
-
{{< command >}}
210
-
$ curl -X GET http://localhost:4566/_aws/execute-api/<REST_API_ID>/dev/test
217
+
The response would be:
211
218
219
+
```json
212
220
{"message":"Hello World"}
213
-
{{< /command >}}
221
+
```
214
222
215
223
## New API Gateway implementation
216
224
217
-
{{< callout >}}
225
+
:::note
218
226
The new API Gateway implementation for both v1 (REST API) and v2 (HTTP API), introduced in [LocalStack 3.8.0](https://blog.localstack.cloud/localstack-release-v-3-8-0/#new-api-gateway-provider), is now the default in 4.0.
219
227
If you were using the `PROVIDER_OVERRIDE_APIGATEWAY=next_gen` flag, please remove it as it is no longer required.
220
228
221
229
The legacy provider (`PROVIDER_OVERRIDE_APIGATEWAY=legacy`) is temporarily available but deprecated and will be removed in the next major release.
222
230
We strongly recommend migrating to the new implementation.
223
-
{{< /callout >}}
231
+
:::
224
232
225
233
We're entirely reworked how REST and HTTP APIs are invoked, to closely match the behavior on AWS.
226
234
This new implementation has improved parity on several key areas:
Upon deployment of the Serverless project, LocalStack creates a new API Gateway V2 endpoint.
351
357
To retrieve the list of APIs and verify the WebSocket endpoint, you can use the `awslocal` CLI:
352
358
353
-
{{< command >}}
354
-
$ awslocal apigatewayv2 get-apis
359
+
```bash
360
+
awslocal apigatewayv2 get-apis
361
+
```
362
+
363
+
The response would be:
364
+
365
+
```json
355
366
{
356
367
"Items": [{
357
368
"ApiEndpoint": "ws://localhost:4510",
358
369
"ApiId": "129ca37e",
359
370
...
360
371
}]
361
372
}
362
-
{{< / command >}}
373
+
```
363
374
364
375
In the above example, the WebSocket endpoint is `ws://localhost:4510`.
365
376
Assuming your Serverless project contains a simple Lambda `handler.js` like this:
@@ -375,12 +386,12 @@ You can send a message to the WebSocket at `ws://localhost:4510` and the same me
375
386
To push data from a backend service to the WebSocket connection, you can use the [Amazon API Gateway Management API](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/apigatewaymanagementapi/index.html).
376
387
In LocalStack, use the following CLI command (replace `<connectionId>` with your WebSocket connection ID):
377
388
378
-
{{< command >}}
379
-
$ awslocal apigatewaymanagementapi \
389
+
```bash
390
+
awslocal apigatewaymanagementapi \
380
391
post-to-connection \
381
392
--connection-id '<connectionId>' \
382
393
--data '{"msg": "Hi"}'
383
-
{{< / command >}}
394
+
```
384
395
385
396
## Custom IDs for API Gateway resources via tags
386
397
@@ -390,18 +401,23 @@ This can be useful to ensure a static endpoint URL for your API, simplifying tes
390
401
To assign a custom ID to an API Gateway REST API, use the `create-rest-api` command with the `tags={"_custom_id_":"myid123"}` parameter.
391
402
The following example assigns the custom ID `"myid123"` to the API:
0 commit comments