Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/directory/directory.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ export const directory = {
{
path: 'src/pages/[platform]/build-a-backend/add-aws-services/logging/view-logs/index.mdx',
section: 'backend'
},
{
path: 'src/pages/[platform]/build-a-backend/add-aws-services/logging/cloudwatch/index.mdx',
section: 'backend'
}
]
},
Expand Down Expand Up @@ -1015,6 +1019,9 @@ export const directory = {
{
path: 'src/pages/[platform]/frontend/logging/index.mdx',
children: [
{
path: 'src/pages/[platform]/frontend/logging/cloudwatch/index.mdx'
},
{
path: 'src/pages/[platform]/frontend/logging/send-logs/index.mdx'
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { getCustomStaticPath } from '@/utils/getCustomStaticPath';

export const meta = {
title: 'CloudWatch Logs',
description: 'Set up an Amazon CloudWatch log group and configure IAM permissions for the Amplify CloudWatch client.',
platforms: [
'android',
'swift'
],
};

export const getStaticPaths = async () => {
return getCustomStaticPath(meta.platforms);
};

export function getStaticProps(context) {
return {
props: {
platform: context.params.platform,
meta
}
};
}

Use the [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html) to create an [Amazon CloudWatch log group](https://aws.amazon.com/cloudwatch/) and grant your app the permissions it needs. For more on adding custom AWS resources to your Amplify backend, see [Custom resources](/[platform]/build-a-backend/add-aws-services/custom-resources/).

## Set up a CloudWatch log group

```ts title="amplify/backend.ts"
import { defineBackend } from "@aws-amplify/backend";
import { auth } from "./auth/resource";
import { data } from "./data/resource";
import { Policy, PolicyStatement } from "aws-cdk-lib/aws-iam";
import { LogGroup } from "aws-cdk-lib/aws-logs";

const backend = defineBackend({
auth,
data,
});

const loggingStack = backend.createStack("logging-stack");

// Create a CloudWatch log group
const logGroup = new LogGroup(loggingStack, "LogGroup", {
logGroupName: "/app/my-app",
});

// Grant log write permissions to authenticated users
const loggingPolicy = new Policy(loggingStack, "LoggingPolicy", {
statements: [
new PolicyStatement({
actions: [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
],
resources: [logGroup.logGroupArn],
}),
],
});

backend.auth.resources.authenticatedUserIamRole.attachInlinePolicy(loggingPolicy);
```

If you are not using the CDK, ensure your authenticated IAM role has permission to write to your target log group:

```json
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": "arn:aws:logs:<region>:<account-id>:log-group:<log-group-name>:*"
}]
}
```

For more information, see the [Amazon CloudWatch Logs documentation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/iam-access-control-overview-cwl.html).

## Next steps

Use the [CloudWatch client](/[platform]/frontend/logging/cloudwatch/) to send logs from your app.
Loading
Loading