Skip to content

Commit b4082e2

Browse files
Action logs for ECS
1 parent a2ab8d2 commit b4082e2

2 files changed

Lines changed: 200 additions & 1 deletion

File tree

dotcom-rendering/cdk/lib/__snapshots__/renderingStack.test.ts.snap

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,145 @@ exports[`The RenderingCDKStack matches the snapshot for Tag Page Rendering CODE
19561956
},
19571957
"Type": "AWS::IAM::Policy",
19581958
},
1959+
"EcsActionDelivery": {
1960+
"Properties": {
1961+
"DeliveryDestinationArn": {
1962+
"Fn::GetAtt": [
1963+
"EcsActionDeliveryDestination",
1964+
"Arn",
1965+
],
1966+
},
1967+
"DeliverySourceName": "tag-page-rendering-CODE-source",
1968+
"Tags": [
1969+
{
1970+
"Key": "gu:cdk:version",
1971+
"Value": "TEST",
1972+
},
1973+
{
1974+
"Key": "gu:repo",
1975+
"Value": "guardian/dotcom-rendering",
1976+
},
1977+
{
1978+
"Key": "Stack",
1979+
"Value": "frontend",
1980+
},
1981+
{
1982+
"Key": "Stage",
1983+
"Value": "CODE",
1984+
},
1985+
],
1986+
},
1987+
"Type": "AWS::Logs::Delivery",
1988+
},
1989+
"EcsActionDeliveryDestination": {
1990+
"Properties": {
1991+
"DestinationResourceArn": {
1992+
"Fn::GetAtt": [
1993+
"EcsActionLogGroup559EC54A",
1994+
"Arn",
1995+
],
1996+
},
1997+
"Name": "tag-page-rendering-CODE-destination",
1998+
"Tags": [
1999+
{
2000+
"Key": "gu:cdk:version",
2001+
"Value": "TEST",
2002+
},
2003+
{
2004+
"Key": "gu:repo",
2005+
"Value": "guardian/dotcom-rendering",
2006+
},
2007+
{
2008+
"Key": "Stack",
2009+
"Value": "frontend",
2010+
},
2011+
{
2012+
"Key": "Stage",
2013+
"Value": "CODE",
2014+
},
2015+
],
2016+
},
2017+
"Type": "AWS::Logs::DeliveryDestination",
2018+
},
2019+
"EcsActionDeliverySource": {
2020+
"Properties": {
2021+
"LogType": "ACTION_LOGS",
2022+
"Name": "tag-page-rendering-CODE-source",
2023+
"ResourceArn": {
2024+
"Fn::GetAtt": [
2025+
"tagpagerenderingEcsClusterE7696595",
2026+
"Arn",
2027+
],
2028+
},
2029+
"Tags": [
2030+
{
2031+
"Key": "gu:cdk:version",
2032+
"Value": "TEST",
2033+
},
2034+
{
2035+
"Key": "gu:repo",
2036+
"Value": "guardian/dotcom-rendering",
2037+
},
2038+
{
2039+
"Key": "Stack",
2040+
"Value": "frontend",
2041+
},
2042+
{
2043+
"Key": "Stage",
2044+
"Value": "CODE",
2045+
},
2046+
],
2047+
},
2048+
"Type": "AWS::Logs::DeliverySource",
2049+
},
2050+
"EcsActionLogGroup559EC54A": {
2051+
"DeletionPolicy": "Delete",
2052+
"Properties": {
2053+
"LogGroupName": "/aws/vendedlogs/ecs/action-logs/tag-page-rendering-CODE",
2054+
"RetentionInDays": 7,
2055+
"Tags": [
2056+
{
2057+
"Key": "gu:cdk:version",
2058+
"Value": "TEST",
2059+
},
2060+
{
2061+
"Key": "gu:repo",
2062+
"Value": "guardian/dotcom-rendering",
2063+
},
2064+
{
2065+
"Key": "Stack",
2066+
"Value": "frontend",
2067+
},
2068+
{
2069+
"Key": "Stage",
2070+
"Value": "CODE",
2071+
},
2072+
],
2073+
},
2074+
"Type": "AWS::Logs::LogGroup",
2075+
"UpdateReplacePolicy": "Delete",
2076+
},
2077+
"EcsActionLogGroupPolicyResourcePolicyD8381A26": {
2078+
"Properties": {
2079+
"PolicyDocument": {
2080+
"Fn::Join": [
2081+
"",
2082+
[
2083+
"{"Statement":[{"Action":["logs:CreateLogStream","logs:PutLogEvents"],"Effect":"Allow","Principal":{"Service":"delivery.logs.amazonaws.com"},"Resource":"",
2084+
{
2085+
"Fn::GetAtt": [
2086+
"EcsActionLogGroup559EC54A",
2087+
"Arn",
2088+
],
2089+
},
2090+
""}],"Version":"2012-10-17"}",
2091+
],
2092+
],
2093+
},
2094+
"PolicyName": "TagPageRenderingCODEEcsActionLogGroupPolicy1FA5FF4D",
2095+
},
2096+
"Type": "AWS::Logs::ResourcePolicy",
2097+
},
19592098
"EcsService81FC6EF6": {
19602099
"DependsOn": [
19612100
"EcsTaskDefinitionTaskRoleB7B6D8DD",

dotcom-rendering/cdk/lib/renderingStack.ts

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import { GuCname } from '@guardian/cdk/lib/constructs/dns/dns-records';
1010
import { GuAllowPolicy } from '@guardian/cdk/lib/constructs/iam';
1111
import { GuLoadBalancedAppExperimental } from '@guardian/cdk/lib/experimental/patterns/gu-load-balanced-app';
1212
import type { GuAsgCapacity } from '@guardian/cdk/lib/types';
13-
import { aws_cloudwatch, type App as CDKApp, Duration } from 'aws-cdk-lib';
13+
import {
14+
aws_cloudwatch,
15+
type App as CDKApp,
16+
Duration,
17+
RemovalPolicy,
18+
} from 'aws-cdk-lib';
1419
import type { ScalingInterval } from 'aws-cdk-lib/aws-applicationautoscaling';
1520
import { AdjustmentType, StepScalingPolicy } from 'aws-cdk-lib/aws-autoscaling';
1621
import { Metric, Unit } from 'aws-cdk-lib/aws-cloudwatch';
@@ -19,6 +24,14 @@ import type { InstanceType } from 'aws-cdk-lib/aws-ec2';
1924
import { Peer } from 'aws-cdk-lib/aws-ec2';
2025
import type { CfnService, ScalableTaskCount } from 'aws-cdk-lib/aws-ecs';
2126
import { ClusterSettings } from 'aws-cdk-lib/aws-ecs/mixins';
27+
import { Effect, PolicyStatement, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
28+
import {
29+
CfnDelivery,
30+
CfnDeliveryDestination,
31+
CfnDeliverySource,
32+
LogGroup,
33+
RetentionDays,
34+
} from 'aws-cdk-lib/aws-logs';
2235
import { Subscription, SubscriptionProtocol, Topic } from 'aws-cdk-lib/aws-sns';
2336
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
2437
import { getUserData } from './userData';
@@ -345,6 +358,53 @@ export class RenderingCDKStack extends CDKStack {
345358
'Could not create CPU scaling policy for ECS',
346359
);
347360
}
361+
362+
// Add Action Logs
363+
// This should provide infra level information such as how long it takes to pull an image
364+
//
365+
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/action-logs-getting-started.html
366+
const ecsServiceName = `${guApp}-${stage}`;
367+
368+
const actionLogGroup = new LogGroup(this, 'EcsActionLogGroup', {
369+
logGroupName: `/aws/vendedlogs/ecs/action-logs/${ecsServiceName}`,
370+
retention: RetentionDays.ONE_WEEK,
371+
removalPolicy: RemovalPolicy.DESTROY,
372+
});
373+
374+
actionLogGroup.addToResourcePolicy(
375+
new PolicyStatement({
376+
effect: Effect.ALLOW,
377+
principals: [
378+
new ServicePrincipal('delivery.logs.amazonaws.com'),
379+
],
380+
actions: ['logs:CreateLogStream', 'logs:PutLogEvents'],
381+
resources: [actionLogGroup.logGroupArn],
382+
}),
383+
);
384+
385+
const deliverySource = new CfnDeliverySource(
386+
this,
387+
'EcsActionDeliverySource',
388+
{
389+
name: `${ecsServiceName}-source`,
390+
resourceArn: app.ecsService.cluster.clusterArn,
391+
logType: 'ACTION_LOGS',
392+
},
393+
);
394+
395+
const deliveryDestination = new CfnDeliveryDestination(
396+
this,
397+
'EcsActionDeliveryDestination',
398+
{
399+
name: `${ecsServiceName}-destination`,
400+
destinationResourceArn: actionLogGroup.logGroupArn,
401+
},
402+
);
403+
404+
new CfnDelivery(this, 'EcsActionDelivery', {
405+
deliverySourceName: deliverySource.name,
406+
deliveryDestinationArn: deliveryDestination.attrArn,
407+
});
348408
}
349409
}
350410

0 commit comments

Comments
 (0)