Integrating AWS Lambda, API Gateway, and SNS?service
Samarth Pant
Information Technology Analyst @ TCS | Contextual Master | OpenTofu | Terraform |
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.
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"
? ? }
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.
Once the Topic is created, we have to create a Subscription, follow the below steps to create a Subscription:
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:
领英推荐
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:
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.
Now our integration part is completed.
Testing the integration
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
Account Manager at Nife
1 年Thankes for sharing
@ ARTH 2.0| RHEL8|AWS Certified Solutions Architect |DevOps| Git | GitHub| Open CV|Freelancing
2 年Great work
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 ?
Lead Solutions Architect @Elevation Services
2 年Thanks for sharing!