Deploy a Function to Lambda Using the Node.js AWS SDK
In this post, we will give you information about Deploy a Function to Lambda Using the Node.js AWS SDK – onlinecode. Here we will give you detail about Deploy a Function to Lambda Using the Node.js AWS SDK – onlinecode And how to use it also give you a demo for it if it is necessary.
The AWS API has an endpoint for deploying a function on Lambda. With a little bit of work, you can upload a Lambda function using the AWS SDK for Node.js. Here’s how you can upload and run a Lambda function in 3 steps:
1. Upload the Function Bundle to S3
Unfortunately, the AWS API requires you to store your bundled Lambda function as a .zip
file on S3, and that S3 bucket needs to be in
the same AWS region as your Lambda function.
You can read more about uploading objects to S3 in Node.js here. Here’s the abridged version. First, suppose you have a simple test.js
file that contains a handler
function:
exports.handler = async function(event, context) {
return { statusCode: 200, body: 'Hello, World' };
};
Lambda will execute this function for you and return “Hello World”. But first, you need to archive this test.js
file into a .zip
file
and upload it to S3. To bundle a zip file, you can use the adm-zip package on npm:
const AdmZip = require('adm-zip');
const AWS = require('aws-sdk');
const file = new AdmZip();
file.addFile('test.js', Buffer.from('
exports.handler = async function(event, context) {
return { statusCode: 200, body: 'Hello, World' };
};
'));
file.writeZip('./test.zip');
// Make sure the configs are set!
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1'
});
const s3 = new AWS.S3();
await new Promise((resolve, reject) => {
s3.upload({
Bucket: awsBucket, // Make this your AWS bucket
Body: fs.createReadStream('./test.zip'),
Key: 'test.zip'
}, (err, data) => err == null ? resolve(data) : reject(err));
});
2. Create a Lambda Function
Now that the file is on S3, you can create a Lambda function and invoke it using the AWS.Lambda()
helper:
const AWS = require('aws-sdk');
const promisify = require('util').promisify;
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1'
});
const lambda = new AWS.Lambda();
// Actually create the function with the given name and runtime.
const opts = {
FunctionName: 'nodetest',
Runtime: 'nodejs12.x',
// Whatever role, doesn't matter
Role: 'add actual role that starts with 'arn:aws:iam::' here',
// 'test' is for 'test.js', and 'handler' is for 'exports.handler'.
Handler: 'test.handler',
Code: {
'S3Bucket': awsBucket,
'S3Key': 'test.zip'
}
};
const fn = await promisify(lambda.createFunction).call(lambda, opts);
functionArn = fn.FunctionArn; // The "id" of the lambda function
// Let API Gateway call this Lambda function
await promisify(lambda.addPermission).call(lambda, {
FunctionName: 'nodetest',
StatementId: 'doesntmatter',
Action: 'lambda:InvokeFunction',
Principal: 'apigateway.amazonaws.com'
});
const res = await promisify(lambda.invoke).call(lambda, {
FunctionName: 'nodetest'
});
res.Payload; // '{"statusCode":200,"body":"Hello, World"}'
For convenience, the above code uses Node.js’ util.promisify()
helper, since the AWS SDK doesn’t currently support promises.
Learn more about util.promisify()
here.
3. Create an API Gateway to Access the Function via HTTP
So now you have a Lambda function that you can invoke via the AWS
SDK. But what about invoking it via HTTP? That’s what you need the
AWS API Gateway API for. You need to create a new REST API and add an integration to it.
Step by step, you need to:
- Create a new REST API
- Add a resource to the REST API
- Add a
GET
method to the resource - Hook up the
GET
method to call Lambda
Here’s the full script:
this.timeout(5000);
const AWS = require('aws-sdk');
const promisify = require('util').promisify;
AWS.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
region: 'us-east-1'
});
const gateway = new AWS.APIGateway();
// Create a new API
const api = await promisify(gateway.createRestApi).call(gateway, {
name: 'My Test API'
});
const restApiId = api.id;
// Create a new endpoint (resource) at '/test/
const resources = await promisify(gateway.getResources).call(gateway, { restApiId });
const resource = await promisify(gateway.createResource).call(gateway, {
restApiId,
// Parent resource is the "root" resource
parentId: resources.items[0].id,
pathPart: 'test'
});
await promisify(gateway.putMethod).call(gateway, {
restApiId,
resourceId: resource.id,
httpMethod: 'GET',
authorizationType: 'NONE'
});
// Configure the endpoint to use the Lambda function
await promisify(gateway.putIntegration).call(gateway, {
restApiId,
resourceId: resource.id,
httpMethod: 'GET',
integrationHttpMethod: 'POST',
type: 'AWS_PROXY',
uri: 'arn:aws:apigateway:us-east-1:lambda:path//2015-03-31/functions/${functionArn}/invocations'
});
await promisify(gateway.createDeployment).call(gateway, { restApiId, stageName: 'prod' });
await promisify(gateway.putMethodResponse).call(gateway, {
restApiId,
resourceId: resource.id,
httpMethod: 'GET',
statusCode: '200'
});
await promisify(gateway.putIntegrationResponse).call(gateway, {
restApiId,
resourceId: resource.id,
httpMethod: 'GET',
statusCode: '200'
});
// Now call the function using Axios!
const axios = require('axios');
const res = await axios.get('https://${api.id}.execute-api.us-east-1.amazonaws.com/prod/test');
res.data; // 'Hello, World'
More Node Tutorials
- Working with UUID in Node
- Using bcrypt-js to Hash Passwords in JavaScript
- Working with the Node.js assert Library
- Modify Authorized redirect_uris For Google OAuth
- Sleep in NodeJS
- Convert HTML to Pug
- Convert Pug to HTML
Node.js is an open-source, cross-platform JavaScript runtime environment. Node.js allows you to run JavaScript code outside of a web browser. This makes it possible to create a wide variety of applications, including web servers, real-time chat applications, and data processing pipelines.
Node.js is built on top of the V8 JavaScript engine, which is also used by Google Chrome. This means that Node.js applications can take advantage of the same performance and features as Chrome.
Node.js is a popular choice for building web servers. This is because Node.js is very efficient at handling concurrent requests. Node.js uses a non-blocking event loop to handle requests, which means that it can handle multiple requests at the same time without slowing down.
Node.js is also a good choice for building real-time chat applications. This is because Node.js is able to push data to clients in real time without the need for polling. This makes it possible to create a smooth and responsive user experience.
Node.js is a powerful tool that can be used to create a wide variety of applications. If you are looking for a JavaScript runtime environment that is fast, scalable, and efficient, then Node.js is a great choice.
Here are some of the benefits of using Node.js:
- Performance: Node.js is very efficient at handling concurrent requests. This makes it a good choice for building web servers and real-time chat applications.
- Scalability: Node.js is designed to scale horizontally. This means that you can add more nodes to your cluster to handle more traffic.
- Efficiency: Node.js uses a non-blocking event loop to handle requests. This makes it possible to handle multiple requests at the same time without slowing down.
- Community: Node.js has a large and active community. This means that there are many resources available to help you learn and use Node.js.
Hope this code and post will helped you for implement Deploy a Function to Lambda Using the Node.js AWS SDK – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs