From a980973681316057ab37a49db62b7bbe32f180a0 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 01:32:51 +0000 Subject: [PATCH 01/15] generate lambda nodejs project --- apps/sandbox/lambda-nodejs20.x/.gitignore | 207 ++++++++++++++++++ apps/sandbox/lambda-nodejs20.x/README.md | 149 +++++++++++++ .../lambda-nodejs20.x/events/event.json | 62 ++++++ .../lambda-nodejs20.x/hello-world/.npmignore | 1 + .../lambda-nodejs20.x/hello-world/Dockerfile | 10 + .../lambda-nodejs20.x/hello-world/app.mjs | 23 ++ .../hello-world/package.json | 16 ++ .../hello-world/tests/unit/test-handler.mjs | 20 ++ apps/sandbox/lambda-nodejs20.x/samconfig.toml | 30 +++ apps/sandbox/lambda-nodejs20.x/template.yaml | 43 ++++ 10 files changed, 561 insertions(+) create mode 100644 apps/sandbox/lambda-nodejs20.x/.gitignore create mode 100644 apps/sandbox/lambda-nodejs20.x/README.md create mode 100644 apps/sandbox/lambda-nodejs20.x/events/event.json create mode 100644 apps/sandbox/lambda-nodejs20.x/hello-world/.npmignore create mode 100644 apps/sandbox/lambda-nodejs20.x/hello-world/Dockerfile create mode 100644 apps/sandbox/lambda-nodejs20.x/hello-world/app.mjs create mode 100644 apps/sandbox/lambda-nodejs20.x/hello-world/package.json create mode 100644 apps/sandbox/lambda-nodejs20.x/hello-world/tests/unit/test-handler.mjs create mode 100644 apps/sandbox/lambda-nodejs20.x/samconfig.toml create mode 100644 apps/sandbox/lambda-nodejs20.x/template.yaml diff --git a/apps/sandbox/lambda-nodejs20.x/.gitignore b/apps/sandbox/lambda-nodejs20.x/.gitignore new file mode 100644 index 000000000..5854f05ec --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/.gitignore @@ -0,0 +1,207 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/osx,node,linux,windows,sam +# Edit at https://www.toptal.com/developers/gitignore?templates=osx,node,linux,windows,sam + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### Node ### +# Logs +logs +*.log +npm-debug.log* +yarn-debug.log* +yarn-error.log* +lerna-debug.log* + +# Diagnostic reports (https://nodejs.org/api/report.html) +report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json + +# Runtime data +pids +*.pid +*.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage +*.lcov + +# nyc test coverage +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# Bower dependency directory (https://bower.io/) +bower_components + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) +build/Release + +# Dependency directories +node_modules/ +jspm_packages/ + +# TypeScript v1 declaration files +typings/ + +# TypeScript cache +*.tsbuildinfo + +# Optional npm cache directory +.npm + +# Optional eslint cache +.eslintcache + +# Optional stylelint cache +.stylelintcache + +# Microbundle cache +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history +.node_repl_history + +# Output of 'npm pack' +*.tgz + +# Yarn Integrity file +.yarn-integrity + +# dotenv environment variables file +.env +.env.test +.env*.local + +# parcel-bundler cache (https://parceljs.org/) +.cache +.parcel-cache + +# Next.js build output +.next + +# Nuxt.js build / generate output +.nuxt +dist + +# Storybook build outputs +.out +.storybook-out +storybook-static + +# rollup.js default build output +dist/ + +# Gatsby files +.cache/ +# Comment in the public line in if your project uses Gatsby and not Next.js +# https://nextjs.org/blog/next-9-1#public-directory-support +# public + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless/ + +# FuseBox cache +.fusebox/ + +# DynamoDB Local files +.dynamodb/ + +# TernJS port file +.tern-port + +# Stores VSCode versions used for testing VSCode extensions +.vscode-test + +# Temporary folders +tmp/ +temp/ + +### OSX ### +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### SAM ### +# Ignore build directories for the AWS Serverless Application Model (SAM) +# Info: https://aws.amazon.com/serverless/sam/ +# Docs: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-reference.html + +**/.aws-sam + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# End of https://www.toptal.com/developers/gitignore/api/osx,node,linux,windows,sam diff --git a/apps/sandbox/lambda-nodejs20.x/README.md b/apps/sandbox/lambda-nodejs20.x/README.md new file mode 100644 index 000000000..86c5ed08d --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/README.md @@ -0,0 +1,149 @@ +# lambda-nodejs20.x + +This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. + +- hello-world - Code for the application's Lambda function and Project Dockerfile. +- events - Invocation events that you can use to invoke the function. +- hello-world/tests - Unit tests for the application code. +- template.yaml - A template that defines the application's AWS resources. + +The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. + +## Deploy the sample application + +The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. + +To use the SAM CLI, you need the following tools. + +- Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) +- SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) + +You may need the following for local testing. + +- Node.js - [Install Node.js 20](https://nodejs.org/en/), including the NPM package management tool. + +To build and deploy your application for the first time, run the following in your shell: + +```bash +sam build +sam deploy --guided +``` + +The first command will build a docker image from a Dockerfile and then the source of your application inside the Docker image. The second command will package and deploy your application to AWS, with a series of prompts: + +- **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. +- **AWS Region**: The AWS region you want to deploy your app to. +- **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. +- **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. +- **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. + +You can find your API Gateway Endpoint URL in the output values displayed after deployment. + +## Use the SAM CLI to build and test locally + +Build your application with the `sam build` command. + +```bash +lambda-nodejs20.x$ sam build +``` + +The SAM CLI builds a docker image from a Dockerfile and then installs dependencies defined in `hello-world/package.json` inside the docker image. The processed template file is saved in the `.aws-sam/build` folder. + +- **Note**: The Dockerfile included in this sample application uses `npm install` by default. If you are building your code for production, you can modify it to use `npm ci` instead. + +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. + +Run functions locally and invoke them with the `sam local invoke` command. + +```bash +lambda-nodejs20.x$ sam local invoke HelloWorldFunction --event events/event.json +``` + +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. + +```bash +lambda-nodejs20.x$ sam local start-api +lambda-nodejs20.x$ curl http://localhost:3000/ +``` + +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. + +```yaml +Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get +``` + +## Add a resource to your application + +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. + +## Fetch, tail, and filter Lambda function logs + +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. + +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. + +```bash +lambda-nodejs20.x$ sam logs -n HelloWorldFunction --stack-name lambda-nodejs20.x --tail +``` + +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). + +## Unit tests + +Tests are defined in the `hello-world/tests` folder in this project. Use NPM to install the [Mocha test framework](https://mochajs.org/) and run unit tests from your local machine. + +```bash +lambda-nodejs20.x$ cd hello-world +hello-world$ npm install +hello-world$ npm run test +``` + +## Cleanup + +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: + +```bash +sam delete --stack-name lambda-nodejs20.x +``` + +## Resources + +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. + +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) + +## Login with the AWS CLI + +> [!NOTE] +> This and the following sections assume that you are working in the AWS account +> `org-sagebase-itsandbox` with the role `Developer` and that you are deploying +> to the `us-east-1` region. If this assumption is correct, you should be able +> to simply copy-paste the following commands, otherwise adapting the +> configuration should be straightforward. + +Create the config file if it doesn't exist yet. + +```console +mkdir ~/.aws && touch ~/.aws/config +``` + +As a Developer working in Sage IT Sandbox AWS account, add the following profile to the config file. + +```ini +[profile profile-name] +sso_start_url = https://d-906769aa66.awsapps.com/start +sso_region = us-east-1 +sso_account_id = XXXXXXXXX +sso_role_name = Developer +``` + +Login with the AWS CLI: + +```console +aws --profile profile-name sso login +``` diff --git a/apps/sandbox/lambda-nodejs20.x/events/event.json b/apps/sandbox/lambda-nodejs20.x/events/event.json new file mode 100644 index 000000000..070ad8e01 --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/events/event.json @@ -0,0 +1,62 @@ +{ + "body": "{\"message\": \"hello world\"}", + "resource": "/{proxy+}", + "path": "/path/to/resource", + "httpMethod": "POST", + "isBase64Encoded": false, + "queryStringParameters": { + "foo": "bar" + }, + "pathParameters": { + "proxy": "/path/to/resource" + }, + "stageVariables": { + "baz": "qux" + }, + "headers": { + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Encoding": "gzip, deflate, sdch", + "Accept-Language": "en-US,en;q=0.8", + "Cache-Control": "max-age=0", + "CloudFront-Forwarded-Proto": "https", + "CloudFront-Is-Desktop-Viewer": "true", + "CloudFront-Is-Mobile-Viewer": "false", + "CloudFront-Is-SmartTV-Viewer": "false", + "CloudFront-Is-Tablet-Viewer": "false", + "CloudFront-Viewer-Country": "US", + "Host": "1234567890.execute-api.us-east-1.amazonaws.com", + "Upgrade-Insecure-Requests": "1", + "User-Agent": "Custom User Agent String", + "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", + "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", + "X-Forwarded-For": "127.0.0.1, 127.0.0.2", + "X-Forwarded-Port": "443", + "X-Forwarded-Proto": "https" + }, + "requestContext": { + "accountId": "123456789012", + "resourceId": "123456", + "stage": "prod", + "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", + "requestTime": "09/Apr/2015:12:34:56 +0000", + "requestTimeEpoch": 1428582896000, + "identity": { + "cognitoIdentityPoolId": null, + "accountId": null, + "cognitoIdentityId": null, + "caller": null, + "accessKey": null, + "sourceIp": "127.0.0.1", + "cognitoAuthenticationType": null, + "cognitoAuthenticationProvider": null, + "userArn": null, + "userAgent": "Custom User Agent String", + "user": null + }, + "path": "/prod/path/to/resource", + "resourcePath": "/{proxy+}", + "httpMethod": "POST", + "apiId": "1234567890", + "protocol": "HTTP/1.1" + } +} diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/.npmignore b/apps/sandbox/lambda-nodejs20.x/hello-world/.npmignore new file mode 100644 index 000000000..e7e1fb04f --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/hello-world/.npmignore @@ -0,0 +1 @@ +tests/* diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/Dockerfile b/apps/sandbox/lambda-nodejs20.x/hello-world/Dockerfile new file mode 100644 index 000000000..73381988a --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/hello-world/Dockerfile @@ -0,0 +1,10 @@ +FROM public.ecr.aws/lambda/nodejs:20 + +COPY app.mjs package*.json ./ + +RUN npm install +# If you are building your code for production, instead include a package-lock.json file on this directory and use: +# RUN npm ci --production + +# Command can be overwritten by providing a different command in the template directly. +CMD ["app.lambdaHandler"] diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/app.mjs b/apps/sandbox/lambda-nodejs20.x/hello-world/app.mjs new file mode 100644 index 000000000..7bfac1ae6 --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/hello-world/app.mjs @@ -0,0 +1,23 @@ +/** + * + * Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format + * @param {Object} event - API Gateway Lambda Proxy Input Format + * + * Context doc: https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-context.html + * @param {Object} context + * + * Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html + * @returns {Object} object - API Gateway Lambda Proxy Output Format + * + */ + +export const lambdaHandler = async (event, context) => { + const response = { + statusCode: 200, + body: JSON.stringify({ + message: 'hello world', + }), + }; + + return response; +}; diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/package.json b/apps/sandbox/lambda-nodejs20.x/hello-world/package.json new file mode 100644 index 000000000..109ed147f --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/hello-world/package.json @@ -0,0 +1,16 @@ +{ + "name": "hello_world", + "version": "1.0.0", + "description": "hello world sample for NodeJS", + "main": "app.js", + "repository": "https://github.com/awslabs/aws-sam-cli/tree/develop/samcli/local/init/templates/cookiecutter-aws-sam-hello-nodejs", + "author": "SAM CLI", + "license": "MIT", + "dependencies": { + "axios": ">=1.6.0" + }, + "devDependencies": { + "chai": "^4.3.6", + "mocha": "^10.2.0" + } +} diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/tests/unit/test-handler.mjs b/apps/sandbox/lambda-nodejs20.x/hello-world/tests/unit/test-handler.mjs new file mode 100644 index 000000000..41b790639 --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/hello-world/tests/unit/test-handler.mjs @@ -0,0 +1,20 @@ +'use strict'; + +import { lambdaHandler } from '../../app.mjs'; +import { expect } from 'chai'; +var event, context; + +describe('Tests index', function () { + it('verifies successful response', async () => { + const result = await lambdaHandler(event, context); + + expect(result).to.be.an('object'); + expect(result.statusCode).to.equal(200); + expect(result.body).to.be.an('string'); + + let response = JSON.parse(result.body); + + expect(response).to.be.an('object'); + expect(response.message).to.be.equal('hello world'); + }); +}); diff --git a/apps/sandbox/lambda-nodejs20.x/samconfig.toml b/apps/sandbox/lambda-nodejs20.x/samconfig.toml new file mode 100644 index 000000000..1916e15e9 --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/samconfig.toml @@ -0,0 +1,30 @@ +# More information about the configuration file can be found here: +# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html +version = 0.1 + +[default.global.parameters] +stack_name = "lambda-nodejs20.x" + +[default.build.parameters] +parallel = true + +[default.validate.parameters] +lint = true + +[default.deploy.parameters] +capabilities = "CAPABILITY_IAM" +confirm_changeset = true +resolve_s3 = true +resolve_image_repos = true + +[default.package.parameters] +resolve_s3 = true + +[default.sync.parameters] +watch = true + +[default.local_start_api.parameters] +warm_containers = "EAGER" + +[default.local_start_lambda.parameters] +warm_containers = "EAGER" diff --git a/apps/sandbox/lambda-nodejs20.x/template.yaml b/apps/sandbox/lambda-nodejs20.x/template.yaml new file mode 100644 index 000000000..33cb5d0cb --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/template.yaml @@ -0,0 +1,43 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + lambda-nodejs20.x + + Sample SAM Template for lambda-nodejs20.x + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 3 + +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + PackageType: Image + Architectures: + - x86_64 + Events: + HelloWorld: + Type: Api + Properties: + Path: /hello + Method: get + Metadata: + DockerTag: nodejs20.x-v1 + DockerContext: ./hello-world + Dockerfile: Dockerfile + +Outputs: + # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function + # Find out more about other implicit resources you can reference within SAM + # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api + HelloWorldApi: + Description: 'API Gateway endpoint URL for Prod stage for Hello World function' + Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/' + HelloWorldFunction: + Description: 'Hello World Lambda Function ARN' + Value: !GetAtt HelloWorldFunction.Arn + HelloWorldFunctionIamRole: + Description: 'Implicit IAM Role created for Hello World function' + Value: !GetAtt HelloWorldFunctionRole.Arn From 1bc36afcbe1e837b661003031fd3fcae8be93ca4 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 01:51:23 +0000 Subject: [PATCH 02/15] exlude new project from pnpm workspaces --- pnpm-workspace.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index abbc150e0..598487780 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -3,6 +3,7 @@ packages: - 'libs/**' - '!apps/openchallenges/infra' - '!apps/openchallenges/infra/old' + - '!apps/sandbox/lambda-nodejs20.x/hello-world' # prevent the creation of libs/**/api-client-angular/node_modules when running `workspace-install` # that break the apps and tests - '!libs/**/api-client-angular' From 36ab0e5db0cea9839d733fd4aeac164bfb630456 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 01:55:51 +0000 Subject: [PATCH 03/15] create project.json and add the task `build` --- apps/sandbox/lambda-nodejs20.x/README.md | 8 ++++++++ apps/sandbox/lambda-nodejs20.x/project.json | 15 +++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 apps/sandbox/lambda-nodejs20.x/project.json diff --git a/apps/sandbox/lambda-nodejs20.x/README.md b/apps/sandbox/lambda-nodejs20.x/README.md index 86c5ed08d..20d7b1359 100644 --- a/apps/sandbox/lambda-nodejs20.x/README.md +++ b/apps/sandbox/lambda-nodejs20.x/README.md @@ -1,3 +1,11 @@ +# Sandbox Lambda Node.js 20.x + +## Build the project + +```console +nx build sandbox-lambda-nodejs20.x +``` + # lambda-nodejs20.x This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. diff --git a/apps/sandbox/lambda-nodejs20.x/project.json b/apps/sandbox/lambda-nodejs20.x/project.json new file mode 100644 index 000000000..f0e85c048 --- /dev/null +++ b/apps/sandbox/lambda-nodejs20.x/project.json @@ -0,0 +1,15 @@ +{ + "name": "sandbox-lambda-nodejs20.x", + "$schema": "../../../node_modules/nx/schemas/project-schema.json", + "projectType": "library", + "targets": { + "build": { + "executor": "nx:run-commands", + "options": { + "command": "sam build", + "cwd": "{projectRoot}" + } + } + }, + "tags": ["language:javascript"] +} From e8a33e7e07bb603c6dcdef5fd56e860972d7797c Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 03:28:06 +0000 Subject: [PATCH 04/15] flatten the project file structure --- apps/sandbox/lambda-nodejs20.x/{hello-world => }/.npmignore | 0 apps/sandbox/lambda-nodejs20.x/{hello-world => }/Dockerfile | 0 apps/sandbox/lambda-nodejs20.x/{hello-world => }/app.mjs | 0 apps/sandbox/lambda-nodejs20.x/{hello-world => }/package.json | 0 apps/sandbox/lambda-nodejs20.x/samconfig.toml | 2 +- apps/sandbox/lambda-nodejs20.x/template.yaml | 2 +- .../{hello-world => }/tests/unit/test-handler.mjs | 0 7 files changed, 2 insertions(+), 2 deletions(-) rename apps/sandbox/lambda-nodejs20.x/{hello-world => }/.npmignore (100%) rename apps/sandbox/lambda-nodejs20.x/{hello-world => }/Dockerfile (100%) rename apps/sandbox/lambda-nodejs20.x/{hello-world => }/app.mjs (100%) rename apps/sandbox/lambda-nodejs20.x/{hello-world => }/package.json (100%) rename apps/sandbox/lambda-nodejs20.x/{hello-world => }/tests/unit/test-handler.mjs (100%) diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/.npmignore b/apps/sandbox/lambda-nodejs20.x/.npmignore similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/hello-world/.npmignore rename to apps/sandbox/lambda-nodejs20.x/.npmignore diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/Dockerfile b/apps/sandbox/lambda-nodejs20.x/Dockerfile similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/hello-world/Dockerfile rename to apps/sandbox/lambda-nodejs20.x/Dockerfile diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/app.mjs b/apps/sandbox/lambda-nodejs20.x/app.mjs similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/hello-world/app.mjs rename to apps/sandbox/lambda-nodejs20.x/app.mjs diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/package.json b/apps/sandbox/lambda-nodejs20.x/package.json similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/hello-world/package.json rename to apps/sandbox/lambda-nodejs20.x/package.json diff --git a/apps/sandbox/lambda-nodejs20.x/samconfig.toml b/apps/sandbox/lambda-nodejs20.x/samconfig.toml index 1916e15e9..6bb6a59c0 100644 --- a/apps/sandbox/lambda-nodejs20.x/samconfig.toml +++ b/apps/sandbox/lambda-nodejs20.x/samconfig.toml @@ -3,7 +3,7 @@ version = 0.1 [default.global.parameters] -stack_name = "lambda-nodejs20.x" +stack_name = "sandbox-lambda-nodejs20.x" [default.build.parameters] parallel = true diff --git a/apps/sandbox/lambda-nodejs20.x/template.yaml b/apps/sandbox/lambda-nodejs20.x/template.yaml index 33cb5d0cb..4f8d5be56 100644 --- a/apps/sandbox/lambda-nodejs20.x/template.yaml +++ b/apps/sandbox/lambda-nodejs20.x/template.yaml @@ -25,7 +25,7 @@ Resources: Method: get Metadata: DockerTag: nodejs20.x-v1 - DockerContext: ./hello-world + DockerContext: . Dockerfile: Dockerfile Outputs: diff --git a/apps/sandbox/lambda-nodejs20.x/hello-world/tests/unit/test-handler.mjs b/apps/sandbox/lambda-nodejs20.x/tests/unit/test-handler.mjs similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/hello-world/tests/unit/test-handler.mjs rename to apps/sandbox/lambda-nodejs20.x/tests/unit/test-handler.mjs From 29fac9deb0f6735467be44d6475a866d137e3d0b Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 03:30:06 +0000 Subject: [PATCH 05/15] document build-image --- apps/sandbox/lambda-nodejs20.x/README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/apps/sandbox/lambda-nodejs20.x/README.md b/apps/sandbox/lambda-nodejs20.x/README.md index 20d7b1359..8ef965edd 100644 --- a/apps/sandbox/lambda-nodejs20.x/README.md +++ b/apps/sandbox/lambda-nodejs20.x/README.md @@ -6,6 +6,12 @@ nx build sandbox-lambda-nodejs20.x ``` +## Build the Docker image of the Lambda function + +```console +nx build-image sandbox-lambda-nodejs20.x +``` + # lambda-nodejs20.x This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. From 5362eb41a2664f6d898a5b0e5bb4239fea0d8d84 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 03:51:35 +0000 Subject: [PATCH 06/15] update pnpm workspace config --- pnpm-workspace.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 598487780..53490b45a 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -3,7 +3,7 @@ packages: - 'libs/**' - '!apps/openchallenges/infra' - '!apps/openchallenges/infra/old' - - '!apps/sandbox/lambda-nodejs20.x/hello-world' + - '!apps/sandbox/lambda-nodejs20.x' # prevent the creation of libs/**/api-client-angular/node_modules when running `workspace-install` # that break the apps and tests - '!libs/**/api-client-angular' From dbd6e38edf7f5ad679e4958b7ab202944128a21f Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sat, 30 Nov 2024 03:55:59 +0000 Subject: [PATCH 07/15] ignore Hadoling warning --- apps/sandbox/lambda-nodejs20.x/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/sandbox/lambda-nodejs20.x/Dockerfile b/apps/sandbox/lambda-nodejs20.x/Dockerfile index 73381988a..28c4d081e 100644 --- a/apps/sandbox/lambda-nodejs20.x/Dockerfile +++ b/apps/sandbox/lambda-nodejs20.x/Dockerfile @@ -1,5 +1,6 @@ FROM public.ecr.aws/lambda/nodejs:20 +# hadolint ignore=DL3045 COPY app.mjs package*.json ./ RUN npm install From 6606f4e017308cfac619ba4bba19996a310affbc Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 21:31:02 +0000 Subject: [PATCH 08/15] fix hadolint issue instead of ignoring it --- apps/sandbox/lambda-nodejs20.x/Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/sandbox/lambda-nodejs20.x/Dockerfile b/apps/sandbox/lambda-nodejs20.x/Dockerfile index 28c4d081e..21767f1d5 100644 --- a/apps/sandbox/lambda-nodejs20.x/Dockerfile +++ b/apps/sandbox/lambda-nodejs20.x/Dockerfile @@ -1,7 +1,6 @@ FROM public.ecr.aws/lambda/nodejs:20 -# hadolint ignore=DL3045 -COPY app.mjs package*.json ./ +COPY app.mjs package*.json ${LAMBDA_TASK_ROOT}/ RUN npm install # If you are building your code for production, instead include a package-lock.json file on this directory and use: From 693532a13227f3c9620beb3e3344374abaa94f30 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 21:33:41 +0000 Subject: [PATCH 09/15] rename project --- .../.gitignore | 0 .../.npmignore | 0 .../Dockerfile | 0 .../README.md | 22 +++++++++---------- .../app.mjs | 0 .../events/event.json | 0 .../package.json | 0 .../project.json | 2 +- .../samconfig.toml | 2 +- .../template.yaml | 4 ++-- .../tests/unit/test-handler.mjs | 0 11 files changed, 15 insertions(+), 15 deletions(-) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/.gitignore (100%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/.npmignore (100%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/Dockerfile (100%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/README.md (94%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/app.mjs (100%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/events/event.json (100%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/package.json (100%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/project.json (88%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/samconfig.toml (94%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/template.yaml (95%) rename apps/sandbox/{lambda-nodejs20.x => lambda-nodejs}/tests/unit/test-handler.mjs (100%) diff --git a/apps/sandbox/lambda-nodejs20.x/.gitignore b/apps/sandbox/lambda-nodejs/.gitignore similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/.gitignore rename to apps/sandbox/lambda-nodejs/.gitignore diff --git a/apps/sandbox/lambda-nodejs20.x/.npmignore b/apps/sandbox/lambda-nodejs/.npmignore similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/.npmignore rename to apps/sandbox/lambda-nodejs/.npmignore diff --git a/apps/sandbox/lambda-nodejs20.x/Dockerfile b/apps/sandbox/lambda-nodejs/Dockerfile similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/Dockerfile rename to apps/sandbox/lambda-nodejs/Dockerfile diff --git a/apps/sandbox/lambda-nodejs20.x/README.md b/apps/sandbox/lambda-nodejs/README.md similarity index 94% rename from apps/sandbox/lambda-nodejs20.x/README.md rename to apps/sandbox/lambda-nodejs/README.md index 8ef965edd..9f253dd5b 100644 --- a/apps/sandbox/lambda-nodejs20.x/README.md +++ b/apps/sandbox/lambda-nodejs/README.md @@ -1,18 +1,18 @@ -# Sandbox Lambda Node.js 20.x +# Sandbox Lambda Node.js ## Build the project ```console -nx build sandbox-lambda-nodejs20.x +nx build sandbox-lambda-nodejs ``` ## Build the Docker image of the Lambda function ```console -nx build-image sandbox-lambda-nodejs20.x +nx build-image sandbox-lambda-nodejs ``` -# lambda-nodejs20.x +# lambda-nodejs This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. @@ -58,7 +58,7 @@ You can find your API Gateway Endpoint URL in the output values displayed after Build your application with the `sam build` command. ```bash -lambda-nodejs20.x$ sam build +lambda-nodejs$ sam build ``` The SAM CLI builds a docker image from a Dockerfile and then installs dependencies defined in `hello-world/package.json` inside the docker image. The processed template file is saved in the `.aws-sam/build` folder. @@ -70,14 +70,14 @@ Test a single function by invoking it directly with a test event. An event is a Run functions locally and invoke them with the `sam local invoke` command. ```bash -lambda-nodejs20.x$ sam local invoke HelloWorldFunction --event events/event.json +lambda-nodejs$ sam local invoke HelloWorldFunction --event events/event.json ``` The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. ```bash -lambda-nodejs20.x$ sam local start-api -lambda-nodejs20.x$ curl http://localhost:3000/ +lambda-nodejs$ sam local start-api +lambda-nodejs$ curl http://localhost:3000/ ``` The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. @@ -102,7 +102,7 @@ To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` `NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. ```bash -lambda-nodejs20.x$ sam logs -n HelloWorldFunction --stack-name lambda-nodejs20.x --tail +lambda-nodejs$ sam logs -n HelloWorldFunction --stack-name lambda-nodejs --tail ``` You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). @@ -112,7 +112,7 @@ You can find more information and examples about filtering Lambda function logs Tests are defined in the `hello-world/tests` folder in this project. Use NPM to install the [Mocha test framework](https://mochajs.org/) and run unit tests from your local machine. ```bash -lambda-nodejs20.x$ cd hello-world +lambda-nodejs$ cd hello-world hello-world$ npm install hello-world$ npm run test ``` @@ -122,7 +122,7 @@ hello-world$ npm run test To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: ```bash -sam delete --stack-name lambda-nodejs20.x +sam delete --stack-name lambda-nodejs ``` ## Resources diff --git a/apps/sandbox/lambda-nodejs20.x/app.mjs b/apps/sandbox/lambda-nodejs/app.mjs similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/app.mjs rename to apps/sandbox/lambda-nodejs/app.mjs diff --git a/apps/sandbox/lambda-nodejs20.x/events/event.json b/apps/sandbox/lambda-nodejs/events/event.json similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/events/event.json rename to apps/sandbox/lambda-nodejs/events/event.json diff --git a/apps/sandbox/lambda-nodejs20.x/package.json b/apps/sandbox/lambda-nodejs/package.json similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/package.json rename to apps/sandbox/lambda-nodejs/package.json diff --git a/apps/sandbox/lambda-nodejs20.x/project.json b/apps/sandbox/lambda-nodejs/project.json similarity index 88% rename from apps/sandbox/lambda-nodejs20.x/project.json rename to apps/sandbox/lambda-nodejs/project.json index f0e85c048..b7b202812 100644 --- a/apps/sandbox/lambda-nodejs20.x/project.json +++ b/apps/sandbox/lambda-nodejs/project.json @@ -1,5 +1,5 @@ { - "name": "sandbox-lambda-nodejs20.x", + "name": "sandbox-lambda-nodejs", "$schema": "../../../node_modules/nx/schemas/project-schema.json", "projectType": "library", "targets": { diff --git a/apps/sandbox/lambda-nodejs20.x/samconfig.toml b/apps/sandbox/lambda-nodejs/samconfig.toml similarity index 94% rename from apps/sandbox/lambda-nodejs20.x/samconfig.toml rename to apps/sandbox/lambda-nodejs/samconfig.toml index 6bb6a59c0..00c39a9c4 100644 --- a/apps/sandbox/lambda-nodejs20.x/samconfig.toml +++ b/apps/sandbox/lambda-nodejs/samconfig.toml @@ -3,7 +3,7 @@ version = 0.1 [default.global.parameters] -stack_name = "sandbox-lambda-nodejs20.x" +stack_name = "sandbox-lambda-nodejs" [default.build.parameters] parallel = true diff --git a/apps/sandbox/lambda-nodejs20.x/template.yaml b/apps/sandbox/lambda-nodejs/template.yaml similarity index 95% rename from apps/sandbox/lambda-nodejs20.x/template.yaml rename to apps/sandbox/lambda-nodejs/template.yaml index 4f8d5be56..eb14a6236 100644 --- a/apps/sandbox/lambda-nodejs20.x/template.yaml +++ b/apps/sandbox/lambda-nodejs/template.yaml @@ -1,9 +1,9 @@ AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: > - lambda-nodejs20.x + lambda-nodejs - Sample SAM Template for lambda-nodejs20.x + Sample SAM Template for lambda-nodejs # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst Globals: diff --git a/apps/sandbox/lambda-nodejs20.x/tests/unit/test-handler.mjs b/apps/sandbox/lambda-nodejs/tests/unit/test-handler.mjs similarity index 100% rename from apps/sandbox/lambda-nodejs20.x/tests/unit/test-handler.mjs rename to apps/sandbox/lambda-nodejs/tests/unit/test-handler.mjs From a6934cea96808299a84e50d7c6eb2d1f22f76645 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 21:43:31 +0000 Subject: [PATCH 10/15] add task `serve-detach` --- apps/sandbox/lambda-nodejs/project.json | 6 ++++++ docker/sandbox/networks.yml | 4 ++++ docker/sandbox/serve-detach.sh | 13 +++++++++++++ docker/sandbox/services/lambda-nodejs.yml | 9 +++++++++ docker/sandbox/volumes.yml | 3 +++ 5 files changed, 35 insertions(+) create mode 100644 docker/sandbox/networks.yml create mode 100755 docker/sandbox/serve-detach.sh create mode 100644 docker/sandbox/services/lambda-nodejs.yml create mode 100644 docker/sandbox/volumes.yml diff --git a/apps/sandbox/lambda-nodejs/project.json b/apps/sandbox/lambda-nodejs/project.json index b7b202812..e136b63ad 100644 --- a/apps/sandbox/lambda-nodejs/project.json +++ b/apps/sandbox/lambda-nodejs/project.json @@ -9,6 +9,12 @@ "command": "sam build", "cwd": "{projectRoot}" } + }, + "serve-detach": { + "executor": "nx:run-commands", + "options": { + "command": "docker/sandbox/serve-detach.sh {projectName}" + } } }, "tags": ["language:javascript"] diff --git a/docker/sandbox/networks.yml b/docker/sandbox/networks.yml new file mode 100644 index 000000000..af674ad9c --- /dev/null +++ b/docker/sandbox/networks.yml @@ -0,0 +1,4 @@ +networks: + sandbox: + name: sandbox + driver: bridge diff --git a/docker/sandbox/serve-detach.sh b/docker/sandbox/serve-detach.sh new file mode 100755 index 000000000..a6bfcfd5f --- /dev/null +++ b/docker/sandbox/serve-detach.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +args=( + # List of services in alphanumeric order + --file docker/sandbox/services/lambda-nodejs.yml + + --file docker/sandbox/networks.yml + --file docker/sandbox/volumes.yml + + up $1 --detach --remove-orphans +) + +docker compose "${args[@]}" diff --git a/docker/sandbox/services/lambda-nodejs.yml b/docker/sandbox/services/lambda-nodejs.yml new file mode 100644 index 000000000..b3ae566dd --- /dev/null +++ b/docker/sandbox/services/lambda-nodejs.yml @@ -0,0 +1,9 @@ +services: + sandbox-lambda-nodejs: + image: ghcr.io/sage-bionetworks/sandbox-lambda-nodejs:${SANDBOX_VERSION:-local} + container_name: sandbox-lambda-nodejs + restart: always + networks: + - sandbox + ports: + - '9000:8080' diff --git a/docker/sandbox/volumes.yml b/docker/sandbox/volumes.yml new file mode 100644 index 000000000..69f2f52d5 --- /dev/null +++ b/docker/sandbox/volumes.yml @@ -0,0 +1,3 @@ +# volumes: +# iatlas-postgres-data: +# name: iatlas-postgres-data From 033c0df2abf501977801bf3898675fca3a9da73c Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 21:47:16 +0000 Subject: [PATCH 11/15] add task `serve` --- apps/sandbox/lambda-nodejs/project.json | 6 ++++++ docker/sandbox/serve-detach.sh | 0 docker/sandbox/serve.sh | 13 +++++++++++++ 3 files changed, 19 insertions(+) mode change 100755 => 100644 docker/sandbox/serve-detach.sh create mode 100755 docker/sandbox/serve.sh diff --git a/apps/sandbox/lambda-nodejs/project.json b/apps/sandbox/lambda-nodejs/project.json index e136b63ad..45cdfb229 100644 --- a/apps/sandbox/lambda-nodejs/project.json +++ b/apps/sandbox/lambda-nodejs/project.json @@ -10,6 +10,12 @@ "cwd": "{projectRoot}" } }, + "serve": { + "executor": "nx:run-commands", + "options": { + "command": "docker/sandbox/serve.sh {projectName}" + } + }, "serve-detach": { "executor": "nx:run-commands", "options": { diff --git a/docker/sandbox/serve-detach.sh b/docker/sandbox/serve-detach.sh old mode 100755 new mode 100644 diff --git a/docker/sandbox/serve.sh b/docker/sandbox/serve.sh new file mode 100755 index 000000000..d74c8d446 --- /dev/null +++ b/docker/sandbox/serve.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +args=( + # List of services in alphanumeric order + --file docker/sandbox/services/lambda-nodejs.yml + + --file docker/sandbox/networks.yml + --file docker/sandbox/volumes.yml + + up $1 --remove-orphans +) + +docker compose "${args[@]}" From 3c29daa75296267a1401b752e9c00cfae83c5741 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 22:01:11 +0000 Subject: [PATCH 12/15] document the serve tasks --- apps/sandbox/lambda-nodejs/README.md | 16 ++++++++++++++++ docker/sandbox/serve-detach.sh | 0 2 files changed, 16 insertions(+) mode change 100644 => 100755 docker/sandbox/serve-detach.sh diff --git a/apps/sandbox/lambda-nodejs/README.md b/apps/sandbox/lambda-nodejs/README.md index 9f253dd5b..25c2b7063 100644 --- a/apps/sandbox/lambda-nodejs/README.md +++ b/apps/sandbox/lambda-nodejs/README.md @@ -12,6 +12,22 @@ nx build sandbox-lambda-nodejs nx build-image sandbox-lambda-nodejs ``` +## Start the Lambda function locally with Docker Compose + +Starts the Lambda function in the foreground, allowing you to view logs and interact with it +directly. + +```console +nx serve sandbox-lambda-nodejs +``` + +Starts the Lambda function in detached mode, running it in the background. This is useful if you +want to continue using the terminal for other tasks while the function runs. + +```console +nx serve-detach sandbox-lambda-nodejs +``` + # lambda-nodejs This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. diff --git a/docker/sandbox/serve-detach.sh b/docker/sandbox/serve-detach.sh old mode 100644 new mode 100755 From 69c70c75a6c8045b8fd91e7c018d48b0cc52ceb9 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 22:43:04 +0000 Subject: [PATCH 13/15] document the invoke task --- apps/sandbox/lambda-nodejs/README.md | 16 ++++++++++++++++ apps/sandbox/lambda-nodejs/events/event.json | 2 +- apps/sandbox/lambda-nodejs/project.json | 7 +++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/apps/sandbox/lambda-nodejs/README.md b/apps/sandbox/lambda-nodejs/README.md index 25c2b7063..52965ed75 100644 --- a/apps/sandbox/lambda-nodejs/README.md +++ b/apps/sandbox/lambda-nodejs/README.md @@ -28,6 +28,22 @@ want to continue using the terminal for other tasks while the function runs. nx serve-detach sandbox-lambda-nodejs ``` +## Invoke the Lambda function locally + +To invoke the Lambda function after starting it locally, use the following command: + +```console +nx run sandbox-lambda-nodejs:invoke --event +``` + +Replace `` with the path to your JSON file containing the event payload relative +to the location of the project folder. For example, if your event payload is stored in a file +located at `events/event.json` relative to the project folder: + +```console +nx run sandbox-lambda-nodejs:invoke --event events/event.json +``` + # lambda-nodejs This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. diff --git a/apps/sandbox/lambda-nodejs/events/event.json b/apps/sandbox/lambda-nodejs/events/event.json index 070ad8e01..95791046f 100644 --- a/apps/sandbox/lambda-nodejs/events/event.json +++ b/apps/sandbox/lambda-nodejs/events/event.json @@ -1,5 +1,5 @@ { - "body": "{\"message\": \"hello world\"}", + "body": "{\"message\": \"hello world 2\"}", "resource": "/{proxy+}", "path": "/path/to/resource", "httpMethod": "POST", diff --git a/apps/sandbox/lambda-nodejs/project.json b/apps/sandbox/lambda-nodejs/project.json index 45cdfb229..deb76abd7 100644 --- a/apps/sandbox/lambda-nodejs/project.json +++ b/apps/sandbox/lambda-nodejs/project.json @@ -21,6 +21,13 @@ "options": { "command": "docker/sandbox/serve-detach.sh {projectName}" } + }, + "invoke": { + "executor": "nx:run-commands", + "options": { + "command": "curl -X POST 'http://localhost:9000/2015-03-31/functions/function/invocations' --data @{args.event}", + "cwd": "{projectRoot}" + } } }, "tags": ["language:javascript"] From 7caa3d1d67c16dbb2c5a7f381f62bafc57c05a6b Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Sun, 1 Dec 2024 22:52:49 +0000 Subject: [PATCH 14/15] remove generated README content --- apps/sandbox/lambda-nodejs/README.md | 150 --------------------------- 1 file changed, 150 deletions(-) diff --git a/apps/sandbox/lambda-nodejs/README.md b/apps/sandbox/lambda-nodejs/README.md index 52965ed75..232de6fd0 100644 --- a/apps/sandbox/lambda-nodejs/README.md +++ b/apps/sandbox/lambda-nodejs/README.md @@ -43,153 +43,3 @@ located at `events/event.json` relative to the project folder: ```console nx run sandbox-lambda-nodejs:invoke --event events/event.json ``` - -# lambda-nodejs - -This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. - -- hello-world - Code for the application's Lambda function and Project Dockerfile. -- events - Invocation events that you can use to invoke the function. -- hello-world/tests - Unit tests for the application code. -- template.yaml - A template that defines the application's AWS resources. - -The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. - -## Deploy the sample application - -The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. - -To use the SAM CLI, you need the following tools. - -- Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) -- SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) - -You may need the following for local testing. - -- Node.js - [Install Node.js 20](https://nodejs.org/en/), including the NPM package management tool. - -To build and deploy your application for the first time, run the following in your shell: - -```bash -sam build -sam deploy --guided -``` - -The first command will build a docker image from a Dockerfile and then the source of your application inside the Docker image. The second command will package and deploy your application to AWS, with a series of prompts: - -- **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. -- **AWS Region**: The AWS region you want to deploy your app to. -- **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. -- **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. -- **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. - -You can find your API Gateway Endpoint URL in the output values displayed after deployment. - -## Use the SAM CLI to build and test locally - -Build your application with the `sam build` command. - -```bash -lambda-nodejs$ sam build -``` - -The SAM CLI builds a docker image from a Dockerfile and then installs dependencies defined in `hello-world/package.json` inside the docker image. The processed template file is saved in the `.aws-sam/build` folder. - -- **Note**: The Dockerfile included in this sample application uses `npm install` by default. If you are building your code for production, you can modify it to use `npm ci` instead. - -Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. - -Run functions locally and invoke them with the `sam local invoke` command. - -```bash -lambda-nodejs$ sam local invoke HelloWorldFunction --event events/event.json -``` - -The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. - -```bash -lambda-nodejs$ sam local start-api -lambda-nodejs$ curl http://localhost:3000/ -``` - -The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. - -```yaml -Events: - HelloWorld: - Type: Api - Properties: - Path: /hello - Method: get -``` - -## Add a resource to your application - -The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. - -## Fetch, tail, and filter Lambda function logs - -To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. - -`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. - -```bash -lambda-nodejs$ sam logs -n HelloWorldFunction --stack-name lambda-nodejs --tail -``` - -You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). - -## Unit tests - -Tests are defined in the `hello-world/tests` folder in this project. Use NPM to install the [Mocha test framework](https://mochajs.org/) and run unit tests from your local machine. - -```bash -lambda-nodejs$ cd hello-world -hello-world$ npm install -hello-world$ npm run test -``` - -## Cleanup - -To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: - -```bash -sam delete --stack-name lambda-nodejs -``` - -## Resources - -See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. - -Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) - -## Login with the AWS CLI - -> [!NOTE] -> This and the following sections assume that you are working in the AWS account -> `org-sagebase-itsandbox` with the role `Developer` and that you are deploying -> to the `us-east-1` region. If this assumption is correct, you should be able -> to simply copy-paste the following commands, otherwise adapting the -> configuration should be straightforward. - -Create the config file if it doesn't exist yet. - -```console -mkdir ~/.aws && touch ~/.aws/config -``` - -As a Developer working in Sage IT Sandbox AWS account, add the following profile to the config file. - -```ini -[profile profile-name] -sso_start_url = https://d-906769aa66.awsapps.com/start -sso_region = us-east-1 -sso_account_id = XXXXXXXXX -sso_role_name = Developer -``` - -Login with the AWS CLI: - -```console -aws --profile profile-name sso login -``` From 447e4d9f5b2f49604a28e4af1a98b27a91d59cde Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Tue, 3 Dec 2024 03:19:08 +0000 Subject: [PATCH 15/15] Fix pnpm workspaces --- pnpm-workspace.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 53490b45a..3c04f385b 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -3,7 +3,7 @@ packages: - 'libs/**' - '!apps/openchallenges/infra' - '!apps/openchallenges/infra/old' - - '!apps/sandbox/lambda-nodejs20.x' + - '!apps/sandbox/lambda-nodejs' # prevent the creation of libs/**/api-client-angular/node_modules when running `workspace-install` # that break the apps and tests - '!libs/**/api-client-angular'