Skip to content

Commit cce80ef

Browse files
authored
chore: update snippets & event-handler docs (aws-powertools#3864)
1 parent 98f5c45 commit cce80ef

File tree

9 files changed

+137
-69
lines changed

9 files changed

+137
-69
lines changed

.github/scripts/release_patch_package_json.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ if (process.argv.length < 3) {
1717
}
1818
const basePath = resolve(process.argv[2]);
1919
const packageJsonPath = join(basePath, 'package.json');
20-
const alphaPackages = [
21-
'@aws-lambda-powertools/event-handler'
22-
];
20+
const alphaPackages = [];
2321
const betaPackages = [];
2422

2523
(() => {

docs/environment-variables.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ Each Utility page provides information on example values and allowed values.
3232

3333
## Dev Mode
3434

35-
Whether you're prototyping locally or against a non-production environment, you can use `POWERTOOLS_DEV` to increase verbosity across multiple utilities.
35+
Whether you're prototyping locally or against a non-production environment, you can use `POWERTOOLS_DEV` to increase verbosity across multiple utilities or disable certain features.
3636

37-
When `POWERTOOLS_DEV` is set to a truthy value (`1`, `true`), it'll have the following effects:
37+
When `POWERTOOLS_DEV` is set to a truthy value (`1`, `true`, `on`), it'll have the following effects:
3838

39-
| Utility | Effect |
40-
| ----------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
41-
| **Logger** | Increase JSON indentation to 4 and uses global `console` to emit logs to ease testing and local debugging when running functions locally. However, Amazon CloudWatch Logs view will degrade as each new line is treated as a new message |
42-
| **Tracer** | Disables tracing operations in non-Lambda environments. This already happens automatically in the Tracer utility |
43-
| **Metrics** | Disables emitting metrics to stdout. Can be overridden by explicitly setting `POWERTOOLS_METRICS_DISABLED` to `false` |
39+
| Utility | Effect |
40+
| ----------------- | --------------------------------------------------------------------------------------------------------------------- |
41+
| **Logger** | Increase JSON indentation to 4, uses global `console` to emit logs, and format stack traces |
42+
| **Tracer** | Disable tracing operations. This already happens automatically when running in non-Lambda environments |
43+
| **Metrics** | Disable emitting metrics to stdout. Can be overridden by explicitly setting `POWERTOOLS_METRICS_DISABLED` to `false` |

docs/features/event-handler/appsync-events.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ stateDiagram-v2
4141
* Easily handle publish and subscribe events with dedicated handler methods
4242
* Automatic routing based on namespace and channel patterns
4343
* Support for wildcard patterns to create catch-all handlers
44-
* Process events in parallel corontrol aggregation for batch processing
44+
* Process events in parallel and control aggregation for batch processing
4545
* Graceful error handling for individual events
4646

4747
## Terminology
@@ -59,6 +59,8 @@ It handles connection management, message broadcasting, authentication, and moni
5959

6060
You must have an existing AppSync Events API with real-time capabilities enabled and IAM permissions to invoke your AWS Lambda function. That said, there are no additional permissions required to use Event Handler as routing requires no dependency.
6161

62+
Additionally, if you want the result of your handler to be used by AppSync you must set the integration type to `REQUEST_RESPONSE`.
63+
6264
=== "template.yaml"
6365

6466
```yaml
@@ -245,7 +247,7 @@ You can access to the original Lambda event or context for additional informatio
245247

246248
=== "Access event and context"
247249

248-
```typescript hl_lines="6"
250+
```typescript hl_lines="10"
249251
--8<-- "examples/snippets/event-handler/appsync-events/accessEventAndContext.ts"
250252
```
251253

docs/getting-started/lambda-layers.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,42 @@ We publish the Lambda Layer for Powertools for AWS Lambda in all commercial regi
1717

1818
Open an [issue](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new?template=feature_request.yml&title=Feature%20request%3A%20missing%20Lambda%20layer%20region) in our GitHub repository to request it.
1919

20-
| Region | Layer ARN |
21-
| ---------------- | -------------------------------------------------------------------------------------------------------------------- |
22-
| `us-east-1` | [arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
23-
| `us-east-2` | [arn:aws:lambda:us-east-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
24-
| `us-west-1` | [arn:aws:lambda:us-west-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
25-
| `us-west-2` | [arn:aws:lambda:us-west-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
26-
| `ap-south-1` | [arn:aws:lambda:ap-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
27-
| `ap-south-2` | [arn:aws:lambda:ap-south-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
28-
| `ap-east-1` | [arn:aws:lambda:ap-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
29-
| `ap-northeast-1` | [arn:aws:lambda:ap-northeast-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
30-
| `ap-northeast-2` | [arn:aws:lambda:ap-northeast-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
31-
| `ap-northeast-3` | [arn:aws:lambda:ap-northeast-3:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
32-
| `ap-southeast-1` | [arn:aws:lambda:ap-southeast-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
33-
| `ap-southeast-2` | [arn:aws:lambda:ap-southeast-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
34-
| `ap-southeast-3` | [arn:aws:lambda:ap-southeast-3:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
35-
| `ap-southeast-4` | [arn:aws:lambda:ap-southeast-4:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
36-
| `ap-southeast-5` | [arn:aws:lambda:ap-southeast-5:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
37-
| `ap-southeast-7` | [arn:aws:lambda:ap-southeast-7:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
38-
| `eu-central-1` | [arn:aws:lambda:eu-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
39-
| `eu-central-2` | [arn:aws:lambda:eu-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
40-
| `eu-west-1` | [arn:aws:lambda:eu-west-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
41-
| `eu-west-2` | [arn:aws:lambda:eu-west-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
42-
| `eu-west-3` | [arn:aws:lambda:eu-west-3:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
43-
| `eu-north-1` | [arn:aws:lambda:eu-north-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
44-
| `eu-south-1` | [arn:aws:lambda:eu-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
45-
| `eu-south-2` | [arn:aws:lambda:eu-south-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
46-
| `ca-central-1` | [arn:aws:lambda:ca-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
47-
| `ca-west-1` | [arn:aws:lambda:ca-west-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
48-
| `sa-east-1` | [arn:aws:lambda:sa-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
49-
| `af-south-1` | [arn:aws:lambda:af-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
50-
| `me-south-1` | [arn:aws:lambda:me-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
51-
| `me-central-1` | [arn:aws:lambda:me-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
52-
| `il-central-1` | [arn:aws:lambda:il-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
53-
| `mx-central-1` | [arn:aws:lambda:mx-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
54-
| `us-gov-west-1` | [arn:aws-us-gov:lambda:us-gov-west-1:165093116878:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
55-
| `us-gov-east-1` | [arn:aws-us-gov:lambda:us-gov-east-1:165087284144:layer:AWSLambdaPowertoolsTypeScriptV2:24](#){: .copyMe} |
20+
| Region | Layer ARN |
21+
| ---------------- | --------------------------------------------------------------------------------------------------------- |
22+
| `us-east-1` | [arn:aws:lambda:us-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
23+
| `us-east-2` | [arn:aws:lambda:us-east-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
24+
| `us-west-1` | [arn:aws:lambda:us-west-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
25+
| `us-west-2` | [arn:aws:lambda:us-west-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
26+
| `ap-south-1` | [arn:aws:lambda:ap-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
27+
| `ap-south-2` | [arn:aws:lambda:ap-south-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
28+
| `ap-east-1` | [arn:aws:lambda:ap-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
29+
| `ap-northeast-1` | [arn:aws:lambda:ap-northeast-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
30+
| `ap-northeast-2` | [arn:aws:lambda:ap-northeast-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
31+
| `ap-northeast-3` | [arn:aws:lambda:ap-northeast-3:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
32+
| `ap-southeast-1` | [arn:aws:lambda:ap-southeast-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
33+
| `ap-southeast-2` | [arn:aws:lambda:ap-southeast-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
34+
| `ap-southeast-3` | [arn:aws:lambda:ap-southeast-3:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
35+
| `ap-southeast-4` | [arn:aws:lambda:ap-southeast-4:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
36+
| `ap-southeast-5` | [arn:aws:lambda:ap-southeast-5:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
37+
| `ap-southeast-7` | [arn:aws:lambda:ap-southeast-7:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
38+
| `eu-central-1` | [arn:aws:lambda:eu-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
39+
| `eu-central-2` | [arn:aws:lambda:eu-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
40+
| `eu-west-1` | [arn:aws:lambda:eu-west-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
41+
| `eu-west-2` | [arn:aws:lambda:eu-west-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
42+
| `eu-west-3` | [arn:aws:lambda:eu-west-3:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
43+
| `eu-north-1` | [arn:aws:lambda:eu-north-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
44+
| `eu-south-1` | [arn:aws:lambda:eu-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
45+
| `eu-south-2` | [arn:aws:lambda:eu-south-2:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
46+
| `ca-central-1` | [arn:aws:lambda:ca-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
47+
| `ca-west-1` | [arn:aws:lambda:ca-west-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
48+
| `sa-east-1` | [arn:aws:lambda:sa-east-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
49+
| `af-south-1` | [arn:aws:lambda:af-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
50+
| `me-south-1` | [arn:aws:lambda:me-south-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
51+
| `me-central-1` | [arn:aws:lambda:me-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
52+
| `il-central-1` | [arn:aws:lambda:il-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
53+
| `mx-central-1` | [arn:aws:lambda:mx-central-1:094274105915:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
54+
| `us-gov-west-1` | [arn:aws-us-gov:lambda:us-gov-west-1:165093116878:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
55+
| `us-gov-east-1` | [arn:aws-us-gov:lambda:us-gov-east-1:165087284144:layer:AWSLambdaPowertoolsTypeScriptV2:25](#){: .copyMe} |
5656

5757
### Lookup Layer ARN via AWS SSM Parameter Store
5858

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { AppSyncEventsResolver } from '@aws-lambda-powertools/event-handler/appsync-events';
2+
import { Logger } from '@aws-lambda-powertools/logger';
23
import type { Context } from 'aws-lambda';
34

5+
const logger = new Logger({
6+
serviceName: 'serverlessAirline',
7+
});
48
const app = new AppSyncEventsResolver();
59

610
app.onPublish('/*', (payload, event, context) => {
711
const { headers } = event.request; // (1)!
812
const { awsRequestId } = context;
13+
logger.info('headers', { headers, awsRequestId });
914

1015
// your business logic here
1116

1217
return payload;
1318
});
1419

1520
export const handler = async (event: unknown, context: Context) =>
16-
await app.resolve(event, context);
21+
app.resolve(event, context);

examples/snippets/event-handler/appsync-events/debugLogging.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ app.onPublish('/default/foo', (payload) => {
1313
});
1414

1515
export const handler = async (event: unknown, context: Context) =>
16-
await app.resolve(event, context);
16+
app.resolve(event, context);
Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,88 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
4+
Globals:
5+
Function:
6+
Timeout: 5
7+
MemorySize: 256
8+
Runtime: nodejs22.x
9+
Tracing: Active
10+
Environment:
11+
Variables:
12+
POWERTOOLS_LOG_LEVEL: INFO
13+
POWERTOOLS_SERVICE_NAME: hello
14+
115
Resources:
16+
HelloWorldFunction:
17+
Type: AWS::Serverless::Function
18+
Properties:
19+
Handler: index.handler
20+
CodeUri: hello_world
21+
222
WebsocketAPI:
3-
Type: AWS::AppSync::Api
4-
Properties:
5-
EventConfig:
6-
AuthProviders:
7-
- AuthType: API_KEY
8-
ConnectionAuthModes:
9-
- AuthType: API_KEY
10-
DefaultPublishAuthModes:
11-
- AuthType: API_KEY
12-
DefaultSubscribeAuthModes:
13-
- AuthType: API_KEY
14-
Name: RealTimeEventAPI
23+
Type: AWS::AppSync::Api
24+
Properties:
25+
EventConfig:
26+
AuthProviders:
27+
- AuthType: API_KEY
28+
ConnectionAuthModes:
29+
- AuthType: API_KEY
30+
DefaultPublishAuthModes:
31+
- AuthType: API_KEY
32+
DefaultSubscribeAuthModes:
33+
- AuthType: API_KEY
34+
Name: RealTimeEventAPI
1535

16-
WebasocketApiKey:
17-
Type: AWS::AppSync::ApiKey
18-
Properties:
19-
ApiId: !GetAtt WebsocketAPI.ApiId
20-
Description: "API KEY"
21-
Expires: 90
36+
WebsocketApiKey:
37+
Type: AWS::AppSync::ApiKey
38+
Properties:
39+
ApiId: !GetAtt WebsocketAPI.ApiId
2240

2341
WebsocketAPINamespace:
2442
Type: AWS::AppSync::ChannelNamespace
2543
Properties:
2644
ApiId: !GetAtt WebsocketAPI.ApiId
27-
Name: powertools
45+
Name: powertools
46+
HandlerConfigs:
47+
OnPublish:
48+
Behavior: DIRECT
49+
Integration:
50+
DataSourceName: powertools_lambda
51+
LambdaConfig:
52+
InvokeType: REQUEST_RESPONSE
53+
OnSubscribe:
54+
Behavior: DIRECT
55+
Integration:
56+
DataSourceName: powertools_lambda
57+
LambdaConfig:
58+
InvokeType: REQUEST_RESPONSE
59+
60+
DataSourceIAMRole:
61+
Type: AWS::IAM::Role
62+
Properties:
63+
AssumeRolePolicyDocument:
64+
Version: '2012-10-17'
65+
Statement:
66+
- Effect: Allow
67+
Principal:
68+
Service: appsync.amazonaws.com
69+
Action: sts:AssumeRole
70+
Policies:
71+
- PolicyName: LambdaInvokePolicy
72+
PolicyDocument:
73+
Version: '2012-10-17'
74+
Statement:
75+
- Effect: Allow
76+
Action:
77+
- lambda:InvokeFunction
78+
Resource: !GetAtt HelloWorldFunction.Arn
79+
80+
NameSpaceDataSource:
81+
Type: AWS::AppSync::DataSource
82+
Properties:
83+
ApiId: !GetAtt WebsocketAPI.ApiId
84+
LambdaConfig:
85+
LambdaFunctionArn: !GetAtt HelloWorldFunction.Arn
86+
Name: powertools_lambda
87+
ServiceRoleArn: !GetAtt DataSourceIAMRole.Arn
88+
Type: AWS_LAMBDA

examples/snippets/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript#readme",
2626
"devDependencies": {
2727
"@aws-lambda-powertools/batch": "^2.19.0",
28+
"@aws-lambda-powertools/event-handler": "^2.19.0",
2829
"@aws-lambda-powertools/idempotency": "^2.19.0",
2930
"@aws-lambda-powertools/jmespath": "^2.19.0",
3031
"@aws-lambda-powertools/logger": "^2.19.0",

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)