Integrating AWS Lambda, API Gateway, and SNS?service

Integrating AWS Lambda, API Gateway, and SNS?service

Hey Folks, I have created an architecture such that when a user clicks on the link(provided by AWS API Gateway), the IP address of the user is sent to a user’s email address, later we can also include additional functionalities in it.

Services of AWS used for implementing the architecture:

Let us now understand how to configure the services:

I created a Lambda Function in Python that fetches the Client's IP Address and eMails it to a user.

  • From the home page of AWS, navigate to the Lambda service page. Click on Create Function button.
  • Fill up the information as-per below screenshot and click on "Create Function" :

No alt text provided for this image

  • Add the below-mentioned code:

import json
import boto3


def ipAddressFinder(event, context):
? ? print("body: " ,event)
? ? MY_SNS_TOPIC_ARN = 'Your SNS topic ARN(aailable on home screen of your SNS Topic)'
? ? sns_client = boto3.client('sns')
? ? sns_client.publish(
? ? ? ? ArnTopic = MY_SNS_TOPIC_ARN,
? ? ? ? Subject = 'New Execution of Lambda Function',
? ? ? ? Message = 'Your Lambda Function ipAddressFinder has been invoked\n' +
? ? ? ? ? ? ? ? ? 'The IP Address of Client is: ' + event['headers']['X-Forwarded-For'] + '\n' +
? ? ? ? ? ? ? ? ? 'Please Review the Activity\n' +
? ? ? ? ? ? ? ? ? 'Thanks & Regards'
? ? )
? ??
? ? return {
? ? ? ? "statusCode": 200,
? ? ? ? "body": "Function Executed Successfully"
? ? }        

  • As we have renamed the function so now we will have to configure lambda to run this function whenever the Lambda Function is called.
  • To implement the above point, click on the Edit button present under "Runtime Settings" tab on the same page. Inside the Handler tab, replace the function name present after "." with your function name.

No alt text provided for this image

Now our Lambda function is ready and we will be configuring the other services.


I created an SNS topic that performs the functionality to send emails to an email address.

  • From the home page of AWS, navigate to SNS service page. Enter the Topic Name and click on "Next Steps" button.
  • Fill up the information as-per below screenshot and click on "Create Topic":

No alt text provided for this image

Once the Topic is created, we have to create a Subscription, follow the below steps to create a Subscription:

  • On the Topic page, click on Create Subscription button, in Protocol select Email and then in Endpoint enter email address where you want to receive the IPAddress email and click on Create Subscription:

No alt text provided for this image

A confirmation email would be sent, click on link provided in the email to confirm the receiving of emails for the Subscription

I created an API Gateway having an endpoint as the Lambda Function.

Below are the steps to create and configure the Lambda Function:

  • Navigate to the API Gateway service page. Click on Create API button, select Rest API, and fill info as per below:

No alt text provided for this image

Now our API Gateway is created, now we have to create an endpoint that points to the Lambda function created previously.

Open the created API Gateway and add an endpoint to it using the below steps:

  • Move to the Resources tab, click on Actions, and then on Create Resource:

No alt text provided for this image

  • Enter the resource Name and Path, click on Create Resource

No alt text provided for this image

  • Now select the Resource created and from Actions select Create Method, select Lambda Function option and select the function created previously:

No alt text provided for this image


No alt text provided for this image

For every request of the client, by default API Gateway will be proxying the request to the Lambda Function, but as our use case is to fetch the IP of client, in the Integration Request phase, we will have to select Use Lambda Proxy integration(reference) option and then Save it.

Now the requests will directly be sent to the backend Lambda function without the interference of API gateway so that the Lambda function gets all detail of the client in the Request Header part and then extracts the IP Address of Client to perform further functionality.

  • To implement our API Gateway, we will have to select Deploy API from Actions and fill in details and click on the Deploy button.

No alt text provided for this image

Now our integration part is completed.

Testing the integration

  • Move to the Stage tab in API gateway and click on stage under which the Gateway was deployed.
  • A URL would be provided labeled as Invoke URL(this is the URL using which we can access API gateway)

No alt text provided for this image

  • Append the URL provided with the Resource created:

No alt text provided for this image

  • As per the integration, an email would be received on the email ID mentioned in the SNS Subscription and as per the code written in the Lambda function.

No alt text provided for this image
Kowshik G Y

Digital Marketing Associate | Passionate About Digital Transformation & Audience Engagement

1 年

Reserve your spot for our upcoming AWS Discovery Day workshop on securing your AWS cloud. Register today.? Registration Link: https://lnkd.in/g4Vwx-pn

  • 该图片无替代文字
回复
Rizwana Shah

Account Manager at Nife

1 年

Thankes for sharing

RamyaBhargavi Jella

@ ARTH 2.0| RHEL8|AWS Certified Solutions Architect |DevOps| Git | GitHub| Open CV|Freelancing

2 年

Great work

Shrishti Kapoor

Accenture | 4 x RedHat Certified | Aviatrix Certified Engineer | AI-900 | Technical Trainer | IIEC-DOT Volunteer | Technical Research Writer Enthusiast..!! ??DevOps ??Cloud-AWS, Azure ??ML/AI/DL

2 年

Good work ?

Himanshu Pant

Lead Solutions Architect @Elevation Services

2 年

Thanks for sharing!

要查看或添加评论,请登录

Samarth Pant的更多文章

社区洞察

其他会员也浏览了