DEV Community

Lasantha Sanjeewa Silva
Lasantha Sanjeewa Silva

Posted on • Updated on

Deploying AWS Lambda Function Using AWS CLI

Install AWS CLI and checking whether the AWS CLI version.

https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

Checking installation success.

Alt Text

After that configure AWS CLI using Access Key ID and Secret Access Key. You can get Access Key ID and Secret Access Key go to AWS Management Console and click user name and go to My Security Credentials and click Access keys. If you haven't access the key you can click create a new access key.

Alt Text

Create a folder and inside this folder create trust-policy.json file.

trust-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

After that create IAM Role Using the following CLI command.

aws iam create-role --role-name basic-lambda-role --assume-role-policy-document file://trust-policy.json

You can get IAM role details and copy ARN details using the following CLI command.

aws iam get-role --role-name basic-lambda-role

Create basics-lambda.js file and store it previously created folder and zip this file.

basic-lambda.js
exports.handler = async function (event) {
const { numberA, numberB } = event;

return {
sumResult: numberA + numberB,
};
};

ZIP this file like the following.
Ex : basic-lambda-role.zip

After that create a lambda function using following CLI command.

aws lambda create-function \
--function-name basic-lambda \
--runtime nodejs14.x \
--zip-file fileb://basic-lambda-role.zip \
--handler basic-lambda.handler \
--role arn:aws:iam::932747549174:role/basic-lambda-role

You can see following output.

Alt Text

Go to AWS Management Console and search lambda and go to this service. After you can see created lambda function.

Alt Text

Go to your code part section and click the Test button. You can configure test event. You want to give the Event Name and values.

Alt Text

Finally, click again test button. You can see your function is working.

Alt Text

You can delete the lambda function using AWS Management Console or CLI.

Alt Text

CLI command for remove lambda function

aws lambda delete-function --function-name basic-lambda

Thanks for reading the Article.
Reference - https://gitlab.com/lasantha96/aws-lambda-function-using-aws-cli.git

Top comments (0)