Using Aws Lambda

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CLI

account and installing the AWS


1.Setting up an AWS
account
Step 1.1: Sign up for an AWS

Sign up for an AWS account at https://aws.tmazon.com/ if you


already. haven't
Line Interface)
Step 1.2: Install the AWS CLI (Command

Follow the official instructions to install the AWS CLIon your


system: https://aws.amazon.com/cli/.

Step 1.3: Configure the AWSCLI

Run the command aws configure in your terminal and enter your access
key, secret key, default region, and output format when prompted.
2. Creating a Lambda function
Step 2.1: Create a project directory

Create a new directory for your Lambda function on your local system and
navigate to it in the terminal.

Step 2.2: Write the Lambda functioncode

In the project directory,create afile named index. js withthe following


code:

exports.handler = async (event) => {


Const response = {
statusCode: 200,
oody: JSON. Stringify('Hello from Lambda!'),
return response;

This simple Lambdafunction returns a "Hello from Lambda!"


code. message with: a20 status
3. Deploying the Lambdafunction
Step 3.1: Package the Lambda function

In the terminal, navigate to your project


directory and create a zir, fl.
containing index. js. You can use the following command on
macOS:
Linux or
zip function.zip index.js

Step 3.2: Create an IAM role for the


Lambda function
In the AwS
Management)Management
service. Console, navigate to the IAM (Identity and Access
Click on Roles" in the left
sidebar and then click on "Create role."
Select "ILambda" as the service
that will use this role and
Permissions." click "Next:
Attach
the"AWSLambdaBasicExecutioReview."
Add any desired tags and nRole" policy
click "Next:
and click "Next: Tags."

Enter a name for the role


(e.g.,
role." "lambda basic execution")and click
"Create
Step 3.3: Deploy the
Lambda function using the AWS CLI
Run the following
the ARN (Amazon command in the terminal,
Resource Name) of the IAMreplacing Yo IR RCLE ARN
role vou Created
with
in Step 3.2:
aws lambda create-function \
--function-name myLambdaFunction \
--runtime nodejsl4.x \
--role YOUR ROLE ARN \
--handler index. handler \
--zip-file fileb:// funct ion.zip

4. Testing the Lambda function


Step 4.1: Test the Lambda function using the AWS CLI

Run the following command to test your Lambda function:

aws lambda invoke \


--function-name myLambda Function \
--payload ' } \
response.json

The response. jsonfile should contain the response from your Lambdafunction.
which should look like this:

Che"': 200,
"\"iello írom Iambda!\

5. Creating an API Gateway to expose the Lambda function

Step 5.1: Create a REST API using the AWS Management Console

Management Console.
Navigate tothe APIGateway service in the AWS

Click on "Create API" and then select "REST API."


your Arl.
Choose "New API" and provide a name and description for
Click "Create API."

Step 5.2: Create aresource and method for the API

Inthe "Resources" section of your newly created API, click on "Create.


Resource."

Provide aname for theresource (e.g., "HelloLambda") and click eoa.


Resource."
With the new resource selected, click on "Create Method" and cho0se ":Er"
from the dropdown menu.
Function."
In the "Integration type" section, select "Lambda
function.
Choose the same AWS region where you deployed your Lambda
of your Lambda function
In the "Lambda Function" field, enter the name
(e.g.,"myLambdaFunction").
confirmation popup.
Click "Save" and then click OK" in the

Step 5-3: Deploy the API

menu, click on "Deploy API."


In the Actions" dropdown
"New Stage" and provide a name for the stage (e.g., "prod").
Select
Click Deploy."

API
Step 5.4:Test the
URL" for vour API. You can use this
"Invoke
you will see an using a toollike curlor Postman.
After deployment,Lambda function
URL to test your

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy