diff --git a/plugins/out_cloudwatch_logs/cloudwatch_api.c b/plugins/out_cloudwatch_logs/cloudwatch_api.c index 56e91e6d557..79b11b75ac4 100644 --- a/plugins/out_cloudwatch_logs/cloudwatch_api.c +++ b/plugins/out_cloudwatch_logs/cloudwatch_api.c @@ -50,6 +50,8 @@ #define ERR_CODE_ALREADY_EXISTS "ResourceAlreadyExistsException" #define ERR_CODE_INVALID_SEQUENCE_TOKEN "InvalidSequenceTokenException" +#define ERR_CODE_NOT_FOUND "ResourceNotFoundException" + #define AMZN_REQUEST_ID_HEADER "x-amzn-RequestId" @@ -1242,6 +1244,9 @@ int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream) flb_sds_t body; flb_sds_t tmp; flb_sds_t error; + int ret; + +retry_create_stream: flb_plg_info(ctx->ins, "Creating log stream %s in log group %s", stream->name, ctx->log_group); @@ -1300,6 +1305,28 @@ int create_log_stream(struct flb_cloudwatch *ctx, struct log_stream *stream) flb_http_client_destroy(c); return 0; } + + if (strcmp(error, ERR_CODE_NOT_FOUND) == 0) { + flb_sds_destroy(body); + flb_sds_destroy(error); + flb_http_client_destroy(c); + + if (ctx->create_group == FLB_TRUE) { + flb_plg_info(ctx->ins, "Log Group %s not found. Will attempt to create it.", + ctx->log_group); + ret = create_log_group(ctx); + if (ret < 0) { + return -1; + } else { + /* retry stream creation */ + goto retry_create_stream; + } + } else { + flb_plg_error(ctx->ins, "Log Group %s not found and `auto_create_group` disabled.", + ctx->log_group); + } + return -1; + } /* some other error occurred; notify user */ flb_aws_print_error(c->resp.payload, c->resp.payload_size, "CreateLogStream", ctx->ins); diff --git a/plugins/out_cloudwatch_logs/cloudwatch_logs.c b/plugins/out_cloudwatch_logs/cloudwatch_logs.c index 2f31e99888e..b493aa1c593 100644 --- a/plugins/out_cloudwatch_logs/cloudwatch_logs.c +++ b/plugins/out_cloudwatch_logs/cloudwatch_logs.c @@ -390,13 +390,6 @@ static void cb_cloudwatch_flush(struct flb_event_chunk *event_chunk, ctx->buf->put_events_calls = 0; - if (ctx->create_group == FLB_TRUE && ctx->group_created == FLB_FALSE) { - ret = create_log_group(ctx); - if (ret < 0) { - FLB_OUTPUT_RETURN(FLB_RETRY); - } - } - stream = get_log_stream(ctx, event_chunk->tag, flb_sds_len(event_chunk->tag)); if (!stream) {