:<\/span><\/p>\n\n\n\nservice: express-serverless\nprovider:\n name: aws\n runtime: nodejs12.x\n region: eu-west-1\n stage: ${env:NODE_ENV}\n environment:\n NODE_ENV: ${env:NODE_ENV}\n iamRoleStatements:\n - Effect: 'Allow'\n Action:\n - 'secretsmanager:GetSecretValue'\n Resource:\n - '*'\n\npackage:\n exclude:\n - node_modules\/**\n - defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" defer\" src\/**\n\nlayers:\n nodeModules:\n path: layer\n compatibleRuntimes:\n - nodejs12.x\n\n\u2026..\n\nfunctions:\n app:\n handler: dist\/app.handler\n layers:\n - {Ref: NodeModulesLambdaLayer}\n events:\n - http:\n path: \/api\n method: ANY\n cors: true\n - http:\n path: \/api\/{proxy+}\n method: ANY\n cors: true\n vpc:\n securityGroupIds:\n - {Ref: LambdaSecurityGroup}\n subnetIds:\n - \"subnet-1\"\n - \"subnet-2\"\n - \"subnet-3\"\n\nplugins:\n - serverless-offline<\/pre>\n\n\n\nNote that in this file all the essential parts of the infrastructure are defined, such as the Cloud Provider on which we are going to deploy our Lambda function, its handler (the module we exported to the <\/span>app.ts<\/span><\/i> file) and the Lambda Layers to attach the function to.<\/span><\/p>\n\n\n\nWith this configuration, in fact, we will create a Lambda Layer containing all the node modules. By doing so, we considerably reduce the size of our function, obtaining performance advantages during its execution.<\/span><\/p>\n\n\n\nLet\u2019s go ahead: how is the Lambda function invoked? <\/span>Through the <\/b>http events<\/i><\/b> defined in the Serverless file! <\/b>This allows the creation of an API Gateway with which it will be possible to call our function.<\/span><\/p>\n\n\n\nThe resource with path <\/span>\/api\/{proxy+}<\/span><\/i> will be the one that will proxy the backend routes. We have thus created a single resource on the API Gateway side that allows us to invoke all the REST APIs.<\/span><\/p>\n\n\n\nLocal testing<\/h2>\n\n\n\n Not everyone knows that it is possible to test APIs without necessarily having to release them on AWS Lambda every time you make changes in the source code. That\u2019s the great advantage of the Node.js package called <\/span>serverless-offline<\/i><\/b>! <\/span><\/p>\n\n\n\nIt is a Serverless plugin that emulates AWS Lambda and API Gateway on your machine allowing you to speed up development activities.<\/span><\/p>\n\n\n\nBefore you can use it, however, you will need to locally create a <\/span>Postgres database<\/b> and run the Sequelize migrations:<\/span><\/p>\n\n\n\nFrom the root of the project we execute the command<\/p>\n\n\n\n
docker-compose up -d<\/pre>\n\n\n\nAnd then:<\/p>\n\n\n\n
npm run migrate-db-local<\/pre>\n\n\n\nAt this point, we can test APIs. Run the <\/span>npm run start<\/span><\/i> command to compile our TypeScript code in JavaScript and emulate the Lambda via the <\/span>serverless-offline<\/span><\/i> plugin.<\/span><\/p>\n\n\n\nAs soon as the command is executed, you should see the following output on the terminal:\n<\/p>\n\n\n\n <\/figure>\n\n\n\nAt this point we are ready to test the API with the tool we are most familiar with (Postman, Curl). For example, using the curl command we can execute the command from the terminal:<\/span>\n\n <\/p>\n\n\n\ncurl http:\/\/localhost:3000\/dev\/api\/book\/<\/em><\/p>\n\n\n\nDeploy <\/h2>\n\n\n\n Go ahead and take the last step: the application deploy.<\/p>\n\n\n\n
Running the <\/span>npm run deploy-dev<\/span><\/i> command will start the release process. The first time the following output will appear:<\/span><\/p>\n\n\n\n <\/figure>\n\n\n\nOn the AWS account a Cloudformation stack containing the resources we need has been generated.<\/p>\n\n\n\n
By accessing the console, we will find the just-created Lambda function among the list of Lambda functions available\n<\/p>\n\n\n\n <\/figure>\n\n\n\nBefore testing the APIs it is necessary to run the <\/span>Sequelize<\/span><\/i> migrations to create tables on the Aurora Serverless database.<\/span>\n\nBe sure you are able to connect to the database on AWS. To do this, it will be necessary to create a bastion machine on our AWS account and divert traffic from ours to the bastion. <\/span>\n\nUse the <\/span>sshuttle<\/span><\/i> command:<\/span>\n\n <\/p>\n\n\n\nsshuttle --dns -r ubuntu@EC2_BASTION_IP YOUR_VPC_CIDR --ssh-cmd 'ssh -i YOUR_PEM_KEY'<\/pre>\n\n\n\nAt this point,launch the migration by running the following command:<\/p>\n\n\n\n
npm run migrate-dev<\/pre>\n\n\n\nOnce the migrations on Aurora Serverless are completed, testing the APIs through API Gateway is the only thing left to do. \n<\/p>\n\n\n\n
The API Gateway endpoint has already been printed in the output of the npm run deploy-dev command. Let’s try it:<\/p>\n\n\n\n
curl https:\/\/sxfd74jes5.execute-api.eu-west-1.amazonaws.com\/dev\/api\/book\/<\/em><\/p>\n\n\n\nThe release process of our Serverless infrastructure on the AWS account is completed!<\/p>\n\n\n\n
Conclusion<\/h2>\n\n\n\n To conclude, in this article we discussed<\/span> how to create a Serverless application on AWS Lambda using Node.js as the runtime engine.<\/b><\/p>\n\n\n\n We decided to write the proposed project with TypeScript to have the advantage of using a transpiled, widely supported, and known language. <\/p>\n\n\n\n
The choice of using Node.js as a runtime engine allows for a <\/span>very limited Cold Start time<\/b><\/a> as JavaScript is directly interpreted by the underline engine. Moreover, thanks to the configuration used, the source code is separated from the dependencies – saved on Lambda Layer, instead – drastically reducing the size of our source and improving startup performance.<\/span><\/p>\n\n\n\nHappy with this solution? \ud83d\ude42 <\/p>\n\n\n\n
If you are interested in this topic, keep reading in our serverless section, or check out this resource on how to build a Node.js\/TypeScript REST API with Express.js<\/a>.<\/p>\n\n\n\nSee you in 15 days on #Proud2beCloud<\/a> for a new article!<\/p>\n","protected":false},"excerpt":{"rendered":"On Amazon Web Services the Serverless computational service par excellence remains Lambda, a must-have service when talking about this paradigm. […]<\/p>\n","protected":false},"author":14,"featured_media":1654,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[478],"tags":[256,396,357,264,388,386,390,398,268,394,392],"yoast_head":"\n
How to build a Serverless backend with TypeScript, Node.js, and AWS Lambda. - Proud2beCloud Blog<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n \n\t \n\t \n\t \n