1

I had created the amazon sns client, provided the credential, set the region and create the publish message and use the publish method with it's attribute. it doesn't show any log or the exception and nothing happen on topic. Where i gone wrong? Thank you in advance.

Background


I had created lambda function using "implements RequestHandler {}". whenever error occur i want to push that error message to a desired topic basically trying to error handlig. Lambda function is working fine when there is no error and the publishing to topic is not used on the function it self. below code is applied only on error handle section. Thank you.

Here is my code:

String msg = "Message";
String ACCESS_KEY = "";
String SECRET_KEY = "";
BasicAWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);
AmazonSNSClient snsClient = new AmazonSNSClient(new AWSStaticCredentialsProvider(credentials));
snsClient.setRegion(Region.getRegion(Regions.MyRegion));
String topicArn = "topic arn";
PublishRequest publishRequest = new PublishRequest(topicArn, msg);
snsClient.publish(publishRequest);
jagwar
  • 31
  • 6
  • Try to include as much information as possible. It's understandable you wouldn't want to include your access key and secret, so including something like `` and `` in those strings makes it obvious that you included your key, but just obfuscated it for the question, and not that you just left these as empty strings which would obviously be a problem. Nothing should be secret about your Region, and I can find no reference in the docs to `Regions.MyRegion` – Jamie Starke Mar 28 '18 at 14:45
  • @JamieStarke Thank you for your comment. Regarding to Key's you understand correct and for regions too i take the same take technique to hide. It's understandable that regions is not that much private. I will take as reference for the next question. Thank you once again for your reply. – jagwar Mar 29 '18 at 05:51

3 Answers3

2

If your code is running in AWS Lambda, then credentials will automatically be provided. There is no need to use BasicAWSCredentials.

Simply call:

AmazonSNSClient snsClient = new AmazonSNSClient();

To discover the region in which your Lambda function is running, see: How can one determine the current region within an AWS Lambda function?

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • @ John Rotenstein, Thank you for your answer it make clear picture that i don't need the credential otherwise i was in confusion. Regarding the regions i use this "snsClient.setRegion(Region.getRegion(Regions.EU_CENTRAL_1));" I tried the simple way that is without credential stuffs. Still it doesn't publish to the respective topic. Any suggestions? Thank you once more. – jagwar Mar 29 '18 at 06:04
  • If it is not working, then you will need to look at the error that is reported. Logs are sent to CloudWatch Logs, but you first need to assign permissions to the Role used by the Lambda function so that it can access CloudWatch Logs (`AWSLambdaBasicExecutionRole`). – John Rotenstein Mar 29 '18 at 09:19
0

I managed to get this to work with the following code:

enter image description here

eldeo
  • 51
  • 5
0

Finally the problem is solved, Actually it was due to the networking issues

For detail what i done and issues here

For more detail look at this link Click here

jagwar
  • 31
  • 6