Steps to use TypeScript in aws lambda
Initiate aws lambda structure using
SAM stands for Serverless Application Model and it’s an open source framework for building serverless applications
(SAM github). We will use it for current example.
The following command will start initiating our lambda.
Settings are pretty straightforward, but just in case you need them:
As a result the following structure will be created;
As you can see we have
app.js file which contains actual code of initiated lambda function:
Step 1. Update package.json file
compile script and needed devDependencies (
@types/aws-lambda, @types/node, typescript) to
Step 2. Create tsconfig.json (TypeScript configuration file)
module- allows to use different modularization options. In this case
CommonJSis used because aws runtime environment understands it without additional settings see docs
moduleResolution- defines strategy to locate modules.
classicstrategy did not work, that’s why
nodeis used see docs
target- specifies which language features will stay in output files see docs
Step 3. Convert js files into ts files
Apart from changing extension we will also change implementation to start using types
Step 4. Update template.yaml file with new path to source code
Right now we need to let
NOTE: Be aware that
Handler property has to point to app.js file and function in it. If you named that function differently, you need to change this field correspondingly.
Deploy lambda and check it
SAM will need correct AWS credentials to execute deployment.
To check your lambda you can find your API gateway url in aws console or just check your output from
sam deploy command.
TypeScript usage in aws lambda is very simple, but brings a lot of benefits to our developer’s life.
In the next articles we will deploy TypeScript lambda using
Serverless framework and built in