Skip to content

Commit ad323ae

Browse files
authored
docs(maintenance): fix install instructions & switch test runner (aws-powertools#3030)
1 parent 23a33dc commit ad323ae

8 files changed

+84
-65
lines changed

examples/app/README.md

+26-18
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,61 @@
22

33
This is a deployable CDK app that deploys AWS Lambda functions as part of a CloudFormation stack. These Lambda functions use the utilities made available as part of Powertools for AWS Lambda (TypeScript) to demonstrate their usage.
44

5-
> **Note**
6-
> You will need to have a valid AWS Account in order to deploy these resources. These resources may incur costs to your AWS Account. The cost from **some services** are covered by the [AWS Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) but not all of them. If you don't have an AWS Account follow [these instructions to create one](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).
5+
> [!Warning]
6+
> You will need to have a valid AWS Account in order to deploy these resources. Many of the services in the example are covered [AWS Free Tier](https://aws.amazon.com/free/?all-free-tier.sort-by=item.additionalFields.SortRank&all-free-tier.sort-order=asc&awsf.Free%20Tier%20Types=*all&awsf.Free%20Tier%20Categories=*all) but you may incur charges if you exceed the free tier limits.
7+
If you don't have an AWS Account follow [these instructions to create one](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).
78

89
The example functions, located in the `functions` folder, are frontend by a REST API that is deployed using AWS API Gateway.
910

1011
The API has three endpoints:
1112

12-
* `POST /` - Adds an item to the DynamoDB table
13-
* `GET /` - Retrieves all items from the DynamoDB table
14-
* `GET /{id}` - Retrieves a specific item from the DynamoDB table
13+
* `POST /` - Adds an item to the DynamoDB table
14+
* `GET /` - Retrieves all items from the DynamoDB table
15+
* `GET /{id}` - Retrieves a specific item from the DynamoDB table
1516

1617
## Deploying the stack
1718

18-
* Navigate to this location of the repo in your terminal (`examples/cdk`)
19-
* `npm ci`
20-
* `npm run cdk deploy --all --profile <YOUR_AWS_PROFILE>`
19+
> [!Note]
20+
> The `examples/app` directory where this example is located is part of a monorepo. If you are interested in deploying the example only, follow the instructions below.
21+
> If instead you are working on the monorepo and want to deploy the example, follow the instructions in the [CONTRIBUTING](../../CONTRIBUTING.md#dev-setup) doc, then run `npm run cdk deploy -w examples/app` from the project's root.
2122
22-
Note: Prior to deploying you may need to run `cdk bootstrap aws://<YOU_AWS_ACCOUNT_ID>/<AWS_REGION> --profile <YOUR_AWS_PROFILE>` if you have not already bootstrapped your account for CDK.
23+
If this is the first time you're using CDK in your AWS Account & AWS Region, you may need to run `npm run cdk bootstrap aws://<YOU_AWS_ACCOUNT_ID>/<AWS_REGION> --profile <YOUR_AWS_PROFILE>` to bootstrap your account for CDK.
2324

24-
> **Note**
25-
> You can find your API Gateway Endpoint URL in the output values displayed after deployment.
25+
Then, still from within the `examples/app` directory, run the following commands:
26+
27+
* `npm i --prefix ./` to install the dependencies
28+
* `npm run cdk deploy` and follow the prompts to deploy the stack
29+
30+
When the deployment is complete, you will see the output values that include the API Gateway Endpoint URL.
2631

2732
## Execute the functions via API Gateway
2833

2934
Use the API Gateway Endpoint URL from the output values to execute the functions. First, let's add two items to the DynamoDB Table by running:
3035

3136
```bash
32-
curl -XPOST --header 'Content-Type: application/json' --data '{"id":"myfirstitem","name":"Some Name for the first item"}' https://randomid12345.execute-api.eu-central-1.amazonaws.com/prod/
33-
curl -XPOST --header 'Content-Type: application/json' --data '{"id":"myseconditem","name":"Some Name for the second item"}' https://randomid1245.execute-api.eu-central-1.amazonaws.com/prod/
37+
curl -XPOST --header 'Content-Type: application/json' --data '{"id":"myfirstitem","name":"Some Name for the first item"}' https://<api-id>.execute-api.eu-central-1.amazonaws.com/prod/
38+
curl -XPOST --header 'Content-Type: application/json' --data '{"id":"myseconditem","name":"Some Name for the second item"}' https://<api-id>.execute-api.eu-central-1.amazonaws.com/prod/
3439
````
3540

3641
Now, let's retrieve all items by running:
3742
3843
```sh
39-
curl -XGET https://randomid12345.execute-api.eu-central-1.amazonaws.com/prod/
44+
curl -XGET https://<api-id>.execute-api.eu-central-1.amazonaws.com/prod/
4045
```
4146
4247
And finally, let's retrieve a specific item by running:
48+
4349
```bash
44-
curl -XGET https://randomid12345.execute-api.eu-central-1.amazonaws.com/prod/myseconditem/
50+
curl -XGET https://<api-id>.execute-api.eu-central-1.amazonaws.com/prod/myseconditem/
4551
```
4652

4753
## Observe the outputs in AWS CloudWatch & X-Ray
4854

4955
### CloudWatch
5056

5157
If we check the logs in CloudWatch, we can see that the logs are structured like this
52-
```
58+
59+
```text
5360
2022-04-26T17:00:23.808Z e8a51294-6c6a-414c-9777-6b0f24d8739b DEBUG
5461
{
5562
"level": "DEBUG",
@@ -62,10 +69,11 @@ If we check the logs in CloudWatch, we can see that the logs are structured like
6269

6370
By having structured logs like this, we can easily search and analyse them in [CloudWatch Logs Insight](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/AnalyzingLogData.html). Run the following query to get all messages for a specific `awsRequestId`:
6471

65-
````
72+
````text
6673
filter awsRequestId="bcd50969-3a55-49b6-a997-91798b3f133a"
6774
| fields timestamp, message
6875
````
76+
6977
### AWS X-Ray
7078

7179
As we have enabled tracing for our Lambda-Funtions, you can visit [AWS CloudWatch Console](https://console.aws.amazon.com/cloudwatch/) and see [Traces](https://docs.aws.amazon.com/xray/latest/devguide/xray-concepts.html#xray-concepts-traces) and a [Service Map](https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-using-xray-maps.html) for our application.
@@ -76,4 +84,4 @@ To delete the sample application that you created, run the command below while i
7684

7785
```bash
7886
cdk destroy
79-
```
87+
```

examples/app/cdk/example-stack.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
13
import { RemovalPolicy, Stack, type StackProps } from 'aws-cdk-lib';
24
import { LambdaIntegration, RestApi } from 'aws-cdk-lib/aws-apigateway';
35
import {
@@ -21,6 +23,12 @@ import { StringParameter } from 'aws-cdk-lib/aws-ssm';
2123
import type { Construct } from 'constructs';
2224
import { FunctionWithLogGroup } from './function-with-logstream-construct.js';
2325

26+
// Get the current file URL
27+
const __filename = fileURLToPath(import.meta.url);
28+
29+
// Get the current directory
30+
const __dirname = dirname(__filename);
31+
2432
export class PowertoolsExampleStack extends Stack {
2533
public constructor(scope: Construct, id: string, props?: StackProps) {
2634
super(scope, id, props);
@@ -80,7 +88,7 @@ export class PowertoolsExampleStack extends Stack {
8088
* Because we are using ESM and tree shake, we create an optimized bundle.
8189
*/
8290
const putItemFn = new FunctionWithLogGroup(this, 'put-item-fn', {
83-
entry: './functions/put-item.ts',
91+
entry: join(__dirname, '..', './functions/put-item.ts'),
8492
functionName: 'powertools-example-put-item',
8593
bundling: {
8694
minify: true,
@@ -124,7 +132,7 @@ export class PowertoolsExampleStack extends Stack {
124132
* in a centralized way across all your functions.
125133
*/
126134
const getAllItemsFn = new FunctionWithLogGroup(this, 'get-all-items-fn', {
127-
entry: './functions/get-all-items.ts',
135+
entry: join(__dirname, '..', './functions/get-all-items.ts'),
128136
functionName: 'powertools-example-get-all-items',
129137
layers: [powertoolsLayer], // we use the powertools layer
130138
bundling: {
@@ -150,7 +158,7 @@ export class PowertoolsExampleStack extends Stack {
150158
* dependencies in it.
151159
*/
152160
const getByIdFn = new FunctionWithLogGroup(this, 'get-by-id-fn', {
153-
entry: './functions/get-by-id.ts',
161+
entry: join(__dirname, '..', './functions/get-by-id.ts'),
154162
functionName: 'powertools-example-get-by-id',
155163
bundling: {
156164
minify: true,
@@ -172,7 +180,7 @@ export class PowertoolsExampleStack extends Stack {
172180
this,
173181
'process-items-stream-fn',
174182
{
175-
entry: './functions/process-items-stream.ts',
183+
entry: join(__dirname, '..', './functions/process-items-stream.ts'),
176184
functionName: 'powertools-example-process-items-stream',
177185
layers: [powertoolsLayer],
178186
bundling: {

examples/app/jest.config.cjs

-14
This file was deleted.

examples/app/package.json

+3-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"test": "npm run test:unit",
1414
"lint": "biome lint .",
1515
"lint:fix": "biome check --write .",
16-
"test:unit": "export POWERTOOLS_DEV=true && jest --silent",
16+
"test:unit": "export POWERTOOLS_DEV=true && vitest --run --silent",
1717
"test:e2e": "echo 'To be implemented ...'",
1818
"cdk": "cdk"
1919
},
@@ -29,16 +29,14 @@
2929
},
3030
"devDependencies": {
3131
"@types/aws-lambda": "^8.10.145",
32-
"@types/jest": "^29.5.12",
3332
"@types/node": "22.5.2",
3433
"aws-cdk": "^2.155.0",
3534
"aws-cdk-lib": "^2.155.0",
3635
"constructs": "^10.3.0",
37-
"jest": "^29.7.0",
3836
"source-map-support": "^0.5.21",
39-
"ts-jest": "^29.2.5",
4037
"tsx": "^4.19.0",
41-
"typescript": "^5.5.4"
38+
"typescript": "^5.5.4",
39+
"vitest": "^2.0.5"
4240
},
4341
"dependencies": {
4442
"@aws-lambda-powertools/batch": "^2.7.0",
@@ -51,13 +49,9 @@
5149
"@aws-sdk/lib-dynamodb": "^3.637.0",
5250
"@middy/core": "^4.7.0",
5351
"@types/aws-lambda": "^8.10.145",
54-
"@types/jest": "^29.5.12",
5552
"@types/node": "22.5.2",
5653
"constructs": "^10.3.0",
5754
"esbuild": "^0.23.1",
58-
"jest": "^29.7.0",
59-
"ts-jest": "^29.2.5",
60-
"ts-node": "^10.9.2",
6155
"typescript": "^5.5.4"
6256
}
6357
}

examples/app/tests/cdk-app.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { App } from 'aws-cdk-lib';
22
import { Template } from 'aws-cdk-lib/assertions';
3+
import { describe, it } from 'vitest';
34
import { PowertoolsExampleStack } from '../cdk/example-stack.js';
45

5-
test('CDK code synthesize', () => {
6+
describe('CDK example stack', () => {
67
const app = new App();
78
const stack = new PowertoolsExampleStack(app, 'MyTestStack');
8-
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 4);
9+
10+
it('has 4 Lambda functions', () => {
11+
Template.fromStack(stack).resourceCountIs('AWS::Lambda::Function', 4);
12+
});
913
});

examples/app/vitest.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineProject } from 'vitest/config';
2+
3+
export default defineProject({
4+
test: {
5+
environment: 'node',
6+
},
7+
});

0 commit comments

Comments
 (0)