Thanks to serverless, I deployed easily AWS Lambda function my own AWS account. Currently, I wanna to deploy my lambda function to my customers AWS account automatically. I will provide them with a UI that they can enter their AWS credentials (Key + Secret). After that, it will call my API to automatically deploy my solution to their AWS accounts. May I ask that how I can programmatically deploy lambda function using serverless?
Ideally I would like to be able to run a command from outside the directory of the application being deployed.
This way we could use a node command execution from javascript code using node’s child process.
The problem if I understood correctly, is that currently you can only run serverless deploy if you are in the root of the directory of the application you want to deploy.
An idea might be to use the existing command + flag deploy --config to point to a config file somewhere else in the file system.
In this case we could programmatically deploy an application.
Start by creating a Serverless Framework project if you haven’t already. You can create a new service using the serverless create command and select the desired runtime (e.g., Node.js, Python, etc.) and service template (e.g., AWS Node.js, AWS Python, etc.). Ensure that you have AWS credentials configured on your local machine or within your deployment environment. You can set these credentials using the AWS Command Line Interface (CLI) or by setting environment variables. These credentials should have the necessary permissions to create Lambda functions, IAM roles, and other resources.
Here’s an example of how you can programmatically deploy a Serverless service to a customer’s AWS account using Node.js and the Serverless Framework:
const { execSync } = require(‘child_process’);
// Set the AWS credentials for the Serverless Framework
execSync(sls config credentials --provider aws --key ${customerAwsAccessKey} --secret ${customerAwsSecretKey});
// Deploy the Serverless service
execSync(‘sls deploy’);