Integrate AWS CloudWatch with a Spring Boot application
Integrating AWS CloudWatch with a Spring Boot application involves setting up monitoring and logging to track application performance, errors, and metrics. Here's a step-by-step guide to help you achieve this integration:
1. Add Required Dependencies
For Maven:
Add these dependencies in your pom.xml file:
<dependencies>
<!-- Micrometer CloudWatch Metrics -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-cloudwatch2</artifactId>
<version>1.11.0</version> <!-- Use the latest version -->
</dependency>
<!-- Spring Boot Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- AWS SDK for CloudWatch -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>cloudwatch</artifactId>
<version>2.20.0</version> <!-- Use the latest version -->
</dependency>
</dependencies>
For Gradle:
Add these dependencies to your build.gradle file:
dependencies {
implementation 'io.micrometer:micrometer-registry-cloudwatch2:1.11.0'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'software.amazon.awssdk:cloudwatch:2.20.0'
}
2. Configure AWS Credentials
AWS CloudWatch requires authentication to push logs and metrics. You can configure credentials in one of the following ways:
1. Environment Variables:
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_REGION=your-region
2. AWS Credentials File: Use the ~/.aws/credentials file:
[default]
aws_access_key_id=your-access-key
aws_secret_access_key=your-secret-key
3. IAM Role (Preferred for AWS-hosted environments): Assign an IAM role to the instance or service running your application.
3. Enable Metrics Export with Micrometer
Micrometer integrates seamlessly with CloudWatch for metrics reporting. Create a configuration class for the CloudWatch registry.
Example:
import io.micrometer.cloudwatch2.CloudWatchMeterRegistry;
import io.micrometer.cloudwatch2.CloudWatchConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import java.time.Duration;
@Configuration
public class CloudWatchMetricsConfig {
@Bean
public CloudWatchMeterRegistry cloudWatchMeterRegistry() {
CloudWatchConfig config = new CloudWatchConfig() {
@Override
public String get(String key) {
return null; // Use default configurations
}
@Override
public Duration step() {
return Duration.ofMinutes(1); // Interval to push metrics
}
};
CloudWatchClient client = CloudWatchClient.builder().build();
return new CloudWatchMeterRegistry(config, client);
}
}
4. Configure Spring Boot Actuator
Spring Boot Actuator provides default metrics for monitoring.
- Add these properties to application.yml or application.properties:
application.yml:
management:
endpoints:
web:
exposure:
include: "*"
metrics:
export:
cloudwatch:
enabled: true
5. Logging Integration
To send application logs to CloudWatch:
Option 1: Use AWS CloudWatch Agent
Install and configure the AWS CloudWatch Agent on your server to forward application logs to CloudWatch.
Option 2: Use a Logback Appender
Include the AWS CloudWatch Logback Appender dependency:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-logs</artifactId>
<version>1.12.548</version>
</dependency>
Update your logback-spring.xml file to send logs to CloudWatch:
<appender name="CLOUDWATCH" class="com.amazonaws.services.logs.logback.CloudWatchAppender">
<region>us-west-2</region>
<logGroup>spring-boot-logs</logGroup>
<logStream>application-stream</logStream>
<awsAccessKeyId>${AWS_ACCESS_KEY_ID}</awsAccessKeyId>
<awsSecretKey>${AWS_SECRET_ACCESS_KEY}</awsSecretKey>
</appender>
<root level="INFO">
<appender-ref ref="CLOUDWATCH" />
</root>
6. Custom Metrics
To define custom metrics, inject MeterRegistry into your services and register new metrics:
Example:
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;
@Service
public class CustomMetricsService {
public CustomMetricsService(MeterRegistry registry) {
registry.gauge("custom.metric", 42); // Example of a custom metric
}
}
7. Deploy and Monitor
- Deploy the application and check the CloudWatch Metrics section in the AWS Console.
- View logs under CloudWatch Logs.
- Use the CloudWatch Dashboard to visualize metrics and set up alerts.
Best Practices
- Use IAM Roles instead of hardcoding credentials.
- Minimize metric publishing intervals to reduce costs.
- Enable alerts for critical metrics using CloudWatch Alarms.