-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { IAspect, Tags, aws_apigatewayv2, aws_iam, aws_logs } from 'aws-cdk-lib' | ||
import { RetentionDays } from 'aws-cdk-lib/aws-logs' | ||
import { Construct, IConstruct } from 'constructs' | ||
|
||
export class HTTPApiGatewayLogs implements IAspect { | ||
constructor() {} | ||
|
||
public visit(node: IConstruct): void { | ||
if (!(node instanceof aws_apigatewayv2.CfnStage)) { | ||
return | ||
} | ||
|
||
const scope = new Construct(node.node.scope!, 'logs') | ||
Tags.of(scope).add('custom:aspect', HTTPApiGatewayLogs.name) | ||
|
||
const group = new aws_logs.LogGroup(scope, `group`, { | ||
logGroupName: `/${node.node.scope!}`, | ||
retention: RetentionDays.ONE_WEEK, | ||
}) | ||
|
||
const role = new aws_iam.Role(scope, `role`, { | ||
assumedBy: new aws_iam.ServicePrincipal('apigateway.amazonaws.com'), | ||
}) | ||
|
||
const policy = new aws_iam.PolicyStatement({ | ||
actions: [ | ||
'logs:CreateLogGroup', | ||
'logs:CreateLogStream', | ||
'logs:DescribeLogGroups', | ||
'logs:DescribeLogStreams', | ||
'logs:PutLogEvents', | ||
'logs:GetLogEvents', | ||
'logs:FilterLogEvents', | ||
], | ||
resources: ['*'], | ||
}) | ||
|
||
role.addToPolicy(policy) | ||
group.grantWrite(role) | ||
|
||
node.accessLogSettings = { | ||
destinationArn: group.logGroupArn, | ||
format: JSON.stringify({ | ||
requestId: '$context.requestId', | ||
requestTime: '$context.requestTime', | ||
requestTimeEpoch: '$context.requestTimeEpoch', | ||
userAgent: '$context.identity.userAgent', | ||
httpMethod: '$context.httpMethod', | ||
domainName: '$context.domainName', | ||
status: '$context.status', | ||
path: '$context.path', | ||
}), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Stack, StackProps, aws_apigatewayv2 } from 'aws-cdk-lib' | ||
import { IConstruct } from 'constructs' | ||
|
||
export interface AccessStackProps extends StackProps {} | ||
|
||
export class AccessStack extends Stack { | ||
api = new aws_apigatewayv2.HttpApi(this, 'api') | ||
|
||
constructor(scope: IConstruct, id: string, props?: AccessStackProps) { | ||
super(scope, id, props) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters