Strategy to validate Elastic Beanstalk environment in CodePipeline

0

I have an Elastic Beanstalk environment that's deployed using AWS CDK and AWS CodePipeline. Currently, problems with the Beanstalk deployment aren't necessarily reported back by CloudFormation. For example if there's a bug in the application code that prevents the updated environment from launching successfully, the CloudFormation / pipeline deployment will succeed and then the Beanstalk environment will soon-after transition into an unhealthy state.

I'd like CodePipeline to detect this failure and mark the pipeline execution as failed, blocking promotion to subsequent stages. I'm looking for advice on how to do this. I'm imagining two approaches:

  1. Add a CodeBuild step to the pipeline after the CDK deployment that uses the AWS CLI to check on the environment status
  2. Add a custom resource Lambda function that checks on the environment status after the deployment as part of the CDK deployment process

Maybe it's also possible to adjust the Beanstalk settings to do this validation automatically, but there seems to be some open issues related to this (https://github.com/aws/elastic-beanstalk-roadmap/issues/162)

I'm leaning toward the second option above but would love to get some input on this before getting started.

1 Answer
1

The second option you mentioned, adding a custom resource Lambda function that checks on the environment status after the deployment as part of the CDK deployment process, would be the best approach to accomplish this task.

This approach would allow you to programmatically check the status of the Beanstalk environment after the deployment and return a failure status to the pipeline if the environment is in an unhealthy state. This would allow the pipeline to be marked as failed and block promotion to subsequent stages.

You can use the AWS SDK for JavaScript in Node.js, AWS SDK for Python (Boto3), or any other SDK to interact with Elastic Beanstalk and check the environment status.

Here's an example of how you might implement this using a Lambda function and the AWS SDK for Python (Boto3):

Create a new Lambda function in the same region as your Elastic Beanstalk environment.

In the Lambda function, import the Boto3 library and create a new Elastic Beanstalk client.

Use the describe_environment_health method to check the status of the environment.

If the environment is in an unhealthy state, raise an exception that will be caught by CloudFormation and mark the pipeline execution as failed.

In your CDK code, include the Lambda function as a custom resource in your CloudFormation stack.

After the CDK deployment, the Lambda function will be invoked and will check the status of the Beanstalk environment.

You can also use CloudWatch Event Rules to schedule your Lambda function to check the environment status periodically and raise an exception if the environment is in an unhealthy state.

This approach would provide a more automated solution that would make sure that the pipeline execution is marked as failed whenever there's a problem with the Beanstalk deployment, and that it

profile picture
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions