Skip to content

Commit 654b0ac

Browse files
committed
get api gateway done
1 parent fe6da99 commit 654b0ac

File tree

1 file changed

+74
-57
lines changed

1 file changed

+74
-57
lines changed

src/content/docs/aws/services/api-gateway.md

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "API Gateway"
3-
linkTitle: "API Gateway"
43
description: Get started with API Gateway on LocalStack
54
tags: ["Free", "Base"]
65
persistence: supported
@@ -15,7 +14,7 @@ API Gateway supports standard HTTP methods such as `GET`, `POST`, `PUT`, `PATCH`
1514
LocalStack supports API Gateway V1 (REST API) in the Free plan, and API Gateway V2 (HTTP, Management and WebSocket API) in the Base plan.
1615
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.
1716

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.
1918

2019
## Getting started
2120

@@ -50,16 +49,16 @@ The above code defines a function named `apiHandler` that returns a response wit
5049
Zip the file and upload it to LocalStack using the `awslocal` CLI.
5150
Run the following command:
5251

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 \
5655
--function-name apigw-lambda \
5756
--runtime nodejs16.x \
5857
--handler lambda.apiHandler \
5958
--memory-size 128 \
6059
--zip-file fileb://function.zip \
6160
--role arn:aws:iam::111111111111:role/apigw
62-
{{< /command >}}
61+
```
6362

6463
This creates a new Lambda function named `apigw-lambda` with the code you specified.
6564

@@ -68,9 +67,9 @@ This creates a new Lambda function named `apigw-lambda` with the code you specif
6867
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.
6968
Here's an example command:
7069

71-
{{< command >}}
72-
$ awslocal apigateway create-rest-api --name 'API Gateway Lambda integration'
73-
{{< /command >}}
70+
```bash
71+
awslocal apigateway create-rest-api --name 'API Gateway Lambda integration'
72+
```
7473

7574
This creates a new REST API named `API Gateway Lambda integration`.
7675
The above command returns the following response:
@@ -97,9 +96,9 @@ You'll need this ID for the next step.
9796

9897
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:
9998

100-
{{< command >}}
101-
$ awslocal apigateway get-resources --rest-api-id <REST_API_ID>
102-
{{< /command >}}
99+
```bash
100+
awslocal apigateway get-resources --rest-api-id <REST_API_ID>
101+
```
103102

104103
The above command returns the following response:
105104

@@ -122,12 +121,12 @@ You'll need this ID for the next step.
122121
Create a new resource for the API using the [`CreateResource`](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateResource.html) API.
123122
Use the ID of the resource returned in the previous step as the parent ID:
124123

125-
{{< command >}}
126-
$ awslocal apigateway create-resource \
124+
```bash
125+
awslocal apigateway create-resource \
127126
--rest-api-id <REST_API_ID> \
128127
--parent-id <PARENT_ID> \
129128
--path-part "{somethingId}"
130-
{{< /command >}}
129+
```
131130

132131
The above command returns the following response:
133132

@@ -148,14 +147,14 @@ You'll need this Resource ID for the next step.
148147
Add a `GET` method to the resource using the [`PutMethod`](https://docs.aws.amazon.com/apigateway/latest/api/API_PutMethod.html) API.
149148
Use the ID of the resource returned in the previous step as the Resource ID:
150149

151-
{{< command >}}
150+
```bash
152151
awslocal apigateway put-method \
153152
--rest-api-id <REST_API_ID> \
154153
--resource-id <RESOURCE_ID> \
155154
--http-method GET \
156155
--request-parameters "method.request.path.somethingId=true" \
157156
--authorization-type "NONE"
158-
{{< /command >}}
157+
```
159158

160159
The above command returns the following response:
161160

@@ -172,16 +171,16 @@ The above command returns the following response:
172171

173172
Now, create a new integration for the method using the [`PutIntegration`](https://docs.aws.amazon.com/apigateway/latest/api/API_PutIntegration.html) API.
174173

175-
{{< command >}}
176-
$ awslocal apigateway put-integration \
174+
```bash
175+
awslocal apigateway put-integration \
177176
--rest-api-id <REST_API_ID> \
178177
--resource-id <RESOURCE_ID> \
179178
--http-method GET \
180179
--type AWS_PROXY \
181180
--integration-http-method POST \
182181
--uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:apigw-lambda/invocations \
183182
--passthrough-behavior WHEN_NO_MATCH
184-
{{< /command >}}
183+
```
185184

186185
The above command integrates the `GET` method with the Lambda function created in the first step.
187186
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.
190189

191190
Create a new deployment for the API using the [`CreateDeployment`](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateDeployment.html) API:
192191

193-
{{< command >}}
194-
$ awslocal apigateway create-deployment \
192+
```bash
193+
awslocal apigateway create-deployment \
195194
--rest-api-id <REST_API_ID> \
196195
--stage-name dev
197-
{{< /command >}}
196+
```
198197

199198
Your API is now ready to be invoked.
200199
You can use [curl](https://curl.se/) or any HTTP REST client to invoke the API endpoint:
201200

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:
204206

207+
```json
205208
{"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+
```
207216

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:
211218

219+
```json
212220
{"message":"Hello World"}
213-
{{< /command >}}
221+
```
214222

215223
## New API Gateway implementation
216224

217-
{{< callout >}}
225+
:::note
218226
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.
219227
If you were using the `PROVIDER_OVERRIDE_APIGATEWAY=next_gen` flag, please remove it as it is no longer required.
220228

221229
The legacy provider (`PROVIDER_OVERRIDE_APIGATEWAY=legacy`) is temporarily available but deprecated and will be removed in the next major release.
222230
We strongly recommend migrating to the new implementation.
223-
{{< /callout >}}
231+
:::
224232

225233
We're entirely reworked how REST and HTTP APIs are invoked, to closely match the behavior on AWS.
226234
This new implementation has improved parity on several key areas:
@@ -320,15 +328,13 @@ http://localhost:4566/_aws/execute-api/0v1p6q6/local/my/path1
320328

321329
This format is sometimes used in case of local DNS issues.
322330

323-
{{< callout >}}
324-
331+
:::note
325332
If you are using LocalStack 4.0, the following `_user_request_` format is deprecated, and you should use the format above.
326333

327334
```shell
328335
http://localhost:4566/restapis/<apiId>/<stageName>/_user_request_/<path>
329336
```
330-
331-
{{< / callout >}}
337+
:::
332338

333339
### WebSocket APIs (Pro)
334340

@@ -350,16 +356,21 @@ functions:
350356
Upon deployment of the Serverless project, LocalStack creates a new API Gateway V2 endpoint.
351357
To retrieve the list of APIs and verify the WebSocket endpoint, you can use the `awslocal` CLI:
352358

353-
{{< command >}}
354-
$ awslocal apigatewayv2 get-apis
359+
```bash
360+
awslocal apigatewayv2 get-apis
361+
```
362+
363+
The response would be:
364+
365+
```json
355366
{
356367
"Items": [{
357368
"ApiEndpoint": "ws://localhost:4510",
358369
"ApiId": "129ca37e",
359370
...
360371
}]
361372
}
362-
{{< / command >}}
373+
```
363374

364375
In the above example, the WebSocket endpoint is `ws://localhost:4510`.
365376
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
375386
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).
376387
In LocalStack, use the following CLI command (replace `<connectionId>` with your WebSocket connection ID):
377388

