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
sns.us-east-1.amazonaws.com
port 443. – stdunbar