- Create a Topic named “API-messages” and select the type as Standard.
Fig:1
- Now create a Subscription, Protocol shall be Email and give the e-mail ID. Confirmation mail will be sent to your mail ID.
- Create a Lambda function with the info:
Fig:2
- In the function go to code source and enter the code given below:
def lambda_handler(event, context):
return {
'statusCode': 200,
'headers': {},
'body': event['requestContext']['identity']['sourceIp'],
'isBase64Encoded': False
}
- Select the REST API public:
Fig 3
Fig 4
- Create a Resource and a method:
Fig 5
Fig 6 Create a method with GET and select ‘Mock’ and save it.
Fig 6 Select the ‘Integration Response’.
Fig 7 In the Integration Response, Select the Mapping Templates and add ‘application/json’. Type the desired message in the template.
- Create another Resource named ‘Lambda
Fig 8
Fig 9
Fig 10
Fig 11 Type API Gateway and select the API gateway.
- Select the default permissions shown in the list.
- Give the role name as “api-gw-sns-role” and create the role.
Fig 12 In order to give additional permissions, select the role and click on in-line policy.
- Select JSON and enter the following code:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "*"
}
]
}
- Give the policy a name and select Create Policy
- Create a resource named ‘sns’ in the API gateway that we created earlier. Change the type to POST . Enter all the details and select the appropriate options shown in the image below.
Fig 13
Fig 14 The Execution role has the ARN (Amazon Resource Name) of the IAM role that we created.
- In the Method Request option, add query strings. One is ‘Message’, other is ‘TopicArn’.
Fig 15
- In the integration request, create another query string. In the ‘Mapped from’ option, enter ‘method.request.querystring.Message’. This will give the info about the how the data is mapped between the method request and the integration request. So the same for ‘TopicArn’ - ‘method.request.querystring.TopicArn’
Fig 16
- Now deploy the API. Go the Actions dropdown and deploy the API. After there will be an invoke URL that will be generated.
Fig 17
- With the invoke URL we can access the resources that we want by adding the name of the resources at the end of the URL. For ex: add ‘/mock’ at the end of the URL. Below are the results.
Fig 18
Fig 19 Lambda function is now invoked which will display the IP of the requester in my case its my IP.
- We can test the sns API directly from the API console:
Fig 20 Click on the Test button to test this API.
- On in the Query strings, give the enter the parameters like - “TopicArn=arn:aws:sns:us-east-1:634211996823:API-messages&Message=I+have+mastered+Devops”. Here the TopiArn is the ARN of the SNS topic that we created and the Message is the message that we wish to send. Messages cant have spaces so instead ‘+’ is used.
Fig 21
Below message is the Notification received via the SNS to mail ID.
Fig 22
- All the services that were created can be deleted and its straight forward.
- There will be some roles created by the lambda function automatically which should be tracked and deleted.
- Clear the Cloud Watch logs as well.