378-
{{< command >}}
379-
$ awslocal apigatewaymanagementapi \
389+
```bash
390+
awslocal apigatewaymanagementapi \
380391
post-to-connection \
381392
--connection-id '<connectionId>' \
382393
--data '{"msg": "Hi"}'
383-
{{< / command >}}
394+
```
384395

385396
## Custom IDs for API Gateway resources via tags
386397

@@ -390,18 +401,23 @@ This can be useful to ensure a static endpoint URL for your API, simplifying tes
390401
To assign a custom ID to an API Gateway REST API, use the `create-rest-api` command with the `tags={"_custom_id_":"myid123"}` parameter.
391402
The following example assigns the custom ID `"myid123"` to the API:
392403

393-
{{< command >}}
394-
$ awslocal apigateway create-rest-api --name my-api --tags '{"_custom_id_":"myid123"}'
404+
```bash
405+
awslocal apigateway create-rest-api --name my-api --tags '{"_custom_id_":"myid123"}'
406+
```
407+
408+
The response would be:
409+
410+
```json
395411
{
396412
"id": "myid123",
397413
....
398414
}
399-
{{< / command >}}
415+
```
400416

401417
You can also configure the protocol type, the possible values being `HTTP` and `WEBSOCKET`:
402418

403-
{{< command >}}
404-
$ awslocal apigatewayv2 create-api \
419+
```bash
420+
awslocal apigatewayv2 create-api \
405421
--name=my-api \
406422
--protocol-type=HTTP --tags="_custom_id_=my-api"
407423
{
@@ -413,12 +429,12 @@ $ awslocal apigatewayv2 create-api \
413429
"_custom_id_": "my-api"
414430
}
415431
}
416-
{{< / command >}}
432+
```
417433

418-
{{< callout >}}
434+
:::note
419435
Setting the API Gateway ID via `_custom_id_` works only on the creation of the resource, but not on update in LocalStack.
420436
Ensure that you set the `_custom_id_` tag on creation of the resource.
421-
{{< /callout >}}
437+
:::
422438

423439
## Custom Domain Names with API Gateway (Pro)
424440

@@ -430,14 +446,15 @@ Assuming your custom domain is set up as `test.example.com` to point to your RES
430446

431447
You should include the `Host` header with the custom domain name in your request, so you don't need to set up any custom DNS to resolve to LocalStack.
432448

433-
{{< command >}}
434-
$ curl -H 'Host: test.example.com' http://localhost:4566/base-path
435-
{{< / command >}}
449+
```bash
450+
curl -H 'Host: test.example.com' http://localhost:4566/base-path
451+
```
436452

437453
The request above will be equivalent to the following request:
438-
{{< command >}}
439-
$ curl http://<your-api-id>.execute-api.localhost.localstack.cloud:4566/dev/
440-
{{< / command >}}
454+
455+
```bash
456+
curl http://<your-api-id>.execute-api.localhost.localstack.cloud:4566/dev/
457+
```
441458

442459
## API Gateway Resource Browser
443460

@@ -447,7 +464,7 @@ You can access the Resource Browser by opening the LocalStack Web Application in
447464
The Resource Browser displays [API Gateway V1](https://app.localstack.cloud/resources/gateway/v1) and [API Gateway V2](https://app.localstack.cloud/resources/gateway/v2) resources.
448465
You can click on individual resources to view their details.
449466

450-
<img src="api-gateway-resource-browser.png" alt="API Gateway Resource Browser" title="API Gateway Resource Browser" width="900" />
467+
![API Gateway Resource Browser](/images/aws/api-gateway-resource-browser.png)
451468

452469
The Resource Browser allows you to perform the following actions:
453470

0 commit comments

Comments
 (0)