手柄CloudWatch可以在Lambda的球衣容器资源中触发温暖的触发器

时间:2018-12-18 12:47:03

标签: amazon-web-services jersey aws-lambda amazon-cloudwatch

我们为用户资源提供的jersey lambda包含一些数据库连接,rds,dynamo等。

以前,(在Lambda中使用jersey之前),我们通过使用简单的选择查询对数据库执行ping操作来保持rds连接有效。变暖事件是从cloudwatch触发的,并通过检查输入流中子字符串的存在来在lambda中进行识别。

然后,这些调用在处理程序类上调用一个单独的方法,以进行预热并因此获得与所使用的处理程序类相同的数据库会话实例的访问权限。

在球衣上,我们使用代理处理程序'Key not valid for use in specified state. (System.Security)'JerseyLambdaContainerHandler.getAwsProxyHandler。 这意味着我无权访问处理程序类的类实例。

关于如何使会议保持活力的任何想法。我最初的想法是:

  1. 实例化资源类之外的会话,并将其传入资源,就像使用参数注入such as here
  2. 一样。
  3. 也许我们可以让云监视在专用于变暖事件的资源上​​调用特定路径。

有没有人做过类似的事情或对此有任何想法?

容器设置或多或少像here所述:

LambdaContainerHandler.proxyStream

资源看起来与下面类似,请注意数据库客户端。

    class StreamLambdaHandler : RequestStreamHandler {

        @Throws(IOException::class)
        override fun handleRequest(inputStream: InputStream, outputStream: OutputStream, context: Context) {
            handler!!.proxyStream(inputStream, outputStream, context)
        }

        companion object {
            private var handler: SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse>? = null

            init {
                try {
                    handler = SpringLambdaContainerHandler.getAwsProxyHandler(PetStoreSpringAppConfig::class.java)
                } catch (e: ContainerInitializationException) {
                    // if we fail here. We re-throw the exception to force another cold start
                    e.printStackTrace()
                    throw RuntimeException("Could not initialize Spring framework", e)
                }

            }
        }
    }

ping事件,在收到cloudwatch触发器时调用:

    @Path("/user")
    class TestUser(
        private val idpClient: AWSCognitoIdentityProvider = AWSCognitoIdentityProviderClientBuilder.defaultClient(),
        private val ddb: DynamoDB = DynamoDB(AmazonDynamoDBClientBuilder.defaultClient()),
        private val s3Client: AmazonS3 = AmazonS3ClientBuilder.defaultClient(),
        private val session: Session = getSession()
    ) {

        @POST
        @Produces(MediaType.APPLICATION_JSON)
        fun createUser(): CreateTestUserResponse {

            val (email, password) = try {
                TestUserUtilities.createAndVerifyUser(idpClient, session)
            } catch (e: Exception) {
                log.error("error creating user", e)
                throw ServerErrorException(
                    httpStatus = Response.Status.INTERNAL_SERVER_ERROR,
                    errorMessage = e.message ?: "unknown error"
                )
            }

            return CreateTestUserResponse(
                email = email,
            )
        }

    }

0 个答案:

没有答案