

# Using CloudWatch Logs with AWS Batch
<a name="using_cloudwatch_logs"></a>

You can configure your AWS Batch jobs on EC2 resources to send detailed log information and metrics to CloudWatch Logs. Doing this, you can view different logs from your jobs in one convenient location. For more information about CloudWatch Logs, see [What is Amazon CloudWatch Logs?](https://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/WhatIsCloudWatchLogs.html) in the *Amazon CloudWatch User Guide*.

**Note**  
By default, CloudWatch Logs is turned on for AWS Fargate containers.

To turn on and customize CloudWatch Logs logging, review the following one-time configuration tasks:
+ For AWS Batch compute environments that are based on EC2 resources, add an IAM policy to the `ecsInstanceRole` role. For more information, see [Tutorial: Add a CloudWatch Logs IAM policy](cwl_iam_policy.md).
+ Create an Amazon EC2 launch template that includes detailed CloudWatch monitoring, then specify the template when you create your AWS Batch compute environment. You can also install the CloudWatch agent on an existing image and then specify the image in the AWS Batch first-run wizard.
+ (Optional) Configure the awslogs driver. You can add parameters that change the default behavior on both EC2 and Fargate resources. For more information, see [Use the awslogs log driver](using_awslogs.md).

**Topics**
+ [Tutorial: Add a CloudWatch Logs IAM policy](cwl_iam_policy.md)
+ [Install and configure the CloudWatch agent](installing_cwl_agent.md)
+ [Tutorial: View CloudWatch Logs](viewing_cwlogs.md)

# Tutorial: Add a CloudWatch Logs IAM policy
<a name="cwl_iam_policy"></a>

Before your jobs can send log data and detailed metrics to CloudWatch Logs, you must create an IAM policy that uses the CloudWatch Logs APIs. After you create the IAM policy, attach it to the `ecsInstanceRole` role.

**Note**  
If the `ECS-CloudWatchLogs` policy isn't attached to the `ecsInstanceRole` role, basic metrics can still be sent to CloudWatch Logs. However, the basic metrics don't include log data or detailed metrics such as free disk space.

AWS Batch compute environments use Amazon EC2 resources. When you create a compute environment using the AWS Batch first-run wizard, AWS Batch creates the `ecsInstanceRole` role and configures the environment with it.

If you aren't using the first-run wizard, you can specify the `ecsInstanceRole` role when you create a compute environment in the AWS Command Line Interface or AWS Batch API. For more information, see the [AWS CLI Command Reference](https://docs.aws.amazon.com/cli/latest/reference/) or [AWS Batch API Reference](https://docs.aws.amazon.com/batch/latest/APIReference/API_CreateComputeEnvironment.html).

**To create the `ECS-CloudWatchLogs` IAM policy**

1. Open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the navigation pane, choose **Policies**. 

1. Choose **Create policy**.

1. Choose **JSON**, then enter the following policy:

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "logs:CreateLogGroup",
                   "logs:CreateLogStream",
                   "logs:PutLogEvents",
                   "logs:DescribeLogStreams"
               ],
               "Resource": [
                   "arn:aws:logs:*:*:*"
               ]
           }
       ]
   }
   ```

------

1. Choose **Next: Tags**.

1. (Optional) For **Add tags**, choose **Add tag** to add a tag to the policy.

1. Choose **Next: Review**.

1. On the **Review policy** page, for **Name**, enter **ECS-CloudWatchLogs**, and then enter an optional **Description**.

1. Choose **Create policy**.

**To attach the `ECS-CloudWatchLogs` policy to `ecsInstanceRole`**

1. Open the IAM console at [https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/).

1. In the navigation pane, choose **Roles**. 

1. Choose `ecsInstanceRole`. If the role doesn't exist, follow the procedures in [Amazon ECS instance role](instance_IAM_role.md) to create the role.

1. Choose **Add Permissions**, then choose **Attach policies**.

1. Choose the **ECS-CloudWatchLogs** policy and then choose **Attach policy**.

# Install and configure the CloudWatch agent
<a name="installing_cwl_agent"></a>

You can create an Amazon EC2 launch template that includes CloudWatch monitoring. For more information, see [ Launch an instance from a launch template](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html#lt-initiate-launch-template) and [ Advanced details](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html#lt-advanced-details) in the *Amazon EC2 User Guide*.

You can also install the CloudWatch agent on an existing Amazon EC2 AMI and then specify the image in the AWS Batch first-run wizard. For more information, see [ Installing the CloudWatch agent](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-EC2-Instance.html) and [Getting started with AWS Batch tutorials](Batch_GetStarted.md).

**Note**  
Launch templates are not supported on AWS Fargate resources.

# Tutorial: View CloudWatch Logs
<a name="viewing_cwlogs"></a>

You can view and search CloudWatch Logs logs in the AWS Management Console.

**Note**  
It might take a few minutes for data to display in CloudWatch Logs.

**To view your CloudWatch Logs data**

1. Open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the left navigation pane, choose **Logs**, then choose **Log groups**.  
![\[CloudWatch console log groups\]](http://docs.aws.amazon.com/batch/latest/userguide/images/cwl-log-groups.png)

1. Choose a log group to view.  
![\[CloudWatch console log streams\]](http://docs.aws.amazon.com/batch/latest/userguide/images/cw_log_stream.png)

1. Choose a log stream to view. By default, the streams are identified by the first 200 characters of the job name and the Amazon ECS task ID.
**Tip**  
To download log stream data, choose **Actions**.  
![\[CloudWatch console log events\]](http://docs.aws.amazon.com/batch/latest/userguide/images/cw_log_events.png)