1
votes

I'm trying to publish a message to SNS topic from my java lambda function but function is timing out. I got the data i needed from the logic i wrote but it's timing out while sending the data as SNS message. I have given full access to my lambda function to publish SNS message. Here is the Code.

AmazonSNS client = AmazonSNSClientBuilder.defaultClient();
String resultIs = "Testing Lambda";
final PublishRequest publishRequest = new PublishRequest("my_SNS_Topic_ARN", resultIs, "Testing email");
logger.log(" publishing message ");
//it's timing out here
final PublishResult publishResponse = client.publish(publishRequest);

I also tried to build the SNS client like this but it did not work.

AmazonSNSClientBuilder builder = AmazonSNSClientBuilder.standard().withCredentials(new DefaultAWSCredentialsProviderChain());
builder.setRegion(System.getenv("Region"));
AmazonSNS amazonSNS = builder.build();

Do i need to provide credentials? is there any simple way to build the client and publish to SNS topic from lambda java function.

I tried this way too as suggested, it timed out.

AmazonSNS client = AmazonSNSClientBuilder.standard().build();
client.publish("arn:aws:sns:***", "Test","Data");

Exception after increasing the timer to 1 minute and using the code above.

tp.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:744)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:726)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:686)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:668)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:532)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:512)
at com.amazonaws.services.sns.AmazonSNSClient.doInvoke(AmazonSNSClient.java:2800)
at com.amazonaws.services.sns.AmazonSNSClient.invoke(AmazonSNSClient.java:2767)
at com.amazonaws.services.sns.AmazonSNSClient.invoke(AmazonSNSClient.java:2756)
at com.amazonaws.services.sns.AmazonSNSClient.executePublish(AmazonSNSClient.java:1974)
at com.amazonaws.services.sns.AmazonSNSClient.publish(AmazonSNSClient.java:1946)
at com.amazonaws.services.sns.AmazonSNSClient.publish(AmazonSNSClient.java:1991)
at jdbcsample.JDBCSample.getCurrentTime(JDBCSample.java:155)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to sns.us-east-1.amazonaws.com:443 [sns.us-east-1.amazonaws.com/52.46.136.210] failed: connect timed out
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.amazonaws.http.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:76)
at com.amazonaws.http.conn.$Proxy3.connect(Unknown Source)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1297)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1113)
... 18 more
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:368)
at com.amazonaws.http.conn.ssl.SdkTLSSocketFactory.connectSocket(SdkTLSSocketFactory.java:142)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
... 34 more

END RequestId: cc9a0689-2a17-4061-adab-10043366f2e6
REPORT RequestId: cc9a0689-2a17-4061-adab-10043366f2e6  Duration: 49021.54 ms    
Billed Duration: 49100 ms   Memory Size: 512 MB Max Memory Used: 139 MB Init 
Duration: 335.45 ms 
3
Is your Lambda in a VPC that might block access to SNS? If you extend the timeout of the Lambda do you get any further?stdunbar
i did gave the execution role used by lambda function full access to SNS policysur1480
It's not the role, it's the security group for your VPC. It doesn't look like it's allowing access to sns.us-east-1.amazonaws.com port 443.stdunbar
Thank you that was the problem, i resolved it.sur1480

3 Answers

2
votes

To create sns client in a lambda function, the line AmazonSNSClientBuilder.standard().build() is enough.

Ensure the lambda execution role has the attached policy

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sns:Publish"
            ],
            "Resource": "arn:aws:sns:*:*:*"
        }
    ]
}

Copy the right arn for your topic from SNS Console. If you haven't an Topic, create one. enter image description here

enter image description here

My working example is

AmazonSNS client = AmazonSNSClientBuilder.standard().build();
client.publish("arn:aws:sns:eu-central-1:0xxxxx:xxxxxx", "I'm a test message", "Test Message");
0
votes

If you haven't increased your Lambda function's timeout that may be worth looking at. The default is 3 seconds but it can be increased to up to 15 minutes.

0
votes

Have you ensured, that your function has not been deployed inside a VPC enter image description here

and your SNS Topic has right Access Policy? (See in SNS Console)

{
  "Version": "2008-10-17",
  "Id": "__default_policy_ID",
  "Statement": [
    {
      "Sid": "__default_statement_ID",
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": [
        "SNS:GetTopicAttributes",
        "SNS:SetTopicAttributes",
        "SNS:AddPermission",
        "SNS:RemovePermission",
        "SNS:DeleteTopic",
        "SNS:Subscribe",
        "SNS:ListSubscriptionsByTopic",
        "SNS:Publish",
        "SNS:Receive"
      ],
      "Resource": "arn:aws:sns:eu-central-1:0xxxxxxxxxxxx:xxxxxxxxxx",
      "Condition": {
        "StringEquals": {
          "AWS:SourceOwner": "xxxxxxxxxxx"
        }
      }
    }
  ]
}