If you ever needed to make an API call outside of your AWS account, you need to configure a few things on AWS. Below is the process to achieve that. I assume you already know how to configure your API gateway and are familiar with JavaScript and/or Python.
AWS Lambda
- AWS Lambda is an Amazon Web Services compute service that runs your back-end code in response to events and manages compute resources for you.
- The code running on AWS Lambda is called a Lambda function.
- You can write your code in the integrated Cloud9 editor within the AWS management console OR if your code requires custom libraries, you can create a .ZIP file containing all necessary components upload it as a codebase.
- You can also select from pre-built samples, or blueprints.
- Code can be written in JavaScript using Node.js, Python, .NET, Ruby, Go or Java.
- A Lambda function contains code, dependencies, and configuration information
- Configuration includes information like the handler that will receive the event, the AWS Identity and Access Management (IAM) role that AWS Lambda can use to execute the Lambda function
AWS Lambda can receive event data from multiple sources as shown below and perform various operations to provide required response.
To create a Lambda function
- Sign in to the Lambda console.
- Choose Create function.
- For Function name, enter
my-function
. - Choose Create function.
The example function returns a 200 response to clients, and the text Hello from Lambda!
.
The default Lambda function code should look similar to the following:
1 |
|
We can invoke Lambda Functions by integrating with following interfaces based on each use case:
- API Gateway : Amazon API Gateway is an AWS service for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs at any scale.
- AWS SDKs : Provides language-specific APIs (for e.g : NodeJS - aws-sdk, Python - boto3).
- AWS CloudWatch Events : CloudWatch Events can invoke lambda function asynchronously with an event document that wraps the event from its source.
Calling Lambda function using an API Gateway
Now we’ll create a Lambda Function in Python3.8 to connect to DynamoDB and fetch some records.
You can create a table from DynamoDB Console
To access DynamoDB, from our Lambda function, we need to add Permissions to the Lambda function Role.
The example function returns a 200 response to clients, with data from the mentioned table.
1 |
|
We will now create an HTTP API from API Gateway to receive requests from clients.
Steps
- Sign in to the API Gateway console
- Do one of the following:
- To create your first API, for HTTP API, choose Build.
- If you’ve created an API before, choose Create API, and then choose Build for HTTP API.
- For Integrations, choose Add integration.
- Choose Lambda.
- For Lambda function, enter
my-function
. - For API name, enter
my-http-api
. - Choose Next.
- Review the route that API Gateway creates for you, and then choose Next.
- Review the stage that API Gateway creates for you, and then choose Next.
- Choose Create.
Now you’ve created an HTTP API with a Lambda integration that’s ready to receive requests from clients.
To test your API
- Sign in to the API Gateway console
- Choose your API.
- Note your API’s invoke URL.
Copy your API’s invoke URL, and enter it in a web browser. Append the name of your Lambda function to your invoke URL to call your Lambda function. By default, the API Gateway console creates a route with the same name as your Lambda function, my-function.
The full URL should look likehttps://{account-id}.execute-api.{region-id}.amazonaws.com/my-function
.
Your browser sends aGET
request to the API.Verify your API’s response. You should see the response in above given format in your browser.
Calling Lambda function using an AWS SDK
Now we’ll create a Lambda Function in NodeJS to POST a request to any external API and will invoke this Lambda function from our Client side using AWS-SDK.
The example function returns a 200 response to clients, with data from the requested external API.
1 |
|
To invoke this Lambda function from Client side :
- boto3 - Python
1 |
|
- aws-sdk - NodeJS (CJS)
1 |
|
Hence, we can use this response from our Lambda function.
Calling Lambda function using AWS CloudWatch Event
Now we’ll create a Lambda Function in Python3.8 to Send an Email Alert Everyday except Sunday at 9:30 am IST (4:00am GMT).
To Schedule this, we will use Cloudwatch Events
Pre-requisites:
- Verified Source Email to Send Email From To Know More
- Permissions for Lambda Function to Send Email:
1 |
|
`🧮 send-email.py`
1 |
|
To create CloudWatch Event - Rule - to schedule Lambda function invocation
- Go to Cloudwatch console.
- Go to Event -> Rules
- Create Rule
- Add details as shown below:
Click on Configure details, your Lambda invocation will be scheduled.