简体   繁体   中英

Java Lambda Function stuck when publishing to AWS IoT

I am trying to publish to a AWS-IoT Topic from a Lambda written using Spring-boot. I am using the following dependencies,

<dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-core</artifactId>
        <version>1.11.710</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-iot</artifactId>
        <version>1.11.710</version>
    </dependency>

Here is the necessary configuration

@Bean
public AWSIotData awsIotDataClient() {
    return AWSIotDataClient.builder().standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("https://<my-account-specific>.iot.us-west-1.amazonaws.com", Regions.US_WEST_1.getName()))
            .withCredentials(DefaultAWSCredentialsProviderChain.getInstance())
            .build();
}

Here is the implementation of where I am publishing the data to aws-Iot topic.

    @Autowired
    private AWSIotData awsIotDataClient;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void publish(String topic, MyObject response) throws JsonProcessingException {
        logger.info("Publishing to topic: {} and payload : {}", topic, response);

PublishRequest publishRequest = new PublishRequest().withPayload(
                ByteBuffer.wrap(objectMapper.writeValueAsBytes(response))).withQos(1).withTopic(topic);

        PublishResult result = awsIotDataClient.publish(publishRequest);
        logger.info("Successfully published message to topic: {} and payload: {}, with result: ", topic, response, result);
    }

This implementation works ok when I am running locally. But when I run it in AWS-Lambda, it reaches till the publish and never gets a response for over 40 secs, and lambda eventually times out. The lambda role has AWSIoTFullAccess, AWSLambdaVPCAccessExecutionRole policies attached to it. So, any guidance here will be appreciated. Thank you.

Here is the outbound security group of my VPC. It also has an IGW attached to it. The IGW is also associated with the subnets that the lambda is running in. 在此处输入图片说明

A Lambda function in a VPC cannot use an Internet Gateway directly, because the Lambda function will never be assigned a public IP address. A Lambda function in a VPC must be in a private subnet of the VPC with a route to a NAT Gateway, in order to access the Internet. Since the AWS IoT service is not yet supported by VPC Endpoints, your only option is to use a NAT Gateway if the Lambda function must reside in a VPC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM